/
db_pgsql.php
353 lines (299 loc) · 9.19 KB
/
db_pgsql.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
<?php
//DB PostgreSQL Class @0-C881546C
/*
* Database Management for PHP
*
* Copyright (c) 1998-2000 NetUSE AG
* Boris Erdmann, Kristian Koehntopp
*
* Modified by Vitaliy Radchuk <vitaliy.radchuk@yessoftware.com>
*
* db_pgsql.php
*
*/
class DB_PostgreSQL {
var $DBHost = "";
var $DBPort = "";
var $DBDatabase = "";
var $DBUser = "";
var $DBPassword = "";
var $Persistent = false;
var $Link_ID = 0;
var $Query_ID = 0;
var $Record = array();
var $Row = 0;
var $Seq_Table = "db_sequence";
var $Errno = 0;
var $Error = "";
var $Auto_Free = 1; # Set this to 1 for automatic pg_freeresult on
# last record.
var $Connected = false;
var $Encoding = "";
var $TotalRow = 0;
var $Start = 0;
var $Limit = -1;
function getNusoap()
{
include_once 'nusoap.php';
$wsdl = 'http://localhost:8081/hris_wsnya/server/wsdl.php?wsdl';
//create instance
$nusoap = new nusoap_client ( $wsdl, true );
$user = "wsclient";
$pass = "secret";
//encrypt header value
$user = base64_encode ( $user );
$pass = base64_encode ( $pass );
$header = '<AuthSoapHeader>
<UserName>' . $user . '</UserName>
<Password>' . $pass . '</Password>
</AuthSoapHeader>';
//set header
$nusoap->setHeaders ( $header );
return $nusoap;
}
function getResultData($ws_client,$params)
{
foreach($_COOKIE as $cookie_name => $cookie){
$ws_client->setCookie($cookie_name,$cookie);
}
$ws_data = $ws_client->call('ws_proccess',$params);
//print_r($ws_data);
//exit;
if ($ws_client->fault) {
exit ( $ws_client->faultstring );
} else {
$err = $ws_client->getError ();
if ($err) {
exit ( $err );
}
}
$ws_data = unserialize (base64_decode ($ws_data));
return $ws_data;
}
function ifadd($add, $me) {
if("" != $add) return " ".$me.$add;
}
/* public: constructor */
function DB_Sql($query = "") {
$this->query($query);
}
function try_connect() {
/*$cstr = "dbname='". str_replace(array("\\", "'"), array("\\\\", "\\'"), $this->DBDatabase) . "'" .
$this->ifadd($this->DBHost, "host=").
$this->ifadd($this->DBPort, "port=").
$this->ifadd($this->DBUser, "user=").
$this->ifadd($this->DBPassword, "password=");
$this->Query_ID = 0;
if ($this->Persistent)
$this->Link_ID = @pg_pconnect($cstr);
else
$this->Link_ID = @pg_connect($cstr);
$this->Connected = $this->Link_ID ? true : false;*/
return true;
}
function connect() {
/* if (!$this->Connected) {
$this->Query_ID = 0;
$cstr = "dbname='". str_replace(array("\\", "'"), array("\\\\", "\\'"), $this->DBDatabase) . "'" .
$this->ifadd($this->DBHost, "host=").
$this->ifadd($this->DBPort, "port=").
$this->ifadd($this->DBUser, "user=").
$this->ifadd($this->DBPassword, "password=");
if ($this->Persistent)
$this->Link_ID=pg_pconnect($cstr);
else
$this->Link_ID=pg_connect($cstr);
if (!$this->Link_ID) {
$this->Halt("Cannot connect to database: " . pg_errormessage());
return false;
}
if ($this->Encoding)
@pg_set_client_encoding($this->Link_ID, $this->Encoding);
$this->Connected = true;
}*/
}
function query($Query_String) {
/* No empty queries, please, since PHP4 chokes on them. */
if ($Query_String == "")
/* The empty query string is passed on from the constructor,
* when calling the class without a query, e.g. in situations
* like these: '$db = new DB_Sql_Subclass;'
*/
return 0;
//$this->connect();
# printf("<br>Debug: query = %s<br>\n", $Query_String);
//$this->Query_ID = pg_Exec($this->Link_ID, $Query_String);
$ws_client = $this->getNusoap();
$params = array('search' => '',
'getParams' => json_encode(array('start' => $this->Start,'limit' => $this->Limit,'query' => rawurlencode($Query_String))),
'controller' => json_encode(array('module' => 'bds','class' => 'abstract', 'method' => 'read', 'type' => 'json')),
'postParams' => json_encode($_POST),
'jsonItems' => '',
'start' => 0,
'limit' => 0);
$ws_data = $this->getResultData($ws_client, $params);
//print_r($params);
//exit;
$this->Query_ID = $ws_data['data'];
$this->TotalRow = $ws_data['total'];
$this->Row = 0;
//$this->Error = pg_ErrorMessage($this->Link_ID);
$this->Errno = ($this->Error == "") ? 0 : 1;
if (!$ws_data['success']) {
$this->Errors->addError("Error: " . $ws_data['message']);
throw new Exception("Error: " . $ws_data['message']);
}
return $this->Query_ID;
}
function next_record() {
if (!$this->Query_ID)
return 0;
if (!isset($this->Query_ID[$this->Row]))
return 0;
$this->Record = $this->Query_ID[$this->Row];
$this->Row++;
//$this->Error = pg_ErrorMessage($this->Link_ID);
//$this->Errno = ($this->Error == "")?0:1;
$stat = is_array($this->Record);
if (!$stat && $this->Auto_Free) {
//pg_freeresult($this->Query_ID);
$this->Query_ID = 0;
}
return $stat;
}
function has_next_row(){
return isset($this->Query_ID[$this->Row]);
}
function seek($pos) {
$this->Row = $pos;
return true;
}
function lock($table, $mode = "write") {
if ($mode == "write") {
$result = pg_Exec($this->Link_ID, "lock table $table");
} else {
$result = 1;
}
return $result;
}
function unlock() {
return pg_Exec($this->Link_ID, "commit");
}
/* public: sequence numbers */
function nextid($seq_name) {
$this->connect();
if ($this->lock($this->Seq_Table)) {
/* get sequence number (locked) and increment */
$q = sprintf("select nextid from %s where seq_name = '%s' LIMIT 1",
$this->Seq_Table,
$seq_name);
$id = @pg_Exec($this->Link_ID, $q);
$res = @pg_Fetch_Array($id, 0);
/* No current value, make one */
if (!is_array($res)) {
$currentid = 0;
$q = sprintf("insert into %s values('%s', %s)",
$this->Seq_Table,
$seq_name,
$currentid);
$id = @pg_Exec($this->Link_ID, $q);
} else {
$currentid = $res["nextid"];
}
$nextid = $currentid + 1;
$q = sprintf("update %s set nextid = '%s' where seq_name = '%s'",
$this->Seq_Table,
$nextid,
$seq_name);
$id = @pg_Exec($this->Link_ID, $q);
$this->unlock();
} else {
$this->Errors->addError("Database error: Cannot lock ".$this->Seq_Table." - has it been created?");
return 0;
}
return $nextid;
}
function metadata($table) {
$count = 0;
$id = 0;
$res = array();
$this->connect();
$id = pg_exec($this->Link_ID, "select * from $table LIMIT 1");
if ($id < 0) {
$this->Error = pg_ErrorMessage($id);
$this->Errno = 1;
$this->Errors->addError("Metadata query failed: " . $this->Error);
return 0;
}
$count = pg_NumFields($id);
for ($i=0; $i<$count; $i++) {
$res[$i]["table"] = $table;
$res[$i]["name"] = pg_FieldName ($id, $i);
$res[$i]["type"] = pg_FieldType ($id, $i);
$res[$i]["len"] = pg_FieldSize ($id, $i);
$res[$i]["flags"] = "";
}
pg_FreeResult($id);
return $res;
}
function affected_rows() {
return pg_cmdtuples($this->Query_ID);
}
function num_rows() {
return pg_numrows($this->Query_ID);
}
function num_fields() {
return pg_numfields($this->Query_ID);
}
function nf() {
return $this->num_rows();
}
function np() {
print $this->num_rows();
}
function f($Name) {
return $this->Record && array_key_exists($Name, $this->Record) ? $this->Record[$Name] : "";
}
function p($Name) {
print $this->Record[$Name];
}
function free_result() {
@pg_freeresult($this->Query_ID);
$this->Query_ID = 0;
}
function close()
{
if ($this->Query_ID) {
$this->free_result();
}
/*
For better perfomance, now php(by docs) must close connection when script finished
if ($this->Connected && !$this->Persistent) {
pg_close($this->Link_ID);
$this->Connected = true;
}
*/
}
function halt($msg) {
printf("</td></tr></table><b>Database error:</b> %s<br>\n", $msg);
printf("<b>PostgreSQL Error</b><br>\n");
die("Session halted.");
}
function table_names() {
$this->query("select relname from pg_class where relkind = 'r' and not relname like 'pg_%'");
$i=0;
while ($this->next_record())
{
$return[$i]["table_name"]= $this->f(0);
$return[$i]["tablespace_name"]=$this->DBDatabase;
$return[$i]["database"]=$this->DBDatabase;
$i++;
}
return $return;
}
function esc($value) {
return pg_escape_string($value);
}
}
//End DB PostgreSQL Class
?>