You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

5770 lines
159 KiB

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