1 : /*
2 : +----------------------------------------------------------------------+
3 : | PHP Version 6 |
4 : +----------------------------------------------------------------------+
5 : | Copyright (c) 1997-2009 The PHP Group |
6 : +----------------------------------------------------------------------+
7 : | This source file is subject to version 3.01 of the PHP license, |
8 : | that is bundled with this package in the file LICENSE, and is |
9 : | available through the world-wide-web at the following url: |
10 : | http://www.php.net/license/3_01.txt |
11 : | If you did not receive a copy of the PHP license and are unable to |
12 : | obtain it through the world-wide-web, please send a note to |
13 : | license@php.net so we can mail you a copy immediately. |
14 : +----------------------------------------------------------------------+
15 : | Authors: Zeev Suraski <zeev@zend.com> |
16 : | Jouni Ahto <jouni.ahto@exdec.fi> |
17 : | Yasuo Ohgaki <yohgaki@php.net> |
18 : | Youichi Iwakiri <yiwakiri@st.rim.or.jp> (pg_copy_*) |
19 : | Chris Kings-Lynne <chriskl@php.net> (v3 protocol) |
20 : +----------------------------------------------------------------------+
21 : */
22 :
23 : /* $Id: pgsql.c 290146 2009-11-02 13:33:24Z iliaa $ */
24 :
25 : #include <stdlib.h>
26 :
27 : #define PHP_PGSQL_PRIVATE 1
28 :
29 : #ifdef HAVE_CONFIG_H
30 : #include "config.h"
31 : #endif
32 :
33 : #define SMART_STR_PREALLOC 512
34 :
35 : #include "php.h"
36 : #include "php_ini.h"
37 : #include "ext/standard/php_standard.h"
38 : #include "ext/standard/php_smart_str.h"
39 : #include "ext/ereg/php_regex.h"
40 :
41 : #undef PACKAGE_BUGREPORT
42 : #undef PACKAGE_NAME
43 : #undef PACKAGE_STRING
44 : #undef PACKAGE_TARNAME
45 : #undef PACKAGE_VERSION
46 : #include "php_pgsql.h"
47 : #include "php_globals.h"
48 : #include "zend_exceptions.h"
49 :
50 : #if HAVE_PGSQL
51 :
52 : #ifndef InvalidOid
53 : #define InvalidOid ((Oid) 0)
54 : #endif
55 :
56 : #define PGSQL_ASSOC 1<<0
57 : #define PGSQL_NUM 1<<1
58 : #define PGSQL_BOTH (PGSQL_ASSOC|PGSQL_NUM)
59 :
60 : #define PGSQL_STATUS_LONG 1
61 : #define PGSQL_STATUS_STRING 2
62 :
63 : #define PGSQL_MAX_LENGTH_OF_LONG 30
64 : #define PGSQL_MAX_LENGTH_OF_DOUBLE 60
65 :
66 : #define PGSQL_RETURN_OID(oid) do { \
67 : if (oid > LONG_MAX) { \
68 : smart_str s = {0}; \
69 : smart_str_append_unsigned(&s, oid); \
70 : smart_str_0(&s); \
71 : RETURN_STRINGL(s.c, s.len, 0); \
72 : } \
73 : RETURN_LONG((long)oid); \
74 : } while(0)
75 :
76 :
77 : #if HAVE_PQSETNONBLOCKING
78 : #define PQ_SETNONBLOCKING(pg_link, flag) PQsetnonblocking(pg_link, flag)
79 : #else
80 : #define PQ_SETNONBLOCKING(pg_link, flag) 0
81 : #endif
82 :
83 : #define CHECK_DEFAULT_LINK(x) if ((x) == -1) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "No PostgreSQL link opened yet"); }
84 :
85 : #ifndef HAVE_PQFREEMEM
86 : #define PQfreemem free
87 : #endif
88 :
89 : ZEND_DECLARE_MODULE_GLOBALS(pgsql)
90 : static PHP_GINIT_FUNCTION(pgsql);
91 :
92 : /* {{{ arginfo */
93 : ZEND_BEGIN_ARG_INFO_EX(arginfo_pg_connect, 0, 0, 1)
94 : ZEND_ARG_INFO(0, connection_string)
95 : ZEND_ARG_INFO(0, connect_type)
96 : ZEND_ARG_INFO(0, host)
97 : ZEND_ARG_INFO(0, port)
98 : ZEND_ARG_INFO(0, options)
99 : ZEND_ARG_INFO(0, tty)
100 : ZEND_ARG_INFO(0, database)
101 : ZEND_END_ARG_INFO()
102 :
103 : ZEND_BEGIN_ARG_INFO_EX(arginfo_pg_pconnect, 0, 0, 1)
104 : ZEND_ARG_INFO(0, connection_string)
105 : ZEND_ARG_INFO(0, host)
106 : ZEND_ARG_INFO(0, port)
107 : ZEND_ARG_INFO(0, options)
108 : ZEND_ARG_INFO(0, tty)
109 : ZEND_ARG_INFO(0, database)
110 : ZEND_END_ARG_INFO()
111 :
112 : #if HAVE_PQPARAMETERSTATUS
113 : ZEND_BEGIN_ARG_INFO_EX(arginfo_pg_parameter_status, 0, 0, 1)
114 : ZEND_ARG_INFO(0, connection)
115 : ZEND_ARG_INFO(0, param_name)
116 : ZEND_END_ARG_INFO()
117 : #endif
118 :
119 : ZEND_BEGIN_ARG_INFO_EX(arginfo_pg_close, 0, 0, 0)
120 : ZEND_ARG_INFO(0, connection)
121 : ZEND_END_ARG_INFO()
122 :
123 : ZEND_BEGIN_ARG_INFO_EX(arginfo_pg_dbname, 0, 0, 0)
124 : ZEND_ARG_INFO(0, connection)
125 : ZEND_END_ARG_INFO()
126 :
127 : ZEND_BEGIN_ARG_INFO_EX(arginfo_pg_last_error, 0, 0, 0)
128 : ZEND_ARG_INFO(0, connection)
129 : ZEND_END_ARG_INFO()
130 :
131 : ZEND_BEGIN_ARG_INFO_EX(arginfo_pg_options, 0, 0, 0)
132 : ZEND_ARG_INFO(0, connection)
133 : ZEND_END_ARG_INFO()
134 :
135 : ZEND_BEGIN_ARG_INFO_EX(arginfo_pg_port, 0, 0, 0)
136 : ZEND_ARG_INFO(0, connection)
137 : ZEND_END_ARG_INFO()
138 :
139 : ZEND_BEGIN_ARG_INFO_EX(arginfo_pg_tty, 0, 0, 0)
140 : ZEND_ARG_INFO(0, connection)
141 : ZEND_END_ARG_INFO()
142 :
143 : ZEND_BEGIN_ARG_INFO_EX(arginfo_pg_host, 0, 0, 0)
144 : ZEND_ARG_INFO(0, connection)
145 : ZEND_END_ARG_INFO()
146 :
147 : ZEND_BEGIN_ARG_INFO_EX(arginfo_pg_version, 0, 0, 0)
148 : ZEND_ARG_INFO(0, connection)
149 : ZEND_END_ARG_INFO()
150 :
151 : ZEND_BEGIN_ARG_INFO_EX(arginfo_pg_ping, 0, 0, 0)
152 : ZEND_ARG_INFO(0, connection)
153 : ZEND_END_ARG_INFO()
154 :
155 : ZEND_BEGIN_ARG_INFO_EX(arginfo_pg_query, 0, 0, 0)
156 : ZEND_ARG_INFO(0, connection)
157 : ZEND_ARG_INFO(0, query)
158 : ZEND_END_ARG_INFO()
159 :
160 : #if HAVE_PQEXECPARAMS
161 : ZEND_BEGIN_ARG_INFO_EX(arginfo_pg_query_params, 0, 0, 0)
162 : ZEND_ARG_INFO(0, connection)
163 : ZEND_ARG_INFO(0, query)
164 : ZEND_ARG_INFO(0, params)
165 : ZEND_END_ARG_INFO()
166 : #endif
167 :
168 : #if HAVE_PQPREPARE
169 : ZEND_BEGIN_ARG_INFO_EX(arginfo_pg_prepare, 0, 0, 0)
170 : ZEND_ARG_INFO(0, connection)
171 : ZEND_ARG_INFO(0, stmtname)
172 : ZEND_ARG_INFO(0, query)
173 : ZEND_END_ARG_INFO()
174 : #endif
175 :
176 : #if HAVE_PQEXECPREPARED
177 : ZEND_BEGIN_ARG_INFO_EX(arginfo_pg_execute, 0, 0, 0)
178 : ZEND_ARG_INFO(0, connection)
179 : ZEND_ARG_INFO(0, stmtname)
180 : ZEND_ARG_INFO(0, params)
181 : ZEND_END_ARG_INFO()
182 : #endif
183 :
184 : ZEND_BEGIN_ARG_INFO_EX(arginfo_pg_num_rows, 0, 0, 1)
185 : ZEND_ARG_INFO(0, result)
186 : ZEND_END_ARG_INFO()
187 :
188 : ZEND_BEGIN_ARG_INFO_EX(arginfo_pg_num_fields, 0, 0, 1)
189 : ZEND_ARG_INFO(0, result)
190 : ZEND_END_ARG_INFO()
191 :
192 : #if HAVE_PQCMDTUPLES
193 : ZEND_BEGIN_ARG_INFO_EX(arginfo_pg_affected_rows, 0, 0, 1)
194 : ZEND_ARG_INFO(0, result)
195 : ZEND_END_ARG_INFO()
196 : #endif
197 :
198 : ZEND_BEGIN_ARG_INFO_EX(arginfo_pg_last_notice, 0, 0, 1)
199 : ZEND_ARG_INFO(0, connection)
200 : ZEND_END_ARG_INFO()
201 :
202 : #ifdef HAVE_PQFTABLE
203 : ZEND_BEGIN_ARG_INFO_EX(arginfo_pg_field_table, 0, 0, 2)
204 : ZEND_ARG_INFO(0, result)
205 : ZEND_ARG_INFO(0, field_number)
206 : ZEND_ARG_INFO(0, oid_only)
207 : ZEND_END_ARG_INFO()
208 : #endif
209 :
210 : ZEND_BEGIN_ARG_INFO_EX(arginfo_pg_field_name, 0, 0, 2)
211 : ZEND_ARG_INFO(0, result)
212 : ZEND_ARG_INFO(0, field_number)
213 : ZEND_END_ARG_INFO()
214 :
215 : ZEND_BEGIN_ARG_INFO_EX(arginfo_pg_field_size, 0, 0, 2)
216 : ZEND_ARG_INFO(0, result)
217 : ZEND_ARG_INFO(0, field_number)
218 : ZEND_END_ARG_INFO()
219 :
220 : ZEND_BEGIN_ARG_INFO_EX(arginfo_pg_field_type, 0, 0, 2)
221 : ZEND_ARG_INFO(0, result)
222 : ZEND_ARG_INFO(0, field_number)
223 : ZEND_END_ARG_INFO()
224 :
225 : ZEND_BEGIN_ARG_INFO_EX(arginfo_pg_field_type_oid, 0, 0, 2)
226 : ZEND_ARG_INFO(0, result)
227 : ZEND_ARG_INFO(0, field_number)
228 : ZEND_END_ARG_INFO()
229 :
230 : ZEND_BEGIN_ARG_INFO_EX(arginfo_pg_field_num, 0, 0, 2)
231 : ZEND_ARG_INFO(0, result)
232 : ZEND_ARG_INFO(0, field_name)
233 : ZEND_END_ARG_INFO()
234 :
235 : ZEND_BEGIN_ARG_INFO_EX(arginfo_pg_fetch_result, 0, 0, 1)
236 : ZEND_ARG_INFO(0, result)
237 : ZEND_ARG_INFO(0, row_number)
238 : ZEND_ARG_INFO(0, field_name)
239 : ZEND_END_ARG_INFO()
240 :
241 : ZEND_BEGIN_ARG_INFO_EX(arginfo_pg_fetch_row, 0, 0, 1)
242 : ZEND_ARG_INFO(0, result)
243 : ZEND_ARG_INFO(0, row)
244 : ZEND_ARG_INFO(0, result_type)
245 : ZEND_END_ARG_INFO()
246 :
247 : ZEND_BEGIN_ARG_INFO_EX(arginfo_pg_fetch_assoc, 0, 0, 1)
248 : ZEND_ARG_INFO(0, result)
249 : ZEND_ARG_INFO(0, row)
250 : ZEND_END_ARG_INFO()
251 :
252 : ZEND_BEGIN_ARG_INFO_EX(arginfo_pg_fetch_array, 0, 0, 1)
253 : ZEND_ARG_INFO(0, result)
254 : ZEND_ARG_INFO(0, row)
255 : ZEND_ARG_INFO(0, result_type)
256 : ZEND_END_ARG_INFO()
257 :
258 : ZEND_BEGIN_ARG_INFO_EX(arginfo_pg_fetch_object, 0, 0, 1)
259 : ZEND_ARG_INFO(0, result)
260 : ZEND_ARG_INFO(0, row)
261 : ZEND_ARG_INFO(0, class_name)
262 : ZEND_ARG_INFO(0, l)
263 : ZEND_ARG_INFO(0, ctor_params)
264 : ZEND_END_ARG_INFO()
265 :
266 : ZEND_BEGIN_ARG_INFO_EX(arginfo_pg_fetch_all, 0, 0, 1)
267 : ZEND_ARG_INFO(0, result)
268 : ZEND_END_ARG_INFO()
269 :
270 : ZEND_BEGIN_ARG_INFO_EX(arginfo_pg_fetch_all_columns, 0, 0, 1)
271 : ZEND_ARG_INFO(0, result)
272 : ZEND_ARG_INFO(0, column_number)
273 : ZEND_END_ARG_INFO()
274 :
275 : ZEND_BEGIN_ARG_INFO_EX(arginfo_pg_result_seek, 0, 0, 2)
276 : ZEND_ARG_INFO(0, result)
277 : ZEND_ARG_INFO(0, offset)
278 : ZEND_END_ARG_INFO()
279 :
280 : ZEND_BEGIN_ARG_INFO_EX(arginfo_pg_field_prtlen, 0, 0, 1)
281 : ZEND_ARG_INFO(0, result)
282 : ZEND_ARG_INFO(0, row)
283 : ZEND_ARG_INFO(0, field_name_or_number)
284 : ZEND_END_ARG_INFO()
285 :
286 : ZEND_BEGIN_ARG_INFO_EX(arginfo_pg_field_is_null, 0, 0, 1)
287 : ZEND_ARG_INFO(0, result)
288 : ZEND_ARG_INFO(0, row)
289 : ZEND_ARG_INFO(0, field_name_or_number)
290 : ZEND_END_ARG_INFO()
291 :
292 : ZEND_BEGIN_ARG_INFO_EX(arginfo_pg_free_result, 0, 0, 1)
293 : ZEND_ARG_INFO(0, result)
294 : ZEND_END_ARG_INFO()
295 :
296 : ZEND_BEGIN_ARG_INFO_EX(arginfo_pg_last_oid, 0, 0, 1)
297 : ZEND_ARG_INFO(0, result)
298 : ZEND_END_ARG_INFO()
299 :
300 : ZEND_BEGIN_ARG_INFO_EX(arginfo_pg_trace, 0, 0, 1)
301 : ZEND_ARG_INFO(0, filename)
302 : ZEND_ARG_INFO(0, mode)
303 : ZEND_ARG_INFO(0, connection)
304 : ZEND_END_ARG_INFO()
305 :
306 : ZEND_BEGIN_ARG_INFO_EX(arginfo_pg_untrace, 0, 0, 0)
307 : ZEND_ARG_INFO(0, connection)
308 : ZEND_END_ARG_INFO()
309 :
310 : ZEND_BEGIN_ARG_INFO_EX(arginfo_pg_lo_create, 0, 0, 0)
311 : ZEND_ARG_INFO(0, connection)
312 : ZEND_ARG_INFO(0, large_object_id)
313 : ZEND_END_ARG_INFO()
314 :
315 : ZEND_BEGIN_ARG_INFO_EX(arginfo_pg_lo_unlink, 0, 0, 0)
316 : ZEND_ARG_INFO(0, connection)
317 : ZEND_ARG_INFO(0, large_object_oid)
318 : ZEND_END_ARG_INFO()
319 :
320 : ZEND_BEGIN_ARG_INFO_EX(arginfo_pg_lo_open, 0, 0, 0)
321 : ZEND_ARG_INFO(0, connection)
322 : ZEND_ARG_INFO(0, large_object_oid)
323 : ZEND_ARG_INFO(0, mode)
324 : ZEND_END_ARG_INFO()
325 :
326 : ZEND_BEGIN_ARG_INFO_EX(arginfo_pg_lo_close, 0, 0, 1)
327 : ZEND_ARG_INFO(0, large_object)
328 : ZEND_END_ARG_INFO()
329 :
330 : ZEND_BEGIN_ARG_INFO_EX(arginfo_pg_lo_read, 0, 0, 1)
331 : ZEND_ARG_INFO(0, large_object)
332 : ZEND_ARG_INFO(0, len)
333 : ZEND_END_ARG_INFO()
334 :
335 : ZEND_BEGIN_ARG_INFO_EX(arginfo_pg_lo_write, 0, 0, 2)
336 : ZEND_ARG_INFO(0, large_object)
337 : ZEND_ARG_INFO(0, buf)
338 : ZEND_ARG_INFO(0, len)
339 : ZEND_END_ARG_INFO()
340 :
341 : ZEND_BEGIN_ARG_INFO_EX(arginfo_pg_lo_read_all, 0, 0, 1)
342 : ZEND_ARG_INFO(0, large_object)
343 : ZEND_END_ARG_INFO()
344 :
345 : ZEND_BEGIN_ARG_INFO_EX(arginfo_pg_lo_import, 0, 0, 0)
346 : ZEND_ARG_INFO(0, connection)
347 : ZEND_ARG_INFO(0, filename)
348 : ZEND_ARG_INFO(0, large_object_oid)
349 : ZEND_END_ARG_INFO()
350 :
351 : ZEND_BEGIN_ARG_INFO_EX(arginfo_pg_lo_export, 0, 0, 0)
352 : ZEND_ARG_INFO(0, connection)
353 : ZEND_ARG_INFO(0, objoid)
354 : ZEND_ARG_INFO(0, filename)
355 : ZEND_END_ARG_INFO()
356 :
357 : ZEND_BEGIN_ARG_INFO_EX(arginfo_pg_lo_seek, 0, 0, 2)
358 : ZEND_ARG_INFO(0, large_object)
359 : ZEND_ARG_INFO(0, offset)
360 : ZEND_ARG_INFO(0, whence)
361 : ZEND_END_ARG_INFO()
362 :
363 : ZEND_BEGIN_ARG_INFO_EX(arginfo_pg_lo_tell, 0, 0, 1)
364 : ZEND_ARG_INFO(0, large_object)
365 : ZEND_END_ARG_INFO()
366 :
367 : #if HAVE_PQSETERRORVERBOSITY
368 : ZEND_BEGIN_ARG_INFO_EX(arginfo_pg_set_error_verbosity, 0, 0, 0)
369 : ZEND_ARG_INFO(0, connection)
370 : ZEND_ARG_INFO(0, verbosity)
371 : ZEND_END_ARG_INFO()
372 : #endif
373 :
374 : #if HAVE_PQCLIENTENCODING
375 : ZEND_BEGIN_ARG_INFO_EX(arginfo_pg_set_client_encoding, 0, 0, 0)
376 : ZEND_ARG_INFO(0, connection)
377 : ZEND_ARG_INFO(0, encoding)
378 : ZEND_END_ARG_INFO()
379 :
380 : ZEND_BEGIN_ARG_INFO_EX(arginfo_pg_client_encoding, 0, 0, 0)
381 : ZEND_ARG_INFO(0, connection)
382 : ZEND_END_ARG_INFO()
383 : #endif
384 :
385 : ZEND_BEGIN_ARG_INFO_EX(arginfo_pg_end_copy, 0, 0, 0)
386 : ZEND_ARG_INFO(0, connection)
387 : ZEND_END_ARG_INFO()
388 :
389 : ZEND_BEGIN_ARG_INFO_EX(arginfo_pg_put_line, 0, 0, 0)
390 : ZEND_ARG_INFO(0, connection)
391 : ZEND_ARG_INFO(0, query)
392 : ZEND_END_ARG_INFO()
393 :
394 : ZEND_BEGIN_ARG_INFO_EX(arginfo_pg_copy_to, 0, 0, 2)
395 : ZEND_ARG_INFO(0, connection)
396 : ZEND_ARG_INFO(0, table_name)
397 : ZEND_ARG_INFO(0, delimiter)
398 : ZEND_ARG_INFO(0, null_as)
399 : ZEND_END_ARG_INFO()
400 :
401 : ZEND_BEGIN_ARG_INFO_EX(arginfo_pg_copy_from, 0, 0, 3)
402 : ZEND_ARG_INFO(0, connection)
403 : ZEND_ARG_INFO(0, table_name)
404 : ZEND_ARG_INFO(0, rows)
405 : ZEND_ARG_INFO(0, delimiter)
406 : ZEND_ARG_INFO(0, null_as)
407 : ZEND_END_ARG_INFO()
408 :
409 : #if HAVE_PQESCAPE
410 : ZEND_BEGIN_ARG_INFO_EX(arginfo_pg_escape_string, 0, 0, 0)
411 : ZEND_ARG_INFO(0, connection)
412 : ZEND_ARG_INFO(0, data)
413 : ZEND_END_ARG_INFO()
414 :
415 : ZEND_BEGIN_ARG_INFO_EX(arginfo_pg_escape_bytea, 0, 0, 0)
416 : ZEND_ARG_INFO(0, connection)
417 : ZEND_ARG_INFO(0, data)
418 : ZEND_END_ARG_INFO()
419 :
420 : ZEND_BEGIN_ARG_INFO_EX(arginfo_pg_unescape_bytea, 0, 0, 1)
421 : ZEND_ARG_INFO(0, data)
422 : ZEND_END_ARG_INFO()
423 : #endif
424 :
425 : ZEND_BEGIN_ARG_INFO_EX(arginfo_pg_result_error, 0, 0, 1)
426 : ZEND_ARG_INFO(0, result)
427 : ZEND_END_ARG_INFO()
428 :
429 : #if HAVE_PQRESULTERRORFIELD
430 : ZEND_BEGIN_ARG_INFO_EX(arginfo_pg_result_error_field, 0, 0, 2)
431 : ZEND_ARG_INFO(0, result)
432 : ZEND_ARG_INFO(0, fieldcode)
433 : ZEND_END_ARG_INFO()
434 : #endif
435 :
436 : ZEND_BEGIN_ARG_INFO_EX(arginfo_pg_connection_status, 0, 0, 1)
437 : ZEND_ARG_INFO(0, connnection)
438 : ZEND_END_ARG_INFO()
439 :
440 : #if HAVE_PGTRANSACTIONSTATUS
441 : ZEND_BEGIN_ARG_INFO_EX(arginfo_pg_transaction_status, 0, 0, 1)
442 : ZEND_ARG_INFO(0, connnection)
443 : ZEND_END_ARG_INFO()
444 : #endif
445 :
446 : ZEND_BEGIN_ARG_INFO_EX(arginfo_pg_connection_reset, 0, 0, 1)
447 : ZEND_ARG_INFO(0, connection)
448 : ZEND_END_ARG_INFO()
449 :
450 : ZEND_BEGIN_ARG_INFO_EX(arginfo_pg_cancel_query, 0, 0, 1)
451 : ZEND_ARG_INFO(0, connection)
452 : ZEND_END_ARG_INFO()
453 :
454 : ZEND_BEGIN_ARG_INFO_EX(arginfo_pg_connection_busy, 0, 0, 1)
455 : ZEND_ARG_INFO(0, connection)
456 : ZEND_END_ARG_INFO()
457 :
458 : ZEND_BEGIN_ARG_INFO_EX(arginfo_pg_send_query, 0, 0, 2)
459 : ZEND_ARG_INFO(0, connection)
460 : ZEND_ARG_INFO(0, query)
461 : ZEND_END_ARG_INFO()
462 :
463 : #if HAVE_PQSENDQUERYPARAMS
464 : ZEND_BEGIN_ARG_INFO_EX(arginfo_pg_send_query_params, 0, 0, 3)
465 : ZEND_ARG_INFO(0, connection)
466 : ZEND_ARG_INFO(0, query)
467 : ZEND_ARG_INFO(0, params)
468 : ZEND_END_ARG_INFO()
469 : #endif
470 :
471 : #if HAVE_PQSENDPREPARE
472 : ZEND_BEGIN_ARG_INFO_EX(arginfo_pg_send_prepare, 0, 0, 3)
473 : ZEND_ARG_INFO(0, connection)
474 : ZEND_ARG_INFO(0, stmtname)
475 : ZEND_ARG_INFO(0, query)
476 : ZEND_END_ARG_INFO()
477 : #endif
478 :
479 : #if HAVE_PQSENDQUERYPREPARED
480 : ZEND_BEGIN_ARG_INFO_EX(arginfo_pg_send_execute, 0, 0, 3)
481 : ZEND_ARG_INFO(0, connection)
482 : ZEND_ARG_INFO(0, stmtname)
483 : ZEND_ARG_INFO(0, params)
484 : ZEND_END_ARG_INFO()
485 : #endif
486 :
487 : ZEND_BEGIN_ARG_INFO_EX(arginfo_pg_get_result, 0, 0, 1)
488 : ZEND_ARG_INFO(0, connection)
489 : ZEND_END_ARG_INFO()
490 :
491 : ZEND_BEGIN_ARG_INFO_EX(arginfo_pg_result_status, 0, 0, 1)
492 : ZEND_ARG_INFO(0, result)
493 : ZEND_ARG_INFO(0, result_type)
494 : ZEND_END_ARG_INFO()
495 :
496 : ZEND_BEGIN_ARG_INFO_EX(arginfo_pg_get_notify, 0, 0, 0)
497 : ZEND_ARG_INFO(0, connection)
498 : ZEND_ARG_INFO(0, e)
499 : ZEND_END_ARG_INFO()
500 :
501 : ZEND_BEGIN_ARG_INFO_EX(arginfo_pg_get_pid, 0, 0, 0)
502 : ZEND_ARG_INFO(0, connection)
503 : ZEND_END_ARG_INFO()
504 :
505 : ZEND_BEGIN_ARG_INFO_EX(arginfo_pg_meta_data, 0, 0, 2)
506 : ZEND_ARG_INFO(0, db)
507 : ZEND_ARG_INFO(0, table)
508 : ZEND_END_ARG_INFO()
509 :
510 : ZEND_BEGIN_ARG_INFO_EX(arginfo_pg_convert, 0, 0, 3)
511 : ZEND_ARG_INFO(0, db)
512 : ZEND_ARG_INFO(0, table)
513 : ZEND_ARG_INFO(0, values)
514 : ZEND_ARG_INFO(0, options)
515 : ZEND_END_ARG_INFO()
516 :
517 : ZEND_BEGIN_ARG_INFO_EX(arginfo_pg_insert, 0, 0, 3)
518 : ZEND_ARG_INFO(0, db)
519 : ZEND_ARG_INFO(0, table)
520 : ZEND_ARG_INFO(0, values)
521 : ZEND_ARG_INFO(0, options)
522 : ZEND_END_ARG_INFO()
523 :
524 : ZEND_BEGIN_ARG_INFO_EX(arginfo_pg_update, 0, 0, 4)
525 : ZEND_ARG_INFO(0, db)
526 : ZEND_ARG_INFO(0, table)
527 : ZEND_ARG_INFO(0, fields)
528 : ZEND_ARG_INFO(0, ids)
529 : ZEND_ARG_INFO(0, options)
530 : ZEND_END_ARG_INFO()
531 :
532 : ZEND_BEGIN_ARG_INFO_EX(arginfo_pg_delete, 0, 0, 3)
533 : ZEND_ARG_INFO(0, db)
534 : ZEND_ARG_INFO(0, table)
535 : ZEND_ARG_INFO(0, ids)
536 : ZEND_ARG_INFO(0, options)
537 : ZEND_END_ARG_INFO()
538 :
539 : ZEND_BEGIN_ARG_INFO_EX(arginfo_pg_select, 0, 0, 3)
540 : ZEND_ARG_INFO(0, db)
541 : ZEND_ARG_INFO(0, table)
542 : ZEND_ARG_INFO(0, ids)
543 : ZEND_ARG_INFO(0, options)
544 : ZEND_END_ARG_INFO()
545 : /* }}} */
546 :
547 : /* {{{ pgsql_functions[]
548 : */
549 : const zend_function_entry pgsql_functions[] = {
550 : /* connection functions */
551 : PHP_FE(pg_connect, arginfo_pg_connect)
552 : PHP_FE(pg_pconnect, arginfo_pg_pconnect)
553 : PHP_FE(pg_close, arginfo_pg_close)
554 : PHP_FE(pg_connection_status, arginfo_pg_connection_status)
555 : PHP_FE(pg_connection_busy, arginfo_pg_connection_busy)
556 : PHP_FE(pg_connection_reset, arginfo_pg_connection_reset)
557 : PHP_FE(pg_host, arginfo_pg_host)
558 : PHP_FE(pg_dbname, arginfo_pg_dbname)
559 : PHP_FE(pg_port, arginfo_pg_port)
560 : PHP_FE(pg_tty, arginfo_pg_tty)
561 : PHP_FE(pg_options, arginfo_pg_options)
562 : PHP_FE(pg_version, arginfo_pg_version)
563 : PHP_FE(pg_ping, arginfo_pg_ping)
564 : #if HAVE_PQPARAMETERSTATUS
565 : PHP_FE(pg_parameter_status, arginfo_pg_parameter_status)
566 : #endif
567 : #if HAVE_PGTRANSACTIONSTATUS
568 : PHP_FE(pg_transaction_status, arginfo_pg_transaction_status)
569 : #endif
570 : /* query functions */
571 : PHP_FE(pg_query, arginfo_pg_query)
572 : #if HAVE_PQEXECPARAMS
573 : PHP_FE(pg_query_params, arginfo_pg_query_params)
574 : #endif
575 : #if HAVE_PQPREPARE
576 : PHP_FE(pg_prepare, arginfo_pg_prepare)
577 : #endif
578 : #if HAVE_PQEXECPREPARED
579 : PHP_FE(pg_execute, arginfo_pg_execute)
580 : #endif
581 : PHP_FE(pg_send_query, arginfo_pg_send_query)
582 : #if HAVE_PQSENDQUERYPARAMS
583 : PHP_FE(pg_send_query_params, arginfo_pg_send_query_params)
584 : #endif
585 : #if HAVE_PQSENDPREPARE
586 : PHP_FE(pg_send_prepare, arginfo_pg_send_prepare)
587 : #endif
588 : #if HAVE_PQSENDQUERYPREPARED
589 : PHP_FE(pg_send_execute, arginfo_pg_send_execute)
590 : #endif
591 : PHP_FE(pg_cancel_query, arginfo_pg_cancel_query)
592 : /* result functions */
593 : PHP_FE(pg_fetch_result, arginfo_pg_fetch_result)
594 : PHP_FE(pg_fetch_row, arginfo_pg_fetch_row)
595 : PHP_FE(pg_fetch_assoc, arginfo_pg_fetch_assoc)
596 : PHP_FE(pg_fetch_array, arginfo_pg_fetch_array)
597 : PHP_FE(pg_fetch_object, arginfo_pg_fetch_object)
598 : PHP_FE(pg_fetch_all, arginfo_pg_fetch_all)
599 : PHP_FE(pg_fetch_all_columns, arginfo_pg_fetch_all_columns)
600 : #if HAVE_PQCMDTUPLES
601 : PHP_FE(pg_affected_rows,arginfo_pg_affected_rows)
602 : #endif
603 : PHP_FE(pg_get_result, arginfo_pg_get_result)
604 : PHP_FE(pg_result_seek, arginfo_pg_result_seek)
605 : PHP_FE(pg_result_status,arginfo_pg_result_status)
606 : PHP_FE(pg_free_result, arginfo_pg_free_result)
607 : PHP_FE(pg_last_oid, arginfo_pg_last_oid)
608 : PHP_FE(pg_num_rows, arginfo_pg_num_rows)
609 : PHP_FE(pg_num_fields, arginfo_pg_num_fields)
610 : PHP_FE(pg_field_name, arginfo_pg_field_name)
611 : PHP_FE(pg_field_num, arginfo_pg_field_num)
612 : PHP_FE(pg_field_size, arginfo_pg_field_size)
613 : PHP_FE(pg_field_type, arginfo_pg_field_type)
614 : PHP_FE(pg_field_type_oid, arginfo_pg_field_type_oid)
615 : PHP_FE(pg_field_prtlen, arginfo_pg_field_prtlen)
616 : PHP_FE(pg_field_is_null,arginfo_pg_field_is_null)
617 : #ifdef HAVE_PQFTABLE
618 : PHP_FE(pg_field_table, arginfo_pg_field_table)
619 : #endif
620 : /* async message function */
621 : PHP_FE(pg_get_notify, arginfo_pg_get_notify)
622 : PHP_FE(pg_get_pid, arginfo_pg_get_pid)
623 : /* error message functions */
624 : PHP_FE(pg_result_error, arginfo_pg_result_error)
625 : #if HAVE_PQRESULTERRORFIELD
626 : PHP_FE(pg_result_error_field, arginfo_pg_result_error_field)
627 : #endif
628 : PHP_FE(pg_last_error, arginfo_pg_last_error)
629 : PHP_FE(pg_last_notice, arginfo_pg_last_notice)
630 : /* copy functions */
631 : PHP_FE(pg_put_line, arginfo_pg_put_line)
632 : PHP_FE(pg_end_copy, arginfo_pg_end_copy)
633 : PHP_FE(pg_copy_to, arginfo_pg_copy_to)
634 : PHP_FE(pg_copy_from, arginfo_pg_copy_from)
635 : /* debug functions */
636 : PHP_FE(pg_trace, arginfo_pg_trace)
637 : PHP_FE(pg_untrace, arginfo_pg_untrace)
638 : /* large object functions */
639 : PHP_FE(pg_lo_create, arginfo_pg_lo_create)
640 : PHP_FE(pg_lo_unlink, arginfo_pg_lo_unlink)
641 : PHP_FE(pg_lo_open, arginfo_pg_lo_open)
642 : PHP_FE(pg_lo_close, arginfo_pg_lo_close)
643 : PHP_FE(pg_lo_read, arginfo_pg_lo_read)
644 : PHP_FE(pg_lo_write, arginfo_pg_lo_write)
645 : PHP_FE(pg_lo_read_all, arginfo_pg_lo_read_all)
646 : PHP_FE(pg_lo_import, arginfo_pg_lo_import)
647 : PHP_FE(pg_lo_export, arginfo_pg_lo_export)
648 : PHP_FE(pg_lo_seek, arginfo_pg_lo_seek)
649 : PHP_FE(pg_lo_tell, arginfo_pg_lo_tell)
650 : /* utility functions */
651 : #if HAVE_PQESCAPE
652 : PHP_FE(pg_escape_string, arginfo_pg_escape_string)
653 : PHP_FE(pg_escape_bytea, arginfo_pg_escape_bytea)
654 : PHP_FE(pg_unescape_bytea, arginfo_pg_unescape_bytea)
655 : #endif
656 : #if HAVE_PQSETERRORVERBOSITY
657 : PHP_FE(pg_set_error_verbosity, arginfo_pg_set_error_verbosity)
658 : #endif
659 : #if HAVE_PQCLIENTENCODING
660 : PHP_FE(pg_client_encoding, arginfo_pg_client_encoding)
661 : PHP_FE(pg_set_client_encoding, arginfo_pg_set_client_encoding)
662 : #endif
663 : /* misc function */
664 : PHP_FE(pg_meta_data, arginfo_pg_meta_data)
665 : PHP_FE(pg_convert, arginfo_pg_convert)
666 : PHP_FE(pg_insert, arginfo_pg_insert)
667 : PHP_FE(pg_update, arginfo_pg_update)
668 : PHP_FE(pg_delete, arginfo_pg_delete)
669 : PHP_FE(pg_select, arginfo_pg_select)
670 : /* aliases for downwards compatibility */
671 : PHP_FALIAS(pg_exec, pg_query, arginfo_pg_query)
672 : PHP_FALIAS(pg_getlastoid, pg_last_oid, arginfo_pg_last_oid)
673 : #if HAVE_PQCMDTUPLES
674 : PHP_FALIAS(pg_cmdtuples, pg_affected_rows, arginfo_pg_affected_rows)
675 : #endif
676 : PHP_FALIAS(pg_errormessage, pg_last_error, arginfo_pg_last_error)
677 : PHP_FALIAS(pg_numrows, pg_num_rows, arginfo_pg_num_rows)
678 : PHP_FALIAS(pg_numfields, pg_num_fields, arginfo_pg_num_fields)
679 : PHP_FALIAS(pg_fieldname, pg_field_name, arginfo_pg_field_name)
680 : PHP_FALIAS(pg_fieldsize, pg_field_size, arginfo_pg_field_size)
681 : PHP_FALIAS(pg_fieldtype, pg_field_type, arginfo_pg_field_type)
682 : PHP_FALIAS(pg_fieldnum, pg_field_num, arginfo_pg_field_num)
683 : PHP_FALIAS(pg_fieldprtlen, pg_field_prtlen, arginfo_pg_field_prtlen)
684 : PHP_FALIAS(pg_fieldisnull, pg_field_is_null, arginfo_pg_field_is_null)
685 : PHP_FALIAS(pg_freeresult, pg_free_result, arginfo_pg_free_result)
686 : PHP_FALIAS(pg_result, pg_fetch_result, arginfo_pg_get_result)
687 : PHP_FALIAS(pg_loreadall, pg_lo_read_all, arginfo_pg_lo_read_all)
688 : PHP_FALIAS(pg_locreate, pg_lo_create, arginfo_pg_lo_create)
689 : PHP_FALIAS(pg_lounlink, pg_lo_unlink, arginfo_pg_lo_unlink)
690 : PHP_FALIAS(pg_loopen, pg_lo_open, arginfo_pg_lo_open)
691 : PHP_FALIAS(pg_loclose, pg_lo_close, arginfo_pg_lo_close)
692 : PHP_FALIAS(pg_loread, pg_lo_read, arginfo_pg_lo_read)
693 : PHP_FALIAS(pg_lowrite, pg_lo_write, arginfo_pg_lo_write)
694 : PHP_FALIAS(pg_loimport, pg_lo_import, arginfo_pg_lo_import)
695 : PHP_FALIAS(pg_loexport, pg_lo_export, arginfo_pg_lo_export)
696 : #if HAVE_PQCLIENTENCODING
697 : PHP_FALIAS(pg_clientencoding, pg_client_encoding, arginfo_pg_client_encoding)
698 : PHP_FALIAS(pg_setclientencoding, pg_set_client_encoding, arginfo_pg_set_client_encoding)
699 : #endif
700 : {NULL, NULL, NULL}
701 : };
702 : /* }}} */
703 :
704 : /* {{{ pgsql_module_entry
705 : */
706 : zend_module_entry pgsql_module_entry = {
707 : STANDARD_MODULE_HEADER,
708 : "pgsql",
709 : pgsql_functions,
710 : PHP_MINIT(pgsql),
711 : PHP_MSHUTDOWN(pgsql),
712 : PHP_RINIT(pgsql),
713 : PHP_RSHUTDOWN(pgsql),
714 : PHP_MINFO(pgsql),
715 : NO_VERSION_YET,
716 : PHP_MODULE_GLOBALS(pgsql),
717 : PHP_GINIT(pgsql),
718 : NULL,
719 : NULL,
720 : STANDARD_MODULE_PROPERTIES_EX
721 : };
722 : /* }}} */
723 :
724 : #ifdef COMPILE_DL_PGSQL
725 : ZEND_GET_MODULE(pgsql)
726 : #endif
727 :
728 : static int le_link, le_plink, le_result, le_lofp, le_string;
729 :
730 : /* {{{ _php_pgsql_trim_message */
731 : static char * _php_pgsql_trim_message(const char *message, int *len)
732 13 : {
733 13 : register int i = strlen(message)-1;
734 :
735 13 : if (i>1 && (message[i-1] == '\r' || message[i-1] == '\n') && message[i] == '.') {
736 0 : --i;
737 : }
738 37 : while (i>0 && (message[i] == '\r' || message[i] == '\n')) {
739 11 : --i;
740 : }
741 13 : ++i;
742 13 : if (len) {
743 6 : *len = i;
744 : }
745 13 : return estrndup(message, i);
746 : }
747 : /* }}} */
748 :
749 : /* {{{ _php_pgsql_trim_result */
750 : static inline char * _php_pgsql_trim_result(PGconn * pgsql, char **buf)
751 2 : {
752 2 : return *buf = _php_pgsql_trim_message(PQerrorMessage(pgsql), NULL);
753 : }
754 : /* }}} */
755 :
756 : #define PQErrorMessageTrim(pgsql, buf) _php_pgsql_trim_result(pgsql, buf)
757 :
758 : #define PHP_PQ_ERROR(text, pgsql) { \
759 : char *msgbuf = _php_pgsql_trim_message(PQerrorMessage(pgsql), NULL); \
760 : php_error_docref(NULL TSRMLS_CC, E_WARNING, text, msgbuf); \
761 : efree(msgbuf); \
762 : } \
763 :
764 : /* {{{ php_pgsql_set_default_link
765 : */
766 : static void php_pgsql_set_default_link(int id TSRMLS_DC)
767 87 : {
768 87 : zend_list_addref(id);
769 :
770 87 : if (PGG(default_link) != -1) {
771 0 : zend_list_delete(PGG(default_link));
772 : }
773 :
774 87 : PGG(default_link) = id;
775 87 : }
776 : /* }}} */
777 :
778 : /* {{{ _close_pgsql_link
779 : */
780 : static void _close_pgsql_link(zend_rsrc_list_entry *rsrc TSRMLS_DC)
781 86 : {
782 86 : PGconn *link = (PGconn *)rsrc->ptr;
783 : PGresult *res;
784 :
785 173 : while ((res = PQgetResult(link))) {
786 1 : PQclear(res);
787 : }
788 86 : PQfinish(link);
789 86 : PGG(num_links)--;
790 86 : }
791 : /* }}} */
792 :
793 : /* {{{ _close_pgsql_plink
794 : */
795 : static void _close_pgsql_plink(zend_rsrc_list_entry *rsrc TSRMLS_DC)
796 1 : {
797 1 : PGconn *link = (PGconn *)rsrc->ptr;
798 : PGresult *res;
799 :
800 2 : while ((res = PQgetResult(link))) {
801 0 : PQclear(res);
802 : }
803 1 : PQfinish(link);
804 1 : PGG(num_persistent)--;
805 1 : PGG(num_links)--;
806 1 : }
807 : /* }}} */
808 :
809 : /* {{{ _php_pgsql_notice_handler
810 : */
811 : static void _php_pgsql_notice_handler(void *resource_id, const char *message)
812 6 : {
813 : php_pgsql_notice *notice;
814 :
815 : TSRMLS_FETCH();
816 6 : if (! PGG(ignore_notices)) {
817 6 : notice = (php_pgsql_notice *)emalloc(sizeof(php_pgsql_notice));
818 6 : notice->message = _php_pgsql_trim_message(message, ¬ice->len);
819 6 : if (PGG(log_notices)) {
820 1 : php_error_docref(NULL TSRMLS_CC, E_NOTICE, "%s", notice->message);
821 : }
822 6 : zend_hash_index_update(&PGG(notices), (ulong)resource_id, (void **)¬ice, sizeof(php_pgsql_notice *), NULL);
823 : }
824 6 : }
825 : /* }}} */
826 :
827 : #define PHP_PGSQL_NOTICE_PTR_DTOR (void (*)(void *))_php_pgsql_notice_ptr_dtor
828 :
829 : /* {{{ _php_pgsql_notice_dtor
830 : */
831 : static void _php_pgsql_notice_ptr_dtor(void **ptr)
832 6 : {
833 6 : php_pgsql_notice *notice = (php_pgsql_notice *)*ptr;
834 6 : if (notice) {
835 6 : efree(notice->message);
836 6 : efree(notice);
837 6 : notice = NULL;
838 : }
839 6 : }
840 : /* }}} */
841 :
842 : /* {{{ _rollback_transactions
843 : */
844 : static int _rollback_transactions(zend_rsrc_list_entry *rsrc TSRMLS_DC)
845 323 : {
846 : PGconn *link;
847 : PGresult *res;
848 : int orig;
849 :
850 323 : if (Z_TYPE_P(rsrc) != le_plink)
851 322 : return 0;
852 :
853 1 : link = (PGconn *) rsrc->ptr;
854 :
855 1 : if (PQ_SETNONBLOCKING(link, 0)) {
856 0 : php_error_docref("ref.pgsql" TSRMLS_CC, E_NOTICE, "Cannot set connection to blocking mode");
857 0 : return -1;
858 : }
859 :
860 2 : while ((res = PQgetResult(link))) {
861 0 : PQclear(res);
862 : }
863 : #if HAVE_PGTRANSACTIONSTATUS && HAVE_PQPROTOCOLVERSION
864 1 : if ((PQprotocolVersion(link) >= 3 && PQtransactionStatus(link) != PQTRANS_IDLE) || PQprotocolVersion(link) < 3)
865 : #endif
866 : {
867 0 : orig = PGG(ignore_notices);
868 0 : PGG(ignore_notices) = 1;
869 : #if HAVE_PGTRANSACTIONSTATUS && HAVE_PQPROTOCOLVERSION
870 0 : res = PQexec(link,"ROLLBACK;");
871 : #else
872 : res = PQexec(link,"BEGIN;");
873 : PQclear(res);
874 : res = PQexec(link,"ROLLBACK;");
875 : #endif
876 0 : PQclear(res);
877 0 : PGG(ignore_notices) = orig;
878 : }
879 :
880 1 : return 0;
881 : }
882 : /* }}} */
883 :
884 : /* {{{ _free_ptr
885 : */
886 : static void _free_ptr(zend_rsrc_list_entry *rsrc TSRMLS_DC)
887 705 : {
888 705 : pgLofp *lofp = (pgLofp *)rsrc->ptr;
889 705 : efree(lofp);
890 705 : }
891 : /* }}} */
892 :
893 : /* {{{ _free_result
894 : */
895 : static void _free_result(zend_rsrc_list_entry *rsrc TSRMLS_DC)
896 1120 : {
897 1120 : pgsql_result_handle *pg_result = (pgsql_result_handle *)rsrc->ptr;
898 :
899 1120 : PQclear(pg_result->result);
900 1120 : efree(pg_result);
901 1120 : }
902 : /* }}} */
903 :
904 : /* {{{ PHP_INI
905 : */
906 : PHP_INI_BEGIN()
907 : STD_PHP_INI_BOOLEAN( "pgsql.allow_persistent", "1", PHP_INI_SYSTEM, OnUpdateBool, allow_persistent, zend_pgsql_globals, pgsql_globals)
908 : STD_PHP_INI_ENTRY_EX("pgsql.max_persistent", "-1", PHP_INI_SYSTEM, OnUpdateLong, max_persistent, zend_pgsql_globals, pgsql_globals, display_link_numbers)
909 : STD_PHP_INI_ENTRY_EX("pgsql.max_links", "-1", PHP_INI_SYSTEM, OnUpdateLong, max_links, zend_pgsql_globals, pgsql_globals, display_link_numbers)
910 : STD_PHP_INI_BOOLEAN( "pgsql.auto_reset_persistent", "0", PHP_INI_SYSTEM, OnUpdateBool, auto_reset_persistent, zend_pgsql_globals, pgsql_globals)
911 : STD_PHP_INI_BOOLEAN( "pgsql.ignore_notice", "0", PHP_INI_ALL, OnUpdateBool, ignore_notices, zend_pgsql_globals, pgsql_globals)
912 : STD_PHP_INI_BOOLEAN( "pgsql.log_notice", "0", PHP_INI_ALL, OnUpdateBool, log_notices, zend_pgsql_globals, pgsql_globals)
913 : PHP_INI_END()
914 : /* }}} */
915 :
916 : /* {{{ PHP_GINIT_FUNCTION
917 : */
918 : static PHP_GINIT_FUNCTION(pgsql)
919 17007 : {
920 17007 : memset(pgsql_globals, 0, sizeof(zend_pgsql_globals));
921 : /* Initilize notice message hash at MINIT only */
922 17007 : zend_hash_init_ex(&pgsql_globals->notices, 0, NULL, PHP_PGSQL_NOTICE_PTR_DTOR, 1, 0);
923 17007 : }
924 : /* }}} */
925 :
926 : /* {{{ PHP_MINIT_FUNCTION
927 : */
928 : PHP_MINIT_FUNCTION(pgsql)
929 17007 : {
930 17007 : REGISTER_INI_ENTRIES();
931 :
932 17007 : le_link = zend_register_list_destructors_ex(_close_pgsql_link, NULL, "pgsql link", module_number);
933 17007 : le_plink = zend_register_list_destructors_ex(NULL, _close_pgsql_plink, "pgsql link persistent", module_number);
934 17007 : le_result = zend_register_list_destructors_ex(_free_result, NULL, "pgsql result", module_number);
935 17007 : le_lofp = zend_register_list_destructors_ex(_free_ptr, NULL, "pgsql large object", module_number);
936 17007 : le_string = zend_register_list_destructors_ex(_free_ptr, NULL, "pgsql string", module_number);
937 : /* For connection option */
938 17007 : REGISTER_LONG_CONSTANT("PGSQL_CONNECT_FORCE_NEW", PGSQL_CONNECT_FORCE_NEW, CONST_CS | CONST_PERSISTENT);
939 : /* For pg_fetch_array() */
940 17007 : REGISTER_LONG_CONSTANT("PGSQL_ASSOC", PGSQL_ASSOC, CONST_CS | CONST_PERSISTENT);
941 17007 : REGISTER_LONG_CONSTANT("PGSQL_NUM", PGSQL_NUM, CONST_CS | CONST_PERSISTENT);
942 17007 : REGISTER_LONG_CONSTANT("PGSQL_BOTH", PGSQL_BOTH, CONST_CS | CONST_PERSISTENT);
943 : /* For pg_connection_status() */
944 17007 : REGISTER_LONG_CONSTANT("PGSQL_CONNECTION_BAD", CONNECTION_BAD, CONST_CS | CONST_PERSISTENT);
945 17007 : REGISTER_LONG_CONSTANT("PGSQL_CONNECTION_OK", CONNECTION_OK, CONST_CS | CONST_PERSISTENT);
946 : #if HAVE_PGTRANSACTIONSTATUS
947 : /* For pg_transaction_status() */
948 17007 : REGISTER_LONG_CONSTANT("PGSQL_TRANSACTION_IDLE", PQTRANS_IDLE, CONST_CS | CONST_PERSISTENT);
949 17007 : REGISTER_LONG_CONSTANT("PGSQL_TRANSACTION_ACTIVE", PQTRANS_ACTIVE, CONST_CS | CONST_PERSISTENT);
950 17007 : REGISTER_LONG_CONSTANT("PGSQL_TRANSACTION_INTRANS", PQTRANS_INTRANS, CONST_CS | CONST_PERSISTENT);
951 17007 : REGISTER_LONG_CONSTANT("PGSQL_TRANSACTION_INERROR", PQTRANS_INERROR, CONST_CS | CONST_PERSISTENT);
952 17007 : REGISTER_LONG_CONSTANT("PGSQL_TRANSACTION_UNKNOWN", PQTRANS_UNKNOWN, CONST_CS | CONST_PERSISTENT);
953 : #endif
954 : #if HAVE_PQSETERRORVERBOSITY
955 : /* For pg_set_error_verbosity() */
956 17007 : REGISTER_LONG_CONSTANT("PGSQL_ERRORS_TERSE", PQERRORS_TERSE, CONST_CS | CONST_PERSISTENT);
957 17007 : REGISTER_LONG_CONSTANT("PGSQL_ERRORS_DEFAULT", PQERRORS_DEFAULT, CONST_CS | CONST_PERSISTENT);
958 17007 : REGISTER_LONG_CONSTANT("PGSQL_ERRORS_VERBOSE", PQERRORS_VERBOSE, CONST_CS | CONST_PERSISTENT);
959 : #endif
960 : /* For lo_seek() */
961 17007 : REGISTER_LONG_CONSTANT("PGSQL_SEEK_SET", SEEK_SET, CONST_CS | CONST_PERSISTENT);
962 17007 : REGISTER_LONG_CONSTANT("PGSQL_SEEK_CUR", SEEK_CUR, CONST_CS | CONST_PERSISTENT);
963 17007 : REGISTER_LONG_CONSTANT("PGSQL_SEEK_END", SEEK_END, CONST_CS | CONST_PERSISTENT);
964 : /* For pg_result_status() return value type */
965 17007 : REGISTER_LONG_CONSTANT("PGSQL_STATUS_LONG", PGSQL_STATUS_LONG, CONST_CS | CONST_PERSISTENT);
966 17007 : REGISTER_LONG_CONSTANT("PGSQL_STATUS_STRING", PGSQL_STATUS_STRING, CONST_CS | CONST_PERSISTENT);
967 : /* For pg_result_status() return value */
968 17007 : REGISTER_LONG_CONSTANT("PGSQL_EMPTY_QUERY", PGRES_EMPTY_QUERY, CONST_CS | CONST_PERSISTENT);
969 17007 : REGISTER_LONG_CONSTANT("PGSQL_COMMAND_OK", PGRES_COMMAND_OK, CONST_CS | CONST_PERSISTENT);
970 17007 : REGISTER_LONG_CONSTANT("PGSQL_TUPLES_OK", PGRES_TUPLES_OK, CONST_CS | CONST_PERSISTENT);
971 17007 : REGISTER_LONG_CONSTANT("PGSQL_COPY_OUT", PGRES_COPY_OUT, CONST_CS | CONST_PERSISTENT);
972 17007 : REGISTER_LONG_CONSTANT("PGSQL_COPY_IN", PGRES_COPY_IN, CONST_CS | CONST_PERSISTENT);
973 17007 : REGISTER_LONG_CONSTANT("PGSQL_BAD_RESPONSE", PGRES_BAD_RESPONSE, CONST_CS | CONST_PERSISTENT);
974 17007 : REGISTER_LONG_CONSTANT("PGSQL_NONFATAL_ERROR", PGRES_NONFATAL_ERROR, CONST_CS | CONST_PERSISTENT);
975 17007 : REGISTER_LONG_CONSTANT("PGSQL_FATAL_ERROR", PGRES_FATAL_ERROR, CONST_CS | CONST_PERSISTENT);
976 : #if HAVE_PQRESULTERRORFIELD
977 : /* For pg_result_error_field() field codes */
978 17007 : REGISTER_LONG_CONSTANT("PGSQL_DIAG_SEVERITY", PG_DIAG_SEVERITY, CONST_CS | CONST_PERSISTENT);
979 17007 : REGISTER_LONG_CONSTANT("PGSQL_DIAG_SQLSTATE", PG_DIAG_SQLSTATE, CONST_CS | CONST_PERSISTENT);
980 17007 : REGISTER_LONG_CONSTANT("PGSQL_DIAG_MESSAGE_PRIMARY", PG_DIAG_MESSAGE_PRIMARY, CONST_CS | CONST_PERSISTENT);
981 17007 : REGISTER_LONG_CONSTANT("PGSQL_DIAG_MESSAGE_DETAIL", PG_DIAG_MESSAGE_DETAIL, CONST_CS | CONST_PERSISTENT);
982 17007 : REGISTER_LONG_CONSTANT("PGSQL_DIAG_MESSAGE_HINT", PG_DIAG_MESSAGE_HINT, CONST_CS | CONST_PERSISTENT);
983 17007 : REGISTER_LONG_CONSTANT("PGSQL_DIAG_STATEMENT_POSITION", PG_DIAG_STATEMENT_POSITION, CONST_CS | CONST_PERSISTENT);
984 : #ifdef PG_DIAG_INTERNAL_POSITION
985 17007 : REGISTER_LONG_CONSTANT("PGSQL_DIAG_INTERNAL_POSITION", PG_DIAG_INTERNAL_POSITION, CONST_CS | CONST_PERSISTENT);
986 : #endif
987 : #ifdef PG_DIAG_INTERNAL_QUERY
988 17007 : REGISTER_LONG_CONSTANT("PGSQL_DIAG_INTERNAL_QUERY", PG_DIAG_INTERNAL_QUERY, CONST_CS | CONST_PERSISTENT);
989 : #endif
990 17007 : REGISTER_LONG_CONSTANT("PGSQL_DIAG_CONTEXT", PG_DIAG_CONTEXT, CONST_CS | CONST_PERSISTENT);
991 17007 : REGISTER_LONG_CONSTANT("PGSQL_DIAG_SOURCE_FILE", PG_DIAG_SOURCE_FILE, CONST_CS | CONST_PERSISTENT);
992 17007 : REGISTER_LONG_CONSTANT("PGSQL_DIAG_SOURCE_LINE", PG_DIAG_SOURCE_LINE, CONST_CS | CONST_PERSISTENT);
993 17007 : REGISTER_LONG_CONSTANT("PGSQL_DIAG_SOURCE_FUNCTION", PG_DIAG_SOURCE_FUNCTION, CONST_CS | CONST_PERSISTENT);
994 : #endif
995 : /* pg_convert options */
996 17007 : REGISTER_LONG_CONSTANT("PGSQL_CONV_IGNORE_DEFAULT", PGSQL_CONV_IGNORE_DEFAULT, CONST_CS | CONST_PERSISTENT);
997 17007 : REGISTER_LONG_CONSTANT("PGSQL_CONV_FORCE_NULL", PGSQL_CONV_FORCE_NULL, CONST_CS | CONST_PERSISTENT);
998 17007 : REGISTER_LONG_CONSTANT("PGSQL_CONV_IGNORE_NOT_NULL", PGSQL_CONV_IGNORE_NOT_NULL, CONST_CS | CONST_PERSISTENT);
999 : /* pg_insert/update/delete/select options */
1000 17007 : REGISTER_LONG_CONSTANT("PGSQL_DML_NO_CONV", PGSQL_DML_NO_CONV, CONST_CS | CONST_PERSISTENT);
1001 17007 : REGISTER_LONG_CONSTANT("PGSQL_DML_EXEC", PGSQL_DML_EXEC, CONST_CS | CONST_PERSISTENT);
1002 17007 : REGISTER_LONG_CONSTANT("PGSQL_DML_ASYNC", PGSQL_DML_ASYNC, CONST_CS | CONST_PERSISTENT);
1003 17007 : REGISTER_LONG_CONSTANT("PGSQL_DML_STRING", PGSQL_DML_STRING, CONST_CS | CONST_PERSISTENT);
1004 17007 : return SUCCESS;
1005 : }
1006 : /* }}} */
1007 :
1008 : /* {{{ PHP_MSHUTDOWN_FUNCTION
1009 : */
1010 : PHP_MSHUTDOWN_FUNCTION(pgsql)
1011 17039 : {
1012 17039 : UNREGISTER_INI_ENTRIES();
1013 17039 : zend_hash_destroy(&PGG(notices));
1014 :
1015 17039 : return SUCCESS;
1016 : }
1017 : /* }}} */
1018 :
1019 : /* {{{ PHP_RINIT_FUNCTION
1020 : */
1021 : PHP_RINIT_FUNCTION(pgsql)
1022 16993 : {
1023 16993 : PGG(default_link)=-1;
1024 16993 : PGG(num_links) = PGG(num_persistent);
1025 16993 : return SUCCESS;
1026 : }
1027 : /* }}} */
1028 :
1029 : /* {{{ PHP_RSHUTDOWN_FUNCTION
1030 : */
1031 : PHP_RSHUTDOWN_FUNCTION(pgsql)
1032 17025 : {
1033 : /* clean up notice messages */
1034 17025 : zend_hash_clean(&PGG(notices));
1035 : /* clean up persistent connection */
1036 17025 : zend_hash_apply(&EG(persistent_list), (apply_func_t) _rollback_transactions TSRMLS_CC);
1037 17025 : return SUCCESS;
1038 : }
1039 : /* }}} */
1040 :
1041 : /* {{{ PHP_MINFO_FUNCTION
1042 : */
1043 : PHP_MINFO_FUNCTION(pgsql)
1044 43 : {
1045 : char buf[256];
1046 :
1047 43 : php_info_print_table_start();
1048 43 : php_info_print_table_header(2, "PostgreSQL Support", "enabled");
1049 : #if HAVE_PG_CONFIG_H
1050 43 : php_info_print_table_row(2, "PostgreSQL(libpq) Version", PG_VERSION);
1051 : #ifdef HAVE_PGSQL_WITH_MULTIBYTE_SUPPORT
1052 43 : php_info_print_table_row(2, "Multibyte character support", "enabled");
1053 : #else
1054 : php_info_print_table_row(2, "Multibyte character support", "disabled");
1055 : #endif
1056 : #ifdef USE_SSL
1057 43 : php_info_print_table_row(2, "SSL support", "enabled");
1058 : #else
1059 : php_info_print_table_row(2, "SSL support", "disabled");
1060 : #endif
1061 : #endif /* HAVE_PG_CONFIG_H */
1062 43 : snprintf(buf, sizeof(buf), "%ld", PGG(num_persistent));
1063 43 : php_info_print_table_row(2, "Active Persistent Links", buf);
1064 43 : snprintf(buf, sizeof(buf), "%ld", PGG(num_links));
1065 43 : php_info_print_table_row(2, "Active Links", buf);
1066 43 : php_info_print_table_end();
1067 :
1068 43 : DISPLAY_INI_ENTRIES();
1069 43 : }
1070 : /* }}} */
1071 :
1072 :
1073 : /* {{{ php_pgsql_do_connect
1074 : */
1075 : static void php_pgsql_do_connect(INTERNAL_FUNCTION_PARAMETERS, int persistent)
1076 87 : {
1077 87 : char *host=NULL,*port=NULL,*options=NULL,*tty=NULL,*dbname=NULL,*connstring=NULL;
1078 : PGconn *pgsql;
1079 87 : smart_str str = {0};
1080 : zval **args[5];
1081 87 : int i, connect_type = 0;
1082 : PGresult *pg_result;
1083 :
1084 87 : if (ZEND_NUM_ARGS() < 1 || ZEND_NUM_ARGS() > 5
1085 : || zend_get_parameters_array_ex(ZEND_NUM_ARGS(), args) == FAILURE) {
1086 0 : WRONG_PARAM_COUNT;
1087 : }
1088 :
1089 87 : smart_str_appends(&str, "pgsql");
1090 :
1091 174 : for (i = 0; i < ZEND_NUM_ARGS(); i++) {
1092 87 : convert_to_string_ex(args[i]);
1093 87 : smart_str_appendc(&str, '_');
1094 87 : smart_str_appendl(&str, Z_STRVAL_PP(args[i]), Z_STRLEN_PP(args[i]));
1095 : }
1096 :
1097 87 : smart_str_0(&str);
1098 :
1099 87 : if (ZEND_NUM_ARGS() == 1) { /* new style, using connection string */
1100 87 : connstring = Z_STRVAL_PP(args[0]);
1101 0 : } else if (ZEND_NUM_ARGS() == 2 ) { /* Safe to add conntype_option, since 2 args was illegal */
1102 0 : connstring = Z_STRVAL_PP(args[0]);
1103 0 : convert_to_long_ex(args[1]);
1104 0 : connect_type = Z_LVAL_PP(args[1]);
1105 : } else {
1106 0 : host = Z_STRVAL_PP(args[0]);
1107 0 : port = Z_STRVAL_PP(args[1]);
1108 0 : dbname = Z_STRVAL_PP(args[ZEND_NUM_ARGS()-1]);
1109 :
1110 0 : switch (ZEND_NUM_ARGS()) {
1111 : case 5:
1112 0 : tty = Z_STRVAL_PP(args[3]);
1113 : /* fall through */
1114 : case 4:
1115 0 : options = Z_STRVAL_PP(args[2]);
1116 : break;
1117 : }
1118 : }
1119 :
1120 88 : if (persistent && PGG(allow_persistent)) {
1121 : zend_rsrc_list_entry *le;
1122 :
1123 : /* try to find if we already have this link in our persistent list */
1124 1 : if (zend_hash_find(&EG(persistent_list), str.c, str.len+1, (void **) &le)==FAILURE) { /* we don't */
1125 : zend_rsrc_list_entry new_le;
1126 :
1127 1 : if (PGG(max_links)!=-1 && PGG(num_links)>=PGG(max_links)) {
1128 0 : php_error_docref(NULL TSRMLS_CC, E_WARNING,
1129 : "Cannot create new link. Too many open links (%ld)", PGG(num_links));
1130 0 : goto err;
1131 : }
1132 1 : if (PGG(max_persistent)!=-1 && PGG(num_persistent)>=PGG(max_persistent)) {
1133 0 : php_error_docref(NULL TSRMLS_CC, E_WARNING,
1134 : "Cannot create new link. Too many open persistent links (%ld)", PGG(num_persistent));
1135 0 : goto err;
1136 : }
1137 :
1138 : /* create the link */
1139 1 : if (connstring) {
1140 1 : pgsql=PQconnectdb(connstring);
1141 : } else {
1142 0 : pgsql=PQsetdb(host,port,options,tty,dbname);
1143 : }
1144 1 : if (pgsql==NULL || PQstatus(pgsql)==CONNECTION_BAD) {
1145 0 : PHP_PQ_ERROR("Unable to connect to PostgreSQL server: %s", pgsql)
1146 0 : if (pgsql) {
1147 0 : PQfinish(pgsql);
1148 : }
1149 0 : goto err;
1150 : }
1151 :
1152 : /* hash it up */
1153 1 : Z_TYPE(new_le) = le_plink;
1154 1 : new_le.ptr = pgsql;
1155 1 : if (zend_hash_update(&EG(persistent_list), str.c, str.len+1, (void *) &new_le, sizeof(zend_rsrc_list_entry), NULL)==FAILURE) {
1156 0 : goto err;
1157 : }
1158 1 : PGG(num_links)++;
1159 1 : PGG(num_persistent)++;
1160 : } else { /* we do */
1161 0 : if (Z_TYPE_P(le) != le_plink) {
1162 0 : RETURN_FALSE;
1163 : }
1164 : /* ensure that the link did not die */
1165 0 : if (PGG(auto_reset_persistent) & 1) {
1166 : /* need to send & get something from backend to
1167 : make sure we catch CONNECTION_BAD everytime */
1168 : PGresult *pg_result;
1169 0 : pg_result = PQexec(le->ptr, "select 1");
1170 0 : PQclear(pg_result);
1171 : }
1172 0 : if (PQstatus(le->ptr)==CONNECTION_BAD) { /* the link died */
1173 0 : if (le->ptr == NULL) {
1174 0 : if (connstring) {
1175 0 : le->ptr=PQconnectdb(connstring);
1176 : } else {
1177 0 : le->ptr=PQsetdb(host,port,options,tty,dbname);
1178 : }
1179 : }
1180 : else {
1181 0 : PQreset(le->ptr);
1182 : }
1183 0 : if (le->ptr==NULL || PQstatus(le->ptr)==CONNECTION_BAD) {
1184 0 : php_error_docref(NULL TSRMLS_CC, E_WARNING,"PostgreSQL link lost, unable to reconnect");
1185 0 : zend_hash_del(&EG(persistent_list),str.c,str.len+1);
1186 0 : goto err;
1187 : }
1188 : }
1189 0 : pgsql = (PGconn *) le->ptr;
1190 : #if HAVE_PQPROTOCOLVERSION && HAVE_PQPARAMETERSTATUS
1191 0 : if (PQprotocolVersion(pgsql) >= 3 && atof(PQparameterStatus(pgsql, "server_version")) >= 7.2) {
1192 : #else
1193 : if (atof(PG_VERSION) >= 7.2) {
1194 : #endif
1195 0 : pg_result = PQexec(pgsql, "RESET ALL;");
1196 0 : PQclear(pg_result);
1197 : }
1198 : }
1199 1 : ZEND_REGISTER_RESOURCE(return_value, pgsql, le_plink);
1200 : } else { /* Non persistent connection */
1201 : zend_rsrc_list_entry *index_ptr,new_index_ptr;
1202 :
1203 : /* first we check the hash for the hashed_details key. if it exists,
1204 : * it should point us to the right offset where the actual pgsql link sits.
1205 : * if it doesn't, open a new pgsql link, add it to the resource list,
1206 : * and add a pointer to it with hashed_details as the key.
1207 : */
1208 86 : if (!(connect_type & PGSQL_CONNECT_FORCE_NEW)
1209 : && zend_hash_find(&EG(regular_list),str.c,str.len+1,(void **) &index_ptr)==SUCCESS) {
1210 : int type;
1211 : ulong link;
1212 : void *ptr;
1213 :
1214 1 : if (Z_TYPE_P(index_ptr) != le_index_ptr) {
1215 0 : RETURN_FALSE;
1216 : }
1217 1 : link = (uintptr_t) index_ptr->ptr;
1218 1 : ptr = zend_list_find(link,&type); /* check if the link is still there */
1219 1 : if (ptr && (type==le_link || type==le_plink)) {
1220 0 : Z_LVAL_P(return_value) = link;
1221 0 : zend_list_addref(link);
1222 0 : php_pgsql_set_default_link(link TSRMLS_CC);
1223 0 : Z_TYPE_P(return_value) = IS_RESOURCE;
1224 0 : goto cleanup;
1225 : } else {
1226 1 : zend_hash_del(&EG(regular_list),str.c,str.len+1);
1227 : }
1228 : }
1229 86 : if (PGG(max_links)!=-1 && PGG(num_links)>=PGG(max_links)) {
1230 0 : php_error_docref(NULL TSRMLS_CC, E_WARNING, "Cannot create new link. Too many open links (%ld)", PGG(num_links));
1231 0 : goto err;
1232 : }
1233 86 : if (connstring) {
1234 86 : pgsql = PQconnectdb(connstring);
1235 : } else {
1236 0 : pgsql = PQsetdb(host,port,options,tty,dbname);
1237 : }
1238 86 : if (pgsql==NULL || PQstatus(pgsql)==CONNECTION_BAD) {
1239 0 : PHP_PQ_ERROR("Unable to connect to PostgreSQL server: %s", pgsql);
1240 0 : if (pgsql) {
1241 0 : PQfinish(pgsql);
1242 : }
1243 0 : goto err;
1244 : }
1245 :
1246 : /* add it to the list */
1247 86 : ZEND_REGISTER_RESOURCE(return_value, pgsql, le_link);
1248 :
1249 : /* add it to the hash */
1250 86 : new_index_ptr.ptr = (void *) Z_LVAL_P(return_value);
1251 86 : Z_TYPE(new_index_ptr) = le_index_ptr;
1252 86 : if (zend_hash_update(&EG(regular_list),str.c,str.len+1,(void *) &new_index_ptr, sizeof(zend_rsrc_list_entry), NULL)==FAILURE) {
1253 0 : goto err;
1254 : }
1255 86 : PGG(num_links)++;
1256 : }
1257 : /* set notice processer */
1258 87 : if (! PGG(ignore_notices) && Z_TYPE_P(return_value) == IS_RESOURCE) {
1259 87 : PQsetNoticeProcessor(pgsql, _php_pgsql_notice_handler, (void*)Z_RESVAL_P(return_value));
1260 : }
1261 87 : php_pgsql_set_default_link(Z_LVAL_P(return_value) TSRMLS_CC);
1262 :
1263 87 : cleanup:
1264 87 : smart_str_free(&str);
1265 87 : return;
1266 :
1267 0 : err:
1268 0 : smart_str_free(&str);
1269 0 : RETURN_FALSE;
1270 : }
1271 : /* }}} */
1272 :
1273 : #if 0
1274 : /* {{{ php_pgsql_get_default_link
1275 : */
1276 : static int php_pgsql_get_default_link(INTERNAL_FUNCTION_PARAMETERS)
1277 : {
1278 : if (PGG(default_link)==-1) { /* no link opened yet, implicitly open one */
1279 : ht = 0;
1280 : php_pgsql_do_connect(INTERNAL_FUNCTION_PARAM_PASSTHRU,0);
1281 : }
1282 : return PGG(default_link);
1283 : }
1284 : /* }}} */
1285 : #endif
1286 :
1287 : /* {{{ proto resource pg_connect(string connection_string[, int connect_type] | [string host, string port [, string options [, string tty,]]] string database)
1288 : Open a PostgreSQL connection */
1289 : PHP_FUNCTION(pg_connect)
1290 86 : {
1291 86 : php_pgsql_do_connect(INTERNAL_FUNCTION_PARAM_PASSTHRU,0);
1292 86 : }
1293 : /* }}} */
1294 :
1295 : /* {{{ proto resource pg_pconnect(string connection_string | [string host, string port [, string options [, string tty,]]] string database)
1296 : Open a persistent PostgreSQL connection */
1297 : PHP_FUNCTION(pg_pconnect)
1298 1 : {
1299 1 : php_pgsql_do_connect(INTERNAL_FUNCTION_PARAM_PASSTHRU,1);
1300 1 : }
1301 : /* }}} */
1302 :
1303 : /* {{{ proto bool pg_close([resource connection])
1304 : Close a PostgreSQL connection */
1305 : PHP_FUNCTION(pg_close)
1306 16 : {
1307 16 : zval *pgsql_link = NULL;
1308 16 : int id = -1, argc = ZEND_NUM_ARGS();
1309 : PGconn *pgsql;
1310 :
1311 16 : if (zend_parse_parameters(argc TSRMLS_CC, "|r", &pgsql_link) == FAILURE) {
1312 0 : return;
1313 : }
1314 :
1315 16 : if (argc == 0) {
1316 0 : id = PGG(default_link);
1317 0 : CHECK_DEFAULT_LINK(id);
1318 : }
1319 :
1320 16 : if (pgsql_link == NULL && id == -1) {
1321 0 : RETURN_FALSE;
1322 : }
1323 :
1324 16 : ZEND_FETCH_RESOURCE2(pgsql, PGconn *, &pgsql_link, id, "PostgreSQL link", le_link, le_plink);
1325 :
1326 16 : if (id==-1) { /* explicit resource number */
1327 16 : zend_list_delete(Z_RESVAL_P(pgsql_link));
1328 : }
1329 :
1330 16 : if (id!=-1
1331 : || (pgsql_link && Z_RESVAL_P(pgsql_link)==PGG(default_link))) {
1332 16 : zend_list_delete(PGG(default_link));
1333 16 : PGG(default_link) = -1;
1334 : }
1335 :
1336 16 : RETURN_TRUE;
1337 : }
1338 : /* }}} */
1339 :
1340 :
1341 : #define PHP_PG_DBNAME 1
1342 : #define PHP_PG_ERROR_MESSAGE 2
1343 : #define PHP_PG_OPTIONS 3
1344 : #define PHP_PG_PORT 4
1345 : #define PHP_PG_TTY 5
1346 : #define PHP_PG_HOST 6
1347 : #define PHP_PG_VERSION 7
1348 :
1349 : /* {{{ php_pgsql_get_link_info
1350 : */
1351 : static void php_pgsql_get_link_info(INTERNAL_FUNCTION_PARAMETERS, int entry_type)
1352 13 : {
1353 13 : zval *pgsql_link = NULL;
1354 13 : int id = -1, argc = ZEND_NUM_ARGS();
1355 : PGconn *pgsql;
1356 : char *msgbuf;
1357 :
1358 13 : if (zend_parse_parameters(argc TSRMLS_CC, "|r", &pgsql_link) == FAILURE) {
1359 0 : return;
1360 : }
1361 :
1362 13 : if (argc == 0) {
1363 2 : id = PGG(default_link);
1364 2 : CHECK_DEFAULT_LINK(id);
1365 : }
1366 :
1367 13 : if (pgsql_link == NULL && id == -1) {
1368 0 : RETURN_FALSE;
1369 : }
1370 :
1371 13 : ZEND_FETCH_RESOURCE2(pgsql, PGconn *, &pgsql_link, id, "PostgreSQL link", le_link, le_plink);
1372 :
1373 13 : switch(entry_type) {
1374 : case PHP_PG_DBNAME:
1375 1 : Z_STRVAL_P(return_value) = PQdb(pgsql);
1376 1 : break;
1377 : case PHP_PG_ERROR_MESSAGE:
1378 2 : RETURN_STRING(PQErrorMessageTrim(pgsql, &msgbuf), 0);
1379 : return;
1380 : case PHP_PG_OPTIONS:
1381 1 : Z_STRVAL_P(return_value) = PQoptions(pgsql);
1382 1 : break;
1383 : case PHP_PG_PORT:
1384 1 : Z_STRVAL_P(return_value) = PQport(pgsql);
1385 1 : break;
1386 : case PHP_PG_TTY:
1387 1 : Z_STRVAL_P(return_value) = PQtty(pgsql);
1388 1 : break;
1389 : case PHP_PG_HOST:
1390 1 : Z_STRVAL_P(return_value) = PQhost(pgsql);
1391 1 : break;
1392 : case PHP_PG_VERSION:
1393 6 : array_init(return_value);
1394 6 : add_assoc_string(return_value, "client", PG_VERSION, 1);
1395 : #if HAVE_PQPROTOCOLVERSION
1396 6 : add_assoc_long(return_value, "protocol", PQprotocolVersion(pgsql));
1397 : #if HAVE_PQPARAMETERSTATUS
1398 6 : if (PQprotocolVersion(pgsql) >= 3) {
1399 6 : add_assoc_string(return_value, "server", (char*)PQparameterStatus(pgsql, "server_version"), 1);
1400 : }
1401 : #endif
1402 : #endif
1403 6 : return;
1404 : default:
1405 0 : RETURN_FALSE;
1406 : }
1407 5 : if (Z_STRVAL_P(return_value)) {
1408 5 : Z_STRLEN_P(return_value) = strlen(Z_STRVAL_P(return_value));
1409 5 : Z_STRVAL_P(return_value) = (char *) estrdup(Z_STRVAL_P(return_value));
1410 : } else {
1411 0 : Z_STRLEN_P(return_value) = 0;
1412 0 : Z_STRVAL_P(return_value) = (char *) estrdup("");
1413 : }
1414 5 : Z_TYPE_P(return_value) = IS_STRING;
1415 : }
1416 : /* }}} */
1417 :
1418 : /* {{{ proto string pg_dbname([resource connection])
1419 : Get the database name */
1420 : PHP_FUNCTION(pg_dbname)
1421 1 : {
1422 1 : php_pgsql_get_link_info(INTERNAL_FUNCTION_PARAM_PASSTHRU,PHP_PG_DBNAME);
1423 1 : }
1424 : /* }}} */
1425 :
1426 : /* {{{ proto string pg_last_error([resource connection])
1427 : Get the error message string */
1428 : PHP_FUNCTION(pg_last_error)
1429 2 : {
1430 2 : php_pgsql_get_link_info(INTERNAL_FUNCTION_PARAM_PASSTHRU,PHP_PG_ERROR_MESSAGE);
1431 2 : }
1432 : /* }}} */
1433 :
1434 : /* {{{ proto string pg_options([resource connection])
1435 : Get the options associated with the connection */
1436 : PHP_FUNCTION(pg_options)
1437 1 : {
1438 1 : php_pgsql_get_link_info(INTERNAL_FUNCTION_PARAM_PASSTHRU,PHP_PG_OPTIONS);
1439 1 : }
1440 : /* }}} */
1441 :
1442 : /* {{{ proto int pg_port([resource connection])
1443 : Return the port number associated with the connection */
1444 : PHP_FUNCTION(pg_port)
1445 1 : {
1446 1 : php_pgsql_get_link_info(INTERNAL_FUNCTION_PARAM_PASSTHRU,PHP_PG_PORT);
1447 1 : }
1448 : /* }}} */
1449 :
1450 : /* {{{ proto string pg_tty([resource connection])
1451 : Return the tty name associated with the connection */
1452 : PHP_FUNCTION(pg_tty)
1453 1 : {
1454 1 : php_pgsql_get_link_info(INTERNAL_FUNCTION_PARAM_PASSTHRU,PHP_PG_TTY);
1455 1 : }
1456 : /* }}} */
1457 :
1458 : /* {{{ proto string pg_host([resource connection])
1459 : Returns the host name associated with the connection */
1460 : PHP_FUNCTION(pg_host)
1461 1 : {
1462 1 : php_pgsql_get_link_info(INTERNAL_FUNCTION_PARAM_PASSTHRU,PHP_PG_HOST);
1463 1 : }
1464 : /* }}} */
1465 :
1466 : /* {{{ proto array pg_version([resource connection])
1467 : Returns an array with client, protocol and server version (when available) */
1468 : PHP_FUNCTION(pg_version)
1469 6 : {
1470 6 : php_pgsql_get_link_info(INTERNAL_FUNCTION_PARAM_PASSTHRU,PHP_PG_VERSION);
1471 6 : }
1472 : /* }}} */
1473 :
1474 : #if HAVE_PQPARAMETERSTATUS
1475 : /* {{{ proto string|false pg_parameter_status([resource connection,] string param_name)
1476 : Returns the value of a server parameter */
1477 : PHP_FUNCTION(pg_parameter_status)
1478 0 : {
1479 : zval *pgsql_link;
1480 : int id;
1481 : PGconn *pgsql;
1482 : char *param;
1483 : int len;
1484 :
1485 0 : if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS() TSRMLS_CC, "rs", &pgsql_link, ¶m, &len) == SUCCESS) {
1486 0 : id = -1;
1487 0 : } else if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", ¶m, &len) == SUCCESS) {
1488 0 : pgsql_link = NULL;
1489 0 : id = PGG(default_link);
1490 : } else {
1491 0 : RETURN_FALSE;
1492 : }
1493 0 : if (pgsql_link == NULL && id == -1) {
1494 0 : RETURN_FALSE;
1495 : }
1496 :
1497 0 : ZEND_FETCH_RESOURCE2(pgsql, PGconn *, &pgsql_link, id, "PostgreSQL link", le_link, le_plink);
1498 :
1499 0 : param = (char*)PQparameterStatus(pgsql, param);
1500 0 : if (param) {
1501 0 : RETURN_STRING(param, 1);
1502 : } else {
1503 0 : RETURN_FALSE;
1504 : }
1505 : }
1506 : /* }}} */
1507 : #endif
1508 :
1509 : /* {{{ proto bool pg_ping([resource connection])
1510 : Ping database. If connection is bad, try to reconnect. */
1511 : PHP_FUNCTION(pg_ping)
1512 1 : {
1513 : zval *pgsql_link;
1514 : int id;
1515 : PGconn *pgsql;
1516 : PGresult *res;
1517 :
1518 1 : if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS() TSRMLS_CC, "r", &pgsql_link) == SUCCESS) {
1519 1 : id = -1;
1520 : } else {
1521 0 : pgsql_link = NULL;
1522 0 : id = PGG(default_link);
1523 : }
1524 1 : if (pgsql_link == NULL && id == -1) {
1525 0 : RETURN_FALSE;
1526 : }
1527 :
1528 1 : ZEND_FETCH_RESOURCE2(pgsql, PGconn *, &pgsql_link, id, "PostgreSQL link", le_link, le_plink);
1529 :
1530 : /* ping connection */
1531 1 : res = PQexec(pgsql, "SELECT 1;");
1532 1 : PQclear(res);
1533 :
1534 : /* check status. */
1535 1 : if (PQstatus(pgsql) == CONNECTION_OK)
1536 1 : RETURN_TRUE;
1537 :
1538 : /* reset connection if it's broken */
1539 0 : PQreset(pgsql);
1540 0 : if (PQstatus(pgsql) == CONNECTION_OK) {
1541 0 : RETURN_TRUE;
1542 : }
1543 0 : RETURN_FALSE;
1544 : }
1545 : /* }}} */
1546 :
1547 : /* {{{ proto resource pg_query([resource connection,] string query)
1548 : Execute a query */
1549 : PHP_FUNCTION(pg_query)
1550 1124 : {
1551 1124 : zval *pgsql_link = NULL;
1552 : char *query;
1553 1124 : int id = -1, query_len, argc = ZEND_NUM_ARGS();
1554 1124 : int leftover = 0;
1555 : PGconn *pgsql;
1556 : PGresult *pgsql_result;
1557 : ExecStatusType status;
1558 : pgsql_result_handle *pg_result;
1559 :
1560 1124 : if (argc == 1) {
1561 65 : if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &query, &query_len) == FAILURE) {
1562 0 : return;
1563 : }
1564 65 : id = PGG(default_link);
1565 65 : CHECK_DEFAULT_LINK(id);
1566 : } else {
1567 1059 : if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rs", &pgsql_link, &query, &query_len) == FAILURE) {
1568 0 : return;
1569 : }
1570 : }
1571 :
1572 1124 : if (pgsql_link == NULL && id == -1) {
1573 0 : RETURN_FALSE;
1574 : }
1575 :
1576 1124 : ZEND_FETCH_RESOURCE2(pgsql, PGconn *, &pgsql_link, id, "PostgreSQL link", le_link, le_plink);
1577 :
1578 1124 : if (PQ_SETNONBLOCKING(pgsql, 0)) {
1579 0 : php_error_docref(NULL TSRMLS_CC, E_NOTICE,"Cannot set connection to blocking mode");
1580 0 : RETURN_FALSE;
1581 : }
1582 2248 : while ((pgsql_result = PQgetResult(pgsql))) {
1583 0 : PQclear(pgsql_result);
1584 0 : leftover = 1;
1585 : }
1586 1124 : if (leftover) {
1587 0 : php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Found results on this connection. Use pg_get_result() to get these results first");
1588 : }
1589 1124 : pgsql_result = PQexec(pgsql, query);
1590 1124 : if ((PGG(auto_reset_persistent) & 2) && PQstatus(pgsql) != CONNECTION_OK) {
1591 0 : PQclear(pgsql_result);
1592 0 : PQreset(pgsql);
1593 0 : pgsql_result = PQexec(pgsql, query);
1594 : }
1595 :
1596 1124 : if (pgsql_result) {
1597 1124 : status = PQresultStatus(pgsql_result);
1598 : } else {
1599 0 : status = (ExecStatusType) PQstatus(pgsql);
1600 : }
1601 :
1602 1124 : switch (status) {
1603 : case PGRES_EMPTY_QUERY:
1604 : case PGRES_BAD_RESPONSE:
1605 : case PGRES_NONFATAL_ERROR:
1606 : case PGRES_FATAL_ERROR:
1607 5 : PHP_PQ_ERROR("Query failed: %s", pgsql);
1608 5 : PQclear(pgsql_result);
1609 5 : RETURN_FALSE;
1610 : break;
1611 : case PGRES_COMMAND_OK: /* successful command that did not return rows */
1612 : default:
1613 1119 : if (pgsql_result) {
1614 1119 : pg_result = (pgsql_result_handle *) emalloc(sizeof(pgsql_result_handle));
1615 1119 : pg_result->conn = pgsql;
1616 1119 : pg_result->result = pgsql_result;
1617 1119 : pg_result->row = 0;
1618 1119 : ZEND_REGISTER_RESOURCE(return_value, pg_result, le_result);
1619 : } else {
1620 0 : PQclear(pgsql_result);
1621 0 : RETURN_FALSE;
1622 : }
1623 : break;
1624 : }
1625 : }
1626 : /* }}} */
1627 :
1628 : #if HAVE_PQEXECPARAMS || HAVE_PQEXECPREPARED || HAVE_PQSENDQUERYPARAMS || HAVE_PQSENDQUERYPREPARED
1629 : /* {{{ _php_pgsql_free_params */
1630 : static void _php_pgsql_free_params(char **params, int num_params)
1631 0 : {
1632 0 : if (num_params > 0) {
1633 : int i;
1634 0 : for (i = 0; i < num_params; i++) {
1635 0 : if (params[i]) {
1636 0 : efree(params[i]);
1637 : }
1638 : }
1639 0 : efree(params);
1640 : }
1641 0 : }
1642 : /* }}} */
1643 : #endif
1644 :
1645 : #if HAVE_PQEXECPARAMS
1646 : /* {{{ proto resource pg_query_params([resource connection,] string query, array params)
1647 : Execute a query */
1648 : PHP_FUNCTION(pg_query_params)
1649 0 : {
1650 0 : zval *pgsql_link = NULL;
1651 : zval *pv_param_arr, **tmp;
1652 : char *query;
1653 0 : int query_len, id = -1, argc = ZEND_NUM_ARGS();
1654 0 : int leftover = 0;
1655 0 : int num_params = 0;
1656 0 : char **params = NULL;
1657 : PGconn *pgsql;
1658 : PGresult *pgsql_result;
1659 : ExecStatusType status;
1660 : pgsql_result_handle *pg_result;
1661 :
1662 0 : if (argc == 2) {
1663 0 : if (zend_parse_parameters(argc TSRMLS_CC, "sa", &query, &query_len, &pv_param_arr) == FAILURE) {
1664 0 : return;
1665 : }
1666 0 : id = PGG(default_link);
1667 0 : CHECK_DEFAULT_LINK(id);
1668 : } else {
1669 0 : if (zend_parse_parameters(argc TSRMLS_CC, "rsa", &pgsql_link, &query, &query_len, &pv_param_arr) == FAILURE) {
1670 0 : return;
1671 : }
1672 : }
1673 :
1674 0 : if (pgsql_link == NULL && id == -1) {
1675 0 : RETURN_FALSE;
1676 : }
1677 :
1678 0 : ZEND_FETCH_RESOURCE2(pgsql, PGconn *, &pgsql_link, id, "PostgreSQL link", le_link, le_plink);
1679 :
1680 0 : if (PQ_SETNONBLOCKING(pgsql, 0)) {
1681 0 : php_error_docref(NULL TSRMLS_CC, E_NOTICE,"Cannot set connection to blocking mode");
1682 0 : RETURN_FALSE;
1683 : }
1684 0 : while ((pgsql_result = PQgetResult(pgsql))) {
1685 0 : PQclear(pgsql_result);
1686 0 : leftover = 1;
1687 : }
1688 0 : if (leftover) {
1689 0 : php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Found results on this connection. Use pg_get_result() to get these results first");
1690 : }
1691 :
1692 0 : zend_hash_internal_pointer_reset(Z_ARRVAL_P(pv_param_arr));
1693 0 : num_params = zend_hash_num_elements(Z_ARRVAL_P(pv_param_arr));
1694 0 : if (num_params > 0) {
1695 0 : int i = 0;
1696 0 : params = (char **)safe_emalloc(sizeof(char *), num_params, 0);
1697 :
1698 0 : for(i = 0; i < num_params; i++) {
1699 0 : if (zend_hash_get_current_data(Z_ARRVAL_P(pv_param_arr), (void **) &tmp) == FAILURE) {
1700 0 : php_error_docref(NULL TSRMLS_CC, E_WARNING,"Error getting parameter");
1701 0 : _php_pgsql_free_params(params, num_params);
1702 0 : RETURN_FALSE;
1703 : }
1704 :
1705 0 : if (Z_TYPE_PP(tmp) == IS_NULL) {
1706 0 : params[i] = NULL;
1707 : } else {
1708 0 : zval tmp_val = **tmp;
1709 0 : zval_copy_ctor(&tmp_val);
1710 0 : convert_to_string(&tmp_val);
1711 0 : if (Z_TYPE(tmp_val) != IS_STRING) {
1712 0 : php_error_docref(NULL TSRMLS_CC, E_WARNING,"Error converting parameter");
1713 0 : zval_dtor(&tmp_val);
1714 0 : _php_pgsql_free_params(params, num_params);
1715 0 : RETURN_FALSE;
1716 : }
1717 0 : params[i] = estrndup(Z_STRVAL(tmp_val), Z_STRLEN(tmp_val));
1718 0 : zval_dtor(&tmp_val);
1719 : }
1720 :
1721 0 : zend_hash_move_forward(Z_ARRVAL_P(pv_param_arr));
1722 : }
1723 : }
1724 :
1725 0 : pgsql_result = PQexecParams(pgsql, query, num_params,
1726 : NULL, (const char * const *)params, NULL, NULL, 0);
1727 0 : if ((PGG(auto_reset_persistent) & 2) && PQstatus(pgsql) != CONNECTION_OK) {
1728 0 : PQclear(pgsql_result);
1729 0 : PQreset(pgsql);
1730 0 : pgsql_result = PQexecParams(pgsql, query, num_params,
1731 : NULL, (const char * const *)params, NULL, NULL, 0);
1732 : }
1733 :
1734 0 : if (pgsql_result) {
1735 0 : status = PQresultStatus(pgsql_result);
1736 : } else {
1737 0 : status = (ExecStatusType) PQstatus(pgsql);
1738 : }
1739 :
1740 0 : _php_pgsql_free_params(params, num_params);
1741 :
1742 0 : switch (status) {
1743 : case PGRES_EMPTY_QUERY:
1744 : case PGRES_BAD_RESPONSE:
1745 : case PGRES_NONFATAL_ERROR:
1746 : case PGRES_FATAL_ERROR:
1747 0 : PHP_PQ_ERROR("Query failed: %s", pgsql);
1748 0 : PQclear(pgsql_result);
1749 0 : RETURN_FALSE;
1750 : break;
1751 : case PGRES_COMMAND_OK: /* successful command that did not return rows */
1752 : default:
1753 0 : if (pgsql_result) {
1754 0 : pg_result = (pgsql_result_handle *) emalloc(sizeof(pgsql_result_handle));
1755 0 : pg_result->conn = pgsql;
1756 0 : pg_result->result = pgsql_result;
1757 0 : pg_result->row = 0;
1758 0 : ZEND_REGISTER_RESOURCE(return_value, pg_result, le_result);
1759 : } else {
1760 0 : PQclear(pgsql_result);
1761 0 : RETURN_FALSE;
1762 : }
1763 : break;
1764 : }
1765 : }
1766 : /* }}} */
1767 : #endif
1768 :
1769 : #if HAVE_PQPREPARE
1770 : /* {{{ proto resource pg_prepare([resource connection,] string stmtname, string query)
1771 : Prepare a query for future execution */
1772 : PHP_FUNCTION(pg_prepare)
1773 0 : {
1774 0 : zval *pgsql_link = NULL;
1775 : char *query, *stmtname;
1776 0 : int query_len, stmtname_len, id = -1, argc = ZEND_NUM_ARGS();
1777 0 : int leftover = 0;
1778 : PGconn *pgsql;
1779 : PGresult *pgsql_result;
1780 : ExecStatusType status;
1781 : pgsql_result_handle *pg_result;
1782 :
1783 0 : if (argc == 2) {
1784 0 : if (zend_parse_parameters(argc TSRMLS_CC, "ss", &stmtname, &stmtname_len, &query, &query_len) == FAILURE) {
1785 0 : return;
1786 : }
1787 0 : id = PGG(default_link);
1788 0 : CHECK_DEFAULT_LINK(id);
1789 : } else {
1790 0 : if (zend_parse_parameters(argc TSRMLS_CC, "rss", &pgsql_link, &stmtname, &stmtname_len, &query, &query_len) == FAILURE) {
1791 0 : return;
1792 : }
1793 : }
1794 :
1795 0 : if (pgsql_link == NULL && id == -1) {
1796 0 : RETURN_FALSE;
1797 : }
1798 :
1799 0 : ZEND_FETCH_RESOURCE2(pgsql, PGconn *, &pgsql_link, id, "PostgreSQL link", le_link, le_plink);
1800 :
1801 0 : if (PQ_SETNONBLOCKING(pgsql, 0)) {
1802 0 : php_error_docref(NULL TSRMLS_CC, E_NOTICE,"Cannot set connection to blocking mode");
1803 0 : RETURN_FALSE;
1804 : }
1805 0 : while ((pgsql_result = PQgetResult(pgsql))) {
1806 0 : PQclear(pgsql_result);
1807 0 : leftover = 1;
1808 : }
1809 0 : if (leftover) {
1810 0 : php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Found results on this connection. Use pg_get_result() to get these results first");
1811 : }
1812 0 : pgsql_result = PQprepare(pgsql, stmtname, query, 0, NULL);
1813 0 : if ((PGG(auto_reset_persistent) & 2) && PQstatus(pgsql) != CONNECTION_OK) {
1814 0 : PQclear(pgsql_result);
1815 0 : PQreset(pgsql);
1816 0 : pgsql_result = PQprepare(pgsql, stmtname, query, 0, NULL);
1817 : }
1818 :
1819 0 : if (pgsql_result) {
1820 0 : status = PQresultStatus(pgsql_result);
1821 : } else {
1822 0 : status = (ExecStatusType) PQstatus(pgsql);
1823 : }
1824 :
1825 0 : switch (status) {
1826 : case PGRES_EMPTY_QUERY:
1827 : case PGRES_BAD_RESPONSE:
1828 : case PGRES_NONFATAL_ERROR:
1829 : case PGRES_FATAL_ERROR:
1830 0 : PHP_PQ_ERROR("Query failed: %s", pgsql);
1831 0 : PQclear(pgsql_result);
1832 0 : RETURN_FALSE;
1833 : break;
1834 : case PGRES_COMMAND_OK: /* successful command that did not return rows */
1835 : default:
1836 0 : if (pgsql_result) {
1837 0 : pg_result = (pgsql_result_handle *) emalloc(sizeof(pgsql_result_handle));
1838 0 : pg_result->conn = pgsql;
1839 0 : pg_result->result = pgsql_result;
1840 0 : pg_result->row = 0;
1841 0 : ZEND_REGISTER_RESOURCE(return_value, pg_result, le_result);
1842 : } else {
1843 0 : PQclear(pgsql_result);
1844 0 : RETURN_FALSE;
1845 : }
1846 : break;
1847 : }
1848 : }
1849 : /* }}} */
1850 : #endif
1851 :
1852 : #if HAVE_PQEXECPREPARED
1853 : /* {{{ proto resource pg_execute([resource connection,] string stmtname, array params)
1854 : Execute a prepared query */
1855 : PHP_FUNCTION(pg_execute)
1856 0 : {
1857 0 : zval *pgsql_link = NULL;
1858 : zval *pv_param_arr, **tmp;
1859 : char *stmtname;
1860 0 : int stmtname_len, id = -1, argc = ZEND_NUM_ARGS();
1861 0 : int leftover = 0;
1862 0 : int num_params = 0;
1863 0 : char **params = NULL;
1864 : PGconn *pgsql;
1865 : PGresult *pgsql_result;
1866 : ExecStatusType status;
1867 : pgsql_result_handle *pg_result;
1868 :
1869 0 : if (argc == 2) {
1870 0 : if (zend_parse_parameters(argc TSRMLS_CC, "sa/", &stmtname, &stmtname_len, &pv_param_arr)==FAILURE) {
1871 0 : return;
1872 : }
1873 0 : id = PGG(default_link);
1874 0 : CHECK_DEFAULT_LINK(id);
1875 : } else {
1876 0 : if (zend_parse_parameters(argc TSRMLS_CC, "rsa/", &pgsql_link, &stmtname, &stmtname_len, &pv_param_arr) == FAILURE) {
1877 0 : return;
1878 : }
1879 : }
1880 :
1881 0 : if (pgsql_link == NULL && id == -1) {
1882 0 : RETURN_FALSE;
1883 : }
1884 :
1885 0 : ZEND_FETCH_RESOURCE2(pgsql, PGconn *, &pgsql_link, id, "PostgreSQL link", le_link, le_plink);
1886 :
1887 0 : if (PQ_SETNONBLOCKING(pgsql, 0)) {
1888 0 : php_error_docref(NULL TSRMLS_CC, E_NOTICE,"Cannot set connection to blocking mode");
1889 0 : RETURN_FALSE;
1890 : }
1891 0 : while ((pgsql_result = PQgetResult(pgsql))) {
1892 0 : PQclear(pgsql_result);
1893 0 : leftover = 1;
1894 : }
1895 0 : if (leftover) {
1896 0 : php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Found results on this connection. Use pg_get_result() to get these results first");
1897 : }
1898 :
1899 0 : zend_hash_internal_pointer_reset(Z_ARRVAL_P(pv_param_arr));
1900 0 : num_params = zend_hash_num_elements(Z_ARRVAL_P(pv_param_arr));
1901 0 : if (num_params > 0) {
1902 0 : int i = 0;
1903 0 : params = (char **)safe_emalloc(sizeof(char *), num_params, 0);
1904 :
1905 0 : for(i = 0; i < num_params; i++) {
1906 0 : if (zend_hash_get_current_data(Z_ARRVAL_P(pv_param_arr), (void **) &tmp) == FAILURE) {
1907 0 : php_error_docref(NULL TSRMLS_CC, E_WARNING,"Error getting parameter");
1908 0 : _php_pgsql_free_params(params, num_params);
1909 0 : RETURN_FALSE;
1910 : }
1911 :
1912 0 : if (Z_TYPE_PP(tmp) == IS_NULL) {
1913 0 : params[i] = NULL;
1914 : } else {
1915 0 : zval tmp_val = **tmp;
1916 0 : zval_copy_ctor(&tmp_val);
1917 0 : convert_to_string(&tmp_val);
1918 0 : if (Z_TYPE(tmp_val) != IS_STRING) {
1919 0 : php_error_docref(NULL TSRMLS_CC, E_WARNING,"Error converting parameter");
1920 0 : zval_dtor(&tmp_val);
1921 0 : _php_pgsql_free_params(params, num_params);
1922 0 : RETURN_FALSE;
1923 : }
1924 0 : params[i] = estrndup(Z_STRVAL(tmp_val), Z_STRLEN(tmp_val));
1925 0 : zval_dtor(&tmp_val);
1926 : }
1927 :
1928 0 : zend_hash_move_forward(Z_ARRVAL_P(pv_param_arr));
1929 : }
1930 : }
1931 :
1932 0 : pgsql_result = PQexecPrepared(pgsql, stmtname, num_params,
1933 : (const char * const *)params, NULL, NULL, 0);
1934 0 : if ((PGG(auto_reset_persistent) & 2) && PQstatus(pgsql) != CONNECTION_OK) {
1935 0 : PQclear(pgsql_result);
1936 0 : PQreset(pgsql);
1937 0 : pgsql_result = PQexecPrepared(pgsql, stmtname, num_params,
1938 : (const char * const *)params, NULL, NULL, 0);
1939 : }
1940 :
1941 0 : if (pgsql_result) {
1942 0 : status = PQresultStatus(pgsql_result);
1943 : } else {
1944 0 : status = (ExecStatusType) PQstatus(pgsql);
1945 : }
1946 :
1947 0 : _php_pgsql_free_params(params, num_params);
1948 :
1949 0 : switch (status) {
1950 : case PGRES_EMPTY_QUERY:
1951 : case PGRES_BAD_RESPONSE:
1952 : case PGRES_NONFATAL_ERROR:
1953 : case PGRES_FATAL_ERROR:
1954 0 : PQclear(pgsql_result);
1955 0 : PHP_PQ_ERROR("Query failed: %s", pgsql);
1956 0 : RETURN_FALSE;
1957 : break;
1958 : case PGRES_COMMAND_OK: /* successful command that did not return rows */
1959 : default:
1960 0 : if (pgsql_result) {
1961 0 : pg_result = (pgsql_result_handle *) emalloc(sizeof(pgsql_result_handle));
1962 0 : pg_result->conn = pgsql;
1963 0 : pg_result->result = pgsql_result;
1964 0 : pg_result->row = 0;
1965 0 : ZEND_REGISTER_RESOURCE(return_value, pg_result, le_result);
1966 : } else {
1967 0 : PQclear(pgsql_result);
1968 0 : RETURN_FALSE;
1969 : }
1970 : break;
1971 : }
1972 : }
1973 : /* }}} */
1974 : #endif
1975 :
1976 : #define PHP_PG_NUM_ROWS 1
1977 : #define PHP_PG_NUM_FIELDS 2
1978 : #define PHP_PG_CMD_TUPLES 3
1979 :
1980 : /* {{{ php_pgsql_get_result_info
1981 : */
1982 : static void php_pgsql_get_result_info(INTERNAL_FUNCTION_PARAMETERS, int entry_type)
1983 12 : {
1984 : zval *result;
1985 : PGresult *pgsql_result;
1986 : pgsql_result_handle *pg_result;
1987 :
1988 12 : if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &result) == FAILURE) {
1989 1 : return;
1990 : }
1991 :
1992 11 : ZEND_FETCH_RESOURCE(pg_result, pgsql_result_handle *, &result, -1, "PostgreSQL result", le_result);
1993 :
1994 11 : pgsql_result = pg_result->result;
1995 :
1996 11 : switch (entry_type) {
1997 : case PHP_PG_NUM_ROWS:
1998 7 : Z_LVAL_P(return_value) = PQntuples(pgsql_result);
1999 7 : break;
2000 : case PHP_PG_NUM_FIELDS:
2001 3 : Z_LVAL_P(return_value) = PQnfields(pgsql_result);
2002 3 : break;
2003 : case PHP_PG_CMD_TUPLES:
2004 : #if HAVE_PQCMDTUPLES
2005 1 : Z_LVAL_P(return_value) = atoi(PQcmdTuples(pgsql_result));
2006 : #else
2007 : php_error_docref(NULL TSRMLS_CC, E_WARNING, "Not supported under this build");
2008 : Z_LVAL_P(return_value) = 0;
2009 : #endif
2010 1 : break;
2011 : default:
2012 0 : RETURN_FALSE;
2013 : }
2014 11 : Z_TYPE_P(return_value) = IS_LONG;
2015 : }
2016 : /* }}} */
2017 :
2018 : /* {{{ proto int pg_num_rows(resource result)
2019 : Return the number of rows in the result */
2020 : PHP_FUNCTION(pg_num_rows)
2021 8 : {
2022 8 : php_pgsql_get_result_info(INTERNAL_FUNCTION_PARAM_PASSTHRU,PHP_PG_NUM_ROWS);
2023 8 : }
2024 : /* }}} */
2025 :
2026 : /* {{{ proto int pg_num_fields(resource result)
2027 : Return the number of fields in the result */
2028 : PHP_FUNCTION(pg_num_fields)
2029 3 : {
2030 3 : php_pgsql_get_result_info(INTERNAL_FUNCTION_PARAM_PASSTHRU,PHP_PG_NUM_FIELDS);
2031 3 : }
2032 : /* }}} */
2033 :
2034 : #if HAVE_PQCMDTUPLES
2035 : /* {{{ proto int pg_affected_rows(resource result)
2036 : Returns the number of affected tuples */
2037 : PHP_FUNCTION(pg_affected_rows)
2038 1 : {
2039 1 : php_pgsql_get_result_info(INTERNAL_FUNCTION_PARAM_PASSTHRU,PHP_PG_CMD_TUPLES);
2040 1 : }
2041 : /* }}} */
2042 : #endif
2043 :
2044 : /* {{{ proto string pg_last_notice(resource connection)
2045 : Returns the last notice set by the backend */
2046 : PHP_FUNCTION(pg_last_notice)
2047 3 : {
2048 : zval *pgsql_link;
2049 : PGconn *pg_link;
2050 3 : int id = -1;
2051 : php_pgsql_notice **notice;
2052 :
2053 3 : if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &pgsql_link) == FAILURE) {
2054 0 : return;
2055 : }
2056 : /* Just to check if user passed valid resoruce */
2057 3 : ZEND_FETCH_RESOURCE2(pg_link, PGconn *, &pgsql_link, id, "PostgreSQL link", le_link, le_plink);
2058 :
2059 3 : if (zend_hash_index_find(&PGG(notices), Z_RESVAL_P(pgsql_link), (void **)¬ice) == FAILURE) {
2060 0 : RETURN_FALSE;
2061 : }
2062 3 : RETURN_STRINGL((*notice)->message, (*notice)->len, 1);
2063 : }
2064 : /* }}} */
2065 :
2066 : /* {{{ get_field_name
2067 : */
2068 : static char *get_field_name(PGconn *pgsql, Oid oid, HashTable *list TSRMLS_DC)
2069 3 : {
2070 : PGresult *result;
2071 3 : smart_str str = {0};
2072 : zend_rsrc_list_entry *field_type;
2073 3 : char *ret=NULL;
2074 :
2075 : /* try to lookup the type in the resource list */
2076 3 : smart_str_appends(&str, "pgsql_oid_");
2077 3 : smart_str_append_unsigned(&str, oid);
2078 3 : smart_str_0(&str);
2079 :
2080 3 : if (zend_hash_find(list,str.c,str.len+1,(void **) &field_type)==SUCCESS) {
2081 0 : ret = estrdup((char *)field_type->ptr);
2082 : } else { /* hash all oid's */
2083 : int i,num_rows;
2084 : int oid_offset,name_offset;
2085 : char *tmp_oid, *end_ptr, *tmp_name;
2086 : zend_rsrc_list_entry new_oid_entry;
2087 :
2088 3 : if ((result = PQexec(pgsql,"select oid,typname from pg_type")) == NULL || PQresultStatus(result) != PGRES_TUPLES_OK) {
2089 0 : if (result) {
2090 0 : PQclear(result);
2091 : }
2092 0 : smart_str_free(&str);
2093 0 : return STR_EMPTY_ALLOC();
2094 : }
2095 3 : num_rows = PQntuples(result);
2096 3 : oid_offset = PQfnumber(result,"oid");
2097 3 : name_offset = PQfnumber(result,"typname");
2098 :
2099 705 : for (i=0; i<num_rows; i++) {
2100 702 : if ((tmp_oid = PQgetvalue(result,i,oid_offset))==NULL) {
2101 0 : continue;
2102 : }
2103 :
2104 702 : str.len = 0;
2105 702 : smart_str_appends(&str, "pgsql_oid_");
2106 702 : smart_str_appends(&str, tmp_oid);
2107 702 : smart_str_0(&str);
2108 :
2109 702 : if ((tmp_name = PQgetvalue(result,i,name_offset))==NULL) {
2110 0 : continue;
2111 : }
2112 702 : Z_TYPE(new_oid_entry) = le_string;
2113 702 : new_oid_entry.ptr = estrdup(tmp_name);
2114 702 : zend_hash_update(list,str.c,str.len+1,(void *) &new_oid_entry, sizeof(zend_rsrc_list_entry), NULL);
2115 702 : if (!ret && strtoul(tmp_oid, &end_ptr, 10)==oid) {
2116 3 : ret = estrdup(tmp_name);
2117 : }
2118 : }
2119 3 : PQclear(result);
2120 : }
2121 :
2122 3 : smart_str_free(&str);
2123 3 : return ret;
2124 : }
2125 : /* }}} */
2126 :
2127 : #ifdef HAVE_PQFTABLE
2128 : /* {{{ proto mixed pg_field_table(resource result, int field_number[, bool oid_only])
2129 : Returns the name of the table field belongs to, or table's oid if oid_only is true */
2130 : PHP_FUNCTION(pg_field_table)
2131 0 : {
2132 : zval *result;
2133 : pgsql_result_handle *pg_result;
2134 0 : long fnum = -1;
2135 0 : zend_bool return_oid = 0;
2136 : Oid oid;
2137 0 : smart_str hash_key = {0};
2138 : char *table_name;
2139 : zend_rsrc_list_entry *field_table;
2140 :
2141 0 : if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rl|b", &result, &fnum, &return_oid) == FAILURE) {
2142 0 : return;
2143 : }
2144 :
2145 0 : ZEND_FETCH_RESOURCE(pg_result, pgsql_result_handle *, &result, -1, "PostgreSQL result", le_result);
2146 :
2147 0 : if (fnum < 0 || fnum >= PQnfields(pg_result->result)) {
2148 0 : php_error_docref(NULL TSRMLS_CC, E_WARNING, "Bad field offset specified");
2149 0 : RETURN_FALSE;
2150 : }
2151 :
2152 0 : oid = PQftable(pg_result->result, fnum);
2153 :
2154 0 : if (InvalidOid == oid) {
2155 0 : RETURN_FALSE;
2156 : }
2157 :
2158 :
2159 0 : if (return_oid) {
2160 : #if UINT_MAX > LONG_MAX /* Oid is unsigned int, we don't need this code, where LONG is wider */
2161 0 : if (oid > LONG_MAX) {
2162 0 : smart_str oidstr = {0};
2163 0 : smart_str_append_unsigned(&oidstr, oid);
2164 0 : smart_str_0(&oidstr);
2165 0 : RETURN_STRINGL(oidstr.c, oidstr.len, 0);
2166 : } else
2167 : #endif
2168 0 : RETURN_LONG((long)oid);
2169 : }
2170 :
2171 : /* try to lookup the table name in the resource list */
2172 0 : smart_str_appends(&hash_key, "pgsql_table_oid_");
2173 0 : smart_str_append_unsigned(&hash_key, oid);
2174 0 : smart_str_0(&hash_key);
2175 :
2176 0 : if (zend_hash_find(&EG(regular_list), hash_key.c, hash_key.len+1, (void **) &field_table) == SUCCESS) {
2177 0 : smart_str_free(&hash_key);
2178 0 : RETURN_STRING((char *)field_table->ptr, 1);
2179 : } else { /* Not found, lookup by querying PostgreSQL system tables */
2180 : PGresult *tmp_res;
2181 0 : smart_str querystr = {0};
2182 : zend_rsrc_list_entry new_field_table;
2183 :
2184 0 : smart_str_appends(&querystr, "select relname from pg_class where oid=");
2185 0 : smart_str_append_unsigned(&querystr, oid);
2186 0 : smart_str_0(&querystr);
2187 :
2188 :
2189 0 : if ((tmp_res = PQexec(pg_result->conn, querystr.c)) == NULL || PQresultStatus(tmp_res) != PGRES_TUPLES_OK) {
2190 0 : if (tmp_res) {
2191 0 : PQclear(tmp_res);
2192 : }
2193 0 : smart_str_free(&querystr);
2194 0 : smart_str_free(&hash_key);
2195 0 : RETURN_FALSE;
2196 : }
2197 :
2198 0 : smart_str_free(&querystr);
2199 :
2200 0 : if ((table_name = PQgetvalue(tmp_res, 0, 0)) == NULL) {
2201 0 : PQclear(tmp_res);
2202 0 : smart_str_free(&hash_key);
2203 0 : RETURN_FALSE;
2204 : }
2205 :
2206 0 : Z_TYPE(new_field_table) = le_string;
2207 0 : new_field_table.ptr = estrdup(table_name);
2208 0 : zend_hash_update(&EG(regular_list), hash_key.c, hash_key.len+1, (void *) &new_field_table, sizeof(zend_rsrc_list_entry), NULL);
2209 :
2210 0 : smart_str_free(&hash_key);
2211 0 : PQclear(tmp_res);
2212 0 : RETURN_STRING(table_name, 1);
2213 : }
2214 :
2215 : }
2216 : /* }}} */
2217 : #endif
2218 :
2219 : #define PHP_PG_FIELD_NAME 1
2220 : #define PHP_PG_FIELD_SIZE 2
2221 : #define PHP_PG_FIELD_TYPE 3
2222 : #define PHP_PG_FIELD_TYPE_OID 4
2223 :
2224 : /* {{{ php_pgsql_get_field_info
2225 : */
2226 : static void php_pgsql_get_field_info(INTERNAL_FUNCTION_PARAMETERS, int entry_type)
2227 9 : {
2228 : zval *result;
2229 : long field;
2230 : PGresult *pgsql_result;
2231 : pgsql_result_handle *pg_result;
2232 : Oid oid;
2233 :
2234 9 : if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rl", &result, &field) == FAILURE) {
2235 0 : return;
2236 : }
2237 :
2238 9 : ZEND_FETCH_RESOURCE(pg_result, pgsql_result_handle *, &result, -1, "PostgreSQL result", le_result);
2239 :
2240 9 : pgsql_result = pg_result->result;
2241 :
2242 9 : if (field < 0 || field >= PQnfields(pgsql_result)) {
2243 0 : php_error_docref(NULL TSRMLS_CC, E_WARNING, "Bad field offset specified");
2244 0 : RETURN_FALSE;
2245 : }
2246 :
2247 9 : switch (entry_type) {
2248 : case PHP_PG_FIELD_NAME:
2249 3 : Z_STRVAL_P(return_value) = PQfname(pgsql_result, field);
2250 3 : Z_STRLEN_P(return_value) = strlen(Z_STRVAL_P(return_value));
2251 3 : Z_STRVAL_P(return_value) = estrndup(Z_STRVAL_P(return_value),Z_STRLEN_P(return_value));
2252 3 : Z_TYPE_P(return_value) = IS_STRING;
2253 3 : break;
2254 : case PHP_PG_FIELD_SIZE:
2255 3 : Z_LVAL_P(return_value) = PQfsize(pgsql_result, field);
2256 3 : Z_TYPE_P(return_value) = IS_LONG;
2257 3 : break;
2258 : case PHP_PG_FIELD_TYPE:
2259 3 : Z_STRVAL_P(return_value) = get_field_name(pg_result->conn, PQftype(pgsql_result, field), &EG(regular_list) TSRMLS_CC);
2260 3 : Z_STRLEN_P(return_value) = strlen(Z_STRVAL_P(return_value));
2261 3 : Z_TYPE_P(return_value) = IS_STRING;
2262 3 : break;
2263 : case PHP_PG_FIELD_TYPE_OID:
2264 :
2265 0 : oid = PQftype(pgsql_result, field);
2266 : #if UINT_MAX > LONG_MAX
2267 0 : if (oid > LONG_MAX) {
2268 0 : smart_str s = {0};
2269 0 : smart_str_append_unsigned(&s, oid);
2270 0 : smart_str_0(&s);
2271 0 : Z_STRVAL_P(return_value) = s.c;
2272 0 : Z_STRLEN_P(return_value) = s.len;
2273 0 : Z_TYPE_P(return_value) = IS_STRING;
2274 : } else
2275 : #endif
2276 : {
2277 0 : Z_LVAL_P(return_value) = (long)oid;
2278 0 : Z_TYPE_P(return_value) = IS_LONG;
2279 : }
2280 0 : break;
2281 : default:
2282 0 : RETURN_FALSE;
2283 : }
2284 : }
2285 : /* }}} */
2286 :
2287 : /* {{{ proto string pg_field_name(resource result, int field_number)
2288 : Returns the name of the field */
2289 : PHP_FUNCTION(pg_field_name)
2290 3 : {
2291 3 : php_pgsql_get_field_info(INTERNAL_FUNCTION_PARAM_PASSTHRU,PHP_PG_FIELD_NAME);
2292 3 : }
2293 : /* }}} */
2294 :
2295 : /* {{{ proto int pg_field_size(resource result, int field_number)
2296 : Returns the internal size of the field */
2297 : PHP_FUNCTION(pg_field_size)
2298 3 : {
2299 3 : php_pgsql_get_field_info(INTERNAL_FUNCTION_PARAM_PASSTHRU,PHP_PG_FIELD_SIZE);
2300 3 : }
2301 : /* }}} */
2302 :
2303 : /* {{{ proto string pg_field_type(resource result, int field_number)
2304 : Returns the type name for the given field */
2305 : PHP_FUNCTION(pg_field_type)
2306 3 : {
2307 3 : php_pgsql_get_field_info(INTERNAL_FUNCTION_PARAM_PASSTHRU,PHP_PG_FIELD_TYPE);
2308 3 : }
2309 : /* }}} */
2310 :
2311 :
2312 : /* {{{ proto string pg_field_type_oid(resource result, int field_number)
2313 : Returns the type oid for the given field */
2314 : PHP_FUNCTION(pg_field_type_oid)
2315 0 : {
2316 0 : php_pgsql_get_field_info(INTERNAL_FUNCTION_PARAM_PASSTHRU,PHP_PG_FIELD_TYPE_OID);
2317 0 : }
2318 : /* }}} */
2319 :
2320 : /* {{{ proto int pg_field_num(resource result, string field_name)
2321 : Returns the field number of the named field */
2322 : PHP_FUNCTION(pg_field_num)
2323 2 : {
2324 : zval *result;
2325 : char *field;
2326 : int field_len;
2327 : PGresult *pgsql_result;
2328 : pgsql_result_handle *pg_result;
2329 :
2330 2 : if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rs", &result, &field, &field_len) == FAILURE) {
2331 0 : return;
2332 : }
2333 :
2334 2 : ZEND_FETCH_RESOURCE(pg_result, pgsql_result_handle *, &result, -1, "PostgreSQL result", le_result);
2335 :
2336 2 : pgsql_result = pg_result->result;
2337 :
2338 2 : Z_LVAL_P(return_value) = PQfnumber(pgsql_result, field);
2339 2 : Z_TYPE_P(return_value) = IS_LONG;
2340 : }
2341 : /* }}} */
2342 :
2343 : /* {{{ proto mixed pg_fetch_result(resource result, [int row_number,] mixed field_name)
2344 : Returns values from a result identifier */
2345 : PHP_FUNCTION(pg_fetch_result)
2346 2004 : {
2347 2004 : zval *result, **field=NULL;
2348 : long row;
2349 : PGresult *pgsql_result;
2350 : pgsql_result_handle *pg_result;
2351 2004 : int field_offset, pgsql_row, argc = ZEND_NUM_ARGS();
2352 :
2353 2004 : if (argc == 2) {
2354 2 : if (zend_parse_parameters(argc TSRMLS_CC, "rZ", &result, &field) == FAILURE) {
2355 0 : return;
2356 : }
2357 : } else {
2358 2002 : if (zend_parse_parameters(argc TSRMLS_CC, "rlZ", &result, &row, &field) == FAILURE) {
2359 0 : return;
2360 : }
2361 : }
2362 :
2363 2004 : ZEND_FETCH_RESOURCE(pg_result, pgsql_result_handle *, &result, -1, "PostgreSQL result", le_result);
2364 :
2365 2004 : pgsql_result = pg_result->result;
2366 2004 : if (argc == 2) {
2367 2 : if (pg_result->row < 0) {
2368 0 : pg_result->row = 0;
2369 : }
2370 2 : pgsql_row = pg_result->row;
2371 2 : if (pgsql_row >= PQntuples(pgsql_result)) {
2372 0 : RETURN_FALSE;
2373 : }
2374 : } else {
2375 2002 : pgsql_row = row;
2376 2002 : if (pgsql_row < 0 || pgsql_row >= PQntuples(pgsql_result)) {
2377 0 : php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to jump to row %ld on PostgreSQL result index %ld",
2378 : row, Z_LVAL_P(result));
2379 0 : RETURN_FALSE;
2380 : }
2381 : }
2382 2004 : switch(Z_TYPE_PP(field)) {
2383 : case IS_STRING:
2384 0 : field_offset = PQfnumber(pgsql_result, Z_STRVAL_PP(field));
2385 0 : break;
2386 : default:
2387 2004 : convert_to_long_ex(field);
2388 2004 : field_offset = Z_LVAL_PP(field);
2389 : break;
2390 : }
2391 2004 : if (field_offset<0 || field_offset>=PQnfields(pgsql_result)) {
2392 0 : php_error_docref(NULL TSRMLS_CC, E_WARNING, "Bad column offset specified");
2393 0 : RETURN_FALSE;
2394 : }
2395 :
2396 2004 : if (PQgetisnull(pgsql_result, pgsql_row, field_offset)) {
2397 0 : Z_TYPE_P(return_value) = IS_NULL;
2398 : } else {
2399 2004 : char *value = PQgetvalue(pgsql_result, pgsql_row, field_offset);
2400 2004 : int value_len = PQgetlength(pgsql_result, pgsql_row, field_offset);
2401 2004 : ZVAL_STRINGL(return_value, value, value_len, 1);
2402 : }
2403 : }
2404 : /* }}} */
2405 :
2406 : /* {{{ void php_pgsql_fetch_hash */
2407 : static void php_pgsql_fetch_hash(INTERNAL_FUNCTION_PARAMETERS, long result_type, int into_object)
2408 6021 : {
2409 6021 : zval *result, *zrow = NULL;
2410 : PGresult *pgsql_result;
2411 : pgsql_result_handle *pg_result;
2412 : int i, num_fields, pgsql_row, use_row;
2413 6021 : long row = -1;
2414 : char *field_name;
2415 6021 : zval *ctor_params = NULL;
2416 6021 : zend_class_entry *ce = NULL;
2417 :
2418 6021 : if (into_object) {
2419 2004 : char *class_name = NULL;
2420 : int class_name_len;
2421 :
2422 2004 : if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r|z!sz", &result, &zrow, &class_name, &class_name_len, &ctor_params) == FAILURE) {
2423 0 : return;
2424 : }
2425 2004 : if (!class_name) {
2426 2003 : ce = zend_standard_class_def;
2427 : } else {
2428 1 : ce = zend_fetch_class(class_name, class_name_len, ZEND_FETCH_CLASS_AUTO TSRMLS_CC);
2429 : }
2430 2004 : if (!ce) {
2431 0 : php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not find class '%s'", class_name);
2432 0 : return;
2433 : }
2434 2004 : result_type = PGSQL_ASSOC;
2435 : } else {
2436 4017 : if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r|z!l", &result, &zrow, &result_type) == FAILURE) {
2437 0 : return;
2438 : }
2439 : }
2440 6021 : if (zrow == NULL) {
2441 2013 : row = -1;
2442 : } else {
2443 4008 : convert_to_long(zrow);
2444 4008 : row = Z_LVAL_P(zrow);
2445 : }
2446 6021 : use_row = ZEND_NUM_ARGS() > 1 && row != -1;
2447 :
2448 6021 : if (!(result_type & PGSQL_BOTH)) {
2449 0 : php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid result type");
2450 0 : RETURN_FALSE;
2451 : }
2452 :
2453 6021 : ZEND_FETCH_RESOURCE(pg_result, pgsql_result_handle *, &result, -1, "PostgreSQL result", le_result);
2454 :
2455 6021 : pgsql_result = pg_result->result;
2456 :
2457 6021 : if (use_row) {
2458 4008 : pgsql_row = row;
2459 4008 : pg_result->row = pgsql_row;
2460 4008 : if (pgsql_row < 0 || pgsql_row >= PQntuples(pgsql_result)) {
2461 0 : php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to jump to row %ld on PostgreSQL result index %ld",
2462 : row, Z_LVAL_P(result));
2463 0 : RETURN_FALSE;
2464 : }
2465 : } else {
2466 : /* If 2nd param is NULL, use internal row counter to access next row */
2467 2013 : pgsql_row = pg_result->row;
2468 2013 : if (pgsql_row < 0 || pgsql_row >= PQntuples(pgsql_result)) {
2469 2001 : RETURN_FALSE;
2470 : }
2471 12 : pg_result->row++;
2472 : }
2473 :
2474 4020 : array_init(return_value);
2475 16059 : for (i = 0, num_fields = PQnfields(pgsql_result); i < num_fields; i++) {
2476 12039 : if (PQgetisnull(pgsql_result, pgsql_row, i)) {
2477 4009 : if (result_type & PGSQL_NUM) {
2478 4004 : add_index_null(return_value, i);
2479 : }
2480 4009 : if (result_type & PGSQL_ASSOC) {
2481 6 : field_name = PQfname(pgsql_result, i);
2482 6 : add_assoc_null(return_value, field_name);
2483 : }
2484 : } else {
2485 8030 : char *element = PQgetvalue(pgsql_result, pgsql_row, i);
2486 8030 : if (element) {
2487 : char *data;
2488 : int data_len;
2489 8030 : int should_copy=0;
2490 8030 : const uint element_len = strlen(element);
2491 :
2492 8030 : data = safe_estrndup(element, element_len);
2493 8030 : data_len = element_len;
2494 :
2495 8030 : if (result_type & PGSQL_NUM) {
2496 8012 : add_index_stringl(return_value, i, data, data_len, should_copy);
2497 8012 : should_copy=1;
2498 : }
2499 :
2500 8030 : if (result_type & PGSQL_ASSOC) {
2501 22 : field_name = PQfname(pgsql_result, i);
2502 22 : add_assoc_stringl(return_value, field_name, data, data_len, should_copy);
2503 : }
2504 : }
2505 : }
2506 : }
2507 :
2508 4020 : if (into_object) {
2509 5 : zval dataset = *return_value;
2510 : zend_fcall_info fci;
2511 : zend_fcall_info_cache fcc;
2512 : zval *retval_ptr;
2513 :
2514 5 : object_and_properties_init(return_value, ce, NULL);
2515 5 : zend_merge_properties(return_value, Z_ARRVAL(dataset), 1 TSRMLS_CC);
2516 :
2517 5 : if (ce->constructor) {
2518 1 : fci.size = sizeof(fci);
2519 1 : fci.function_table = &ce->function_table;
2520 1 : fci.function_name = NULL;
2521 1 : fci.symbol_table = NULL;
2522 1 : fci.object_ptr = return_value;
2523 1 : fci.retval_ptr_ptr = &retval_ptr;
2524 2 : if (ctor_params && Z_TYPE_P(ctor_params) != IS_NULL) {
2525 1 : if (Z_TYPE_P(ctor_params) == IS_ARRAY) {
2526 1 : HashTable *ht = Z_ARRVAL_P(ctor_params);
2527 : Bucket *p;
2528 :
2529 1 : fci.param_count = 0;
2530 1 : fci.params = safe_emalloc(sizeof(zval*), ht->nNumOfElements, 0);
2531 1 : p = ht->pListHead;
2532 4 : while (p != NULL) {
2533 2 : fci.params[fci.param_count++] = (zval**)p->pData;
2534 2 : p = p->pListNext;
2535 : }
2536 : } else {
2537 : /* Two problems why we throw exceptions here: PHP is typeless
2538 : * and hence passing one argument that's not an array could be
2539 : * by mistake and the other way round is possible, too. The
2540 : * single value is an array. Also we'd have to make that one
2541 : * argument passed by reference.
2542 : */
2543 0 : zend_throw_exception(zend_exception_get_default(TSRMLS_C), "Parameter ctor_params must be an array", 0 TSRMLS_CC);
2544 0 : return;
2545 : }
2546 : } else {
2547 0 : fci.param_count = 0;
2548 0 : fci.params = NULL;
2549 : }
2550 1 : fci.no_separation = 1;
2551 :
2552 1 : fcc.initialized = 1;
2553 1 : fcc.function_handler = ce->constructor;
2554 1 : fcc.calling_scope = EG(scope);
2555 1 : fcc.called_scope = Z_OBJCE_P(return_value);
2556 1 : fcc.object_ptr = return_value;
2557 :
2558 1 : if (zend_call_function(&fci, &fcc TSRMLS_CC) == FAILURE) {
2559 0 : zend_throw_exception_ex(zend_exception_get_default(TSRMLS_C), 0 TSRMLS_CC, "Could not execute %s::%s()", ce->name, ce->constructor->common.function_name);
2560 : } else {
2561 1 : if (retval_ptr) {
2562 1 : zval_ptr_dtor(&retval_ptr);
2563 : }
2564 : }
2565 1 : if (fci.params) {
2566 1 : efree(fci.params);
2567 : }
2568 4 : } else if (ctor_params) {
2569 0 : zend_throw_exception_ex(zend_exception_get_default(TSRMLS_C), 0 TSRMLS_CC, "Class %s does not have a constructor hence you cannot use ctor_params", ce->name);
2570 : }
2571 : }
2572 : }
2573 : /* }}} */
2574 :
2575 : /* {{{ proto array pg_fetch_row(resource result [, int row [, int result_type]])
2576 : Get a row as an enumerated array */
2577 : PHP_FUNCTION(pg_fetch_row)
2578 2004 : {
2579 2004 : php_pgsql_fetch_hash(INTERNAL_FUNCTION_PARAM_PASSTHRU, PGSQL_NUM, 0);
2580 2004 : }
2581 : /* }}} */
2582 :
2583 : /* {{{ proto array pg_fetch_assoc(resource result [, int row])
2584 : Fetch a row as an assoc array */
2585 : PHP_FUNCTION(pg_fetch_assoc)
2586 3 : {
2587 : /* pg_fetch_assoc() is added from PHP 4.3.0. It should raise error, when
2588 : there is 3rd parameter */
2589 3 : if (ZEND_NUM_ARGS() > 2)
2590 0 : WRONG_PARAM_COUNT;
2591 3 : php_pgsql_fetch_hash(INTERNAL_FUNCTION_PARAM_PASSTHRU, PGSQL_ASSOC, 0);
2592 : }
2593 : /* }}} */
2594 :
2595 : /* {{{ proto array pg_fetch_array(resource result [, int row [, int result_type]])
2596 : Fetch a row as an array */
2597 : PHP_FUNCTION(pg_fetch_array)
2598 2010 : {
2599 2010 : php_pgsql_fetch_hash(INTERNAL_FUNCTION_PARAM_PASSTHRU, PGSQL_BOTH, 0);
2600 2010 : }
2601 : /* }}} */
2602 :
2603 : /* {{{ proto object pg_fetch_object(resource result [, int row [, string class_name [, NULL|array ctor_params]]])
2604 : Fetch a row as an object */
2605 : PHP_FUNCTION(pg_fetch_object)
2606 2004 : {
2607 : /* pg_fetch_object() allowed result_type used to be. 3rd parameter
2608 : must be allowed for compatibility */
2609 2004 : php_pgsql_fetch_hash(INTERNAL_FUNCTION_PARAM_PASSTHRU, PGSQL_ASSOC, 1);
2610 2004 : }
2611 : /* }}} */
2612 :
2613 : /* {{{ proto array pg_fetch_all(resource result)
2614 : Fetch all rows into array */
2615 : PHP_FUNCTION(pg_fetch_all)
2616 3 : {
2617 : zval *result;
2618 : PGresult *pgsql_result;
2619 : pgsql_result_handle *pg_result;
2620 :
2621 3 : if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &result) == FAILURE) {
2622 0 : return;
2623 : }
2624 :
2625 3 : ZEND_FETCH_RESOURCE(pg_result, pgsql_result_handle *, &result, -1, "PostgreSQL result", le_result);
2626 :
2627 3 : pgsql_result = pg_result->result;
2628 3 : array_init(return_value);
2629 3 : if (php_pgsql_result2array(pgsql_result, return_value TSRMLS_CC) == FAILURE) {
2630 2 : zval_dtor(return_value);
2631 2 : RETURN_FALSE;
2632 : }
2633 : }
2634 : /* }}} */
2635 :
2636 : /* {{{ proto array pg_fetch_all_columns(resource result [, int column_number])
2637 : Fetch all rows into array */
2638 : PHP_FUNCTION(pg_fetch_all_columns)
2639 0 : {
2640 : zval *result;
2641 : PGresult *pgsql_result;
2642 : pgsql_result_handle *pg_result;
2643 0 : unsigned long colno=0;
2644 : int pg_numrows, pg_row;
2645 : size_t num_fields;
2646 :
2647 0 : if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r|l", &result, &colno) == FAILURE) {
2648 0 : RETURN_FALSE;
2649 : }
2650 :
2651 0 : ZEND_FETCH_RESOURCE(pg_result, pgsql_result_handle *, &result, -1, "PostgreSQL result", le_result);
2652 :
2653 0 : pgsql_result = pg_result->result;
2654 :
2655 0 : num_fields = PQnfields(pgsql_result);
2656 0 : if (colno >= num_fields || colno < 0) {
2657 0 : php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid column number '%ld'", colno);
2658 0 : RETURN_FALSE;
2659 : }
2660 :
2661 0 : array_init(return_value);
2662 :
2663 0 : if ((pg_numrows = PQntuples(pgsql_result)) <= 0) {
2664 0 : return;
2665 : }
2666 :
2667 0 : for (pg_row = 0; pg_row < pg_numrows; pg_row++) {
2668 0 : if (PQgetisnull(pgsql_result, pg_row, colno)) {
2669 0 : add_next_index_null(return_value);
2670 : } else {
2671 0 : add_next_index_string(return_value, PQgetvalue(pgsql_result, pg_row, colno), 1);
2672 : }
2673 : }
2674 : }
2675 : /* }}} */
2676 :
2677 : /* {{{ proto bool pg_result_seek(resource result, int offset)
2678 : Set internal row offset */
2679 : PHP_FUNCTION(pg_result_seek)
2680 2 : {
2681 : zval *result;
2682 : long row;
2683 : pgsql_result_handle *pg_result;
2684 :
2685 2 : if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rl", &result, &row) == FAILURE) {
2686 0 : return;
2687 : }
2688 :
2689 2 : ZEND_FETCH_RESOURCE(pg_result, pgsql_result_handle *, &result, -1, "PostgreSQL result", le_result);
2690 :
2691 2 : if (row < 0 || row >= PQntuples(pg_result->result)) {
2692 0 : RETURN_FALSE;
2693 : }
2694 :
2695 : /* seek to offset */
2696 2 : pg_result->row = row;
2697 2 : RETURN_TRUE;
2698 : }
2699 : /* }}} */
2700 :
2701 :
2702 : #define PHP_PG_DATA_LENGTH 1
2703 : #define PHP_PG_DATA_ISNULL 2
2704 :
2705 : /* {{{ php_pgsql_data_info
2706 : */
2707 : static void php_pgsql_data_info(INTERNAL_FUNCTION_PARAMETERS, int entry_type)
2708 6 : {
2709 : zval *result, **field;
2710 : long row;
2711 : PGresult *pgsql_result;
2712 : pgsql_result_handle *pg_result;
2713 6 : int field_offset, pgsql_row, argc = ZEND_NUM_ARGS();
2714 :
2715 6 : if (argc == 2) {
2716 6 : if (zend_parse_parameters(argc TSRMLS_CC, "rZ", &result, &field) == FAILURE) {
2717 0 : return;
2718 : }
2719 : } else {
2720 0 : if (zend_parse_parameters(argc TSRMLS_CC, "rlZ", &result, &row, &field) == FAILURE) {
2721 0 : return;
2722 : }
2723 : }
2724 :
2725 6 : ZEND_FETCH_RESOURCE(pg_result, pgsql_result_handle *, &result, -1, "PostgreSQL result", le_result);
2726 :
2727 6 : pgsql_result = pg_result->result;
2728 6 : if (argc == 2) {
2729 6 : if (pg_result->row < 0) {
2730 0 : pg_result->row = 0;
2731 : }
2732 6 : pgsql_row = pg_result->row;
2733 6 : if (pgsql_row < 0 || pgsql_row >= PQntuples(pgsql_result)) {
2734 0 : RETURN_FALSE;
2735 : }
2736 : } else {
2737 0 : pgsql_row = row;
2738 0 : if (pgsql_row < 0 || pgsql_row >= PQntuples(pgsql_result)) {
2739 0 : php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to jump to row %ld on PostgreSQL result index %ld",
2740 : row, Z_LVAL_P(result));
2741 0 : RETURN_FALSE;
2742 : }
2743 : }
2744 :
2745 6 : switch(Z_TYPE_PP(field)) {
2746 : case IS_STRING:
2747 0 : convert_to_string_ex(field);
2748 0 : field_offset = PQfnumber(pgsql_result, Z_STRVAL_PP(field));
2749 0 : break;
2750 : default:
2751 6 : convert_to_long_ex(field);
2752 6 : field_offset = Z_LVAL_PP(field);
2753 : break;
2754 : }
2755 6 : if (field_offset < 0 || field_offset >= PQnfields(pgsql_result)) {
2756 0 : php_error_docref(NULL TSRMLS_CC, E_WARNING, "Bad column offset specified");
2757 0 : RETURN_FALSE;
2758 : }
2759 :
2760 6 : switch (entry_type) {
2761 : case PHP_PG_DATA_LENGTH:
2762 3 : Z_LVAL_P(return_value) = PQgetlength(pgsql_result, pgsql_row, field_offset);
2763 3 : break;
2764 : case PHP_PG_DATA_ISNULL:
2765 3 : Z_LVAL_P(return_value) = PQgetisnull(pgsql_result, pgsql_row, field_offset);
2766 : break;
2767 : }
2768 6 : Z_TYPE_P(return_value) = IS_LONG;
2769 : }
2770 : /* }}} */
2771 :
2772 : /* {{{ proto int pg_field_prtlen(resource result, [int row,] mixed field_name_or_number)
2773 : Returns the printed length */
2774 : PHP_FUNCTION(pg_field_prtlen)
2775 3 : {
2776 3 : php_pgsql_data_info(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_PG_DATA_LENGTH);
2777 3 : }
2778 : /* }}} */
2779 :
2780 : /* {{{ proto int pg_field_is_null(resource result, [int row,] mixed field_name_or_number)
2781 : Test if a field is NULL */
2782 : PHP_FUNCTION(pg_field_is_null)
2783 3 : {
2784 3 : php_pgsql_data_info(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_PG_DATA_ISNULL);
2785 3 : }
2786 : /* }}} */
2787 :
2788 : /* {{{ proto bool pg_free_result(resource result)
2789 : Free result memory */
2790 : PHP_FUNCTION(pg_free_result)
2791 5 : {
2792 : zval *result;
2793 : pgsql_result_handle *pg_result;
2794 :
2795 5 : if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &result) == FAILURE) {
2796 0 : return;
2797 : }
2798 :
2799 5 : ZEND_FETCH_RESOURCE(pg_result, pgsql_result_handle *, &result, -1, "PostgreSQL result", le_result);
2800 5 : if (Z_LVAL_P(result) == 0) {
2801 0 : RETURN_FALSE;
2802 : }
2803 5 : zend_list_delete(Z_RESVAL_P(result));
2804 5 : RETURN_TRUE;
2805 : }
2806 : /* }}} */
2807 :
2808 : /* {{{ proto string pg_last_oid(resource result)
2809 : Returns the last object identifier */
2810 : PHP_FUNCTION(pg_last_oid)
2811 3 : {
2812 : zval *result;
2813 : PGresult *pgsql_result;
2814 : pgsql_result_handle *pg_result;
2815 : #ifdef HAVE_PQOIDVALUE
2816 : Oid oid;
2817 : #endif
2818 :
2819 3 : if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &result) == FAILURE) {
2820 0 : return;
2821 : }
2822 :
2823 3 : ZEND_FETCH_RESOURCE(pg_result, pgsql_result_handle *, &result, -1, "PostgreSQL result", le_result);
2824 3 : pgsql_result = pg_result->result;
2825 : #ifdef HAVE_PQOIDVALUE
2826 3 : oid = PQoidValue(pgsql_result);
2827 3 : if (oid == InvalidOid) {
2828 3 : RETURN_FALSE;
2829 : }
2830 0 : PGSQL_RETURN_OID(oid);
2831 : #else
2832 : Z_STRVAL_P(return_value) = (char *) PQoidStatus(pgsql_result);
2833 : if (Z_STRVAL_P(return_value)) {
2834 : RETURN_STRING(Z_STRVAL_P(return_value), 1);
2835 : }
2836 : RETURN_STRING("", 1);
2837 : #endif
2838 : }
2839 : /* }}} */
2840 :
2841 : /* {{{ proto bool pg_trace(string filename [, string mode [, resource connection]])
2842 : Enable tracing a PostgreSQL connection */
2843 : PHP_FUNCTION(pg_trace)
2844 1 : {
2845 1 : char *z_filename, *mode = "w";
2846 : int z_filename_len, mode_len;
2847 1 : zval *pgsql_link = NULL;
2848 1 : int id = -1, argc = ZEND_NUM_ARGS();
2849 : PGconn *pgsql;
2850 1 : FILE *fp = NULL;
2851 : php_stream *stream;
2852 1 : id = PGG(default_link);
2853 :
2854 1 : if (zend_parse_parameters(argc TSRMLS_CC, "s|sr", &z_filename, &z_filename_len, &mode, &mode_len, &pgsql_link) == FAILURE) {
2855 0 : return;
2856 : }
2857 :
2858 1 : if (argc < 3) {
2859 0 : CHECK_DEFAULT_LINK(id);
2860 : }
2861 :
2862 1 : if (pgsql_link == NULL && id == -1) {
2863 0 : RETURN_FALSE;
2864 : }
2865 :
2866 1 : ZEND_FETCH_RESOURCE2(pgsql, PGconn *, &pgsql_link, id, "PostgreSQL link", le_link, le_plink);
2867 :
2868 1 : stream = php_stream_open_wrapper(z_filename, mode, ENFORCE_SAFE_MODE|REPORT_ERRORS, NULL);
2869 :
2870 1 : if (!stream) {
2871 0 : RETURN_FALSE;
2872 : }
2873 :
2874 1 : if (FAILURE == php_stream_cast(stream, PHP_STREAM_AS_STDIO, (void**)&fp, REPORT_ERRORS)) {
2875 0 : php_stream_close(stream);
2876 0 : RETURN_FALSE;
2877 : }
2878 : php_stream_auto_cleanup(stream);
2879 1 : PQtrace(pgsql, fp);
2880 1 : RETURN_TRUE;
2881 : }
2882 : /* }}} */
2883 :
2884 : /* {{{ proto bool pg_untrace([resource connection])
2885 : Disable tracing of a PostgreSQL connection */
2886 : PHP_FUNCTION(pg_untrace)
2887 0 : {
2888 0 : zval *pgsql_link = NULL;
2889 0 : int id = -1, argc = ZEND_NUM_ARGS();
2890 : PGconn *pgsql;
2891 :
2892 0 : if (zend_parse_parameters(argc TSRMLS_CC, "|r", &pgsql_link) == FAILURE) {
2893 0 : return;
2894 : }
2895 :
2896 0 : if (argc == 0) {
2897 0 : id = PGG(default_link);
2898 0 : CHECK_DEFAULT_LINK(id);
2899 : }
2900 :
2901 0 : if (pgsql_link == NULL && id == -1) {
2902 0 : RETURN_FALSE;
2903 : }
2904 :
2905 0 : ZEND_FETCH_RESOURCE2(pgsql, PGconn *, &pgsql_link, id, "PostgreSQL link", le_link, le_plink);
2906 0 : PQuntrace(pgsql);
2907 0 : RETURN_TRUE;
2908 : }
2909 : /* }}} */
2910 :
2911 : /* {{{ proto mixed pg_lo_create([resource connection],[mixed large_object_oid])
2912 : Create a large object */
2913 : PHP_FUNCTION(pg_lo_create)
2914 6 : {
2915 6 : zval *pgsql_link = NULL, *oid = NULL;
2916 : PGconn *pgsql;
2917 6 : Oid pgsql_oid, wanted_oid = InvalidOid;
2918 6 : int id = -1, argc = ZEND_NUM_ARGS();
2919 :
2920 6 : if (zend_parse_parameters(argc TSRMLS_CC, "|zz", &pgsql_link, &oid) == FAILURE) {
2921 0 : return;
2922 : }
2923 :
2924 6 : if ((argc == 1) && (Z_TYPE_P(pgsql_link) != IS_RESOURCE)) {
2925 1 : oid = pgsql_link;
2926 1 : pgsql_link = NULL;
2927 : }
2928 :
2929 6 : if (pgsql_link == NULL) {
2930 1 : id = PGG(default_link);
2931 1 : CHECK_DEFAULT_LINK(id);
2932 1 : if (id == -1) {
2933 0 : RETURN_FALSE;
2934 : }
2935 : }
2936 :
2937 6 : ZEND_FETCH_RESOURCE2(pgsql, PGconn *, &pgsql_link, id, "PostgreSQL link", le_link, le_plink);
2938 :
2939 6 : if (oid) {
2940 : #ifndef HAVE_PG_LO_CREATE
2941 : php_error_docref(NULL TSRMLS_CC, E_NOTICE, "OID value passing not supported");
2942 : #else
2943 3 : switch (Z_TYPE_P(oid)) {
2944 : case IS_STRING:
2945 : {
2946 : char *end_ptr;
2947 0 : wanted_oid = (Oid)strtoul(Z_STRVAL_P(oid), &end_ptr, 10);
2948 0 : if ((Z_STRVAL_P(oid)+Z_STRLEN_P(oid)) != end_ptr) {
2949 : /* wrong integer format */
2950 0 : php_error_docref(NULL TSRMLS_CC, E_NOTICE, "invalid OID value passed");
2951 0 : RETURN_FALSE;
2952 : }
2953 : }
2954 0 : break;
2955 : case IS_UNICODE:
2956 : {
2957 : UChar *end_ptr;
2958 1 : wanted_oid = (Oid)zend_u_strtoul(Z_USTRVAL_P(oid), &end_ptr, 10);
2959 1 : if ((Z_USTRVAL_P(oid)+Z_USTRLEN_P(oid)) != end_ptr) {
2960 : /* wrong integer format */
2961 0 : php_error_docref(NULL TSRMLS_CC, E_NOTICE, "invalid OID value passed");
2962 0 : RETURN_FALSE;
2963 : }
2964 : }
2965 1 : break;
2966 : case IS_LONG:
2967 2 : if (Z_LVAL_P(oid) < (long)InvalidOid) {
2968 0 : php_error_docref(NULL TSRMLS_CC, E_NOTICE, "invalid OID value passed");
2969 0 : RETURN_FALSE;
2970 : }
2971 2 : wanted_oid = (Oid)Z_LVAL_P(oid);
2972 2 : break;
2973 : default:
2974 0 : php_error_docref(NULL TSRMLS_CC, E_NOTICE, "invalid OID value passed");
2975 0 : RETURN_FALSE;
2976 : }
2977 3 : if ((pgsql_oid = lo_create(pgsql, wanted_oid)) == InvalidOid) {
2978 0 : php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to create PostgreSQL large object");
2979 0 : RETURN_FALSE;
2980 : }
2981 :
2982 3 : PGSQL_RETURN_OID(pgsql_oid);
2983 : #endif
2984 : }
2985 :
2986 3 : if ((pgsql_oid = lo_creat(pgsql, INV_READ|INV_WRITE)) == InvalidOid) {
2987 0 : php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to create PostgreSQL large object");
2988 0 : RETURN_FALSE;
2989 : }
2990 :
2991 3 : PGSQL_RETURN_OID(pgsql_oid);
2992 : }
2993 : /* }}} */
2994 :
2995 : /* {{{ proto bool pg_lo_unlink([resource connection,] string large_object_oid)
2996 : Delete a large object */
2997 : PHP_FUNCTION(pg_lo_unlink)
2998 9 : {
2999 9 : zval *pgsql_link = NULL;
3000 : long oid_long;
3001 : char *oid_string, *end_ptr;
3002 : int oid_strlen;
3003 : PGconn *pgsql;
3004 : Oid oid;
3005 9 : int id = -1;
3006 9 : int argc = ZEND_NUM_ARGS();
3007 :
3008 : /* accept string type since Oid type is unsigned int */
3009 9 : if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, argc TSRMLS_CC,
3010 : "rs", &pgsql_link, &oid_string, &oid_strlen) == SUCCESS) {
3011 6 : oid = (Oid)strtoul(oid_string, &end_ptr, 10);
3012 6 : if ((oid_string+oid_strlen) != end_ptr) {
3013 : /* wrong integer format */
3014 0 : php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Wrong OID value passed");
3015 0 : RETURN_FALSE;
3016 : }
3017 : }
3018 3 : else if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, argc TSRMLS_CC,
3019 : "rl", &pgsql_link, &oid_long) == SUCCESS) {
3020 0 : if (oid_long <= InvalidOid) {
3021 0 : php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Invalid OID specified");
3022 0 : RETURN_FALSE;
3023 : }
3024 0 : oid = (Oid)oid_long;
3025 : }
3026 3 : else if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, argc TSRMLS_CC,
3027 : "s", &oid_string, &oid_strlen) == SUCCESS) {
3028 3 : oid = (Oid)strtoul(oid_string, &end_ptr, 10);
3029 3 : if ((oid_string+oid_strlen) != end_ptr) {
3030 : /* wrong integer format */
3031 0 : php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Wrong OID value passed");
3032 0 : RETURN_FALSE;
3033 : }
3034 3 : id = PGG(default_link);
3035 3 : CHECK_DEFAULT_LINK(id);
3036 : }
3037 0 : else if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, argc TSRMLS_CC,
3038 : "l", &oid_long) == SUCCESS) {
3039 0 : if (oid_long <= InvalidOid) {
3040 0 : php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Invalid OID is specified");
3041 0 : RETURN_FALSE;
3042 : }
3043 0 : oid = (Oid)oid_long;
3044 0 : id = PGG(default_link);
3045 0 : CHECK_DEFAULT_LINK(id);
3046 : }
3047 : else {
3048 0 : php_error_docref(NULL TSRMLS_CC, E_WARNING, "Requires 1 or 2 arguments");
3049 0 : RETURN_FALSE;
3050 : }
3051 9 : if (pgsql_link == NULL && id == -1) {
3052 0 : RETURN_FALSE;
3053 : }
3054 :
3055 9 : ZEND_FETCH_RESOURCE2(pgsql, PGconn *, &pgsql_link, id, "PostgreSQL link", le_link, le_plink);
3056 :
3057 9 : if (lo_unlink(pgsql, oid) == -1) {
3058 0 : php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to delete PostgreSQL large object %u", oid);
3059 0 : RETURN_FALSE;
3060 : }
3061 9 : RETURN_TRUE;
3062 : }
3063 : /* }}} */
3064 :
3065 : /* {{{ proto resource pg_lo_open([resource connection,] int large_object_oid, string mode)
3066 : Open a large object and return fd */
3067 : PHP_FUNCTION(pg_lo_open)
3068 3 : {
3069 3 : zval *pgsql_link = NULL;
3070 : long oid_long;
3071 : char *oid_string, *end_ptr, *mode_string;
3072 : int oid_strlen, mode_strlen;
3073 : PGconn *pgsql;
3074 : Oid oid;
3075 3 : int id = -1, pgsql_mode=0, pgsql_lofd;
3076 3 : int create=0;
3077 : pgLofp *pgsql_lofp;
3078 3 : int argc = ZEND_NUM_ARGS();
3079 :
3080 : /* accept string type since Oid is unsigned int */
3081 3 : if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, argc TSRMLS_CC,
3082 : "rss", &pgsql_link, &oid_string, &oid_strlen, &mode_string, &mode_strlen) == SUCCESS) {
3083 3 : oid = (Oid)strtoul(oid_string, &end_ptr, 10);
3084 3 : if ((oid_string+oid_strlen) != end_ptr) {
3085 : /* wrong integer format */
3086 0 : php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Wrong OID value passed");
3087 0 : RETURN_FALSE;
3088 : }
3089 : }
3090 0 : else if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, argc TSRMLS_CC,
3091 : "rls", &pgsql_link, &oid_long, &mode_string, &mode_strlen) == SUCCESS) {
3092 0 : if (oid_long <= InvalidOid) {
3093 0 : php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Invalid OID specified");
3094 0 : RETURN_FALSE;
3095 : }
3096 0 : oid = (Oid)oid_long;
3097 : }
3098 0 : else if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, argc TSRMLS_CC,
3099 : "ss", &oid_string, &oid_strlen, &mode_string, &mode_strlen) == SUCCESS) {
3100 0 : oid = (Oid)strtoul(oid_string, &end_ptr, 10);
3101 0 : if ((oid_string+oid_strlen) != end_ptr) {
3102 : /* wrong integer format */
3103 0 : php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Wrong OID value passed");
3104 0 : RETURN_FALSE;
3105 : }
3106 0 : id = PGG(default_link);
3107 0 : CHECK_DEFAULT_LINK(id);
3108 : }
3109 0 : else if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, argc TSRMLS_CC,
3110 : "ls", &oid_long, &mode_string, &mode_strlen) == SUCCESS) {
3111 0 : if (oid_long <= InvalidOid) {
3112 0 : php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Invalid OID specified");
3113 0 : RETURN_FALSE;
3114 : }
3115 0 : oid = (Oid)oid_long;
3116 0 : id = PGG(default_link);
3117 0 : CHECK_DEFAULT_LINK(id);
3118 : }
3119 : else {
3120 0 : php_error_docref(NULL TSRMLS_CC, E_WARNING, "Requires 1 or 2 arguments");
3121 0 : RETURN_FALSE;
3122 : }
3123 3 : if (pgsql_link == NULL && id == -1) {
3124 0 : RETURN_FALSE;
3125 : }
3126 :
3127 3 : ZEND_FETCH_RESOURCE2(pgsql, PGconn *, &pgsql_link, id, "PostgreSQL link", le_link, le_plink);
3128 :
3129 : /* r/w/+ is little bit more PHP-like than INV_READ/INV_WRITE and a lot of
3130 : faster to type. Unfortunately, doesn't behave the same way as fopen()...
3131 : (Jouni)
3132 : */
3133 :
3134 3 : if (mode_string[0] == 'r') {
3135 0 : pgsql_mode |= INV_READ;
3136 0 : if (mode_string[1] == '+') {
3137 0 : pgsql_mode |= INV_WRITE;
3138 : }
3139 3 : } else if (mode_string[0] == 'w') {
3140 3 : pgsql_mode |= INV_WRITE;
3141 3 : create = 1;
3142 3 : if (mode_string[1] == '+') {
3143 0 : pgsql_mode |= INV_READ;
3144 : }
3145 : }
3146 :
3147 3 : pgsql_lofp = (pgLofp *) emalloc(sizeof(pgLofp));
3148 :
3149 3 : if ((pgsql_lofd = lo_open(pgsql, oid, pgsql_mode)) == -1) {
3150 0 : if (create) {
3151 0 : if ((oid = lo_creat(pgsql, INV_READ|INV_WRITE)) == 0) {
3152 0 : efree(pgsql_lofp);
3153 0 : php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to create PostgreSQL large object");
3154 0 : RETURN_FALSE;
3155 : } else {
3156 0 : if ((pgsql_lofd = lo_open(pgsql, oid, pgsql_mode)) == -1) {
3157 0 : if (lo_unlink(pgsql, oid) == -1) {
3158 0 : efree(pgsql_lofp);
3159 0 : php_error_docref(NULL TSRMLS_CC, E_WARNING, "Something is really messed up! Your database is badly corrupted in a way NOT related to PHP");
3160 0 : RETURN_FALSE;
3161 : }
3162 0 : efree(pgsql_lofp);
3163 0 : php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to open PostgreSQL large object");
3164 0 : RETURN_FALSE;
3165 : } else {
3166 0 : pgsql_lofp->conn = pgsql;
3167 0 : pgsql_lofp->lofd = pgsql_lofd;
3168 0 : Z_LVAL_P(return_value) = zend_list_insert(pgsql_lofp, le_lofp);
3169 0 : Z_TYPE_P(return_value) = IS_LONG;
3170 : }
3171 : }
3172 : } else {
3173 0 : efree(pgsql_lofp);
3174 0 : php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to open PostgreSQL large object");
3175 0 : RETURN_FALSE;
3176 : }
3177 : } else {
3178 3 : pgsql_lofp->conn = pgsql;
3179 3 : pgsql_lofp->lofd = pgsql_lofd;
3180 3 : ZEND_REGISTER_RESOURCE(return_value, pgsql_lofp, le_lofp);
3181 : }
3182 : }
3183 : /* }}} */
3184 :
3185 : /* {{{ proto bool pg_lo_close(resource large_object)
3186 : Close a large object */
3187 : PHP_FUNCTION(pg_lo_close)
3188 3 : {
3189 : zval *pgsql_lofp;
3190 : pgLofp *pgsql;
3191 :
3192 3 : if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &pgsql_lofp) == FAILURE) {
3193 0 : return;
3194 : }
3195 :
3196 3 : ZEND_FETCH_RESOURCE(pgsql, pgLofp *, &pgsql_lofp, -1, "PostgreSQL large object", le_lofp);
3197 :
3198 3 : if (lo_close((PGconn *)pgsql->conn, pgsql->lofd) < 0) {
3199 0 : php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to close PostgreSQL large object descriptor %d", pgsql->lofd);
3200 0 : RETVAL_FALSE;
3201 : } else {
3202 3 : RETVAL_TRUE;
3203 : }
3204 :
3205 3 : zend_list_delete(Z_RESVAL_P(pgsql_lofp));
3206 3 : return;
3207 : }
3208 : /* }}} */
3209 :
3210 : #define PGSQL_LO_READ_BUF_SIZE 8192
3211 :
3212 : /* {{{ proto string pg_lo_read(resource large_object [, int len])
3213 : Read a large object */
3214 : PHP_FUNCTION(pg_lo_read)
3215 1 : {
3216 : zval *pgsql_id;
3217 : long len;
3218 1 : int buf_len = PGSQL_LO_READ_BUF_SIZE, nbytes, argc = ZEND_NUM_ARGS();
3219 : char *buf;
3220 : pgLofp *pgsql;
3221 :
3222 1 : if (zend_parse_parameters(argc TSRMLS_CC, "r|l", &pgsql_id, &len) == FAILURE) {
3223 0 : return;
3224 : }
3225 :
3226 1 : ZEND_FETCH_RESOURCE(pgsql, pgLofp *, &pgsql_id, -1, "PostgreSQL large object", le_lofp);
3227 :
3228 1 : if (argc > 1) {
3229 1 : buf_len = len;
3230 : }
3231 :
3232 1 : buf = (char *) safe_emalloc(sizeof(char), (buf_len+1), 0);
3233 1 : if ((nbytes = lo_read((PGconn *)pgsql->conn, pgsql->lofd, buf, buf_len))<0) {
3234 0 : efree(buf);
3235 0 : RETURN_FALSE;
3236 : }
3237 :
3238 1 : buf[nbytes] = '\0';
3239 1 : RETURN_STRINGL(buf, nbytes, 0);
3240 : }
3241 : /* }}} */
3242 :
3243 : /* {{{ proto int pg_lo_write(resource large_object, string buf [, int len])
3244 : Write a large object */
3245 : PHP_FUNCTION(pg_lo_write)
3246 1 : {
3247 : zval *pgsql_id;
3248 : char *str;
3249 : long z_len;
3250 : int str_len, nbytes;
3251 : int len;
3252 : pgLofp *pgsql;
3253 1 : int argc = ZEND_NUM_ARGS();
3254 :
3255 1 : if (zend_parse_parameters(argc TSRMLS_CC, "rs|l", &pgsql_id, &str, &str_len, &z_len) == FAILURE) {
3256 0 : return;
3257 : }
3258 :
3259 1 : if (argc > 2) {
3260 0 : if (z_len > str_len) {
3261 0 : php_error_docref(NULL TSRMLS_CC, E_WARNING, "Cannot write more than buffer size %d. Tried to write %ld", str_len, z_len);
3262 0 : RETURN_FALSE;
3263 : }
3264 0 : if (z_len < 0) {
3265 0 : php_error_docref(NULL TSRMLS_CC, E_WARNING, "Buffer size must be larger than 0, but %ld was specified", z_len);
3266 0 : RETURN_FALSE;
3267 : }
3268 0 : len = z_len;
3269 : }
3270 : else {
3271 1 : len = str_len;
3272 : }
3273 :
3274 1 : ZEND_FETCH_RESOURCE(pgsql, pgLofp *, &pgsql_id, -1, "PostgreSQL large object", le_lofp);
3275 :
3276 1 : if ((nbytes = lo_write((PGconn *)pgsql->conn, pgsql->lofd, str, len)) == -1) {
3277 0 : RETURN_FALSE;
3278 : }
3279 :
3280 1 : RETURN_LONG(nbytes);
3281 : }
3282 : /* }}} */
3283 :
3284 : /* {{{ proto int pg_lo_read_all(resource large_object)
3285 : Read a large object and send straight to browser */
3286 : PHP_FUNCTION(pg_lo_read_all)
3287 1 : {
3288 : zval *pgsql_id;
3289 : int tbytes;
3290 : volatile int nbytes;
3291 : char buf[PGSQL_LO_READ_BUF_SIZE];
3292 : pgLofp *pgsql;
3293 :
3294 1 : if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &pgsql_id) == FAILURE) {
3295 0 : return;
3296 : }
3297 :
3298 1 : ZEND_FETCH_RESOURCE(pgsql, pgLofp *, &pgsql_id, -1, "PostgreSQL large object", le_lofp);
3299 :
3300 1 : tbytes = 0;
3301 3 : while ((nbytes = lo_read((PGconn *)pgsql->conn, pgsql->lofd, buf, PGSQL_LO_READ_BUF_SIZE))>0) {
3302 1 : PHPWRITE(buf, nbytes);
3303 1 : tbytes += nbytes;
3304 : }
3305 1 : RETURN_LONG(tbytes);
3306 : }
3307 : /* }}} */
3308 :
3309 : /* {{{ proto int pg_lo_import([resource connection, ] string filename [, mixed oid])
3310 : Import large object direct from filesystem */
3311 : PHP_FUNCTION(pg_lo_import)
3312 4 : {
3313 4 : zval *pgsql_link = NULL, *oid = NULL;
3314 : char *file_in;
3315 4 : int id = -1, name_len;
3316 4 : int argc = ZEND_NUM_ARGS();
3317 : PGconn *pgsql;
3318 : Oid returned_oid;
3319 :
3320 4 : if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, argc TSRMLS_CC,
3321 : "rs|z", &pgsql_link, &file_in, &name_len, &oid) == SUCCESS) {
3322 : ;
3323 : }
3324 0 : else if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, argc TSRMLS_CC,
3325 : "s|z", &file_in, &name_len, &oid) == SUCCESS) {
3326 0 : id = PGG(default_link);
3327 0 : CHECK_DEFAULT_LINK(id);
3328 : }
3329 : /* old calling convention, deprecated since PHP 4.2 */
3330 0 : else if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, argc TSRMLS_CC,
3331 : "sr", &file_in, &name_len, &pgsql_link ) == SUCCESS) {
3332 0 : php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Old API is used");
3333 : }
3334 : else {
3335 0 : WRONG_PARAM_COUNT;
3336 : }
3337 :
3338 4 : if (php_check_open_basedir(file_in TSRMLS_CC)) {
3339 0 : RETURN_FALSE;
3340 : }
3341 :
3342 4 : if (pgsql_link == NULL && id == -1) {
3343 0 : RETURN_FALSE;
3344 : }
3345 :
3346 4 : ZEND_FETCH_RESOURCE2(pgsql, PGconn *, &pgsql_link, id, "PostgreSQL link", le_link, le_plink);
3347 :
3348 4 : if (oid) {
3349 : #ifndef HAVE_PG_LO_IMPORT_WITH_OID
3350 3 : php_error_docref(NULL TSRMLS_CC, E_NOTICE, "OID value passing not supported");
3351 : #else
3352 : Oid wanted_oid;
3353 : switch (Z_TYPE_P(oid)) {
3354 : case IS_STRING:
3355 : {
3356 : char *end_ptr;
3357 : wanted_oid = (Oid)strtoul(Z_STRVAL_P(oid), &end_ptr, 10);
3358 : if ((Z_STRVAL_P(oid)+Z_STRLEN_P(oid)) != end_ptr) {
3359 : /* wrong integer format, trailing non-numeric characters */
3360 : php_error_docref(NULL TSRMLS_CC, E_NOTICE, "invalid OID value passed");
3361 : RETURN_FALSE;
3362 : }
3363 : }
3364 : break;
3365 : case IS_UNICODE:
3366 : {
3367 : UChar *end_ptr;
3368 : wanted_oid = (Oid)zend_u_strtoul(Z_USTRVAL_P(oid), &end_ptr, 10);
3369 : if ((Z_USTRVAL_P(oid)+Z_USTRLEN_P(oid)) != end_ptr) {
3370 : /* wrong integer format, trailing non-numeric characters */
3371 : php_error_docref(NULL TSRMLS_CC, E_NOTICE, "invalid OID value passed");
3372 : RETURN_FALSE;
3373 : }
3374 : }
3375 : break;
3376 : case IS_LONG:
3377 : if (Z_LVAL_P(oid) < (long)InvalidOid) {
3378 : php_error_docref(NULL TSRMLS_CC, E_NOTICE, "invalid OID value passed");
3379 : RETURN_FALSE;
3380 : }
3381 : wanted_oid = (Oid)Z_LVAL_P(oid);
3382 : break;
3383 : default:
3384 : php_error_docref(NULL TSRMLS_CC, E_NOTICE, "invalid OID value passed");
3385 : RETURN_FALSE;
3386 : }
3387 :
3388 : returned_oid = lo_import_with_oid(pgsql, file_in, wanted_oid);
3389 :
3390 : if (returned_oid == InvalidOid) {
3391 : RETURN_FALSE;
3392 : }
3393 :
3394 : PGSQL_RETURN_OID(returned_oid);
3395 : #endif
3396 : }
3397 :
3398 4 : returned_oid = lo_import(pgsql, file_in);
3399 :
3400 4 : if (returned_oid == InvalidOid) {
3401 0 : RETURN_FALSE;
3402 : }
3403 4 : PGSQL_RETURN_OID(returned_oid);
3404 : }
3405 : /* }}} */
3406 :
3407 : /* {{{ proto bool pg_lo_export([resource connection, ] int objoid, string filename)
3408 : Export large object direct to filesystem */
3409 : PHP_FUNCTION(pg_lo_export)
3410 1 : {
3411 1 : zval *pgsql_link = NULL;
3412 : char *file_out, *oid_string, *end_ptr;
3413 : int oid_strlen;
3414 1 : int id = -1, name_len;
3415 : long oid_long;
3416 : Oid oid;
3417 : PGconn *pgsql;
3418 1 : int argc = ZEND_NUM_ARGS();
3419 :
3420 : /* allow string to handle large OID value correctly */
3421 1 : if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, argc TSRMLS_CC,
3422 : "rls", &pgsql_link, &oid_long, &file_out, &name_len) == SUCCESS) {
3423 0 : if (oid_long <= InvalidOid) {
3424 0 : php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Invalid OID specified");
3425 0 : RETURN_FALSE;
3426 : }
3427 0 : oid = (Oid)oid_long;
3428 : }
3429 1 : else if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, argc TSRMLS_CC,
3430 : "rss", &pgsql_link, &oid_string, &oid_strlen, &file_out, &name_len) == SUCCESS) {
3431 0 : oid = (Oid)strtoul(oid_string, &end_ptr, 10);
3432 0 : if ((oid_string+oid_strlen) != end_ptr) {
3433 : /* wrong integer format */
3434 0 : php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Wrong OID value passed");
3435 0 : RETURN_FALSE;
3436 : }
3437 : }
3438 1 : else if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, argc TSRMLS_CC,
3439 : "ls", &oid_long, &file_out, &name_len) == SUCCESS) {
3440 0 : if (oid_long <= InvalidOid) {
3441 0 : php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Invalid OID specified");
3442 0 : RETURN_FALSE;
3443 : }
3444 0 : oid = (Oid)oid_long;
3445 0 : id = PGG(default_link);
3446 0 : CHECK_DEFAULT_LINK(id);
3447 : }
3448 1 : else if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, argc TSRMLS_CC,
3449 : "ss", &oid_string, &oid_strlen, &file_out, &name_len) == SUCCESS) {
3450 0 : oid = (Oid)strtoul(oid_string, &end_ptr, 10);
3451 0 : if ((oid_string+oid_strlen) != end_ptr) {
3452 : /* wrong integer format */
3453 0 : php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Wrong OID value passed");
3454 0 : RETURN_FALSE;
3455 : }
3456 0 : id = PGG(default_link);
3457 0 : CHECK_DEFAULT_LINK(id);
3458 : }
3459 1 : else if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, argc TSRMLS_CC,
3460 : "ssr", &oid_string, &oid_strlen, &file_out, &name_len, &pgsql_link) == SUCCESS) {
3461 1 : oid = (Oid)strtoul(oid_string, &end_ptr, 10);
3462 1 : if ((oid_string+oid_strlen) != end_ptr) {
3463 : /* wrong integer format */
3464 0 : php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Wrong OID value passed");
3465 0 : RETURN_FALSE;
3466 : }
3467 : }
3468 0 : else if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, argc TSRMLS_CC,
3469 : "lsr", &oid_long, &file_out, &name_len, &pgsql_link) == SUCCESS) {
3470 0 : php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Old API is used");
3471 0 : if (oid_long <= InvalidOid) {
3472 0 : php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Invalid OID specified");
3473 0 : RETURN_FALSE;
3474 : }
3475 0 : oid = (Oid)oid_long;
3476 : }
3477 : else {
3478 0 : php_error_docref(NULL TSRMLS_CC, E_WARNING, "Requires 2 or 3 arguments");
3479 0 : RETURN_FALSE;
3480 : }
3481 :
3482 1 : if (php_check_open_basedir(file_out TSRMLS_CC)) {
3483 0 : RETURN_FALSE;
3484 : }
3485 :
3486 1 : if (pgsql_link == NULL && id == -1) {
3487 0 : RETURN_FALSE;
3488 : }
3489 :
3490 1 : ZEND_FETCH_RESOURCE2(pgsql, PGconn *, &pgsql_link, id, "PostgreSQL link", le_link, le_plink);
3491 :
3492 1 : if (lo_export(pgsql, oid, file_out)) {
3493 1 : RETURN_TRUE;
3494 : }
3495 0 : RETURN_FALSE;
3496 : }
3497 : /* }}} */
3498 :
3499 : /* {{{ proto bool pg_lo_seek(resource large_object, int offset [, int whence])
3500 : Seeks position of large object */
3501 : PHP_FUNCTION(pg_lo_seek)
3502 1 : {
3503 1 : zval *pgsql_id = NULL;
3504 1 : long offset = 0, whence = SEEK_CUR;
3505 : pgLofp *pgsql;
3506 1 : int argc = ZEND_NUM_ARGS();
3507 :
3508 1 : if (zend_parse_parameters(argc TSRMLS_CC, "rl|l", &pgsql_id, &offset, &whence) == FAILURE) {
3509 0 : return;
3510 : }
3511 1 : if (whence != SEEK_SET && whence != SEEK_CUR && whence != SEEK_END) {
3512 0 : php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid whence parameter");
3513 0 : return;
3514 : }
3515 :
3516 1 : ZEND_FETCH_RESOURCE(pgsql, pgLofp *, &pgsql_id, -1, "PostgreSQL large object", le_lofp);
3517 :
3518 1 : if (lo_lseek((PGconn *)pgsql->conn, pgsql->lofd, offset, whence) > -1) {
3519 1 : RETURN_TRUE;
3520 : } else {
3521 0 : RETURN_FALSE;
3522 : }
3523 : }
3524 : /* }}} */
3525 :
3526 : /* {{{ proto int pg_lo_tell(resource large_object)
3527 : Returns current position of large object */
3528 : PHP_FUNCTION(pg_lo_tell)
3529 1 : {
3530 1 : zval *pgsql_id = NULL;
3531 1 : int offset = 0;
3532 : pgLofp *pgsql;
3533 1 : int argc = ZEND_NUM_ARGS();
3534 :
3535 1 : if (zend_parse_parameters(argc TSRMLS_CC, "r", &pgsql_id) == FAILURE) {
3536 0 : return;
3537 : }
3538 :
3539 1 : ZEND_FETCH_RESOURCE(pgsql, pgLofp *, &pgsql_id, -1, "PostgreSQL large object", le_lofp);
3540 :
3541 1 : offset = lo_tell((PGconn *)pgsql->conn, pgsql->lofd);
3542 1 : RETURN_LONG(offset);
3543 : }
3544 : /* }}} */
3545 :
3546 : #if HAVE_PQSETERRORVERBOSITY
3547 : /* {{{ proto int pg_set_error_verbosity([resource connection,] int verbosity)
3548 : Set error verbosity */
3549 : PHP_FUNCTION(pg_set_error_verbosity)
3550 3 : {
3551 3 : zval *pgsql_link = NULL;
3552 : long verbosity;
3553 3 : int id = -1, argc = ZEND_NUM_ARGS();
3554 : PGconn *pgsql;
3555 :
3556 3 : if (argc == 1) {
3557 3 : if (zend_parse_parameters(argc TSRMLS_CC, "l", &verbosity) == FAILURE) {
3558 0 : return;
3559 : }
3560 3 : id = PGG(default_link);
3561 3 : CHECK_DEFAULT_LINK(id);
3562 : } else {
3563 0 : if (zend_parse_parameters(argc TSRMLS_CC, "rl", &pgsql_link, &verbosity) == FAILURE) {
3564 0 : return;
3565 : }
3566 : }
3567 :
3568 3 : if (pgsql_link == NULL && id == -1) {
3569 0 : RETURN_FALSE;
3570 : }
3571 :
3572 3 : ZEND_FETCH_RESOURCE2(pgsql, PGconn *, &pgsql_link, id, "PostgreSQL link", le_link, le_plink);
3573 :
3574 3 : if (verbosity & (PQERRORS_TERSE|PQERRORS_DEFAULT|PQERRORS_VERBOSE)) {
3575 2 : Z_LVAL_P(return_value) = PQsetErrorVerbosity(pgsql, verbosity);
3576 2 : Z_TYPE_P(return_value) = IS_LONG;
3577 : } else {
3578 1 : RETURN_FALSE;
3579 : }
3580 : }
3581 : /* }}} */
3582 : #endif
3583 :
3584 : #ifdef HAVE_PQCLIENTENCODING
3585 : /* {{{ proto int pg_set_client_encoding([resource connection,] string encoding)
3586 : Set client encoding */
3587 : PHP_FUNCTION(pg_set_client_encoding)
3588 1 : {
3589 : char *encoding;
3590 : int encoding_len;
3591 1 : zval *pgsql_link = NULL;
3592 1 : int id = -1, argc = ZEND_NUM_ARGS();
3593 : PGconn *pgsql;
3594 :
3595 1 : if (argc == 1) {
3596 0 : if (zend_parse_parameters(argc TSRMLS_CC, "s", &encoding, &encoding_len) == FAILURE) {
3597 0 : return;
3598 : }
3599 0 : id = PGG(default_link);
3600 0 : CHECK_DEFAULT_LINK(id);
3601 : } else {
3602 1 : if (zend_parse_parameters(argc TSRMLS_CC, "rs", &pgsql_link, &encoding, &encoding_len) == FAILURE) {
3603 0 : return;
3604 : }
3605 : }
3606 :
3607 1 : if (pgsql_link == NULL && id == -1) {
3608 0 : RETURN_FALSE;
3609 : }
3610 :
3611 1 : ZEND_FETCH_RESOURCE2(pgsql, PGconn *, &pgsql_link, id, "PostgreSQL link", le_link, le_plink);
3612 :
3613 1 : Z_LVAL_P(return_value) = PQsetClientEncoding(pgsql, encoding);
3614 1 : Z_TYPE_P(return_value) = IS_LONG;
3615 : }
3616 : /* }}} */
3617 :
3618 : /* {{{ proto string pg_client_encoding([resource connection])
3619 : Get the current client encoding */
3620 : PHP_FUNCTION(pg_client_encoding)
3621 1 : {
3622 1 : zval *pgsql_link = NULL;
3623 1 : int id = -1, argc = ZEND_NUM_ARGS();
3624 : PGconn *pgsql;
3625 :
3626 1 : if (zend_parse_parameters(argc TSRMLS_CC, "|r", &pgsql_link) == FAILURE) {
3627 0 : return;
3628 : }
3629 :
3630 1 : if (argc == 0) {
3631 0 : id = PGG(default_link);
3632 0 : CHECK_DEFAULT_LINK(id);
3633 : }
3634 :
3635 1 : if (pgsql_link == NULL && id == -1) {
3636 0 : RETURN_FALSE;
3637 : }
3638 :
3639 1 : ZEND_FETCH_RESOURCE2(pgsql, PGconn *, &pgsql_link, id, "PostgreSQL link", le_link, le_plink);
3640 :
3641 : /* Just do the same as found in PostgreSQL sources... */
3642 :
3643 : #ifndef HAVE_PGSQL_WITH_MULTIBYTE_SUPPORT
3644 : #define pg_encoding_to_char(x) "SQL_ASCII"
3645 : #endif
3646 :
3647 1 : Z_STRVAL_P(return_value) = (char *) pg_encoding_to_char(PQclientEncoding(pgsql));
3648 1 : Z_STRLEN_P(return_value) = strlen(Z_STRVAL_P(return_value));
3649 1 : Z_STRVAL_P(return_value) = (char *) estrdup(Z_STRVAL_P(return_value));
3650 1 : Z_TYPE_P(return_value) = IS_STRING;
3651 : }
3652 : /* }}} */
3653 : #endif
3654 :
3655 : #if !HAVE_PQGETCOPYDATA
3656 : #define COPYBUFSIZ 8192
3657 : #endif
3658 :
3659 : /* {{{ proto bool pg_end_copy([resource connection])
3660 : Sync with backend. Completes the Copy command */
3661 : PHP_FUNCTION(pg_end_copy)
3662 0 : {
3663 0 : zval *pgsql_link = NULL;
3664 0 : int id = -1, argc = ZEND_NUM_ARGS();
3665 : PGconn *pgsql;
3666 0 : int result = 0;
3667 :
3668 0 : if (zend_parse_parameters(argc TSRMLS_CC, "|r", &pgsql_link) == FAILURE) {
3669 0 : return;
3670 : }
3671 :
3672 0 : if (argc == 0) {
3673 0 : id = PGG(default_link);
3674 0 : CHECK_DEFAULT_LINK(id);
3675 : }
3676 :
3677 0 : if (pgsql_link == NULL && id == -1) {
3678 0 : RETURN_FALSE;
3679 : }
3680 :
3681 0 : ZEND_FETCH_RESOURCE2(pgsql, PGconn *, &pgsql_link, id, "PostgreSQL link", le_link, le_plink);
3682 :
3683 0 : result = PQendcopy(pgsql);
3684 :
3685 0 : if (result!=0) {
3686 0 : PHP_PQ_ERROR("Query failed: %s", pgsql);
3687 0 : RETURN_FALSE;
3688 : }
3689 0 : RETURN_TRUE;
3690 : }
3691 : /* }}} */
3692 :
3693 :
3694 : /* {{{ proto bool pg_put_line([resource connection,] string query)
3695 : Send null-terminated string to backend server*/
3696 : PHP_FUNCTION(pg_put_line)
3697 0 : {
3698 : char *query;
3699 0 : zval *pgsql_link = NULL;
3700 0 : int query_len, id = -1;
3701 : PGconn *pgsql;
3702 0 : int result = 0, argc = ZEND_NUM_ARGS();
3703 :
3704 0 : if (argc == 1) {
3705 0 : if (zend_parse_parameters(argc TSRMLS_CC, "s", &query, &query_len) == FAILURE) {
3706 0 : return;
3707 : }
3708 0 : id = PGG(default_link);
3709 0 : CHECK_DEFAULT_LINK(id);
3710 : } else {
3711 0 : if (zend_parse_parameters(argc TSRMLS_CC, "rs", &pgsql_link, &query, &query_len) == FAILURE) {
3712 0 : return;
3713 : }
3714 : }
3715 :
3716 0 : if (pgsql_link == NULL && id == -1) {
3717 0 : RETURN_FALSE;
3718 : }
3719 :
3720 0 : ZEND_FETCH_RESOURCE2(pgsql, PGconn *, &pgsql_link, id, "PostgreSQL link", le_link, le_plink);
3721 :
3722 0 : result = PQputline(pgsql, query);
3723 0 : if (result==EOF) {
3724 0 : PHP_PQ_ERROR("Query failed: %s", pgsql);
3725 0 : RETURN_FALSE;
3726 : }
3727 0 : RETURN_TRUE;
3728 : }
3729 : /* }}} */
3730 :
3731 : /* {{{ proto array pg_copy_to(resource connection, string table_name [, string delimiter [, string null_as]])
3732 : Copy table to array */
3733 : PHP_FUNCTION(pg_copy_to)
3734 1 : {
3735 : zval *pgsql_link;
3736 1 : char *table_name, *pg_delim = NULL, *pg_null_as = NULL;
3737 : int table_name_len, pg_delim_len, pg_null_as_len;
3738 : char *query;
3739 1 : int id = -1;
3740 : PGconn *pgsql;
3741 : PGresult *pgsql_result;
3742 : ExecStatusType status;
3743 1 : int copydone = 0;
3744 : #if !HAVE_PQGETCOPYDATA
3745 : char copybuf[COPYBUFSIZ];
3746 : #endif
3747 1 : char *csv = (char *)NULL;
3748 : int ret;
3749 1 : int argc = ZEND_NUM_ARGS();
3750 :
3751 1 : if (zend_parse_parameters(argc TSRMLS_CC, "rs|ss",
3752 : &pgsql_link, &table_name, &table_name_len,
3753 : &pg_delim, &pg_delim_len, &pg_null_as, &pg_null_as_len) == FAILURE) {
3754 0 : return;
3755 : }
3756 1 : if (!pg_delim) {
3757 1 : pg_delim = "\t";
3758 : }
3759 :
3760 1 : ZEND_FETCH_RESOURCE2(pgsql, PGconn *, &pgsql_link, id, "PostgreSQL link", le_link, le_plink);
3761 :
3762 1 : if (!pg_null_as) {
3763 1 : pg_null_as = safe_estrdup("\\\\N");
3764 : }
3765 :
3766 1 : spprintf(&query, 0, "COPY \"%s\" TO STDOUT DELIMITERS '%c' WITH NULL AS '%s'", table_name, *pg_delim, pg_null_as);
3767 :
3768 2 : while ((pgsql_result = PQgetResult(pgsql))) {
3769 0 : PQclear(pgsql_result);
3770 : }
3771 1 : pgsql_result = PQexec(pgsql, query);
3772 1 : efree(pg_null_as);
3773 1 : efree(query);
3774 :
3775 1 : if (pgsql_result) {
3776 1 : status = PQresultStatus(pgsql_result);
3777 : } else {
3778 0 : status = (ExecStatusType) PQstatus(pgsql);
3779 : }
3780 :
3781 1 : switch (status) {
3782 : case PGRES_COPY_OUT:
3783 1 : if (pgsql_result) {
3784 1 : PQclear(pgsql_result);
3785 1 : array_init(return_value);
3786 : #if HAVE_PQGETCOPYDATA
3787 1005 : while (!copydone)
3788 : {
3789 1003 : ret = PQgetCopyData(pgsql, &csv, 0);
3790 1003 : switch (ret) {
3791 : case -1:
3792 1 : copydone = 1;
3793 1 : break;
3794 : case 0:
3795 : case -2:
3796 0 : PHP_PQ_ERROR("getline failed: %s", pgsql);
3797 0 : RETURN_FALSE;
3798 : break;
3799 : default:
3800 1002 : add_next_index_string(return_value, csv, 1);
3801 1002 : PQfreemem(csv);
3802 : break;
3803 : }
3804 : }
3805 : #else
3806 : while (!copydone)
3807 : {
3808 : if ((ret = PQgetline(pgsql, copybuf, COPYBUFSIZ))) {
3809 : PHP_PQ_ERROR("getline failed: %s", pgsql);
3810 : RETURN_FALSE;
3811 : }
3812 :
3813 : if (copybuf[0] == '\\' &&
3814 : copybuf[1] == '.' &&
3815 : copybuf[2] == '\0')
3816 : {
3817 : copydone = 1;
3818 : }
3819 : else
3820 : {
3821 : if (csv == (char *)NULL) {
3822 : csv = estrdup(copybuf);
3823 : } else {
3824 : csv = (char *)erealloc(csv, strlen(csv) + sizeof(char)*(COPYBUFSIZ+1));
3825 : strcat(csv, copybuf);
3826 : }
3827 :
3828 : switch (ret)
3829 : {
3830 : case EOF:
3831 : copydone = 1;
3832 : case 0:
3833 : add_next_index_string(return_value, csv, 1);
3834 : efree(csv);
3835 : csv = (char *)NULL;
3836 : break;
3837 : case 1:
3838 : break;
3839 : }
3840 : }
3841 : }
3842 : if (PQendcopy(pgsql)) {
3843 : PHP_PQ_ERROR("endcopy failed: %s", pgsql);
3844 : RETURN_FALSE;
3845 : }
3846 : #endif
3847 3 : while ((pgsql_result = PQgetResult(pgsql))) {
3848 1 : PQclear(pgsql_result);
3849 : }
3850 : } else {
3851 0 : PQclear(pgsql_result);
3852 0 : RETURN_FALSE;
3853 : }
3854 1 : break;
3855 : default:
3856 0 : PQclear(pgsql_result);
3857 0 : PHP_PQ_ERROR("Copy command failed: %s", pgsql);
3858 0 : RETURN_FALSE;
3859 : break;
3860 : }
3861 : }
3862 : /* }}} */
3863 :
3864 : /* {{{ proto bool pg_copy_from(resource connection, string table_name , array rows [, string delimiter [, string null_as]])
3865 : Copy table from array */
3866 : PHP_FUNCTION(pg_copy_from)
3867 1 : {
3868 1 : zval *pgsql_link = NULL, *pg_rows;
3869 : zval **tmp;
3870 1 : char *table_name, *pg_delim = NULL, *pg_null_as = NULL;
3871 : int table_name_len, pg_delim_len, pg_null_as_len;
3872 1 : int pg_null_as_free = 0;
3873 : char *query;
3874 : HashPosition pos;
3875 1 : int id = -1;
3876 : PGconn *pgsql;
3877 : PGresult *pgsql_result;
3878 : ExecStatusType status;
3879 1 : int argc = ZEND_NUM_ARGS();
3880 :
3881 1 : if (zend_parse_parameters(argc TSRMLS_CC, "rsa|ss",
3882 : &pgsql_link, &table_name, &table_name_len, &pg_rows,
3883 : &pg_delim, &pg_delim_len, &pg_null_as, &pg_null_as_len) == FAILURE) {
3884 0 : return;
3885 : }
3886 1 : if (!pg_delim) {
3887 1 : pg_delim = "\t";
3888 : }
3889 1 : if (!pg_null_as) {
3890 1 : pg_null_as = safe_estrdup("\\\\N");
3891 1 : pg_null_as_free = 1;
3892 : }
3893 :
3894 1 : ZEND_FETCH_RESOURCE2(pgsql, PGconn *, &pgsql_link, id, "PostgreSQL link", le_link, le_plink);
3895 :
3896 1 : spprintf(&query, 0, "COPY \"%s\" FROM STDIN DELIMITERS E'%c' WITH NULL AS E'%s'", table_name, *pg_delim, pg_null_as);
3897 2 : while ((pgsql_result = PQgetResult(pgsql))) {
3898 0 : PQclear(pgsql_result);
3899 : }
3900 1 : pgsql_result = PQexec(pgsql, query);
3901 :
3902 1 : if (pg_null_as_free) {
3903 1 : efree(pg_null_as);
3904 : }
3905 1 : efree(query);
3906 :
3907 1 : if (pgsql_result) {
3908 1 : status = PQresultStatus(pgsql_result);
3909 : } else {
3910 0 : status = (ExecStatusType) PQstatus(pgsql);
3911 : }
3912 :
3913 1 : switch (status) {
3914 : case PGRES_COPY_IN:
3915 1 : if (pgsql_result) {
3916 1 : int command_failed = 0;
3917 1 : PQclear(pgsql_result);
3918 1 : zend_hash_internal_pointer_reset_ex(Z_ARRVAL_P(pg_rows), &pos);
3919 : #if HAVE_PQPUTCOPYDATA
3920 1004 : while (zend_hash_get_current_data_ex(Z_ARRVAL_P(pg_rows), (void **) &tmp, &pos) == SUCCESS) {
3921 1002 : convert_to_string_ex(tmp);
3922 1002 : query = (char *)emalloc(Z_STRLEN_PP(tmp) + 2);
3923 1002 : strlcpy(query, Z_STRVAL_PP(tmp), Z_STRLEN_PP(tmp) + 2);
3924 1002 : if(Z_STRLEN_PP(tmp) > 0 && *(query + Z_STRLEN_PP(tmp) - 1) != '\n') {
3925 0 : strlcat(query, "\n", Z_STRLEN_PP(tmp) + 2);
3926 : }
3927 1002 : if (PQputCopyData(pgsql, query, strlen(query)) != 1) {
3928 0 : efree(query);
3929 0 : PHP_PQ_ERROR("copy failed: %s", pgsql);
3930 0 : RETURN_FALSE;
3931 : }
3932 1002 : efree(query);
3933 1002 : zend_hash_move_forward_ex(Z_ARRVAL_P(pg_rows), &pos);
3934 : }
3935 1 : if (PQputCopyEnd(pgsql, NULL) != 1) {
3936 0 : PHP_PQ_ERROR("putcopyend failed: %s", pgsql);
3937 0 : RETURN_FALSE;
3938 : }
3939 : #else
3940 : while (zend_hash_get_current_data_ex(Z_ARRVAL_P(pg_rows), (void **) &tmp, &pos) == SUCCESS) {
3941 : convert_to_string_ex(tmp);
3942 : query = (char *)emalloc(Z_STRLEN_PP(tmp) + 2);
3943 : strlcpy(query, Z_STRVAL_PP(tmp), Z_STRLEN_PP(tmp) + 2);
3944 : if(Z_STRLEN_PP(tmp) > 0 && *(query + Z_STRLEN_PP(tmp) - 1) != '\n') {
3945 : strlcat(query, "\n", Z_STRLEN_PP(tmp) + 2);
3946 : }
3947 : if (PQputline(pgsql, query)==EOF) {
3948 : efree(query);
3949 : PHP_PQ_ERROR("copy failed: %s", pgsql);
3950 : RETURN_FALSE;
3951 : }
3952 : efree(query);
3953 : zend_hash_move_forward_ex(Z_ARRVAL_P(pg_rows), &pos);
3954 : }
3955 : if (PQputline(pgsql, "\\.\n") == EOF) {
3956 : PHP_PQ_ERROR("putline failed: %s", pgsql);
3957 : RETURN_FALSE;
3958 : }
3959 : if (PQendcopy(pgsql)) {
3960 : PHP_PQ_ERROR("endcopy failed: %s", pgsql);
3961 : RETURN_FALSE;
3962 : }
3963 : #endif
3964 3 : while ((pgsql_result = PQgetResult(pgsql))) {
3965 1 : if (PGRES_COMMAND_OK != PQresultStatus(pgsql_result)) {
3966 0 : PHP_PQ_ERROR("Copy command failed: %s", pgsql);
3967 0 : command_failed = 1;
3968 : }
3969 1 : PQclear(pgsql_result);
3970 : }
3971 1 : if (command_failed) {
3972 0 : RETURN_FALSE;
3973 : }
3974 : } else {
3975 0 : PQclear(pgsql_result);
3976 0 : RETURN_FALSE;
3977 : }
3978 1 : RETURN_TRUE;
3979 : break;
3980 : default:
3981 0 : PQclear(pgsql_result);
3982 0 : PHP_PQ_ERROR("Copy command failed: %s", pgsql);
3983 0 : RETURN_FALSE;
3984 : break;
3985 : }
3986 : }
3987 : /* }}} */
3988 :
3989 : #ifdef HAVE_PQESCAPE
3990 : /* {{{ proto string pg_escape_string([resource connection,] string data)
3991 : Escape string for text/char type */
3992 : PHP_FUNCTION(pg_escape_string)
3993 1 : {
3994 1 : char *from = NULL, *to = NULL;
3995 : zval *pgsql_link;
3996 : #ifdef HAVE_PQESCAPE_CONN
3997 : PGconn *pgsql;
3998 : #endif
3999 : int to_len;
4000 : int from_len;
4001 1 : int id = -1;
4002 :
4003 1 : switch (ZEND_NUM_ARGS()) {
4004 : case 1:
4005 1 : if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &from, &from_len) == FAILURE) {
4006 0 : return;
4007 : }
4008 1 : pgsql_link = NULL;
4009 1 : id = PGG(default_link);
4010 1 : break;
4011 :
4012 : default:
4013 0 : if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rs", &pgsql_link, &from, &from_len) == FAILURE) {
4014 0 : return;
4015 : }
4016 : break;
4017 : }
4018 :
4019 1 : to = (char *) safe_emalloc(from_len, 2, 1);
4020 :
4021 : #ifdef HAVE_PQESCAPE_CONN
4022 1 : if (pgsql_link != NULL || id != -1) {
4023 0 : ZEND_FETCH_RESOURCE2(pgsql, PGconn *, &pgsql_link, id, "PostgreSQL link", le_link, le_plink);
4024 0 : to_len = (int) PQescapeStringConn(pgsql, to, from, (size_t)from_len, NULL);
4025 : } else
4026 : #endif
4027 1 : to_len = (int) PQescapeString(to, from, (size_t)from_len);
4028 :
4029 1 : RETURN_STRINGL(to, to_len, 0);
4030 : }
4031 : /* }}} */
4032 :
4033 : /* {{{ proto string pg_escape_bytea([resource connection,] string data)
4034 : Escape binary for bytea type */
4035 : PHP_FUNCTION(pg_escape_bytea)
4036 3 : {
4037 3 : char *from = NULL, *to = NULL;
4038 : size_t to_len;
4039 3 : int from_len, id = -1;
4040 : #ifdef HAVE_PQESCAPE_BYTEA_CONN
4041 : PGconn *pgsql;
4042 : #endif
4043 : zval *pgsql_link;
4044 :
4045 3 : switch (ZEND_NUM_ARGS()) {
4046 : case 1:
4047 3 : if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &from, &from_len) == FAILURE) {
4048 0 : return;
4049 : }
4050 3 : pgsql_link = NULL;
4051 3 : id = PGG(default_link);
4052 3 : break;
4053 :
4054 : default:
4055 0 : if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rs", &pgsql_link, &from, &from_len) == FAILURE) {
4056 0 : return;
4057 : }
4058 : break;
4059 : }
4060 :
4061 : #ifdef HAVE_PQESCAPE_BYTEA_CONN
4062 4 : if (pgsql_link != NULL || id != -1) {
4063 1 : ZEND_FETCH_RESOURCE2(pgsql, PGconn *, &pgsql_link, id, "PostgreSQL link", le_link, le_plink);
4064 1 : to = (char *)PQescapeByteaConn(pgsql, from, (size_t)from_len, &to_len);
4065 : } else
4066 : #endif
4067 2 : to = (char *)PQescapeBytea((unsigned char*)from, from_len, &to_len);
4068 :
4069 3 : RETVAL_STRINGL(to, to_len-1, 1); /* to_len includes addtional '\0' */
4070 3 : PQfreemem(to);
4071 : }
4072 : /* }}} */
4073 :
4074 : #if !HAVE_PQUNESCAPEBYTEA
4075 : /* PQunescapeBytea() from PostgreSQL 7.3 to provide bytea unescape feature to 7.2 users.
4076 : Renamed to php_pgsql_unescape_bytea() */
4077 : /*
4078 : * PQunescapeBytea - converts the null terminated string representation
4079 : * of a bytea, strtext, into binary, filling a buffer. It returns a
4080 : * pointer to the buffer which is NULL on error, and the size of the
4081 : * buffer in retbuflen. The pointer may subsequently be used as an
4082 : * argument to the function free(3). It is the reverse of PQescapeBytea.
4083 : *
4084 : * The following transformations are reversed:
4085 : * '\0' == ASCII 0 == \000
4086 : * '\'' == ASCII 39 == \'
4087 : * '\\' == ASCII 92 == \\
4088 : *
4089 : * States:
4090 : * 0 normal 0->1->2->3->4
4091 : * 1 \ 1->5
4092 : * 2 \0 1->6
4093 : * 3 \00
4094 : * 4 \000
4095 : * 5 \'
4096 : * 6 \\
4097 : */
4098 : static unsigned char * php_pgsql_unescape_bytea(unsigned char *strtext, size_t *retbuflen)
4099 : {
4100 : size_t buflen;
4101 : unsigned char *buffer,
4102 : *sp,
4103 : *bp;
4104 : unsigned int state = 0;
4105 :
4106 : if (strtext == NULL)
4107 : return NULL;
4108 : buflen = strlen(strtext); /* will shrink, also we discover if
4109 : * strtext */
4110 : buffer = (unsigned char *) emalloc(buflen); /* isn't NULL terminated */
4111 : for (bp = buffer, sp = strtext; *sp != '\0'; bp++, sp++)
4112 : {
4113 : switch (state)
4114 : {
4115 : case 0:
4116 : if (*sp == '\\')
4117 : state = 1;
4118 : *bp = *sp;
4119 : break;
4120 : case 1:
4121 : if (*sp == '\'') /* state=5 */
4122 : { /* replace \' with 39 */
4123 : bp--;
4124 : *bp = '\'';
4125 : buflen--;
4126 : state = 0;
4127 : }
4128 : else if (*sp == '\\') /* state=6 */
4129 : { /* replace \\ with 92 */
4130 : bp--;
4131 : *bp = '\\';
4132 : buflen--;
4133 : state = 0;
4134 : }
4135 : else
4136 : {
4137 : if (isdigit(*sp))
4138 : state = 2;
4139 : else
4140 : state = 0;
4141 : *bp = *sp;
4142 : }
4143 : break;
4144 : case 2:
4145 : if (isdigit(*sp))
4146 : state = 3;
4147 : else
4148 : state = 0;
4149 : *bp = *sp;
4150 : break;
4151 : case 3:
4152 : if (isdigit(*sp)) /* state=4 */
4153 : {
4154 : unsigned char *start, *end, buf[4]; /* 000 + '\0' */
4155 :
4156 : bp -= 3;
4157 : memcpy(buf, sp-2, 3);
4158 : buf[3] = '\0';
4159 : start = buf;
4160 : *bp = (unsigned char)strtoul(start, (char **)&end, 8);
4161 : buflen -= 3;
4162 : state = 0;
4163 : }
4164 : else
4165 : {
4166 : *bp = *sp;
4167 : state = 0;
4168 : }
4169 : break;
4170 : }
4171 : }
4172 : buffer = erealloc(buffer, buflen+1);
4173 : buffer[buflen] = '\0';
4174 :
4175 : *retbuflen = buflen;
4176 : return buffer;
4177 : }
4178 : #endif
4179 :
4180 : /* {{{ proto string pg_unescape_bytea(string data)
4181 : Unescape binary for bytea type */
4182 : PHP_FUNCTION(pg_unescape_bytea)
4183 3 : {
4184 3 : char *from = NULL, *to = NULL, *tmp = NULL;
4185 : size_t to_len;
4186 : int from_len;
4187 3 : if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s",
4188 : &from, &from_len) == FAILURE) {
4189 0 : return;
4190 : }
4191 :
4192 : #if HAVE_PQUNESCAPEBYTEA
4193 3 : tmp = (char *)PQunescapeBytea((unsigned char*)from, &to_len);
4194 3 : to = estrndup(tmp, to_len);
4195 3 : PQfreemem(tmp);
4196 : #else
4197 : to = (char *)php_pgsql_unescape_bytea((unsigned char*)from, &to_len);
4198 : #endif
4199 3 : if (!to) {
4200 0 : RETURN_FALSE;
4201 : }
4202 3 : RETVAL_STRINGL(to, to_len, 0);
4203 : }
4204 : /* }}} */
4205 : #endif
4206 :
4207 : /* {{{ proto string pg_result_error(resource result)
4208 : Get error message associated with result */
4209 : PHP_FUNCTION(pg_result_error)
4210 1 : {
4211 : zval *result;
4212 : PGresult *pgsql_result;
4213 : pgsql_result_handle *pg_result;
4214 1 : char *err = NULL;
4215 :
4216 1 : if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS() TSRMLS_CC, "r",
4217 : &result) == FAILURE) {
4218 0 : RETURN_FALSE;
4219 : }
4220 :
4221 1 : ZEND_FETCH_RESOURCE(pg_result, pgsql_result_handle *, &result, -1, "PostgreSQL result", le_result);
4222 :
4223 1 : pgsql_result = pg_result->result;
4224 1 : if (!pgsql_result) {
4225 0 : RETURN_FALSE;
4226 : }
4227 1 : err = (char *)PQresultErrorMessage(pgsql_result);
4228 1 : RETURN_STRING(err,1);
4229 : }
4230 : /* }}} */
4231 :
4232 : #if HAVE_PQRESULTERRORFIELD
4233 : /* {{{ proto string pg_result_error_field(resource result, int fieldcode)
4234 : Get error message field associated with result */
4235 : PHP_FUNCTION(pg_result_error_field)
4236 12 : {
4237 : zval *result;
4238 : long fieldcode;
4239 : PGresult *pgsql_result;
4240 : pgsql_result_handle *pg_result;
4241 12 : char *field = NULL;
4242 :
4243 12 : if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS() TSRMLS_CC, "rl",
4244 : &result, &fieldcode) == FAILURE) {
4245 0 : RETURN_FALSE;
4246 : }
4247 :
4248 12 : ZEND_FETCH_RESOURCE(pg_result, pgsql_result_handle *, &result, -1, "PostgreSQL result", le_result);
4249 :
4250 12 : pgsql_result = pg_result->result;
4251 12 : if (!pgsql_result) {
4252 0 : RETURN_FALSE;
4253 : }
4254 12 : if (fieldcode & (PG_DIAG_SEVERITY|PG_DIAG_SQLSTATE|PG_DIAG_MESSAGE_PRIMARY|PG_DIAG_MESSAGE_DETAIL
4255 : |PG_DIAG_MESSAGE_HINT|PG_DIAG_STATEMENT_POSITION
4256 : #if PG_DIAG_INTERNAL_POSITION
4257 : |PG_DIAG_INTERNAL_POSITION
4258 : #endif
4259 : #if PG_DIAG_INTERNAL_QUERY
4260 : |PG_DIAG_INTERNAL_QUERY
4261 : #endif
4262 : |PG_DIAG_CONTEXT|PG_DIAG_SOURCE_FILE|PG_DIAG_SOURCE_LINE
4263 : |PG_DIAG_SOURCE_FUNCTION)) {
4264 12 : field = (char *)PQresultErrorField(pgsql_result, fieldcode);
4265 12 : if (field == NULL) {
4266 12 : RETURN_NULL();
4267 : } else {
4268 0 : RETURN_STRING(field, 1);
4269 : }
4270 : } else {
4271 0 : RETURN_FALSE;
4272 : }
4273 : }
4274 : /* }}} */
4275 : #endif
4276 :
4277 : /* {{{ proto int pg_connection_status(resource connnection)
4278 : Get connection status */
4279 : PHP_FUNCTION(pg_connection_status)
4280 2 : {
4281 2 : zval *pgsql_link = NULL;
4282 2 : int id = -1;
4283 : PGconn *pgsql;
4284 :
4285 2 : if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS() TSRMLS_CC, "r",
4286 : &pgsql_link) == FAILURE) {
4287 0 : RETURN_FALSE;
4288 : }
4289 :
4290 2 : ZEND_FETCH_RESOURCE2(pgsql, PGconn *, &pgsql_link, id, "PostgreSQL link", le_link, le_plink);
4291 :
4292 2 : RETURN_LONG(PQstatus(pgsql));
4293 : }
4294 :
4295 : /* }}} */
4296 :
4297 : #if HAVE_PGTRANSACTIONSTATUS
4298 : /* {{{ proto int pg_transaction_status(resource connnection)
4299 : Get transaction status */
4300 : PHP_FUNCTION(pg_transaction_status)
4301 1 : {
4302 1 : zval *pgsql_link = NULL;
4303 1 : int id = -1;
4304 : PGconn *pgsql;
4305 :
4306 1 : if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS() TSRMLS_CC, "r",
4307 : &pgsql_link) == FAILURE) {
4308 0 : RETURN_FALSE;
4309 : }
4310 :
4311 1 : ZEND_FETCH_RESOURCE2(pgsql, PGconn *, &pgsql_link, id, "PostgreSQL link", le_link, le_plink);
4312 :
4313 1 : RETURN_LONG(PQtransactionStatus(pgsql));
4314 : }
4315 : #endif
4316 :
4317 : /* }}} */
4318 :
4319 : /* {{{ proto bool pg_connection_reset(resource connection)
4320 : Reset connection (reconnect) */
4321 : PHP_FUNCTION(pg_connection_reset)
4322 1 : {
4323 : zval *pgsql_link;
4324 1 : int id = -1;
4325 : PGconn *pgsql;
4326 :
4327 1 : if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS() TSRMLS_CC, "r",
4328 : &pgsql_link) == FAILURE) {
4329 0 : RETURN_FALSE;
4330 : }
4331 :
4332 1 : ZEND_FETCH_RESOURCE2(pgsql, PGconn *, &pgsql_link, id, "PostgreSQL link", le_link, le_plink);
4333 :
4334 1 : PQreset(pgsql);
4335 1 : if (PQstatus(pgsql) == CONNECTION_BAD) {
4336 0 : RETURN_FALSE;
4337 : }
4338 1 : RETURN_TRUE;
4339 : }
4340 :
4341 : /* }}} */
4342 :
4343 : #define PHP_PG_ASYNC_IS_BUSY 1
4344 : #define PHP_PG_ASYNC_REQUEST_CANCEL 2
4345 :
4346 : /* {{{ php_pgsql_flush_query
4347 : */
4348 : static int php_pgsql_flush_query(PGconn *pgsql TSRMLS_DC)
4349 39 : {
4350 : PGresult *res;
4351 39 : int leftover = 0;
4352 :
4353 39 : if (PQ_SETNONBLOCKING(pgsql, 1)) {
4354 0 : php_error_docref(NULL TSRMLS_CC, E_NOTICE,"Cannot set connection to nonblocking mode");
4355 0 : return -1;
4356 : }
4357 78 : while ((res = PQgetResult(pgsql))) {
4358 0 : PQclear(res);
4359 0 : leftover++;
4360 : }
4361 39 : PQ_SETNONBLOCKING(pgsql, 0);
4362 39 : return leftover;
4363 : }
4364 : /* }}} */
4365 :
4366 : /* {{{ php_pgsql_do_async
4367 : */
4368 : static void php_pgsql_do_async(INTERNAL_FUNCTION_PARAMETERS, int entry_type)
4369 3 : {
4370 : zval *pgsql_link;
4371 3 : int id = -1;
4372 : PGconn *pgsql;
4373 : PGresult *pgsql_result;
4374 :
4375 3 : if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS() TSRMLS_CC, "r",
4376 : &pgsql_link) == FAILURE) {
4377 0 : RETURN_FALSE;
4378 : }
4379 :
4380 3 : ZEND_FETCH_RESOURCE2(pgsql, PGconn *, &pgsql_link, id, "PostgreSQL link", le_link, le_plink);
4381 :
4382 3 : if (PQ_SETNONBLOCKING(pgsql, 1)) {
4383 0 : php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Cannot set connection to nonblocking mode");
4384 0 : RETURN_FALSE;
4385 : }
4386 3 : switch(entry_type) {
4387 : case PHP_PG_ASYNC_IS_BUSY:
4388 3 : PQconsumeInput(pgsql);
4389 3 : Z_LVAL_P(return_value) = PQisBusy(pgsql);
4390 3 : Z_TYPE_P(return_value) = IS_LONG;
4391 3 : break;
4392 : case PHP_PG_ASYNC_REQUEST_CANCEL:
4393 0 : Z_LVAL_P(return_value) = PQrequestCancel(pgsql);
4394 0 : Z_TYPE_P(return_value) = IS_LONG;
4395 0 : while ((pgsql_result = PQgetResult(pgsql))) {
4396 0 : PQclear(pgsql_result);
4397 : }
4398 0 : break;
4399 : default:
4400 0 : php_error_docref(NULL TSRMLS_CC, E_ERROR, "PostgreSQL module error, please report this error");
4401 : break;
4402 : }
4403 3 : if (PQ_SETNONBLOCKING(pgsql, 0)) {
4404 0 : php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Cannot set connection to blocking mode");
4405 : }
4406 3 : convert_to_boolean_ex(&return_value);
4407 : }
4408 : /* }}} */
4409 :
4410 : /* {{{ proto bool pg_cancel_query(resource connection)
4411 : Cancel request */
4412 : PHP_FUNCTION(pg_cancel_query)
4413 0 : {
4414 0 : php_pgsql_do_async(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_PG_ASYNC_REQUEST_CANCEL);
4415 0 : }
4416 : /* }}} */
4417 :
4418 : /* {{{ proto bool pg_connection_busy(resource connection)
4419 : Get connection is busy or not */
4420 : PHP_FUNCTION(pg_connection_busy)
4421 3 : {
4422 3 : php_pgsql_do_async(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_PG_ASYNC_IS_BUSY);
4423 3 : }
4424 : /* }}} */
4425 :
4426 : /* {{{ proto bool pg_send_query(resource connection, string query)
4427 : Send asynchronous query */
4428 : PHP_FUNCTION(pg_send_query)
4429 2 : {
4430 : zval *pgsql_link;
4431 : char *query;
4432 : int len;
4433 2 : int id = -1;
4434 : PGconn *pgsql;
4435 : PGresult *res;
4436 2 : int leftover = 0;
4437 :
4438 2 : if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rs",
4439 : &pgsql_link, &query, &len) == FAILURE) {
4440 0 : return;
4441 : }
4442 :
4443 2 : ZEND_FETCH_RESOURCE2(pgsql, PGconn *, &pgsql_link, id, "PostgreSQL link", le_link, le_plink);
4444 :
4445 2 : if (PQ_SETNONBLOCKING(pgsql, 1)) {
4446 0 : php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Cannot set connection to nonblocking mode");
4447 0 : RETURN_FALSE;
4448 : }
4449 4 : while ((res = PQgetResult(pgsql))) {
4450 0 : PQclear(res);
4451 0 : leftover = 1;
4452 : }
4453 2 : if (leftover) {
4454 0 : php_error_docref(NULL TSRMLS_CC, E_NOTICE, "There are results on this connection. Call pg_get_result() until it returns FALSE");
4455 : }
4456 2 : if (!PQsendQuery(pgsql, query)) {
4457 0 : if ((PGG(auto_reset_persistent) & 2) && PQstatus(pgsql) != CONNECTION_OK) {
4458 0 : PQreset(pgsql);
4459 : }
4460 0 : if (!PQsendQuery(pgsql, query)) {
4461 0 : RETURN_FALSE;
4462 : }
4463 : }
4464 2 : if (PQ_SETNONBLOCKING(pgsql, 0)) {
4465 0 : php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Cannot set connection to blocking mode");
4466 : }
4467 2 : RETURN_TRUE;
4468 : }
4469 : /* }}} */
4470 :
4471 : #if HAVE_PQSENDQUERYPARAMS
4472 : /* {{{ proto bool pg_send_query_params(resource connection, string query, array params)
4473 : Send asynchronous parameterized query */
4474 : PHP_FUNCTION(pg_send_query_params)
4475 0 : {
4476 : zval *pgsql_link, *pv_param_arr, **tmp;
4477 0 : int num_params = 0;
4478 0 : char **params = NULL;
4479 : char *query;
4480 0 : int query_len, id = -1;
4481 : PGconn *pgsql;
4482 : PGresult *res;
4483 0 : int leftover = 0;
4484 :
4485 0 : if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rsa/", &pgsql_link, &query, &query_len, &pv_param_arr) == FAILURE) {
4486 0 : return;
4487 : }
4488 :
4489 0 : if (pgsql_link == NULL && id == -1) {
4490 0 : RETURN_FALSE;
4491 : }
4492 :
4493 0 : ZEND_FETCH_RESOURCE2(pgsql, PGconn *, &pgsql_link, id, "PostgreSQL link", le_link, le_plink);
4494 :
4495 0 : if (PQ_SETNONBLOCKING(pgsql, 1)) {
4496 0 : php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Cannot set connection to nonblocking mode");
4497 0 : RETURN_FALSE;
4498 : }
4499 0 : while ((res = PQgetResult(pgsql))) {
4500 0 : PQclear(res);
4501 0 : leftover = 1;
4502 : }
4503 0 : if (leftover) {
4504 0 : php_error_docref(NULL TSRMLS_CC, E_NOTICE, "There are results on this connection. Call pg_get_result() until it returns FALSE");
4505 : }
4506 :
4507 0 : zend_hash_internal_pointer_reset(Z_ARRVAL_P(pv_param_arr));
4508 0 : num_params = zend_hash_num_elements(Z_ARRVAL_P(pv_param_arr));
4509 0 : if (num_params > 0) {
4510 0 : int i = 0;
4511 0 : params = (char **)safe_emalloc(sizeof(char *), num_params, 0);
4512 :
4513 0 : for(i = 0; i < num_params; i++) {
4514 0 : if (zend_hash_get_current_data(Z_ARRVAL_P(pv_param_arr), (void **) &tmp) == FAILURE) {
4515 0 : php_error_docref(NULL TSRMLS_CC, E_WARNING,"Error getting parameter");
4516 0 : _php_pgsql_free_params(params, num_params);
4517 0 : RETURN_FALSE;
4518 : }
4519 :
4520 0 : if (Z_TYPE_PP(tmp) == IS_NULL) {
4521 0 : params[i] = NULL;
4522 : } else {
4523 0 : zval tmp_val = **tmp;
4524 0 : zval_copy_ctor(&tmp_val);
4525 0 : convert_to_string(&tmp_val);
4526 0 : if (Z_TYPE(tmp_val) != IS_STRING) {
4527 0 : php_error_docref(NULL TSRMLS_CC, E_WARNING,"Error converting parameter");
4528 0 : zval_dtor(&tmp_val);
4529 0 : _php_pgsql_free_params(params, num_params);
4530 0 : RETURN_FALSE;
4531 : }
4532 0 : params[i] = estrndup(Z_STRVAL(tmp_val), Z_STRLEN(tmp_val));
4533 0 : zval_dtor(&tmp_val);
4534 : }
4535 :
4536 0 : zend_hash_move_forward(Z_ARRVAL_P(pv_param_arr));
4537 : }
4538 : }
4539 :
4540 0 : if (!PQsendQueryParams(pgsql, query, num_params, NULL, (const char * const *)params, NULL, NULL, 0)) {
4541 0 : if ((PGG(auto_reset_persistent) & 2) && PQstatus(pgsql) != CONNECTION_OK) {
4542 0 : PQreset(pgsql);
4543 : }
4544 0 : if (!PQsendQueryParams(pgsql, query, num_params, NULL, (const char * const *)params, NULL, NULL, 0)) {
4545 0 : _php_pgsql_free_params(params, num_params);
4546 0 : RETURN_FALSE;
4547 : }
4548 : }
4549 0 : _php_pgsql_free_params(params, num_params);
4550 0 : if (PQ_SETNONBLOCKING(pgsql, 0)) {
4551 0 : php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Cannot set connection to blocking mode");
4552 : }
4553 0 : RETURN_TRUE;
4554 : }
4555 : /* }}} */
4556 : #endif
4557 :
4558 : #if HAVE_PQSENDPREPARE
4559 : /* {{{ proto bool pg_send_prepare(resource connection, string stmtname, string query)
4560 : Asynchronously prepare a query for future execution */
4561 : PHP_FUNCTION(pg_send_prepare)
4562 0 : {
4563 : zval *pgsql_link;
4564 : char *query, *stmtname;
4565 0 : int stmtname_len, query_len, id = -1;
4566 : PGconn *pgsql;
4567 : PGresult *res;
4568 0 : int leftover = 0;
4569 :
4570 0 : if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rss", &pgsql_link, &stmtname, &stmtname_len, &query, &query_len) == FAILURE) {
4571 0 : return;
4572 : }
4573 :
4574 0 : if (pgsql_link == NULL && id == -1) {
4575 0 : RETURN_FALSE;
4576 : }
4577 :
4578 0 : ZEND_FETCH_RESOURCE2(pgsql, PGconn *, &pgsql_link, id, "PostgreSQL link", le_link, le_plink);
4579 :
4580 0 : if (PQ_SETNONBLOCKING(pgsql, 1)) {
4581 0 : php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Cannot set connection to nonblocking mode");
4582 0 : RETURN_FALSE;
4583 : }
4584 0 : while ((res = PQgetResult(pgsql))) {
4585 0 : PQclear(res);
4586 0 : leftover = 1;
4587 : }
4588 0 : if (leftover) {
4589 0 : php_error_docref(NULL TSRMLS_CC, E_NOTICE, "There are results on this connection. Call pg_get_result() until it returns FALSE");
4590 : }
4591 0 : if (!PQsendPrepare(pgsql, stmtname, query, 0, NULL)) {
4592 0 : if ((PGG(auto_reset_persistent) & 2) && PQstatus(pgsql) != CONNECTION_OK) {
4593 0 : PQreset(pgsql);
4594 : }
4595 0 : if (!PQsendPrepare(pgsql, stmtname, query, 0, NULL)) {
4596 0 : RETURN_FALSE;
4597 : }
4598 : }
4599 0 : if (PQ_SETNONBLOCKING(pgsql, 0)) {
4600 0 : php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Cannot set connection to blocking mode");
4601 : }
4602 0 : RETURN_TRUE;
4603 : }
4604 : /* }}} */
4605 : #endif
4606 :
4607 : #if HAVE_PQSENDQUERYPREPARED
4608 : /* {{{ proto bool pg_send_execute(resource connection, string stmtname, array params)
4609 : Executes prevriously prepared stmtname asynchronously */
4610 : PHP_FUNCTION(pg_send_execute)
4611 0 : {
4612 : zval *pgsql_link;
4613 : zval *pv_param_arr, **tmp;
4614 0 : int num_params = 0;
4615 0 : char **params = NULL;
4616 : char *stmtname;
4617 0 : int stmtname_len, id = -1;
4618 : PGconn *pgsql;
4619 : PGresult *res;
4620 0 : int leftover = 0;
4621 :
4622 0 : if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rsa", &pgsql_link, &stmtname, &stmtname_len, &pv_param_arr) == FAILURE) {
4623 0 : return;
4624 : }
4625 :
4626 0 : if (pgsql_link == NULL && id == -1) {
4627 0 : RETURN_FALSE;
4628 : }
4629 :
4630 0 : ZEND_FETCH_RESOURCE2(pgsql, PGconn *, &pgsql_link, id, "PostgreSQL link", le_link, le_plink);
4631 :
4632 0 : if (PQ_SETNONBLOCKING(pgsql, 1)) {
4633 0 : php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Cannot set connection to nonblocking mode");
4634 0 : RETURN_FALSE;
4635 : }
4636 0 : while ((res = PQgetResult(pgsql))) {
4637 0 : PQclear(res);
4638 0 : leftover = 1;
4639 : }
4640 0 : if (leftover) {
4641 0 : php_error_docref(NULL TSRMLS_CC, E_NOTICE, "There are results on this connection. Call pg_get_result() until it returns FALSE");
4642 : }
4643 :
4644 0 : zend_hash_internal_pointer_reset(Z_ARRVAL_P(pv_param_arr));
4645 0 : num_params = zend_hash_num_elements(Z_ARRVAL_P(pv_param_arr));
4646 0 : if (num_params > 0) {
4647 0 : int i = 0;
4648 0 : params = (char **)safe_emalloc(sizeof(char *), num_params, 0);
4649 :
4650 0 : for(i = 0; i < num_params; i++) {
4651 0 : if (zend_hash_get_current_data(Z_ARRVAL_P(pv_param_arr), (void **) &tmp) == FAILURE) {
4652 0 : php_error_docref(NULL TSRMLS_CC, E_WARNING,"Error getting parameter");
4653 0 : _php_pgsql_free_params(params, num_params);
4654 0 : RETURN_FALSE;
4655 : }
4656 :
4657 0 : if (Z_TYPE_PP(tmp) == IS_NULL) {
4658 0 : params[i] = NULL;
4659 : } else {
4660 0 : zval tmp_val = **tmp;
4661 0 : zval_copy_ctor(&tmp_val);
4662 0 : convert_to_string(&tmp_val);
4663 0 : if (Z_TYPE(tmp_val) != IS_STRING) {
4664 0 : php_error_docref(NULL TSRMLS_CC, E_WARNING,"Error converting parameter");
4665 0 : zval_dtor(&tmp_val);
4666 0 : _php_pgsql_free_params(params, num_params);
4667 0 : RETURN_FALSE;
4668 : }
4669 0 : params[i] = estrndup(Z_STRVAL(tmp_val), Z_STRLEN(tmp_val));
4670 0 : zval_dtor(&tmp_val);
4671 : }
4672 :
4673 0 : zend_hash_move_forward(Z_ARRVAL_P(pv_param_arr));
4674 : }
4675 : }
4676 :
4677 0 : if (!PQsendQueryPrepared(pgsql, stmtname, num_params, (const char * const *)params, NULL, NULL, 0)) {
4678 0 : if ((PGG(auto_reset_persistent) & 2) && PQstatus(pgsql) != CONNECTION_OK) {
4679 0 : PQreset(pgsql);
4680 : }
4681 0 : if (!PQsendQueryPrepared(pgsql, stmtname, num_params, (const char * const *)params, NULL, NULL, 0)) {
4682 0 : _php_pgsql_free_params(params, num_params);
4683 0 : RETURN_FALSE;
4684 : }
4685 : }
4686 0 : _php_pgsql_free_params(params, num_params);
4687 0 : if (PQ_SETNONBLOCKING(pgsql, 0)) {
4688 0 : php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Cannot set connection to blocking mode");
4689 : }
4690 0 : RETURN_TRUE;
4691 : }
4692 : /* }}} */
4693 : #endif
4694 :
4695 : /* {{{ proto resource pg_get_result(resource connection)
4696 : Get asynchronous query result */
4697 : PHP_FUNCTION(pg_get_result)
4698 1 : {
4699 : zval *pgsql_link;
4700 1 : int id = -1;
4701 : PGconn *pgsql;
4702 : PGresult *pgsql_result;
4703 : pgsql_result_handle *pg_result;
4704 :
4705 1 : if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS() TSRMLS_CC, "r", &pgsql_link) == FAILURE) {
4706 0 : RETURN_FALSE;
4707 : }
4708 :
4709 1 : ZEND_FETCH_RESOURCE2(pgsql, PGconn *, &pgsql_link, id, "PostgreSQL link", le_link, le_plink);
4710 :
4711 1 : pgsql_result = PQgetResult(pgsql);
4712 1 : if (!pgsql_result) {
4713 : /* no result */
4714 0 : RETURN_FALSE;
4715 : }
4716 1 : pg_result = (pgsql_result_handle *) emalloc(sizeof(pgsql_result_handle));
4717 1 : pg_result->conn = pgsql;
4718 1 : pg_result->result = pgsql_result;
4719 1 : pg_result->row = 0;
4720 1 : ZEND_REGISTER_RESOURCE(return_value, pg_result, le_result);
4721 : }
4722 : /* }}} */
4723 :
4724 : /* {{{ proto mixed pg_result_status(resource result[, long result_type])
4725 : Get status of query result */
4726 : PHP_FUNCTION(pg_result_status)
4727 2 : {
4728 : zval *result;
4729 2 : long result_type = PGSQL_STATUS_LONG;
4730 : ExecStatusType status;
4731 : PGresult *pgsql_result;
4732 : pgsql_result_handle *pg_result;
4733 :
4734 2 : if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS() TSRMLS_CC, "r|l",
4735 : &result, &result_type) == FAILURE) {
4736 0 : RETURN_FALSE;
4737 : }
4738 :
4739 2 : ZEND_FETCH_RESOURCE(pg_result, pgsql_result_handle *, &result, -1, "PostgreSQL result", le_result);
4740 :
4741 2 : pgsql_result = pg_result->result;
4742 2 : if (result_type == PGSQL_STATUS_LONG) {
4743 1 : status = PQresultStatus(pgsql_result);
4744 1 : RETURN_LONG((int)status);
4745 : }
4746 1 : else if (result_type == PGSQL_STATUS_STRING) {
4747 1 : RETURN_STRING(PQcmdStatus(pgsql_result), 1);
4748 : }
4749 : else {
4750 0 : php_error_docref(NULL TSRMLS_CC, E_WARNING, "Optional 2nd parameter should be PGSQL_STATUS_LONG or PGSQL_STATUS_STRING");
4751 0 : RETURN_FALSE;
4752 : }
4753 : }
4754 : /* }}} */
4755 :
4756 :
4757 : /* {{{ proto array pg_get_notify([resource connection[, result_type]])
4758 : Get asynchronous notification */
4759 : PHP_FUNCTION(pg_get_notify)
4760 1 : {
4761 : zval *pgsql_link;
4762 1 : int id = -1;
4763 1 : long result_type = PGSQL_ASSOC;
4764 : PGconn *pgsql;
4765 : PGnotify *pgsql_notify;
4766 :
4767 1 : if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS() TSRMLS_CC, "r|l",
4768 : &pgsql_link, &result_type) == FAILURE) {
4769 0 : RETURN_FALSE;
4770 : }
4771 :
4772 1 : ZEND_FETCH_RESOURCE2(pgsql, PGconn *, &pgsql_link, id, "PostgreSQL link", le_link, le_plink);
4773 :
4774 1 : if (!(result_type & PGSQL_BOTH)) {
4775 0 : php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid result type");
4776 0 : RETURN_FALSE;
4777 : }
4778 :
4779 1 : PQconsumeInput(pgsql);
4780 1 : pgsql_notify = PQnotifies(pgsql);
4781 1 : if (!pgsql_notify) {
4782 : /* no notify message */
4783 0 : RETURN_FALSE;
4784 : }
4785 1 : array_init(return_value);
4786 1 : if (result_type & PGSQL_NUM) {
4787 0 : add_index_string(return_value, 0, pgsql_notify->relname, 1);
4788 0 : add_index_long(return_value, 1, pgsql_notify->be_pid);
4789 : }
4790 1 : if (result_type & PGSQL_ASSOC) {
4791 1 : add_assoc_string(return_value, "message", pgsql_notify->relname, 1);
4792 1 : add_assoc_long(return_value, "pid", pgsql_notify->be_pid);
4793 : }
4794 1 : PQfreemem(pgsql_notify);
4795 : }
4796 : /* }}} */
4797 :
4798 : /* {{{ proto int pg_get_pid([resource connection)
4799 : Get backend(server) pid */
4800 : PHP_FUNCTION(pg_get_pid)
4801 1 : {
4802 : zval *pgsql_link;
4803 1 : int id = -1;
4804 : PGconn *pgsql;
4805 :
4806 1 : if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS() TSRMLS_CC, "r",
4807 : &pgsql_link) == FAILURE) {
4808 0 : RETURN_FALSE;
4809 : }
4810 :
4811 1 : ZEND_FETCH_RESOURCE2(pgsql, PGconn *, &pgsql_link, id, "PostgreSQL link", le_link, le_plink);
4812 :
4813 1 : RETURN_LONG(PQbackendPID(pgsql));
4814 : }
4815 : /* }}} */
4816 :
4817 : /* {{{ php_pgsql_meta_data
4818 : * TODO: Add meta_data cache for better performance
4819 : */
4820 : PHP_PGSQL_API int php_pgsql_meta_data(PGconn *pg_link, const char *table_name, zval *meta TSRMLS_DC)
4821 41 : {
4822 : PGresult *pg_result;
4823 41 : char *src, *tmp_name, *tmp_name2 = NULL;
4824 41 : smart_str querystr = {0};
4825 : int new_len;
4826 : int i, num_rows;
4827 : zval *elem;
4828 :
4829 41 : if (!*table_name) {
4830 0 : php_error_docref(NULL TSRMLS_CC, E_WARNING, "The table name must be specified");
4831 0 : return FAILURE;
4832 : }
4833 :
4834 41 : src = estrdup(table_name);
4835 41 : tmp_name = php_strtok_r(src, ".", &tmp_name2);
4836 :
4837 41 : if (!tmp_name2 || !*tmp_name2) {
4838 : /* Default schema */
4839 24 : tmp_name2 = tmp_name;
4840 24 : tmp_name = "public";
4841 : }
4842 :
4843 41 : smart_str_appends(&querystr,
4844 : "SELECT a.attname, a.attnum, t.typname, a.attlen, a.attnotNULL, a.atthasdef, a.attndims "
4845 : "FROM pg_class as c, pg_attribute a, pg_type t, pg_namespace n "
4846 : "WHERE a.attnum > 0 AND a.attrelid = c.oid AND c.relname = '");
4847 41 : tmp_name2 = php_addslashes(tmp_name2, strlen(tmp_name2), &new_len, 0 TSRMLS_CC);
4848 41 : smart_str_appendl(&querystr, tmp_name2, new_len);
4849 :
4850 41 : smart_str_appends(&querystr, "' AND c.relnamespace = n.oid AND n.nspname = '");
4851 41 : tmp_name = php_addslashes(tmp_name, strlen(tmp_name), &new_len, 0 TSRMLS_CC);
4852 41 : smart_str_appendl(&querystr, tmp_name, new_len);
4853 :
4854 41 : smart_str_appends(&querystr, "' AND a.atttypid = t.oid ORDER BY a.attnum;");
4855 41 : smart_str_0(&querystr);
4856 :
4857 41 : efree(tmp_name2);
4858 41 : efree(tmp_name);
4859 |