-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathpudlPgSql.php
More file actions
214 lines (148 loc) · 6 KB
/
pudlPgSql.php
File metadata and controls
214 lines (148 loc) · 6 KB
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
<?php
if (!class_exists('pudl',false)) require_once(__DIR__.'/../pudl.php');
require_once(is_owner(__DIR__.'/pudlPgSqlResult.php'));
class pudlPgSql
extends pudl {
////////////////////////////////////////////////////////////////////////////
// DESTRUCTOR
////////////////////////////////////////////////////////////////////////////
public function __destruct() {
$this->disconnect();
parent::__destruct();
}
////////////////////////////////////////////////////////////////////////////
// CREATE A CONNECTION TO THE POSTGRESQL SERVER
// http://php.net/manual/en/function.pg-connect.php
// http://php.net/manual/en/function.pg-pconnect.php
////////////////////////////////////////////////////////////////////////////
public function connect() {
$auth = $this->auth();
//USE EXISTING CONNECTION IF AVAILABLE
if (is_resource($auth['server'])) {
$this->connection = $auth['server'];
//ATTEMPT TO CREATE A PERSISTANT CONNECTION
} else if ($auth['persistent']) {
$this->connection = @pg_pconnect(
" host='" . $auth['server'] . "'" .
" dbname='" . $auth['database'] . "'" .
" user='" . $auth['username'] . "'" .
" password='" . $auth['password'] . "'" .
" connect_timeout='" . $auth['timeout'] . "'" .
" options='--client_encoding=UTF8'"
);
//ATTEMPT TO CREATE A NON-PERSISTANT CONNECTION
} else {
$this->connection = @pg_connect(
" host='" . $auth['server'] . "'" .
" dbname='" . $auth['database'] . "'" .
" user='" . $auth['username'] . "'" .
" password='" . $auth['password'] . "'" .
" connect_timeout='" . $auth['timeout'] . "'" .
" options='--client_encoding=UTF8'"
);
}
if (empty($this->connection)) {
$error = error_get_last();
throw new pudlConnectionException($this,
'Unable to connect to PostgreSQL server ' .
'"' . $auth['server'] . '"' .
' with the username ' .
'"' . $auth['username'] . '"' .
"\nError: " . $error['message']
);
}
// STORE WHICH SERVER WE'RE CONNECTED TO
if (!empty($auth['server'])) {
$this->connected = $auth['server'];
}
}
////////////////////////////////////////////////////////////////////////////
// DISCONNECT FROM THE POSTGRESQL SERVER
// http://php.net/manual/en/function.pg-close.php
////////////////////////////////////////////////////////////////////////////
public function disconnect($trigger=true) {
parent::disconnect($trigger);
if (!$this->connection) return;
pg_close($this->connection);
$this->connection = NULL;
}
////////////////////////////////////////////////////////////////////////////
// ESCAPE AN IDENTIFIER
// http://php.net/manual/en/function.pg-escape-identifier.php
////////////////////////////////////////////////////////////////////////////
public function identifier($identifier) {
return ($this->connection)
? pg_escape_identifier($this->connection, $identifier)
: pg_escape_identifier($identifier);
}
////////////////////////////////////////////////////////////////////////////
// ESCAPE A VALUE
// http://php.net/manual/en/function.pg-escape-string.php
////////////////////////////////////////////////////////////////////////////
public function escape($value) {
return ($this->connection)
? pg_escape_string($this->connection, $value)
: pg_escape_string($value);
}
////////////////////////////////////////////////////////////////////////////
// PROCESS A QUERY
// http://php.net/manual/en/function.pg-query.php
////////////////////////////////////////////////////////////////////////////
protected function process($query) {
if (!$this->connection) return new pudlPgSqlResult($this);
$result = @pg_query($this->connection, $query);
return new pudlPgSqlResult($this, $result);
}
////////////////////////////////////////////////////////////////////////////
// GET THE LAST AUTO INCREMENT NUMBER FROM INSERTED DATA
// https://www.postgresql.org/docs/8.1/functions-sequence.html
////////////////////////////////////////////////////////////////////////////
public function insertId() {
if (!$this->connection) return false;
$result = @pg_query($this->connection, 'SELECT lastval()');
if ($result === false) return false;
$return = @pg_fetch_array($result);
pg_free_result($result);
return is_array($return) ? reset($return) : false;
}
////////////////////////////////////////////////////////////////////////////
// GET THE NUMBER OF ROWS UPDATED BY THE LAST QUERY
// http://php.net/manual/en/function.pg-affected-rows.php
////////////////////////////////////////////////////////////////////////////
public function updated() {
if (!$this->connection) return 0;
return @pg_affected_rows($this->connection);
}
////////////////////////////////////////////////////////////////////////////
// GET THE LAST ERROR NUMBER
// http://php.net/manual/en/function.pg-last-error.php
////////////////////////////////////////////////////////////////////////////
public function errno() {
$error = $this->error();
return empty($error) ? 0 : 1;
}
////////////////////////////////////////////////////////////////////////////
// GET THE LAST ERROR MESSAGE
// http://php.net/manual/en/function.pg-last-error.php
////////////////////////////////////////////////////////////////////////////
public function error() {
if (!$this->connection) return @pg_last_error();
return pg_last_error($this->connection);
}
////////////////////////////////////////////////////////////////////////////
// GENERATE THE UPSERT PART OF THE QUERY
////////////////////////////////////////////////////////////////////////////
protected function _upsert($data) {
if (!pudl_array($data) || empty($data)) return false;
return ' ON CONFLICT (' .
$this->identifier(key($data)) .
') DO UPDATE SET ' .
$this->_update($data);
}
////////////////////////////////////////////////////////////////////////////
// CONVERT DATA TO BLOB
////////////////////////////////////////////////////////////////////////////
protected function blob($value) {
return "E'x\\\\" . bin2hex($value) . "'";
}
}