1 |
e0d09fe3
|
Petr Lukašík
|
<?php
|
2 |
|
|
/*
|
3 |
|
|
*
|
4 |
|
|
* Copyright (c) 1998-2000 NetUSE AG
|
5 |
|
|
* Boris Erdmann, Kristian Koehntopp
|
6 |
|
|
*
|
7 |
|
|
* $Id: db_pgsql.inc,v 1.9 2002/08/26 08:27:43 richardarcher Exp $
|
8 |
|
|
*
|
9 |
|
|
*/
|
10 |
|
|
|
11 |
|
|
class DB_Sql {
|
12 |
|
|
var $Host = "Filek.cz";
|
13 |
|
|
var $Database = "aswi-dictionary";
|
14 |
|
|
var $User = "dbowner";
|
15 |
96b3e7ed
|
Filip Jani
|
var $Password = "";
|
16 |
e0d09fe3
|
Petr Lukašík
|
|
17 |
|
|
/** @var mysqli_result */
|
18 |
|
|
var $result = null;
|
19 |
|
|
|
20 |
|
|
var $row = null;
|
21 |
|
|
|
22 |
|
|
/** @var mysqli */
|
23 |
|
|
var $connection = null;
|
24 |
|
|
var $Debug = 0;
|
25 |
|
|
|
26 |
|
|
/* public: constructor */
|
27 |
|
|
function __construct($query = "") {
|
28 |
|
|
$this->query($query);
|
29 |
|
|
}
|
30 |
|
|
|
31 |
|
|
function connect() {
|
32 |
|
|
$this->connection = new mysqli($this->Host, $this->User, $this->Password, $this->Database);
|
33 |
|
|
$this->connection->set_charset("utf8");
|
34 |
|
|
|
35 |
|
|
if ($this->connection->connect_error) {
|
36 |
|
|
$this->halt("Connection failed: " . $this->connection->connect_error);
|
37 |
|
|
}
|
38 |
|
|
}
|
39 |
|
|
|
40 |
|
|
function query(string $Query_String) {
|
41 |
|
|
/* No empty queries, please, since PHP4 chokes on them. */
|
42 |
|
|
if ($Query_String == "")
|
43 |
|
|
/* The empty query string is passed on from the constructor,
|
44 |
|
|
* when calling the class without a query, e.g. in situations
|
45 |
|
|
* like these: '$db = new DB_Sql_Subclass;'
|
46 |
|
|
*/
|
47 |
|
|
return 0;
|
48 |
|
|
|
49 |
|
|
$this->connect();
|
50 |
|
|
|
51 |
|
|
if ($this->Debug)
|
52 |
|
|
printf("<br>Debug: query = %s<br>\n", $Query_String);
|
53 |
|
|
|
54 |
|
|
$this->result = $this->connection->query($Query_String);
|
55 |
|
|
|
56 |
96b3e7ed
|
Filip Jani
|
// if ($this->result) {
|
57 |
|
|
// $this->halt("Invalid SQL: ".$Query_String);
|
58 |
|
|
// }
|
59 |
e0d09fe3
|
Petr Lukašík
|
|
60 |
|
|
return $this->result;
|
61 |
|
|
}
|
62 |
|
|
|
63 |
|
|
function next_record() {
|
64 |
|
|
$this->row = mysqli_fetch_row($this->result);
|
65 |
|
|
return $this->row;
|
66 |
|
|
}
|
67 |
|
|
|
68 |
|
|
function metadata($table="") {
|
69 |
|
|
$count = 0;
|
70 |
|
|
$id = 0;
|
71 |
|
|
$res = array();
|
72 |
|
|
|
73 |
|
|
if (!empty($table)) {
|
74 |
|
|
$this->connect();
|
75 |
|
|
$id = pg_exec($this->Link_ID, "select * from $table");
|
76 |
|
|
if ($id < 0) {
|
77 |
|
|
$this->Error = pg_ErrorMessage($id);
|
78 |
|
|
$this->Errno = 1;
|
79 |
|
|
$this->halt("Metadata query failed.");
|
80 |
|
|
}
|
81 |
|
|
} else {
|
82 |
|
|
$id = $this->Query_ID;
|
83 |
|
|
if (!$id) {
|
84 |
|
|
$this->halt("No query specified.");
|
85 |
|
|
}
|
86 |
|
|
}
|
87 |
|
|
|
88 |
|
|
$count = pg_NumFields($id);
|
89 |
|
|
|
90 |
|
|
for ($i=0; $i<$count; $i++) {
|
91 |
|
|
$res[$i]["table"] = $table;
|
92 |
|
|
$res[$i]["name"] = pg_FieldName ($id, $i);
|
93 |
|
|
$res[$i]["type"] = pg_FieldType ($id, $i);
|
94 |
|
|
$res[$i]["len"] = pg_FieldSize ($id, $i);
|
95 |
|
|
$res[$i]["flags"] = "";
|
96 |
|
|
}
|
97 |
|
|
|
98 |
|
|
if ($table) {
|
99 |
|
|
pg_FreeResult($id);
|
100 |
|
|
}
|
101 |
|
|
|
102 |
|
|
return $res;
|
103 |
|
|
}
|
104 |
|
|
|
105 |
|
|
function affected_rows() {
|
106 |
|
|
return $this->connection->affected_rows;
|
107 |
|
|
}
|
108 |
|
|
|
109 |
|
|
function num_rows() {
|
110 |
|
|
return $this->result->num_rows;
|
111 |
|
|
}
|
112 |
|
|
|
113 |
|
|
function num_fields() {
|
114 |
|
|
return $this->result->field_count;
|
115 |
|
|
}
|
116 |
|
|
|
117 |
|
|
function f($Name) {
|
118 |
|
|
return $this->result[$Name];
|
119 |
|
|
}
|
120 |
|
|
|
121 |
|
|
function halt($msg) {
|
122 |
|
|
printf("</td></tr></table><b>Database error:</b> %s<br>\n", $msg);
|
123 |
|
|
printf("<b>PostgreSQL Error</b>: %s (%s)<br>\n",
|
124 |
|
|
$this->connection->errno,
|
125 |
|
|
$this->connection->error);
|
126 |
|
|
// die("Session halted."); // potřebuju vidět errory v logu
|
127 |
|
|
}
|
128 |
|
|
|
129 |
|
|
function table_names() {
|
130 |
|
|
$this->query("select relname from pg_class where relkind = 'r' and not relname like 'pg_%'");
|
131 |
|
|
$i=0;
|
132 |
|
|
while ($this->next_record())
|
133 |
|
|
{
|
134 |
|
|
$return[$i]["table_name"]= $this->f(0);
|
135 |
|
|
$return[$i]["tablespace_name"]=$this->Database;
|
136 |
|
|
$return[$i]["database"]=$this->Database;
|
137 |
|
|
$i++;
|
138 |
|
|
}
|
139 |
|
|
return $return;
|
140 |
|
|
}
|
141 |
|
|
}
|
142 |
|
|
?>
|