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.

4486 lines
123 KiB

25 years ago
25 years ago
27 years ago
27 years ago
24 years ago
24 years ago
24 years ago
25 years ago
25 years ago
25 years ago
24 years ago
24 years ago
25 years ago
25 years ago
26 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
24 years ago
25 years ago
25 years ago
25 years ago
25 years ago
26 years ago
24 years ago
25 years ago
24 years ago
24 years ago
24 years ago
24 years ago
24 years ago
25 years ago
25 years ago
24 years ago
24 years ago
24 years ago
24 years ago
24 years ago
25 years ago
25 years ago
25 years ago
24 years ago
  1. /*
  2. +----------------------------------------------------------------------+
  3. | PHP Version 4 |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1997-2002 The PHP Group |
  6. +----------------------------------------------------------------------+
  7. | This source file is subject to version 2.02 of the PHP license, |
  8. | that is bundled with this package in the file LICENSE, and is |
  9. | available at through the world-wide-web at |
  10. | http://www.php.net/license/2_02.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. +----------------------------------------------------------------------+
  20. */
  21. /* $Id$ */
  22. #include <stdlib.h>
  23. #define PHP_PGSQL_PRIVATE 1
  24. #ifdef HAVE_CONFIG_H
  25. #include "config.h"
  26. #endif
  27. #define SMART_STR_PREALLOC 512
  28. #include "php.h"
  29. #include "php_ini.h"
  30. #include "ext/standard/php_standard.h"
  31. #include "ext/standard/php_smart_str.h"
  32. #include "php_pgsql.h"
  33. #include "php_globals.h"
  34. #if HAVE_PGSQL
  35. #ifndef InvalidOid
  36. #define InvalidOid ((Oid) 0)
  37. #endif
  38. #define PGSQL_ASSOC 1<<0
  39. #define PGSQL_NUM 1<<1
  40. #define PGSQL_BOTH (PGSQL_ASSOC|PGSQL_NUM)
  41. #define PGSQL_STATUS_LONG 1
  42. #define PGSQL_STATUS_STRING 2
  43. #define PGSQL_MAX_LENGTH_OF_LONG 30
  44. #define PGSQL_MAX_LENGTH_OF_DOUBLE 60
  45. #define PGSQL_RETURN_OID(oid) do { \
  46. if (oid > LONG_MAX) { \
  47. smart_str s = {0}; \
  48. smart_str_append_unsigned(&s, oid); \
  49. smart_str_0(&s); \
  50. RETURN_STRINGL(s.c, s.len, 0); \
  51. } \
  52. RETURN_LONG((long)oid); \
  53. } while(0)
  54. #if HAVE_PQSETNONBLOCKING
  55. #define PQ_SETNONBLOCKING(pg_link, flag) PQsetnonblocking(pg_link, flag)
  56. #else
  57. #define PQ_SETNONBLOCKING(pg_link, flag) 0
  58. #endif
  59. #define CHECK_DEFAULT_LINK(x) if ((x) == -1) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "No PostgreSQL link opened yet."); }
  60. /* {{{ pgsql_functions[]
  61. */
  62. function_entry pgsql_functions[] = {
  63. /* connection functions */
  64. PHP_FE(pg_connect, NULL)
  65. PHP_FE(pg_pconnect, NULL)
  66. PHP_FE(pg_close, NULL)
  67. PHP_FE(pg_connection_status, NULL)
  68. PHP_FE(pg_connection_busy, NULL)
  69. PHP_FE(pg_connection_reset, NULL)
  70. PHP_FE(pg_host, NULL)
  71. PHP_FE(pg_dbname, NULL)
  72. PHP_FE(pg_port, NULL)
  73. PHP_FE(pg_tty, NULL)
  74. PHP_FE(pg_options, NULL)
  75. PHP_FE(pg_ping, NULL)
  76. /* query functions */
  77. PHP_FE(pg_query, NULL)
  78. PHP_FE(pg_send_query, NULL)
  79. PHP_FE(pg_cancel_query, NULL)
  80. /* result functions */
  81. PHP_FE(pg_fetch_result, NULL)
  82. PHP_FE(pg_fetch_row, NULL)
  83. PHP_FE(pg_fetch_assoc, NULL)
  84. PHP_FE(pg_fetch_array, NULL)
  85. PHP_FE(pg_fetch_object, NULL)
  86. PHP_FE(pg_fetch_all, NULL)
  87. PHP_FE(pg_affected_rows,NULL)
  88. PHP_FE(pg_get_result, NULL)
  89. PHP_FE(pg_result_seek, NULL)
  90. PHP_FE(pg_result_status,NULL)
  91. PHP_FE(pg_free_result, NULL)
  92. PHP_FE(pg_last_oid, NULL)
  93. PHP_FE(pg_num_rows, NULL)
  94. PHP_FE(pg_num_fields, NULL)
  95. PHP_FE(pg_field_name, NULL)
  96. PHP_FE(pg_field_num, NULL)
  97. PHP_FE(pg_field_size, NULL)
  98. PHP_FE(pg_field_type, NULL)
  99. PHP_FE(pg_field_prtlen, NULL)
  100. PHP_FE(pg_field_is_null,NULL)
  101. /* async message function */
  102. PHP_FE(pg_get_notify, NULL)
  103. PHP_FE(pg_get_pid, NULL)
  104. /* error message functions */
  105. PHP_FE(pg_result_error, NULL)
  106. PHP_FE(pg_last_error, NULL)
  107. PHP_FE(pg_last_notice, NULL)
  108. /* copy functions */
  109. PHP_FE(pg_put_line, NULL)
  110. PHP_FE(pg_end_copy, NULL)
  111. PHP_FE(pg_copy_to, NULL)
  112. PHP_FE(pg_copy_from, NULL)
  113. /* debug functions */
  114. PHP_FE(pg_trace, NULL)
  115. PHP_FE(pg_untrace, NULL)
  116. /* large object functions */
  117. PHP_FE(pg_lo_create, NULL)
  118. PHP_FE(pg_lo_unlink, NULL)
  119. PHP_FE(pg_lo_open, NULL)
  120. PHP_FE(pg_lo_close, NULL)
  121. PHP_FE(pg_lo_read, NULL)
  122. PHP_FE(pg_lo_write, NULL)
  123. PHP_FE(pg_lo_read_all, NULL)
  124. PHP_FE(pg_lo_import, NULL)
  125. PHP_FE(pg_lo_export, NULL)
  126. PHP_FE(pg_lo_seek, NULL)
  127. PHP_FE(pg_lo_tell, NULL)
  128. /* utility functions */
  129. #if HAVE_PQESCAPE
  130. PHP_FE(pg_escape_string,NULL)
  131. PHP_FE(pg_escape_bytea, NULL)
  132. PHP_FE(pg_unescape_bytea, NULL)
  133. #endif
  134. #if HAVE_PQCLIENTENCODING
  135. PHP_FE(pg_client_encoding, NULL)
  136. PHP_FE(pg_set_client_encoding, NULL)
  137. #endif
  138. /* misc function */
  139. PHP_FE(pg_meta_data, NULL)
  140. PHP_FE(pg_convert, NULL)
  141. PHP_FE(pg_insert, NULL)
  142. PHP_FE(pg_update, NULL)
  143. PHP_FE(pg_delete, NULL)
  144. PHP_FE(pg_select, NULL)
  145. /* aliases for downwards compatibility */
  146. PHP_FALIAS(pg_exec, pg_query, NULL)
  147. PHP_FALIAS(pg_getlastoid, pg_last_oid, NULL)
  148. PHP_FALIAS(pg_cmdtuples, pg_affected_rows, NULL)
  149. PHP_FALIAS(pg_errormessage, pg_last_error, NULL)
  150. PHP_FALIAS(pg_numrows, pg_num_rows, NULL)
  151. PHP_FALIAS(pg_numfields, pg_num_fields, NULL)
  152. PHP_FALIAS(pg_fieldname, pg_field_name, NULL)
  153. PHP_FALIAS(pg_fieldsize, pg_field_size, NULL)
  154. PHP_FALIAS(pg_fieldtype, pg_field_type, NULL)
  155. PHP_FALIAS(pg_fieldnum, pg_field_num, NULL)
  156. PHP_FALIAS(pg_fieldprtlen, pg_field_prtlen, NULL)
  157. PHP_FALIAS(pg_fieldisnull, pg_field_is_null, NULL)
  158. PHP_FALIAS(pg_freeresult, pg_free_result, NULL)
  159. PHP_FALIAS(pg_result, pg_fetch_result, NULL)
  160. PHP_FALIAS(pg_loreadall, pg_lo_read_all, NULL)
  161. PHP_FALIAS(pg_locreate, pg_lo_create, NULL)
  162. PHP_FALIAS(pg_lounlink, pg_lo_unlink, NULL)
  163. PHP_FALIAS(pg_loopen, pg_lo_open, NULL)
  164. PHP_FALIAS(pg_loclose, pg_lo_close, NULL)
  165. PHP_FALIAS(pg_loread, pg_lo_read, NULL)
  166. PHP_FALIAS(pg_lowrite, pg_lo_write, NULL)
  167. PHP_FALIAS(pg_loimport, pg_lo_import, NULL)
  168. PHP_FALIAS(pg_loexport, pg_lo_export, NULL)
  169. #if HAVE_PQCLIENTENCODING
  170. PHP_FALIAS(pg_clientencoding, pg_client_encoding, NULL)
  171. PHP_FALIAS(pg_setclientencoding, pg_set_client_encoding, NULL)
  172. #endif
  173. {NULL, NULL, NULL}
  174. };
  175. /* }}} */
  176. /* {{{ pgsql_module_entry
  177. */
  178. zend_module_entry pgsql_module_entry = {
  179. STANDARD_MODULE_HEADER,
  180. "pgsql",
  181. pgsql_functions,
  182. PHP_MINIT(pgsql),
  183. PHP_MSHUTDOWN(pgsql),
  184. PHP_RINIT(pgsql),
  185. PHP_RSHUTDOWN(pgsql),
  186. PHP_MINFO(pgsql),
  187. NO_VERSION_YET,
  188. STANDARD_MODULE_PROPERTIES
  189. };
  190. /* }}} */
  191. #ifdef COMPILE_DL_PGSQL
  192. ZEND_GET_MODULE(pgsql)
  193. #endif
  194. static int le_link, le_plink, le_result, le_lofp, le_string;
  195. #ifdef ZTS
  196. int pgsql_globals_id;
  197. #else
  198. php_pgsql_globals pgsql_globals;
  199. #endif
  200. /* {{{ php_pgsql_set_default_link
  201. */
  202. static void php_pgsql_set_default_link(int id TSRMLS_DC)
  203. {
  204. zend_list_addref(id);
  205. if (PGG(default_link) != -1) {
  206. zend_list_delete(PGG(default_link));
  207. }
  208. PGG(default_link) = id;
  209. }
  210. /* }}} */
  211. /* {{{ _close_pgsql_link
  212. */
  213. static void _close_pgsql_link(zend_rsrc_list_entry *rsrc TSRMLS_DC)
  214. {
  215. PGconn *link = (PGconn *)rsrc->ptr;
  216. PGresult *res;
  217. while ((res = PQgetResult(link))) {
  218. PQclear(res);
  219. }
  220. PQfinish(link);
  221. PGG(num_links)--;
  222. }
  223. /* }}} */
  224. /* {{{ _close_pgsql_plink
  225. */
  226. static void _close_pgsql_plink(zend_rsrc_list_entry *rsrc TSRMLS_DC)
  227. {
  228. PGconn *link = (PGconn *)rsrc->ptr;
  229. PGresult *res;
  230. while ((res = PQgetResult(link))) {
  231. PQclear(res);
  232. }
  233. PQfinish(link);
  234. PGG(num_persistent)--;
  235. PGG(num_links)--;
  236. }
  237. /* }}} */
  238. /* {{{ _php_pgsql_notice_handler
  239. */
  240. static void _php_pgsql_notice_handler(void *resource_id, const char *message)
  241. {
  242. php_pgsql_notice *notice;
  243. TSRMLS_FETCH();
  244. if (! PGG(ignore_notices)) {
  245. if (PGG(log_notices)) {
  246. php_log_err((char *) message TSRMLS_CC);
  247. }
  248. notice = (php_pgsql_notice *)emalloc(sizeof(php_pgsql_notice));
  249. notice->len = strlen(message);
  250. notice->message = estrndup(message, notice->len);
  251. zend_hash_index_update(&PGG(notices), *(int *)resource_id, (void **)&notice, sizeof(php_pgsql_notice *), NULL);
  252. }
  253. }
  254. /* }}} */
  255. #define PHP_PGSQL_NOTICE_PTR_DTOR (void (*)(void *))_php_pgsql_notice_ptr_dtor
  256. /* {{{ _php_pgsql_notice_dtor
  257. */
  258. static void _php_pgsql_notice_ptr_dtor(void **ptr)
  259. {
  260. php_pgsql_notice *notice = (php_pgsql_notice *)*ptr;
  261. efree(notice->message);
  262. efree(notice);
  263. }
  264. /* }}} */
  265. /* {{{ _rollback_transactions
  266. */
  267. static int _rollback_transactions(zend_rsrc_list_entry *rsrc TSRMLS_DC)
  268. {
  269. PGconn *link;
  270. PGresult *res;
  271. int orig;
  272. if (Z_TYPE_P(rsrc) != le_plink)
  273. return 0;
  274. link = (PGconn *) rsrc->ptr;
  275. if (PQ_SETNONBLOCKING(link, 0)) {
  276. php_error_docref("ref.pgsql" TSRMLS_CC, E_NOTICE, "Cannot set connection to blocking mode.");
  277. return -1;
  278. }
  279. while ((res = PQgetResult(link))) {
  280. PQclear(res);
  281. }
  282. orig = PGG(ignore_notices);
  283. PGG(ignore_notices) = 1;
  284. res = PQexec(link,"BEGIN;ROLLBACK;");
  285. PQclear(res);
  286. PGG(ignore_notices) = orig;
  287. return 0;
  288. }
  289. /* }}} */
  290. /* {{{ _free_ptr
  291. */
  292. static void _free_ptr(zend_rsrc_list_entry *rsrc TSRMLS_DC)
  293. {
  294. pgLofp *lofp = (pgLofp *)rsrc->ptr;
  295. efree(lofp);
  296. }
  297. /* }}} */
  298. /* {{{ _free_result
  299. */
  300. static void _free_result(zend_rsrc_list_entry *rsrc TSRMLS_DC)
  301. {
  302. pgsql_result_handle *pg_result = (pgsql_result_handle *)rsrc->ptr;
  303. PQclear(pg_result->result);
  304. efree(pg_result);
  305. }
  306. /* }}} */
  307. /* {{{ PHP_INI
  308. */
  309. PHP_INI_BEGIN()
  310. STD_PHP_INI_BOOLEAN("pgsql.allow_persistent", "1", PHP_INI_SYSTEM, OnUpdateBool, allow_persistent, php_pgsql_globals, pgsql_globals)
  311. STD_PHP_INI_ENTRY_EX("pgsql.max_persistent", "-1", PHP_INI_SYSTEM, OnUpdateInt, max_persistent, php_pgsql_globals, pgsql_globals, display_link_numbers)
  312. STD_PHP_INI_ENTRY_EX("pgsql.max_links", "-1", PHP_INI_SYSTEM, OnUpdateInt, max_links, php_pgsql_globals, pgsql_globals, display_link_numbers)
  313. STD_PHP_INI_BOOLEAN("pgsql.auto_reset_persistent", "0", PHP_INI_SYSTEM, OnUpdateBool, auto_reset_persistent, php_pgsql_globals, pgsql_globals)
  314. STD_PHP_INI_BOOLEAN("pgsql.ignore_notice", "0", PHP_INI_ALL, OnUpdateBool, ignore_notices, php_pgsql_globals, pgsql_globals)
  315. STD_PHP_INI_BOOLEAN("pgsql.log_notice", "0", PHP_INI_ALL, OnUpdateBool, log_notices, php_pgsql_globals, pgsql_globals)
  316. PHP_INI_END()
  317. /* }}} */
  318. /* {{{ php_pgsql_init_globals
  319. */
  320. static void php_pgsql_init_globals(php_pgsql_globals *pgsql_globals_p TSRMLS_DC)
  321. {
  322. PGG(num_persistent) = 0;
  323. /* Initilize notice message hash at MINIT only */
  324. zend_hash_init_ex(&PGG(notices), 0, NULL, PHP_PGSQL_NOTICE_PTR_DTOR, 1, 0);
  325. }
  326. /* }}} */
  327. /* {{{ PHP_MINIT_FUNCTION
  328. */
  329. PHP_MINIT_FUNCTION(pgsql)
  330. {
  331. #ifdef ZTS
  332. ts_allocate_id(&pgsql_globals_id, sizeof(php_pgsql_globals), (ts_allocate_ctor) php_pgsql_init_globals, NULL);
  333. #else
  334. php_pgsql_init_globals(&pgsql_globals TSRMLS_CC);
  335. #endif
  336. REGISTER_INI_ENTRIES();
  337. le_link = zend_register_list_destructors_ex(_close_pgsql_link, NULL, "pgsql link", module_number);
  338. le_plink = zend_register_list_destructors_ex(NULL, _close_pgsql_plink, "pgsql link persistent", module_number);
  339. le_result = zend_register_list_destructors_ex(_free_result, NULL, "pgsql result", module_number);
  340. le_lofp = zend_register_list_destructors_ex(_free_ptr, NULL, "pgsql large object", module_number);
  341. le_string = zend_register_list_destructors_ex(_free_ptr, NULL, "pgsql string", module_number);
  342. /* For connection option */
  343. REGISTER_LONG_CONSTANT("PGSQL_CONNECT_FORCE_NEW", PGSQL_CONNECT_FORCE_NEW, CONST_CS | CONST_PERSISTENT);
  344. /* For pg_fetch_array() */
  345. REGISTER_LONG_CONSTANT("PGSQL_ASSOC", PGSQL_ASSOC, CONST_CS | CONST_PERSISTENT);
  346. REGISTER_LONG_CONSTANT("PGSQL_NUM", PGSQL_NUM, CONST_CS | CONST_PERSISTENT);
  347. REGISTER_LONG_CONSTANT("PGSQL_BOTH", PGSQL_BOTH, CONST_CS | CONST_PERSISTENT);
  348. /* For pg_connection_status() */
  349. REGISTER_LONG_CONSTANT("PGSQL_CONNECTION_BAD", CONNECTION_BAD, CONST_CS | CONST_PERSISTENT);
  350. REGISTER_LONG_CONSTANT("PGSQL_CONNECTION_OK", CONNECTION_OK, CONST_CS | CONST_PERSISTENT);
  351. /* For lo_seek() */
  352. REGISTER_LONG_CONSTANT("PGSQL_SEEK_SET", SEEK_SET, CONST_CS | CONST_PERSISTENT);
  353. REGISTER_LONG_CONSTANT("PGSQL_SEEK_CUR", SEEK_CUR, CONST_CS | CONST_PERSISTENT);
  354. REGISTER_LONG_CONSTANT("PGSQL_SEEK_END", SEEK_END, CONST_CS | CONST_PERSISTENT);
  355. /* For pg_result_status() return value type */
  356. REGISTER_LONG_CONSTANT("PGSQL_STATUS_LONG", PGSQL_STATUS_LONG, CONST_CS | CONST_PERSISTENT);
  357. REGISTER_LONG_CONSTANT("PGSQL_STATUS_STRING", PGSQL_STATUS_STRING, CONST_CS | CONST_PERSISTENT);
  358. /* For pg_result_status() return value */
  359. REGISTER_LONG_CONSTANT("PGSQL_EMPTY_QUERY", PGRES_EMPTY_QUERY, CONST_CS | CONST_PERSISTENT);
  360. REGISTER_LONG_CONSTANT("PGSQL_COMMAND_OK", PGRES_COMMAND_OK, CONST_CS | CONST_PERSISTENT);
  361. REGISTER_LONG_CONSTANT("PGSQL_TUPLES_OK", PGRES_TUPLES_OK, CONST_CS | CONST_PERSISTENT);
  362. REGISTER_LONG_CONSTANT("PGSQL_COPY_OUT", PGRES_COPY_OUT, CONST_CS | CONST_PERSISTENT);
  363. REGISTER_LONG_CONSTANT("PGSQL_COPY_IN", PGRES_COPY_IN, CONST_CS | CONST_PERSISTENT);
  364. REGISTER_LONG_CONSTANT("PGSQL_BAD_RESPONSE", PGRES_BAD_RESPONSE, CONST_CS | CONST_PERSISTENT);
  365. REGISTER_LONG_CONSTANT("PGSQL_NONFATAL_ERROR", PGRES_NONFATAL_ERROR, CONST_CS | CONST_PERSISTENT);
  366. REGISTER_LONG_CONSTANT("PGSQL_FATAL_ERROR", PGRES_FATAL_ERROR, CONST_CS | CONST_PERSISTENT);
  367. /* pg_convert options */
  368. REGISTER_LONG_CONSTANT("PGSQL_CONV_IGNORE_DEFAULT", PGSQL_CONV_IGNORE_DEFAULT, CONST_CS | CONST_PERSISTENT);
  369. REGISTER_LONG_CONSTANT("PGSQL_CONV_FORCE_NULL", PGSQL_CONV_FORCE_NULL, CONST_CS | CONST_PERSISTENT);
  370. REGISTER_LONG_CONSTANT("PGSQL_CONV_IGNORE_NOT_NULL", PGSQL_CONV_IGNORE_NOT_NULL, CONST_CS | CONST_PERSISTENT);
  371. /* pg_insert/update/delete/select options */
  372. REGISTER_LONG_CONSTANT("PGSQL_DML_NO_CONV", PGSQL_DML_NO_CONV, CONST_CS | CONST_PERSISTENT);
  373. REGISTER_LONG_CONSTANT("PGSQL_DML_EXEC", PGSQL_DML_EXEC, CONST_CS | CONST_PERSISTENT);
  374. REGISTER_LONG_CONSTANT("PGSQL_DML_ASYNC", PGSQL_DML_ASYNC, CONST_CS | CONST_PERSISTENT);
  375. REGISTER_LONG_CONSTANT("PGSQL_DML_STRING", PGSQL_DML_STRING, CONST_CS | CONST_PERSISTENT);
  376. return SUCCESS;
  377. }
  378. /* }}} */
  379. /* {{{ PHP_MSHUTDOWN_FUNCTION
  380. */
  381. PHP_MSHUTDOWN_FUNCTION(pgsql)
  382. {
  383. UNREGISTER_INI_ENTRIES();
  384. return SUCCESS;
  385. }
  386. /* }}} */
  387. /* {{{ PHP_RINIT_FUNCTION
  388. */
  389. PHP_RINIT_FUNCTION(pgsql)
  390. {
  391. PGG(default_link)=-1;
  392. PGG(num_links) = PGG(num_persistent);
  393. return SUCCESS;
  394. }
  395. /* }}} */
  396. /* {{{ PHP_RSHUTDOWN_FUNCTION
  397. */
  398. PHP_RSHUTDOWN_FUNCTION(pgsql)
  399. {
  400. /* clean up notice messages */
  401. zend_hash_clean(&PGG(notices));
  402. /* clean up persistent connection */
  403. zend_hash_apply(&EG(persistent_list), (apply_func_t) _rollback_transactions TSRMLS_CC);
  404. return SUCCESS;
  405. }
  406. /* }}} */
  407. /* {{{ PHP_MINFO_FUNCTION
  408. */
  409. PHP_MINFO_FUNCTION(pgsql)
  410. {
  411. char buf[256];
  412. php_info_print_table_start();
  413. php_info_print_table_header(2, "PostgreSQL Support", "enabled");
  414. #if HAVE_PG_CONFIG_H
  415. php_info_print_table_row(2, "PostgreSQL(libpq) Version", PG_VERSION);
  416. #ifdef MULTIBYTE
  417. php_info_print_table_row(2, "Multibyte charater support", "enabled");
  418. #else
  419. php_info_print_table_row(2, "Multibyte charater support", "disabled");
  420. #endif
  421. #ifdef USE_SSL
  422. php_info_print_table_row(2, "SSL support", "enabled");
  423. #else
  424. php_info_print_table_row(2, "SSL support", "disabled");
  425. #endif
  426. #endif /* HAVE_PG_CONFIG_H */
  427. sprintf(buf, "%ld", PGG(num_persistent));
  428. php_info_print_table_row(2, "Active Persistent Links", buf);
  429. sprintf(buf, "%ld", PGG(num_links));
  430. php_info_print_table_row(2, "Active Links", buf);
  431. php_info_print_table_end();
  432. DISPLAY_INI_ENTRIES();
  433. }
  434. /* }}} */
  435. /* {{{ php_pgsql_do_connect
  436. */
  437. static void php_pgsql_do_connect(INTERNAL_FUNCTION_PARAMETERS, int persistent)
  438. {
  439. char *host=NULL,*port=NULL,*options=NULL,*tty=NULL,*dbname=NULL,*connstring=NULL;
  440. PGconn *pgsql;
  441. smart_str str = {0};
  442. zval **args[5];
  443. int i, connect_type = 0;
  444. if (ZEND_NUM_ARGS() < 1 || ZEND_NUM_ARGS() > 5
  445. || zend_get_parameters_array_ex(ZEND_NUM_ARGS(), args) == FAILURE) {
  446. WRONG_PARAM_COUNT;
  447. }
  448. smart_str_appends(&str, "pgsql");
  449. for (i = 0; i < ZEND_NUM_ARGS(); i++) {
  450. convert_to_string_ex(args[i]);
  451. smart_str_appendc(&str, '_');
  452. smart_str_appendl(&str, Z_STRVAL_PP(args[i]), Z_STRLEN_PP(args[i]));
  453. }
  454. smart_str_0(&str);
  455. if (ZEND_NUM_ARGS() == 1) { /* new style, using connection string */
  456. connstring = Z_STRVAL_PP(args[0]);
  457. } else if (ZEND_NUM_ARGS() == 2 ) { /* Safe to add conntype_option, since 2 args was illegal */
  458. connstring = Z_STRVAL_PP(args[0]);
  459. convert_to_long_ex(args[1]);
  460. connect_type = Z_LVAL_PP(args[1]);
  461. } else {
  462. host = Z_STRVAL_PP(args[0]);
  463. port = Z_STRVAL_PP(args[1]);
  464. dbname = Z_STRVAL_PP(args[ZEND_NUM_ARGS()-1]);
  465. switch (ZEND_NUM_ARGS()) {
  466. case 5:
  467. tty = Z_STRVAL_PP(args[3]);
  468. /* fall through */
  469. case 4:
  470. options = Z_STRVAL_PP(args[2]);
  471. break;
  472. }
  473. }
  474. if (persistent && PGG(allow_persistent)) {
  475. list_entry *le;
  476. /* try to find if we already have this link in our persistent list */
  477. if (zend_hash_find(&EG(persistent_list), str.c, str.len+1, (void **) &le)==FAILURE) { /* we don't */
  478. list_entry new_le;
  479. if (PGG(max_links)!=-1 && PGG(num_links)>=PGG(max_links)) {
  480. php_error_docref(NULL TSRMLS_CC, E_WARNING,
  481. "Cannot create new link. Too many open links (%d).", PGG(num_links));
  482. goto err;
  483. }
  484. if (PGG(max_persistent)!=-1 && PGG(num_persistent)>=PGG(max_persistent)) {
  485. php_error_docref(NULL TSRMLS_CC, E_WARNING,
  486. "Cannot create new link. Too many open persistent links (%d).", PGG(num_persistent));
  487. goto err;
  488. }
  489. /* create the link */
  490. if (connstring) {
  491. pgsql=PQconnectdb(connstring);
  492. } else {
  493. pgsql=PQsetdb(host,port,options,tty,dbname);
  494. }
  495. if (pgsql==NULL || PQstatus(pgsql)==CONNECTION_BAD) {
  496. php_error_docref(NULL TSRMLS_CC, E_WARNING,
  497. "Unable to connect to PostgreSQL server: %s.", PQerrorMessage(pgsql));
  498. if (pgsql) {
  499. PQfinish(pgsql);
  500. }
  501. goto err;
  502. }
  503. /* hash it up */
  504. Z_TYPE(new_le) = le_plink;
  505. new_le.ptr = pgsql;
  506. if (zend_hash_update(&EG(persistent_list), str.c, str.len+1, (void *) &new_le, sizeof(list_entry), NULL)==FAILURE) {
  507. goto err;
  508. }
  509. PGG(num_links)++;
  510. PGG(num_persistent)++;
  511. } else { /* we do */
  512. if (Z_TYPE_P(le) != le_plink) {
  513. RETURN_FALSE;
  514. }
  515. /* ensure that the link did not die */
  516. if (PGG(auto_reset_persistent)) {
  517. /* need to send & get something from backend to
  518. make sure we catch CONNECTION_BAD everytime */
  519. PGresult *pg_result;
  520. pg_result = PQexec(le->ptr, "select 1");
  521. PQclear(pg_result);
  522. }
  523. if (PQstatus(le->ptr)==CONNECTION_BAD) { /* the link died */
  524. if (le->ptr == NULL) {
  525. if (connstring) {
  526. le->ptr=PQconnectdb(connstring);
  527. } else {
  528. le->ptr=PQsetdb(host,port,options,tty,dbname);
  529. }
  530. }
  531. else {
  532. PQreset(le->ptr);
  533. }
  534. if (le->ptr==NULL || PQstatus(le->ptr)==CONNECTION_BAD) {
  535. php_error_docref(NULL TSRMLS_CC, E_WARNING,"PostgreSQL link lost, unable to reconnect.");
  536. zend_hash_del(&EG(persistent_list),str.c,str.len+1);
  537. goto err;
  538. }
  539. }
  540. pgsql = (PGconn *) le->ptr;
  541. }
  542. ZEND_REGISTER_RESOURCE(return_value, pgsql, le_plink);
  543. } else { // Non persistent connection
  544. list_entry *index_ptr,new_index_ptr;
  545. /* first we check the hash for the hashed_details key. if it exists,
  546. * it should point us to the right offset where the actual pgsql link sits.
  547. * if it doesn't, open a new pgsql link, add it to the resource list,
  548. * and add a pointer to it with hashed_details as the key.
  549. */
  550. if (!(connect_type & PGSQL_CONNECT_FORCE_NEW)
  551. && zend_hash_find(&EG(regular_list),str.c,str.len+1,(void **) &index_ptr)==SUCCESS) {
  552. int type,link;
  553. void *ptr;
  554. if (Z_TYPE_P(index_ptr) != le_index_ptr) {
  555. RETURN_FALSE;
  556. }
  557. link = (int) index_ptr->ptr;
  558. ptr = zend_list_find(link,&type); /* check if the link is still there */
  559. if (ptr && (type==le_link || type==le_plink)) {
  560. Z_LVAL_P(return_value) = link;
  561. zend_list_addref(link);
  562. php_pgsql_set_default_link(link TSRMLS_CC);
  563. Z_TYPE_P(return_value) = IS_RESOURCE;
  564. goto cleanup;
  565. } else {
  566. zend_hash_del(&EG(regular_list),str.c,str.len+1);
  567. }
  568. }
  569. if (PGG(max_links)!=-1 && PGG(num_links)>=PGG(max_links)) {
  570. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Cannot create new link. Too many open links (%d).", PGG(num_links));
  571. goto err;
  572. }
  573. if (connstring) {
  574. pgsql = PQconnectdb(connstring);
  575. } else {
  576. pgsql = PQsetdb(host,port,options,tty,dbname);
  577. }
  578. if (pgsql==NULL || PQstatus(pgsql)==CONNECTION_BAD) {
  579. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to connect to PostgreSQL server: %s.", PQerrorMessage(pgsql));
  580. goto err;
  581. }
  582. /* add it to the list */
  583. ZEND_REGISTER_RESOURCE(return_value, pgsql, le_link);
  584. /* add it to the hash */
  585. new_index_ptr.ptr = (void *) Z_LVAL_P(return_value);
  586. Z_TYPE(new_index_ptr) = le_index_ptr;
  587. if (zend_hash_update(&EG(regular_list),str.c,str.len+1,(void *) &new_index_ptr, sizeof(list_entry), NULL)==FAILURE) {
  588. goto err;
  589. }
  590. PGG(num_links)++;
  591. }
  592. /* set notice processer */
  593. if (! PGG(ignore_notices) && Z_TYPE_P(return_value) == IS_RESOURCE) {
  594. PQsetNoticeProcessor(pgsql, _php_pgsql_notice_handler, (void *)&Z_RESVAL_P(return_value));
  595. }
  596. php_pgsql_set_default_link(Z_LVAL_P(return_value) TSRMLS_CC);
  597. cleanup:
  598. smart_str_free(&str);
  599. return;
  600. err:
  601. smart_str_free(&str);
  602. RETURN_FALSE;
  603. }
  604. /* }}} */
  605. #if 0
  606. /* {{{ php_pgsql_get_default_link
  607. */
  608. static int php_pgsql_get_default_link(INTERNAL_FUNCTION_PARAMETERS)
  609. {
  610. if (PGG(default_link)==-1) { /* no link opened yet, implicitly open one */
  611. ht = 0;
  612. php_pgsql_do_connect(INTERNAL_FUNCTION_PARAM_PASSTHRU,0);
  613. }
  614. return PGG(default_link);
  615. }
  616. /* }}} */
  617. #endif
  618. /* {{{ proto resource pg_connect(string connection_string[, int connect_type] | [string host, string port [, string options [, string tty,]]] string database)
  619. Open a PostgreSQL connection */
  620. PHP_FUNCTION(pg_connect)
  621. {
  622. php_pgsql_do_connect(INTERNAL_FUNCTION_PARAM_PASSTHRU,0);
  623. }
  624. /* }}} */
  625. /* {{{ proto resource pg_pconnect(string connection_string | [string host, string port [, string options [, string tty,]]] string database)
  626. Open a persistent PostgreSQL connection */
  627. PHP_FUNCTION(pg_pconnect)
  628. {
  629. php_pgsql_do_connect(INTERNAL_FUNCTION_PARAM_PASSTHRU,1);
  630. }
  631. /* }}} */
  632. /* {{{ proto bool pg_close([resource connection])
  633. Close a PostgreSQL connection */
  634. PHP_FUNCTION(pg_close)
  635. {
  636. zval **pgsql_link = NULL;
  637. int id;
  638. PGconn *pgsql;
  639. switch (ZEND_NUM_ARGS()) {
  640. case 0:
  641. id = PGG(default_link);
  642. CHECK_DEFAULT_LINK(id);
  643. break;
  644. case 1:
  645. if (zend_get_parameters_ex(1, &pgsql_link)==FAILURE) {
  646. RETURN_FALSE;
  647. }
  648. id = -1;
  649. break;
  650. default:
  651. WRONG_PARAM_COUNT;
  652. break;
  653. }
  654. ZEND_FETCH_RESOURCE2(pgsql, PGconn *, pgsql_link, id, "PostgreSQL link", le_link, le_plink);
  655. if (id==-1) { /* explicit resource number */
  656. zend_list_delete(Z_RESVAL_PP(pgsql_link));
  657. }
  658. if (id!=-1
  659. || (pgsql_link && Z_RESVAL_PP(pgsql_link)==PGG(default_link))) {
  660. zend_list_delete(PGG(default_link));
  661. PGG(default_link) = -1;
  662. }
  663. RETURN_TRUE;
  664. }
  665. /* }}} */
  666. #define PHP_PG_DBNAME 1
  667. #define PHP_PG_ERROR_MESSAGE 2
  668. #define PHP_PG_OPTIONS 3
  669. #define PHP_PG_PORT 4
  670. #define PHP_PG_TTY 5
  671. #define PHP_PG_HOST 6
  672. /* {{{ php_pgsql_get_link_info
  673. */
  674. static void php_pgsql_get_link_info(INTERNAL_FUNCTION_PARAMETERS, int entry_type)
  675. {
  676. zval **pgsql_link = NULL;
  677. int id = -1;
  678. PGconn *pgsql;
  679. switch(ZEND_NUM_ARGS()) {
  680. case 0:
  681. id = PGG(default_link);
  682. CHECK_DEFAULT_LINK(id);
  683. break;
  684. case 1:
  685. if (zend_get_parameters_ex(1, &pgsql_link)==FAILURE) {
  686. RETURN_FALSE;
  687. }
  688. break;
  689. default:
  690. WRONG_PARAM_COUNT;
  691. break;
  692. }
  693. ZEND_FETCH_RESOURCE2(pgsql, PGconn *, pgsql_link, id, "PostgreSQL link", le_link, le_plink);
  694. switch(entry_type) {
  695. case PHP_PG_DBNAME:
  696. Z_STRVAL_P(return_value) = PQdb(pgsql);
  697. break;
  698. case PHP_PG_ERROR_MESSAGE:
  699. Z_STRVAL_P(return_value) = PQerrorMessage(pgsql);
  700. break;
  701. case PHP_PG_OPTIONS:
  702. Z_STRVAL_P(return_value) = PQoptions(pgsql);
  703. break;
  704. case PHP_PG_PORT:
  705. Z_STRVAL_P(return_value) = PQport(pgsql);
  706. break;
  707. case PHP_PG_TTY:
  708. Z_STRVAL_P(return_value) = PQtty(pgsql);
  709. break;
  710. case PHP_PG_HOST:
  711. Z_STRVAL_P(return_value) = PQhost(pgsql);
  712. break;
  713. default:
  714. RETURN_FALSE;
  715. }
  716. Z_STRLEN_P(return_value) = strlen(Z_STRVAL_P(return_value));
  717. Z_STRVAL_P(return_value) = (char *) estrdup(Z_STRVAL_P(return_value));
  718. Z_TYPE_P(return_value) = IS_STRING;
  719. }
  720. /* }}} */
  721. /* {{{ proto string pg_dbname([resource connection])
  722. Get the database name */
  723. PHP_FUNCTION(pg_dbname)
  724. {
  725. php_pgsql_get_link_info(INTERNAL_FUNCTION_PARAM_PASSTHRU,PHP_PG_DBNAME);
  726. }
  727. /* }}} */
  728. /* {{{ proto string pg_last_error([resource connection])
  729. Get the error message string */
  730. PHP_FUNCTION(pg_last_error)
  731. {
  732. php_pgsql_get_link_info(INTERNAL_FUNCTION_PARAM_PASSTHRU,PHP_PG_ERROR_MESSAGE);
  733. }
  734. /* }}} */
  735. /* {{{ proto string pg_options([resource connection])
  736. Get the options associated with the connection */
  737. PHP_FUNCTION(pg_options)
  738. {
  739. php_pgsql_get_link_info(INTERNAL_FUNCTION_PARAM_PASSTHRU,PHP_PG_OPTIONS);
  740. }
  741. /* }}} */
  742. /* {{{ proto int pg_port([resource connection])
  743. Return the port number associated with the connection */
  744. PHP_FUNCTION(pg_port)
  745. {
  746. php_pgsql_get_link_info(INTERNAL_FUNCTION_PARAM_PASSTHRU,PHP_PG_PORT);
  747. }
  748. /* }}} */
  749. /* {{{ proto string pg_tty([resource connection])
  750. Return the tty name associated with the connection */
  751. PHP_FUNCTION(pg_tty)
  752. {
  753. php_pgsql_get_link_info(INTERNAL_FUNCTION_PARAM_PASSTHRU,PHP_PG_TTY);
  754. }
  755. /* }}} */
  756. /* {{{ proto string pg_host([resource connection])
  757. Returns the host name associated with the connection */
  758. PHP_FUNCTION(pg_host)
  759. {
  760. php_pgsql_get_link_info(INTERNAL_FUNCTION_PARAM_PASSTHRU,PHP_PG_HOST);
  761. }
  762. /* }}} */
  763. /* {{{ proto bool pg_ping(resource connection)
  764. Ping database. If connection is bad, try to reconnect. */
  765. PHP_FUNCTION(pg_ping)
  766. {
  767. zval *pgsql_link = NULL;
  768. int id = -1;
  769. PGconn *pgsql;
  770. if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS() TSRMLS_CC, "r",
  771. &pgsql_link) == FAILURE) {
  772. RETURN_FALSE;
  773. }
  774. ZEND_FETCH_RESOURCE2(pgsql, PGconn *, &pgsql_link, id, "PostgreSQL link", le_link, le_plink);
  775. /* ping connection */
  776. PQexec(pgsql, "SELECT 1;");
  777. /* check status. */
  778. if (PQstatus(pgsql) == CONNECTION_OK)
  779. RETURN_TRUE;
  780. /* reset connection if it's broken */
  781. PQreset(pgsql);
  782. if (PQstatus(pgsql) == CONNECTION_OK) {
  783. RETURN_TRUE;
  784. }
  785. RETURN_FALSE;
  786. }
  787. /* }}} */
  788. /* {{{ proto resource pg_query([resource connection,] string query)
  789. Execute a query */
  790. PHP_FUNCTION(pg_query)
  791. {
  792. zval **query, **pgsql_link = NULL;
  793. int id = -1;
  794. int leftover = 0;
  795. PGconn *pgsql;
  796. PGresult *pgsql_result;
  797. ExecStatusType status;
  798. pgsql_result_handle *pg_result;
  799. switch(ZEND_NUM_ARGS()) {
  800. case 1:
  801. if (zend_get_parameters_ex(1, &query)==FAILURE) {
  802. RETURN_FALSE;
  803. }
  804. id = PGG(default_link);
  805. CHECK_DEFAULT_LINK(id);
  806. break;
  807. case 2:
  808. if (zend_get_parameters_ex(2, &pgsql_link, &query)==FAILURE) {
  809. RETURN_FALSE;
  810. }
  811. break;
  812. default:
  813. WRONG_PARAM_COUNT;
  814. break;
  815. }
  816. ZEND_FETCH_RESOURCE2(pgsql, PGconn *, pgsql_link, id, "PostgreSQL link", le_link, le_plink);
  817. convert_to_string_ex(query);
  818. if (PQ_SETNONBLOCKING(pgsql, 0)) {
  819. php_error_docref(NULL TSRMLS_CC, E_NOTICE,"Cannot set connection to blocking mode.");
  820. RETURN_FALSE;
  821. }
  822. while ((pgsql_result = PQgetResult(pgsql))) {
  823. PQclear(pgsql_result);
  824. leftover = 1;
  825. }
  826. if (leftover) {
  827. php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Found results on this connection. Use pg_get_result() to get these results first.");
  828. }
  829. pgsql_result = PQexec(pgsql, Z_STRVAL_PP(query));
  830. if (pgsql_result) {
  831. status = PQresultStatus(pgsql_result);
  832. } else {
  833. status = (ExecStatusType) PQstatus(pgsql);
  834. }
  835. switch (status) {
  836. case PGRES_EMPTY_QUERY:
  837. case PGRES_BAD_RESPONSE:
  838. case PGRES_NONFATAL_ERROR:
  839. case PGRES_FATAL_ERROR:
  840. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Query failed: %s.", PQerrorMessage(pgsql));
  841. RETURN_FALSE;
  842. break;
  843. case PGRES_COMMAND_OK: /* successful command that did not return rows */
  844. default:
  845. if (pgsql_result) {
  846. pg_result = (pgsql_result_handle *) emalloc(sizeof(pgsql_result_handle));
  847. pg_result->conn = pgsql;
  848. pg_result->result = pgsql_result;
  849. pg_result->row = -1;
  850. ZEND_REGISTER_RESOURCE(return_value, pg_result, le_result);
  851. } else {
  852. RETURN_FALSE;
  853. }
  854. break;
  855. }
  856. }
  857. /* }}} */
  858. #define PHP_PG_NUM_ROWS 1
  859. #define PHP_PG_NUM_FIELDS 2
  860. #define PHP_PG_CMD_TUPLES 3
  861. /* {{{ php_pgsql_get_result_info
  862. */
  863. static void php_pgsql_get_result_info(INTERNAL_FUNCTION_PARAMETERS, int entry_type)
  864. {
  865. zval **result;
  866. PGresult *pgsql_result;
  867. pgsql_result_handle *pg_result;
  868. if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &result)==FAILURE) {
  869. WRONG_PARAM_COUNT;
  870. }
  871. ZEND_FETCH_RESOURCE(pg_result, pgsql_result_handle *, result, -1, "PostgreSQL result", le_result);
  872. pgsql_result = pg_result->result;
  873. switch (entry_type) {
  874. case PHP_PG_NUM_ROWS:
  875. Z_LVAL_P(return_value) = PQntuples(pgsql_result);
  876. break;
  877. case PHP_PG_NUM_FIELDS:
  878. Z_LVAL_P(return_value) = PQnfields(pgsql_result);
  879. break;
  880. case PHP_PG_CMD_TUPLES:
  881. #if HAVE_PQCMDTUPLES
  882. Z_LVAL_P(return_value) = atoi(PQcmdTuples(pgsql_result));
  883. #else
  884. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Not supportted under this build.");
  885. Z_LVAL_P(return_value) = 0;
  886. #endif
  887. break;
  888. default:
  889. RETURN_FALSE;
  890. }
  891. Z_TYPE_P(return_value) = IS_LONG;
  892. }
  893. /* }}} */
  894. /* {{{ proto int pg_num_rows(resource result)
  895. Return the number of rows in the result */
  896. PHP_FUNCTION(pg_num_rows)
  897. {
  898. php_pgsql_get_result_info(INTERNAL_FUNCTION_PARAM_PASSTHRU,PHP_PG_NUM_ROWS);
  899. }
  900. /* }}} */
  901. /* {{{ proto int pg_num_fields(resource result)
  902. Return the number of fields in the result */
  903. PHP_FUNCTION(pg_num_fields)
  904. {
  905. php_pgsql_get_result_info(INTERNAL_FUNCTION_PARAM_PASSTHRU,PHP_PG_NUM_FIELDS);
  906. }
  907. /* }}} */
  908. /* {{{ proto int pg_affected_rows(resource result)
  909. Returns the number of affected tuples */
  910. PHP_FUNCTION(pg_affected_rows)
  911. {
  912. php_pgsql_get_result_info(INTERNAL_FUNCTION_PARAM_PASSTHRU,PHP_PG_CMD_TUPLES);
  913. }
  914. /* }}} */
  915. /* {{{ proto string pg_last_notice(resource connection)
  916. Returns the last notice set by the backend */
  917. PHP_FUNCTION(pg_last_notice)
  918. {
  919. zval *pgsql_link;
  920. PGconn *pg_link;
  921. int id = -1;
  922. php_pgsql_notice **notice;
  923. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r",
  924. &pgsql_link) == FAILURE) {
  925. return;
  926. }
  927. /* Just to check if user passed valid resoruce */
  928. ZEND_FETCH_RESOURCE2(pg_link, PGconn *, &pgsql_link, id, "PostgreSQL link", le_link, le_plink);
  929. if (zend_hash_index_find(&PGG(notices), Z_RESVAL_P(pgsql_link), (void **)&notice) == FAILURE) {
  930. RETURN_FALSE;
  931. }
  932. RETURN_STRINGL((*notice)->message, (*notice)->len, 1);
  933. }
  934. /* }}} */
  935. /* {{{ get_field_name
  936. */
  937. static char *get_field_name(PGconn *pgsql, Oid oid, HashTable *list TSRMLS_DC)
  938. {
  939. PGresult *result;
  940. smart_str str = {0};
  941. list_entry *field_type;
  942. char *ret=NULL;
  943. /* try to lookup the type in the resource list */
  944. smart_str_appends(&str, "pgsql_oid_");
  945. smart_str_append_unsigned(&str, oid);
  946. smart_str_0(&str);
  947. if (zend_hash_find(list,str.c,str.len+1,(void **) &field_type)==SUCCESS) {
  948. ret = estrdup((char *)field_type->ptr);
  949. } else { /* hash all oid's */
  950. int i,num_rows;
  951. int oid_offset,name_offset;
  952. char *tmp_oid, *end_ptr, *tmp_name;
  953. list_entry new_oid_entry;
  954. if ((result = PQexec(pgsql,"select oid,typname from pg_type")) == NULL) {
  955. smart_str_free(&str);
  956. return empty_string;
  957. }
  958. num_rows = PQntuples(result);
  959. oid_offset = PQfnumber(result,"oid");
  960. name_offset = PQfnumber(result,"typname");
  961. for (i=0; i<num_rows; i++) {
  962. if ((tmp_oid = PQgetvalue(result,i,oid_offset))==NULL) {
  963. continue;
  964. }
  965. str.len = 0;
  966. smart_str_appends(&str, "pgsql_oid_");
  967. smart_str_appends(&str, tmp_oid);
  968. smart_str_0(&str);
  969. if ((tmp_name = PQgetvalue(result,i,name_offset))==NULL) {
  970. continue;
  971. }
  972. Z_TYPE(new_oid_entry) = le_string;
  973. new_oid_entry.ptr = estrdup(tmp_name);
  974. zend_hash_update(list,str.c,str.len+1,(void *) &new_oid_entry, sizeof(list_entry), NULL);
  975. if (!ret && strtoul(tmp_oid, &end_ptr, 10)==oid) {
  976. ret = estrdup(tmp_name);
  977. }
  978. }
  979. }
  980. smart_str_free(&str);
  981. return ret;
  982. }
  983. /* }}} */
  984. #define PHP_PG_FIELD_NAME 1
  985. #define PHP_PG_FIELD_SIZE 2
  986. #define PHP_PG_FIELD_TYPE 3
  987. /* {{{ php_pgsql_get_field_info
  988. */
  989. static void php_pgsql_get_field_info(INTERNAL_FUNCTION_PARAMETERS, int entry_type)
  990. {
  991. zval **result, **field;
  992. PGresult *pgsql_result;
  993. pgsql_result_handle *pg_result;
  994. if (ZEND_NUM_ARGS() != 2 || zend_get_parameters_ex(2, &result, &field)==FAILURE) {
  995. WRONG_PARAM_COUNT;
  996. }
  997. ZEND_FETCH_RESOURCE(pg_result, pgsql_result_handle *, result, -1, "PostgreSQL result", le_result);
  998. pgsql_result = pg_result->result;
  999. convert_to_long_ex(field);
  1000. if (Z_LVAL_PP(field) < 0 || Z_LVAL_PP(field) >= PQnfields(pgsql_result)) {
  1001. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Bad field offset specified.");
  1002. RETURN_FALSE;
  1003. }
  1004. switch (entry_type) {
  1005. case PHP_PG_FIELD_NAME:
  1006. Z_STRVAL_P(return_value) = PQfname(pgsql_result, Z_LVAL_PP(field));
  1007. Z_STRLEN_P(return_value) = strlen(Z_STRVAL_P(return_value));
  1008. Z_STRVAL_P(return_value) = estrndup(Z_STRVAL_P(return_value),Z_STRLEN_P(return_value));
  1009. Z_TYPE_P(return_value) = IS_STRING;
  1010. break;
  1011. case PHP_PG_FIELD_SIZE:
  1012. Z_LVAL_P(return_value) = PQfsize(pgsql_result, Z_LVAL_PP(field));
  1013. Z_TYPE_P(return_value) = IS_LONG;
  1014. break;
  1015. case PHP_PG_FIELD_TYPE:
  1016. Z_STRVAL_P(return_value) = get_field_name(pg_result->conn, PQftype(pgsql_result, Z_LVAL_PP(field)), &EG(regular_list) TSRMLS_CC);
  1017. Z_STRLEN_P(return_value) = strlen(Z_STRVAL_P(return_value));
  1018. Z_TYPE_P(return_value) = IS_STRING;
  1019. break;
  1020. default:
  1021. RETURN_FALSE;
  1022. }
  1023. }
  1024. /* }}} */
  1025. /* {{{ proto string pg_field_name(resource result, int field_number)
  1026. Returns the name of the field */
  1027. PHP_FUNCTION(pg_field_name)
  1028. {
  1029. php_pgsql_get_field_info(INTERNAL_FUNCTION_PARAM_PASSTHRU,PHP_PG_FIELD_NAME);
  1030. }
  1031. /* }}} */
  1032. /* {{{ proto int pg_field_size(resource result, int field_number)
  1033. Returns the internal size of the field */
  1034. PHP_FUNCTION(pg_field_size)
  1035. {
  1036. php_pgsql_get_field_info(INTERNAL_FUNCTION_PARAM_PASSTHRU,PHP_PG_FIELD_SIZE);
  1037. }
  1038. /* }}} */
  1039. /* {{{ proto string pg_field_type(resource result, int field_number)
  1040. Returns the type name for the given field */
  1041. PHP_FUNCTION(pg_field_type)
  1042. {
  1043. php_pgsql_get_field_info(INTERNAL_FUNCTION_PARAM_PASSTHRU,PHP_PG_FIELD_TYPE);
  1044. }
  1045. /* }}} */
  1046. /* {{{ proto int pg_field_num(resource result, string field_name)
  1047. Returns the field number of the named field */
  1048. PHP_FUNCTION(pg_field_num)
  1049. {
  1050. zval **result, **field;
  1051. PGresult *pgsql_result;
  1052. pgsql_result_handle *pg_result;
  1053. if (ZEND_NUM_ARGS() != 2 || zend_get_parameters_ex(2, &result, &field)==FAILURE) {
  1054. WRONG_PARAM_COUNT;
  1055. }
  1056. ZEND_FETCH_RESOURCE(pg_result, pgsql_result_handle *, result, -1, "PostgreSQL result", le_result);
  1057. pgsql_result = pg_result->result;
  1058. convert_to_string_ex(field);
  1059. Z_LVAL_P(return_value) = PQfnumber(pgsql_result, Z_STRVAL_PP(field));
  1060. Z_TYPE_P(return_value) = IS_LONG;
  1061. }
  1062. /* }}} */
  1063. /* {{{ proto mixed pg_fetch_result(resource result, [int row_number,] mixed field_name)
  1064. Returns values from a result identifier */
  1065. PHP_FUNCTION(pg_fetch_result)
  1066. {
  1067. zval **result, **row, **field=NULL;
  1068. PGresult *pgsql_result;
  1069. pgsql_result_handle *pg_result;
  1070. int field_offset, pgsql_row;
  1071. if ((ZEND_NUM_ARGS() != 3 || zend_get_parameters_ex(3, &result, &row, &field)==FAILURE) &&
  1072. (ZEND_NUM_ARGS() != 2 || zend_get_parameters_ex(2, &result, &field)==FAILURE)) {
  1073. WRONG_PARAM_COUNT;
  1074. }
  1075. ZEND_FETCH_RESOURCE(pg_result, pgsql_result_handle *, result, -1, "PostgreSQL result", le_result);
  1076. pgsql_result = pg_result->result;
  1077. if (ZEND_NUM_ARGS() == 2) {
  1078. if (pg_result->row < 0)
  1079. pg_result->row = 0;
  1080. pgsql_row = pg_result->row;
  1081. if (pgsql_row >= PQntuples(pgsql_result)) {
  1082. RETURN_FALSE;
  1083. }
  1084. } else {
  1085. convert_to_long_ex(row);
  1086. pgsql_row = Z_LVAL_PP(row);
  1087. if (pgsql_row < 0 || pgsql_row >= PQntuples(pgsql_result)) {
  1088. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to jump to row %d on PostgreSQL result index %d.", Z_LVAL_PP(row), Z_LVAL_PP(result));
  1089. RETURN_FALSE;
  1090. }
  1091. }
  1092. switch(Z_TYPE_PP(field)) {
  1093. case IS_STRING:
  1094. field_offset = PQfnumber(pgsql_result, Z_STRVAL_PP(field));
  1095. break;
  1096. default:
  1097. convert_to_long_ex(field);
  1098. field_offset = Z_LVAL_PP(field);
  1099. break;
  1100. }
  1101. if (field_offset<0 || field_offset>=PQnfields(pgsql_result)) {
  1102. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Bad column offset specified.");
  1103. RETURN_FALSE;
  1104. }
  1105. if (PQgetisnull(pgsql_result, pgsql_row, field_offset)) {
  1106. Z_TYPE_P(return_value) = IS_NULL;
  1107. } else {
  1108. Z_STRVAL_P(return_value) = PQgetvalue(pgsql_result, pgsql_row, field_offset);
  1109. Z_STRLEN_P(return_value) = (Z_STRVAL_P(return_value) ? strlen(Z_STRVAL_P(return_value)) : 0);
  1110. Z_STRVAL_P(return_value) = safe_estrndup(Z_STRVAL_P(return_value),Z_STRLEN_P(return_value));
  1111. Z_TYPE_P(return_value) = IS_STRING;
  1112. }
  1113. }
  1114. /* }}} */
  1115. /* {{{ void php_pgsql_fetch_hash */
  1116. static void php_pgsql_fetch_hash(INTERNAL_FUNCTION_PARAMETERS, int result_type)
  1117. {
  1118. zval **result, **row, **arg3;
  1119. PGresult *pgsql_result;
  1120. pgsql_result_handle *pg_result;
  1121. int i, num_fields, pgsql_row;
  1122. char *element, *field_name;
  1123. uint element_len;
  1124. switch (ZEND_NUM_ARGS()) {
  1125. case 1: /* pg_fetch_*(result) */
  1126. if (zend_get_parameters_ex(1, &result) == FAILURE) {
  1127. RETURN_FALSE;
  1128. }
  1129. break;
  1130. case 2: /* pg_fetch_*(result, row) */
  1131. if (zend_get_parameters_ex(2, &result, &row) == FAILURE) {
  1132. RETURN_FALSE;
  1133. }
  1134. break;
  1135. case 3: /* pg_fetch_*(result, row, result_type) */
  1136. if (zend_get_parameters_ex(3, &result, &row, &arg3) == FAILURE) {
  1137. RETURN_FALSE;
  1138. }
  1139. convert_to_long_ex(arg3);
  1140. result_type = Z_LVAL_PP(arg3);
  1141. break;
  1142. default:
  1143. WRONG_PARAM_COUNT;
  1144. break;
  1145. }
  1146. if (!(result_type & PGSQL_BOTH)) {
  1147. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid result type.");
  1148. RETURN_FALSE;
  1149. }
  1150. ZEND_FETCH_RESOURCE(pg_result, pgsql_result_handle *, result, -1, "PostgreSQL result", le_result);
  1151. pgsql_result = pg_result->result;
  1152. if (ZEND_NUM_ARGS() == 1) {
  1153. pg_result->row++;
  1154. pgsql_row = pg_result->row;
  1155. if (pgsql_row < 0 || pgsql_row >= PQntuples(pgsql_result)) {
  1156. RETURN_FALSE;
  1157. }
  1158. } else {
  1159. if (Z_TYPE_PP(row) != IS_NULL) {
  1160. convert_to_long_ex(row);
  1161. pgsql_row = Z_LVAL_PP(row);
  1162. pg_result->row = pgsql_row;
  1163. if (pgsql_row < 0 || pgsql_row >= PQntuples(pgsql_result)) {
  1164. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to jump to row %d on PostgreSQL result index %d.", Z_LVAL_PP(row), Z_LVAL_PP(result));
  1165. RETURN_FALSE;
  1166. }
  1167. } else {
  1168. /* If 2nd param is NULL, use internal row counter to access next row */
  1169. pg_result->row++;
  1170. pgsql_row = pg_result->row;
  1171. if (pgsql_row < 0 || pgsql_row >= PQntuples(pgsql_result)) {
  1172. RETURN_FALSE;
  1173. }
  1174. }
  1175. }
  1176. array_init(return_value);
  1177. for (i = 0, num_fields = PQnfields(pgsql_result); i < num_fields; i++) {
  1178. if (PQgetisnull(pgsql_result, pgsql_row, i)) {
  1179. if (result_type & PGSQL_NUM) {
  1180. add_index_null(return_value, i);
  1181. }
  1182. if (result_type & PGSQL_ASSOC) {
  1183. field_name = PQfname(pgsql_result, i);
  1184. add_assoc_null(return_value, field_name);
  1185. }
  1186. } else {
  1187. element = PQgetvalue(pgsql_result, pgsql_row, i);
  1188. element_len = (element ? strlen(element) : 0);
  1189. if (element) {
  1190. char *data;
  1191. int data_len;
  1192. int should_copy=0;
  1193. if (PG(magic_quotes_runtime)) {
  1194. data = php_addslashes(element, element_len, &data_len, 0 TSRMLS_CC);
  1195. } else {
  1196. data = safe_estrndup(element, element_len);
  1197. data_len = element_len;
  1198. }
  1199. if (result_type & PGSQL_NUM) {
  1200. add_index_stringl(return_value, i, data, data_len, should_copy);
  1201. should_copy=1;
  1202. }
  1203. if (result_type & PGSQL_ASSOC) {
  1204. field_name = PQfname(pgsql_result, i);
  1205. add_assoc_stringl(return_value, field_name, data, data_len, should_copy);
  1206. }
  1207. }
  1208. }
  1209. }
  1210. }
  1211. /* }}} */
  1212. /* {{{ proto array pg_fetch_row(resource result [, int row [, int result_type]])
  1213. Get a row as an enumerated array */
  1214. PHP_FUNCTION(pg_fetch_row)
  1215. {
  1216. php_pgsql_fetch_hash(INTERNAL_FUNCTION_PARAM_PASSTHRU, PGSQL_NUM);
  1217. }
  1218. /* }}} */
  1219. /* {{{ proto array pg_fetch_assoc(resource result [, int row])
  1220. Fetch a row as an assoc array */
  1221. PHP_FUNCTION(pg_fetch_assoc)
  1222. {
  1223. /* pg_fetch_assoc() is added from PHP 4.3.0. It should raise error, when
  1224. there is 3rd parameter */
  1225. if (ZEND_NUM_ARGS() > 2)
  1226. WRONG_PARAM_COUNT;
  1227. php_pgsql_fetch_hash(INTERNAL_FUNCTION_PARAM_PASSTHRU, PGSQL_ASSOC);
  1228. }
  1229. /* }}} */
  1230. /* {{{ proto array pg_fetch_array(resource result [, int row [, int result_type]])
  1231. Fetch a row as an array */
  1232. PHP_FUNCTION(pg_fetch_array)
  1233. {
  1234. php_pgsql_fetch_hash(INTERNAL_FUNCTION_PARAM_PASSTHRU, PGSQL_BOTH);
  1235. }
  1236. /* }}} */
  1237. /* {{{ proto object pg_fetch_object(resource result [, int row])
  1238. Fetch a row as an object */
  1239. PHP_FUNCTION(pg_fetch_object)
  1240. {
  1241. /* pg_fetch_object() allowed result_type used to be. 3rd parameter
  1242. must be allowed for compatibility */
  1243. php_pgsql_fetch_hash(INTERNAL_FUNCTION_PARAM_PASSTHRU, PGSQL_ASSOC);
  1244. if (Z_TYPE_P(return_value)==IS_ARRAY) {
  1245. object_and_properties_init(return_value, ZEND_STANDARD_CLASS_DEF_PTR, Z_ARRVAL_P(return_value));
  1246. }
  1247. }
  1248. /* }}} */
  1249. /* {{{ proto array pg_fetch_all(resource result)
  1250. Fetch all rows into array */
  1251. PHP_FUNCTION(pg_fetch_all)
  1252. {
  1253. zval *result;
  1254. PGresult *pgsql_result;
  1255. pgsql_result_handle *pg_result;
  1256. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r",
  1257. &result) == FAILURE) {
  1258. return;
  1259. }
  1260. ZEND_FETCH_RESOURCE(pg_result, pgsql_result_handle *, &result, -1, "PostgreSQL result", le_result);
  1261. pgsql_result = pg_result->result;
  1262. array_init(return_value);
  1263. if (php_pgsql_result2array(pgsql_result, return_value TSRMLS_CC) == FAILURE) {
  1264. zval_dtor(return_value);
  1265. RETURN_FALSE;
  1266. }
  1267. }
  1268. /* }}} */
  1269. /* {{{ proto mixed pg_result_seek(resource result, int offset)
  1270. Set internal row offset */
  1271. PHP_FUNCTION(pg_result_seek)
  1272. {
  1273. zval *result;
  1274. int row;
  1275. pgsql_result_handle *pg_result;
  1276. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r|l",
  1277. &result, &row) == FAILURE) {
  1278. return;
  1279. }
  1280. ZEND_FETCH_RESOURCE(pg_result, pgsql_result_handle *, &result, -1, "PostgreSQL result", le_result);
  1281. /* Let see if we are better to have another function for this */
  1282. /* if offset is omitted, return current position */
  1283. /* if (ZEND_NUM_ARGS() == 1) */
  1284. /* RETURN_LONG(pg_result->row); */
  1285. if (row < 0 || row >= PQntuples(pg_result->result))
  1286. RETURN_FALSE;
  1287. /* seek to offset */
  1288. pg_result->row = row;
  1289. RETURN_TRUE;
  1290. }
  1291. /* }}} */
  1292. #define PHP_PG_DATA_LENGTH 1
  1293. #define PHP_PG_DATA_ISNULL 2
  1294. /* {{{ php_pgsql_data_info
  1295. */
  1296. static void php_pgsql_data_info(INTERNAL_FUNCTION_PARAMETERS, int entry_type)
  1297. {
  1298. zval **result, **row, **field;
  1299. PGresult *pgsql_result;
  1300. pgsql_result_handle *pg_result;
  1301. int field_offset, pgsql_row;
  1302. if ((ZEND_NUM_ARGS() != 3 || zend_get_parameters_ex(3, &result, &row, &field)==FAILURE) &&
  1303. (ZEND_NUM_ARGS() != 2 || zend_get_parameters_ex(2, &result, &field)==FAILURE)) {
  1304. WRONG_PARAM_COUNT;
  1305. }
  1306. ZEND_FETCH_RESOURCE(pg_result, pgsql_result_handle *, result, -1, "PostgreSQL result", le_result);
  1307. pgsql_result = pg_result->result;
  1308. if (ZEND_NUM_ARGS() == 2) {
  1309. if (pg_result->row < 0)
  1310. pg_result->row = 0;
  1311. pgsql_row = pg_result->row;
  1312. if (pgsql_row < 0 || pgsql_row >= PQntuples(pgsql_result)) {
  1313. RETURN_FALSE;
  1314. }
  1315. } else {
  1316. convert_to_long_ex(row);
  1317. pgsql_row = Z_LVAL_PP(row);
  1318. if (pgsql_row < 0 || pgsql_row >= PQntuples(pgsql_result)) {
  1319. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to jump to row %d on PostgreSQL result index %d.", Z_LVAL_PP(row), Z_LVAL_PP(result));
  1320. RETURN_FALSE;
  1321. }
  1322. }
  1323. switch(Z_TYPE_PP(field)) {
  1324. case IS_STRING:
  1325. convert_to_string_ex(field);
  1326. field_offset = PQfnumber(pgsql_result, Z_STRVAL_PP(field));
  1327. break;
  1328. default:
  1329. convert_to_long_ex(field);
  1330. field_offset = Z_LVAL_PP(field);
  1331. break;
  1332. }
  1333. if (field_offset < 0 || field_offset >= PQnfields(pgsql_result)) {
  1334. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Bad column offset specified.");
  1335. RETURN_FALSE;
  1336. }
  1337. switch (entry_type) {
  1338. case PHP_PG_DATA_LENGTH:
  1339. Z_LVAL_P(return_value) = PQgetlength(pgsql_result, pgsql_row, field_offset);
  1340. break;
  1341. case PHP_PG_DATA_ISNULL:
  1342. Z_LVAL_P(return_value) = PQgetisnull(pgsql_result, pgsql_row, field_offset);
  1343. break;
  1344. }
  1345. Z_TYPE_P(return_value) = IS_LONG;
  1346. }
  1347. /* }}} */
  1348. /* {{{ proto int pg_field_prtlen(resource result, [int row,] mixed field_name_or_number)
  1349. Returns the printed length */
  1350. PHP_FUNCTION(pg_field_prtlen)
  1351. {
  1352. php_pgsql_data_info(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_PG_DATA_LENGTH);
  1353. }
  1354. /* }}} */
  1355. /* {{{ proto int pg_field_is_null(resource result, [int row,] mixed field_name_or_number)
  1356. Test if a field is NULL */
  1357. PHP_FUNCTION(pg_field_is_null)
  1358. {
  1359. php_pgsql_data_info(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_PG_DATA_ISNULL);
  1360. }
  1361. /* }}} */
  1362. /* {{{ proto bool pg_free_result(resource result)
  1363. Free result memory */
  1364. PHP_FUNCTION(pg_free_result)
  1365. {
  1366. zval **result;
  1367. pgsql_result_handle *pg_result;
  1368. if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &result)==FAILURE) {
  1369. WRONG_PARAM_COUNT;
  1370. }
  1371. ZEND_FETCH_RESOURCE(pg_result, pgsql_result_handle *, result, -1, "PostgreSQL result", le_result);
  1372. if (Z_LVAL_PP(result) == 0) {
  1373. RETURN_FALSE;
  1374. }
  1375. zend_list_delete(Z_LVAL_PP(result));
  1376. RETURN_TRUE;
  1377. }
  1378. /* }}} */
  1379. /* {{{ proto string pg_last_oid(resource result)
  1380. Returns the last object identifier */
  1381. PHP_FUNCTION(pg_last_oid)
  1382. {
  1383. zval **result;
  1384. PGresult *pgsql_result;
  1385. pgsql_result_handle *pg_result;
  1386. Oid oid;
  1387. if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &result)==FAILURE) {
  1388. WRONG_PARAM_COUNT;
  1389. }
  1390. ZEND_FETCH_RESOURCE(pg_result, pgsql_result_handle *, result, -1, "PostgreSQL result", le_result);
  1391. pgsql_result = pg_result->result;
  1392. #ifdef HAVE_PQOIDVALUE
  1393. oid = PQoidValue(pgsql_result);
  1394. if (oid == InvalidOid) {
  1395. RETURN_FALSE;
  1396. }
  1397. PGSQL_RETURN_OID(oid);
  1398. #else
  1399. Z_STRVAL_P(return_value) = (char *) PQoidStatus(pgsql_result);
  1400. if (Z_STRVAL_P(return_value)) {
  1401. RETURN_STRING(Z_STRVAL_P(return_value), 1);
  1402. }
  1403. RETURN_STRING(empty_string, 0);
  1404. #endif
  1405. }
  1406. /* }}} */
  1407. /* {{{ proto bool pg_trace(string filename [, string mode [, resource connection]])
  1408. Enable tracing a PostgreSQL connection */
  1409. PHP_FUNCTION(pg_trace)
  1410. {
  1411. zval **z_filename, **z_mode, **z_pgsql_link = NULL;
  1412. int id = -1;
  1413. PGconn *pgsql;
  1414. char *mode = "w";
  1415. FILE *fp = NULL;
  1416. php_stream *stream;
  1417. id = PGG(default_link);
  1418. switch (ZEND_NUM_ARGS()) {
  1419. case 1:
  1420. if (zend_get_parameters_ex(1, &z_filename)==FAILURE) {
  1421. RETURN_FALSE;
  1422. }
  1423. CHECK_DEFAULT_LINK(id);
  1424. break;
  1425. case 2:
  1426. if (zend_get_parameters_ex(2, &z_filename, &z_mode)==FAILURE) {
  1427. RETURN_FALSE;
  1428. }
  1429. CHECK_DEFAULT_LINK(id);
  1430. convert_to_string_ex(z_mode);
  1431. mode = Z_STRVAL_PP(z_mode);
  1432. break;
  1433. case 3:
  1434. if (zend_get_parameters_ex(3, &z_filename, &z_mode, &z_pgsql_link)==FAILURE) {
  1435. RETURN_FALSE;
  1436. }
  1437. convert_to_string_ex(z_mode);
  1438. mode = Z_STRVAL_PP(z_mode);
  1439. break;
  1440. default:
  1441. ZEND_WRONG_PARAM_COUNT();
  1442. break;
  1443. }
  1444. ZEND_FETCH_RESOURCE2(pgsql, PGconn *, z_pgsql_link, id, "PostgreSQL link", le_link, le_plink);
  1445. convert_to_string_ex(z_filename);
  1446. stream = php_stream_open_wrapper(Z_STRVAL_PP(z_filename), mode, ENFORCE_SAFE_MODE|REPORT_ERRORS, NULL);
  1447. if (!stream) {
  1448. RETURN_FALSE;
  1449. }
  1450. if (FAILURE == php_stream_cast(stream, PHP_STREAM_AS_STDIO, (void**)fp, REPORT_ERRORS)) {
  1451. php_stream_close(stream);
  1452. RETURN_FALSE;
  1453. }
  1454. php_stream_auto_cleanup(stream);
  1455. PQtrace(pgsql, fp);
  1456. RETURN_TRUE;
  1457. }
  1458. /* }}} */
  1459. /* {{{ proto bool pg_untrace([resource connection])
  1460. Disable tracing of a PostgreSQL connection */
  1461. PHP_FUNCTION(pg_untrace)
  1462. {
  1463. zval **pgsql_link = NULL;
  1464. int id = -1;
  1465. PGconn *pgsql;
  1466. switch (ZEND_NUM_ARGS()) {
  1467. case 0:
  1468. id = PGG(default_link);
  1469. CHECK_DEFAULT_LINK(id);
  1470. break;
  1471. case 1:
  1472. if (zend_get_parameters_ex(1, &pgsql_link)==FAILURE) {
  1473. RETURN_FALSE;
  1474. }
  1475. break;
  1476. default:
  1477. ZEND_WRONG_PARAM_COUNT();
  1478. break;
  1479. }
  1480. ZEND_FETCH_RESOURCE2(pgsql, PGconn *, pgsql_link, id, "PostgreSQL link", le_link, le_plink);
  1481. PQuntrace(pgsql);
  1482. RETURN_TRUE;
  1483. }
  1484. /* }}} */
  1485. /* {{{ proto int pg_lo_create([resource connection])
  1486. Create a large object */
  1487. PHP_FUNCTION(pg_lo_create)
  1488. {
  1489. zval **pgsql_link = NULL;
  1490. PGconn *pgsql;
  1491. Oid pgsql_oid;
  1492. int id = -1;
  1493. switch(ZEND_NUM_ARGS()) {
  1494. case 0:
  1495. id = PGG(default_link);
  1496. CHECK_DEFAULT_LINK(id);
  1497. break;
  1498. case 1:
  1499. if (zend_get_parameters_ex(1, &pgsql_link)==FAILURE) {
  1500. RETURN_FALSE;
  1501. }
  1502. break;
  1503. default:
  1504. WRONG_PARAM_COUNT;
  1505. break;
  1506. }
  1507. ZEND_FETCH_RESOURCE2(pgsql, PGconn *, pgsql_link, id, "PostgreSQL link", le_link, le_plink);
  1508. /* NOTE: Archive modes not supported until I get some more data. Don't think anybody's
  1509. using it anyway. I believe it's also somehow related to the 'time travel' feature of
  1510. PostgreSQL, that's on the list of features to be removed... Create modes not supported.
  1511. What's the use of an object that can be only written to, but not read from, and vice
  1512. versa? Beats me... And the access type (r/w) must be specified again when opening
  1513. the object, probably (?) overrides this. (Jouni)
  1514. */
  1515. if ((pgsql_oid = lo_creat(pgsql, INV_READ|INV_WRITE)) == InvalidOid) {
  1516. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to create PostgreSQL large object.");
  1517. RETURN_FALSE;
  1518. }
  1519. PGSQL_RETURN_OID(pgsql_oid);
  1520. }
  1521. /* }}} */
  1522. /* {{{ proto bool pg_lo_unlink([resource connection,] string large_object_oid)
  1523. Delete a large object */
  1524. PHP_FUNCTION(pg_lo_unlink)
  1525. {
  1526. zval *pgsql_link = NULL;
  1527. long oid_long;
  1528. char *oid_string, *end_ptr;
  1529. size_t oid_strlen;
  1530. PGconn *pgsql;
  1531. Oid oid;
  1532. int id = -1;
  1533. int argc = ZEND_NUM_ARGS();
  1534. /* accept string type since Oid type is unsigned int */
  1535. if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, argc TSRMLS_CC,
  1536. "rs", &pgsql_link, &oid_string, &oid_strlen) == SUCCESS) {
  1537. oid = (Oid)strtoul(oid_string, &end_ptr, 10);
  1538. if ((oid_string+oid_strlen) != end_ptr) {
  1539. /* wrong integer format */
  1540. php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Wrong OID value passed.");
  1541. RETURN_FALSE;
  1542. }
  1543. }
  1544. else if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, argc TSRMLS_CC,
  1545. "rl", &pgsql_link, &oid_long) == SUCCESS) {
  1546. if (oid_long <= InvalidOid) {
  1547. php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Invalid OID specified.");
  1548. RETURN_FALSE;
  1549. }
  1550. oid = (Oid)oid_long;
  1551. }
  1552. else if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, argc TSRMLS_CC,
  1553. "s", &oid_string, &oid_strlen) == SUCCESS) {
  1554. oid = (Oid)strtoul(oid_string, &end_ptr, 10);
  1555. if ((oid_string+oid_strlen) != end_ptr) {
  1556. /* wrong integer format */
  1557. php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Wrong OID value passed.");
  1558. RETURN_FALSE;
  1559. }
  1560. id = PGG(default_link);
  1561. CHECK_DEFAULT_LINK(id);
  1562. }
  1563. else if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, argc TSRMLS_CC,
  1564. "l", &oid_long) == SUCCESS) {
  1565. if (oid_long <= InvalidOid) {
  1566. php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Invalid OID is specified.");
  1567. RETURN_FALSE;
  1568. }
  1569. oid = (Oid)oid_long;
  1570. id = PGG(default_link);
  1571. CHECK_DEFAULT_LINK(id);
  1572. }
  1573. else {
  1574. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Requires 1 or 2 arguments.");
  1575. RETURN_FALSE;
  1576. }
  1577. ZEND_FETCH_RESOURCE2(pgsql, PGconn *, &pgsql_link, id, "PostgreSQL link", le_link, le_plink);
  1578. if (lo_unlink(pgsql, oid) == -1) {
  1579. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to delete PostgreSQL large object %u.", oid);
  1580. RETURN_FALSE;
  1581. }
  1582. RETURN_TRUE;
  1583. }
  1584. /* }}} */
  1585. /* {{{ proto resource pg_lo_open([resource connection,] int large_object_oid, string mode)
  1586. Open a large object and return fd */
  1587. PHP_FUNCTION(pg_lo_open)
  1588. {
  1589. zval *pgsql_link = NULL;
  1590. long oid_long;
  1591. char *oid_string, *end_ptr, *mode_string;
  1592. size_t oid_strlen, mode_strlen;
  1593. PGconn *pgsql;
  1594. Oid oid;
  1595. int id = -1, pgsql_mode=0, pgsql_lofd;
  1596. int create=0;
  1597. pgLofp *pgsql_lofp;
  1598. int argc = ZEND_NUM_ARGS();
  1599. /* accept string type since Oid is unsigned int */
  1600. if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, argc TSRMLS_CC,
  1601. "rss", &pgsql_link, &oid_string, &oid_strlen, &mode_string, &mode_strlen) == SUCCESS) {
  1602. oid = (Oid)strtoul(oid_string, &end_ptr, 10);
  1603. if ((oid_string+oid_strlen) != end_ptr) {
  1604. /* wrong integer format */
  1605. php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Wrong OID value passed.");
  1606. RETURN_FALSE;
  1607. }
  1608. }
  1609. else if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, argc TSRMLS_CC,
  1610. "rls", &pgsql_link, &oid_long, &mode_string, &mode_strlen) == SUCCESS) {
  1611. if (oid_long <= InvalidOid) {
  1612. php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Invalid OID specified.");
  1613. RETURN_FALSE;
  1614. }
  1615. oid = (Oid)oid_long;
  1616. }
  1617. else if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, argc TSRMLS_CC,
  1618. "ss", &oid_string, &oid_strlen, &mode_string, &mode_strlen) == SUCCESS) {
  1619. oid = (Oid)strtoul(oid_string, &end_ptr, 10);
  1620. if ((oid_string+oid_strlen) != end_ptr) {
  1621. /* wrong integer format */
  1622. php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Wrong OID value passed.");
  1623. RETURN_FALSE;
  1624. }
  1625. id = PGG(default_link);
  1626. CHECK_DEFAULT_LINK(id);
  1627. }
  1628. else if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, argc TSRMLS_CC,
  1629. "ls", &oid_long, &mode_string, &mode_strlen) == SUCCESS) {
  1630. if (oid_long <= InvalidOid) {
  1631. php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Invalid OID specified.");
  1632. RETURN_FALSE;
  1633. }
  1634. oid = (Oid)oid_long;
  1635. id = PGG(default_link);
  1636. CHECK_DEFAULT_LINK(id);
  1637. }
  1638. else {
  1639. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Requires 1 or 2 arguments.");
  1640. RETURN_FALSE;
  1641. }
  1642. ZEND_FETCH_RESOURCE2(pgsql, PGconn *, &pgsql_link, id, "PostgreSQL link", le_link, le_plink);
  1643. /* r/w/+ is little bit more PHP-like than INV_READ/INV_WRITE and a lot of
  1644. faster to type. Unfortunately, doesn't behave the same way as fopen()...
  1645. (Jouni)
  1646. */
  1647. if (strchr(mode_string, 'r') == mode_string) {
  1648. pgsql_mode |= INV_READ;
  1649. if (strchr(mode_string, '+') == mode_string+1) {
  1650. pgsql_mode |= INV_WRITE;
  1651. }
  1652. }
  1653. if (strchr(mode_string, 'w') == mode_string) {
  1654. pgsql_mode |= INV_WRITE;
  1655. create = 1;
  1656. if (strchr(mode_string, '+') == mode_string+1) {
  1657. pgsql_mode |= INV_READ;
  1658. }
  1659. }
  1660. pgsql_lofp = (pgLofp *) emalloc(sizeof(pgLofp));
  1661. if ((pgsql_lofd = lo_open(pgsql, oid, pgsql_mode)) == -1) {
  1662. if (create) {
  1663. if ((oid = lo_creat(pgsql, INV_READ|INV_WRITE)) == 0) {
  1664. efree(pgsql_lofp);
  1665. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to create PostgreSQL large object.");
  1666. RETURN_FALSE;
  1667. } else {
  1668. if ((pgsql_lofd = lo_open(pgsql, oid, pgsql_mode)) == -1) {
  1669. if (lo_unlink(pgsql, oid) == -1) {
  1670. efree(pgsql_lofp);
  1671. 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.");
  1672. RETURN_FALSE;
  1673. }
  1674. efree(pgsql_lofp);
  1675. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to open PostgreSQL large object.");
  1676. RETURN_FALSE;
  1677. } else {
  1678. pgsql_lofp->conn = pgsql;
  1679. pgsql_lofp->lofd = pgsql_lofd;
  1680. Z_LVAL_P(return_value) = zend_list_insert(pgsql_lofp, le_lofp);
  1681. Z_TYPE_P(return_value) = IS_LONG;
  1682. }
  1683. }
  1684. } else {
  1685. efree(pgsql_lofp);
  1686. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to open PostgreSQL large object.");
  1687. RETURN_FALSE;
  1688. }
  1689. } else {
  1690. pgsql_lofp->conn = pgsql;
  1691. pgsql_lofp->lofd = pgsql_lofd;
  1692. ZEND_REGISTER_RESOURCE(return_value, pgsql_lofp, le_lofp);
  1693. }
  1694. }
  1695. /* }}} */
  1696. /* {{{ proto bool pg_lo_close(resource large_object)
  1697. Close a large object */
  1698. PHP_FUNCTION(pg_lo_close)
  1699. {
  1700. zval **pgsql_lofp;
  1701. pgLofp *pgsql;
  1702. switch(ZEND_NUM_ARGS()) {
  1703. case 1:
  1704. if (zend_get_parameters_ex(1, &pgsql_lofp)==FAILURE) {
  1705. RETURN_FALSE;
  1706. }
  1707. break;
  1708. default:
  1709. WRONG_PARAM_COUNT;
  1710. break;
  1711. }
  1712. ZEND_FETCH_RESOURCE(pgsql, pgLofp *, pgsql_lofp, -1, "PostgreSQL large object", le_lofp);
  1713. if (lo_close((PGconn *)pgsql->conn, pgsql->lofd) < 0) {
  1714. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to close PostgreSQL large object descriptor %d.", pgsql->lofd);
  1715. RETVAL_FALSE;
  1716. } else {
  1717. RETVAL_TRUE;
  1718. }
  1719. zend_list_delete(Z_RESVAL_PP(pgsql_lofp));
  1720. return;
  1721. }
  1722. /* }}} */
  1723. #define PGSQL_LO_READ_BUF_SIZE 8192
  1724. /* {{{ proto string pg_lo_read(resource large_object [, int len])
  1725. Read a large object */
  1726. PHP_FUNCTION(pg_lo_read)
  1727. {
  1728. zval **pgsql_id, **len;
  1729. int buf_len = PGSQL_LO_READ_BUF_SIZE, nbytes;
  1730. char *buf;
  1731. pgLofp *pgsql;
  1732. if (ZEND_NUM_ARGS() < 1 || ZEND_NUM_ARGS() > 2 ||
  1733. zend_get_parameters_ex(ZEND_NUM_ARGS(), &pgsql_id, &len) == FAILURE) {
  1734. WRONG_PARAM_COUNT;
  1735. }
  1736. ZEND_FETCH_RESOURCE(pgsql, pgLofp *, pgsql_id, -1, "PostgreSQL large object", le_lofp);
  1737. if (ZEND_NUM_ARGS() > 1) {
  1738. convert_to_long_ex(len);
  1739. buf_len = Z_LVAL_PP(len);
  1740. }
  1741. buf = (char *) emalloc(sizeof(char)*(buf_len+1));
  1742. if ((nbytes = lo_read((PGconn *)pgsql->conn, pgsql->lofd, buf, buf_len))<0) {
  1743. efree(buf);
  1744. RETURN_FALSE;
  1745. }
  1746. buf[nbytes] = '\0';
  1747. RETURN_STRINGL(buf, nbytes, 0);
  1748. }
  1749. /* }}} */
  1750. /* {{{ proto int pg_lo_write(resource large_object, string buf [, int len])
  1751. Write a large object */
  1752. PHP_FUNCTION(pg_lo_write)
  1753. {
  1754. zval **pgsql_id, **str, **z_len;
  1755. int nbytes;
  1756. int len;
  1757. pgLofp *pgsql;
  1758. int argc = ZEND_NUM_ARGS();
  1759. if (argc < 2 || argc > 3 ||
  1760. zend_get_parameters_ex(argc, &pgsql_id, &str, &z_len) == FAILURE) {
  1761. WRONG_PARAM_COUNT;
  1762. }
  1763. convert_to_string_ex(str);
  1764. if (argc > 2) {
  1765. convert_to_long_ex(z_len);
  1766. if (Z_LVAL_PP(z_len) > Z_STRLEN_PP(str)) {
  1767. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Cannot write more than buffer size %d. Tried to write %d.", Z_LVAL_PP(str), Z_LVAL_PP(z_len));
  1768. RETURN_FALSE;
  1769. }
  1770. if (Z_LVAL_PP(z_len) < 0) {
  1771. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Buffer size must be larger than 0, but %d was specified.", Z_LVAL_PP(str), Z_LVAL_PP(z_len));
  1772. RETURN_FALSE;
  1773. }
  1774. len = Z_LVAL_PP(z_len);
  1775. }
  1776. else {
  1777. len = Z_STRLEN_PP(str);
  1778. }
  1779. ZEND_FETCH_RESOURCE(pgsql, pgLofp *, pgsql_id, -1, "PostgreSQL large object", le_lofp);
  1780. if ((nbytes = lo_write((PGconn *)pgsql->conn, pgsql->lofd, Z_STRVAL_PP(str), len)) == -1) {
  1781. RETURN_FALSE;
  1782. }
  1783. RETURN_LONG(nbytes);
  1784. }
  1785. /* }}} */
  1786. /* {{{ proto int pg_lo_read_all(resource large_object)
  1787. Read a large object and send straight to browser */
  1788. PHP_FUNCTION(pg_lo_read_all)
  1789. {
  1790. zval **pgsql_id;
  1791. int tbytes;
  1792. volatile int nbytes;
  1793. char buf[PGSQL_LO_READ_BUF_SIZE];
  1794. pgLofp *pgsql;
  1795. switch(ZEND_NUM_ARGS()) {
  1796. case 1:
  1797. if (zend_get_parameters_ex(1, &pgsql_id)==FAILURE) {
  1798. RETURN_FALSE;
  1799. }
  1800. break;
  1801. default:
  1802. WRONG_PARAM_COUNT;
  1803. break;
  1804. }
  1805. ZEND_FETCH_RESOURCE(pgsql, pgLofp *, pgsql_id, -1, "PostgreSQL large object", le_lofp);
  1806. tbytes = 0;
  1807. while ((nbytes = lo_read((PGconn *)pgsql->conn, pgsql->lofd, buf, PGSQL_LO_READ_BUF_SIZE))>0) {
  1808. php_body_write(buf, nbytes TSRMLS_CC);
  1809. tbytes += nbytes;
  1810. }
  1811. RETURN_LONG(tbytes);
  1812. }
  1813. /* }}} */
  1814. /* {{{ proto int pg_lo_import([resource connection, ] string filename)
  1815. Import large object direct from filesystem */
  1816. PHP_FUNCTION(pg_lo_import)
  1817. {
  1818. zval *pgsql_link = NULL;
  1819. char *file_in;
  1820. int id = -1, name_len;
  1821. int argc = ZEND_NUM_ARGS();
  1822. PGconn *pgsql;
  1823. Oid oid;
  1824. if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, argc TSRMLS_CC,
  1825. "rs", &pgsql_link, &file_in, &name_len) == SUCCESS) {
  1826. ;
  1827. }
  1828. else if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, argc TSRMLS_CC,
  1829. "s", &file_in, &name_len) == SUCCESS) {
  1830. id = PGG(default_link);
  1831. CHECK_DEFAULT_LINK(id);
  1832. }
  1833. else if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, argc TSRMLS_CC,
  1834. "sr", &file_in, &name_len, &pgsql_link ) == SUCCESS) {
  1835. php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Old API is used.");
  1836. }
  1837. else {
  1838. WRONG_PARAM_COUNT;
  1839. }
  1840. if (PG(safe_mode) &&(!php_checkuid(file_in, NULL, CHECKUID_CHECK_FILE_AND_DIR))) {
  1841. RETURN_FALSE;
  1842. }
  1843. if (php_check_open_basedir(file_in TSRMLS_CC)) {
  1844. RETURN_FALSE;
  1845. }
  1846. ZEND_FETCH_RESOURCE2(pgsql, PGconn *, &pgsql_link, id, "PostgreSQL link", le_link, le_plink);
  1847. oid = lo_import(pgsql, file_in);
  1848. if (oid == InvalidOid) {
  1849. RETURN_FALSE;
  1850. }
  1851. PGSQL_RETURN_OID(oid);
  1852. }
  1853. /* }}} */
  1854. /* {{{ proto bool pg_lo_export([resource connection, ] int objoid, string filename)
  1855. Export large object direct to filesystem */
  1856. PHP_FUNCTION(pg_lo_export)
  1857. {
  1858. zval *pgsql_link = NULL;
  1859. char *file_out, *oid_string, *end_ptr;
  1860. size_t oid_strlen;
  1861. int id = -1, name_len;
  1862. long oid_long;
  1863. Oid oid;
  1864. PGconn *pgsql;
  1865. int argc = ZEND_NUM_ARGS();
  1866. /* allow string to handle large OID value correctly */
  1867. if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, argc TSRMLS_CC,
  1868. "rls", &pgsql_link, &oid_long, &file_out, &name_len) == SUCCESS) {
  1869. if (oid_long <= InvalidOid) {
  1870. php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Invalid OID specified.");
  1871. RETURN_FALSE;
  1872. }
  1873. oid = (Oid)oid_long;
  1874. }
  1875. else if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, argc TSRMLS_CC,
  1876. "rss", &pgsql_link, &oid_string, &oid_strlen, &file_out, &name_len) == SUCCESS) {
  1877. oid = (Oid)strtoul(oid_string, &end_ptr, 10);
  1878. if ((oid_string+oid_strlen) != end_ptr) {
  1879. /* wrong integer format */
  1880. php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Wrong OID value passed.");
  1881. RETURN_FALSE;
  1882. }
  1883. }
  1884. else if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, argc TSRMLS_CC,
  1885. "ls", &oid_long, &file_out, &name_len) == SUCCESS) {
  1886. if (oid_long <= InvalidOid) {
  1887. php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Invalid OID specified.");
  1888. RETURN_FALSE;
  1889. }
  1890. oid = (Oid)oid_long;
  1891. id = PGG(default_link);
  1892. CHECK_DEFAULT_LINK(id);
  1893. }
  1894. else if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, argc TSRMLS_CC,
  1895. "ss", &pgsql_link, &oid_string, &oid_strlen, &file_out, &name_len) == SUCCESS) {
  1896. oid = (Oid)strtoul(oid_string, &end_ptr, 10);
  1897. if ((oid_string+oid_strlen) != end_ptr) {
  1898. /* wrong integer format */
  1899. php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Wrong OID value passed.");
  1900. RETURN_FALSE;
  1901. }
  1902. id = PGG(default_link);
  1903. CHECK_DEFAULT_LINK(id);
  1904. }
  1905. else if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, argc TSRMLS_CC,
  1906. "ssr", &oid_string, &oid_strlen, &file_out, &name_len, &pgsql_link) == SUCCESS) {
  1907. oid = (Oid)strtoul(oid_string, &end_ptr, 10);
  1908. if ((oid_string+oid_strlen) != end_ptr) {
  1909. /* wrong integer format */
  1910. php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Wrong OID value passed.");
  1911. RETURN_FALSE;
  1912. }
  1913. }
  1914. else if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, argc TSRMLS_CC,
  1915. "lsr", &oid_long, &file_out, &name_len, &pgsql_link) == SUCCESS) {
  1916. php_error_docref(NULL TSRMLS_CC, E_NOTICE, " %s(): Old API is used");
  1917. if (oid_long <= InvalidOid) {
  1918. php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Invalid OID specified.");
  1919. RETURN_FALSE;
  1920. }
  1921. oid = (Oid)oid_long;
  1922. }
  1923. else {
  1924. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Requires 2 or 3 arguments.");
  1925. RETURN_FALSE;
  1926. }
  1927. if (PG(safe_mode) &&(!php_checkuid(file_out, NULL, CHECKUID_CHECK_FILE_AND_DIR))) {
  1928. RETURN_FALSE;
  1929. }
  1930. if (php_check_open_basedir(file_out TSRMLS_CC)) {
  1931. RETURN_FALSE;
  1932. }
  1933. ZEND_FETCH_RESOURCE2(pgsql, PGconn *, &pgsql_link, id, "PostgreSQL link", le_link, le_plink);
  1934. if (lo_export(pgsql, oid, file_out)) {
  1935. RETURN_TRUE;
  1936. }
  1937. RETURN_FALSE;
  1938. }
  1939. /* }}} */
  1940. /* {{{ proto bool pg_lo_seek(resource large_object, int offset [, int whence])
  1941. Seeks position of large object */
  1942. PHP_FUNCTION(pg_lo_seek)
  1943. {
  1944. zval *pgsql_id = NULL;
  1945. int offset = 0, whence = SEEK_CUR;
  1946. pgLofp *pgsql;
  1947. int argc = ZEND_NUM_ARGS();
  1948. if (zend_parse_parameters(argc TSRMLS_CC, "rl|l", &pgsql_id, &offset, &whence) == FAILURE) {
  1949. return;
  1950. }
  1951. if (whence != SEEK_SET && whence != SEEK_CUR && whence != SEEK_END) {
  1952. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid whence parameter.");
  1953. return;
  1954. }
  1955. ZEND_FETCH_RESOURCE(pgsql, pgLofp *, &pgsql_id, -1, "PostgreSQL large object", le_lofp);
  1956. if (lo_lseek((PGconn *)pgsql->conn, pgsql->lofd, offset, whence )) {
  1957. RETURN_TRUE;
  1958. } else {
  1959. RETURN_FALSE;
  1960. }
  1961. }
  1962. /* }}} */
  1963. /* {{{ proto int pg_lo_tell(resource large_object)
  1964. Returns current position of large object */
  1965. PHP_FUNCTION(pg_lo_tell)
  1966. {
  1967. zval *pgsql_id = NULL;
  1968. int offset = 0;
  1969. pgLofp *pgsql;
  1970. int argc = ZEND_NUM_ARGS();
  1971. if (zend_parse_parameters(argc TSRMLS_CC, "r", &pgsql_id) == FAILURE) {
  1972. return;
  1973. }
  1974. ZEND_FETCH_RESOURCE(pgsql, pgLofp *, &pgsql_id, -1, "PostgreSQL large object", le_lofp);
  1975. offset = lo_tell((PGconn *)pgsql->conn, pgsql->lofd);
  1976. RETURN_LONG(offset);
  1977. }
  1978. /* }}} */
  1979. #ifdef HAVE_PQCLIENTENCODING
  1980. /* {{{ proto int pg_set_client_encoding([resource connection,] string encoding)
  1981. Set client encoding */
  1982. PHP_FUNCTION(pg_set_client_encoding)
  1983. {
  1984. zval **encoding, **pgsql_link = NULL;
  1985. int id = -1;
  1986. PGconn *pgsql;
  1987. switch(ZEND_NUM_ARGS()) {
  1988. case 1:
  1989. if (zend_get_parameters_ex(1, &encoding)==FAILURE) {
  1990. RETURN_FALSE;
  1991. }
  1992. id = PGG(default_link);
  1993. CHECK_DEFAULT_LINK(id);
  1994. break;
  1995. case 2:
  1996. if (zend_get_parameters_ex(2, &pgsql_link, &encoding)==FAILURE) {
  1997. RETURN_FALSE;
  1998. }
  1999. break;
  2000. default:
  2001. WRONG_PARAM_COUNT;
  2002. break;
  2003. }
  2004. ZEND_FETCH_RESOURCE2(pgsql, PGconn *, pgsql_link, id, "PostgreSQL link", le_link, le_plink);
  2005. convert_to_string_ex(encoding);
  2006. Z_LVAL_P(return_value) = PQsetClientEncoding(pgsql, Z_STRVAL_PP(encoding));
  2007. Z_TYPE_P(return_value) = IS_LONG;
  2008. }
  2009. /* }}} */
  2010. /* {{{ proto string pg_client_encoding([resource connection])
  2011. Get the current client encoding */
  2012. PHP_FUNCTION(pg_client_encoding)
  2013. {
  2014. zval **pgsql_link = NULL;
  2015. int id = -1;
  2016. PGconn *pgsql;
  2017. switch(ZEND_NUM_ARGS()) {
  2018. case 0:
  2019. id = PGG(default_link);
  2020. CHECK_DEFAULT_LINK(id);
  2021. break;
  2022. case 1:
  2023. if (zend_get_parameters_ex(1, &pgsql_link)==FAILURE) {
  2024. RETURN_FALSE;
  2025. }
  2026. break;
  2027. default:
  2028. WRONG_PARAM_COUNT;
  2029. break;
  2030. }
  2031. ZEND_FETCH_RESOURCE2(pgsql, PGconn *, pgsql_link, id, "PostgreSQL link", le_link, le_plink);
  2032. /* Just do the same as found in PostgreSQL sources... */
  2033. #ifndef HAVE_PGSQL_WITH_MULTIBYTE_SUPPORT
  2034. #define pg_encoding_to_char(x) "SQL_ASCII"
  2035. #endif
  2036. Z_STRVAL_P(return_value)
  2037. = (char *) pg_encoding_to_char(PQclientEncoding(pgsql));
  2038. Z_STRLEN_P(return_value) = strlen(Z_STRVAL_P(return_value));
  2039. Z_STRVAL_P(return_value) = (char *) estrdup(Z_STRVAL_P(return_value));
  2040. Z_TYPE_P(return_value) = IS_STRING;
  2041. }
  2042. /* }}} */
  2043. #endif
  2044. #define COPYBUFSIZ 8192
  2045. /* {{{ proto bool pg_end_copy([resource connection])
  2046. Sync with backend. Completes the Copy command */
  2047. PHP_FUNCTION(pg_end_copy)
  2048. {
  2049. zval **pgsql_link = NULL;
  2050. int id = -1;
  2051. PGconn *pgsql;
  2052. int result = 0;
  2053. switch(ZEND_NUM_ARGS()) {
  2054. case 0:
  2055. id = PGG(default_link);
  2056. CHECK_DEFAULT_LINK(id);
  2057. break;
  2058. case 1:
  2059. if (zend_get_parameters_ex(1, &pgsql_link)==FAILURE) {
  2060. RETURN_FALSE;
  2061. }
  2062. break;
  2063. default:
  2064. WRONG_PARAM_COUNT;
  2065. break;
  2066. }
  2067. ZEND_FETCH_RESOURCE2(pgsql, PGconn *, pgsql_link, id, "PostgreSQL link", le_link, le_plink);
  2068. result = PQendcopy(pgsql);
  2069. if (result!=0) {
  2070. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Query failed: %s.", PQerrorMessage(pgsql));
  2071. RETURN_FALSE;
  2072. }
  2073. RETURN_TRUE;
  2074. }
  2075. /* }}} */
  2076. /* {{{ proto bool pg_put_line([resource connection,] string query)
  2077. Send null-terminated string to backend server*/
  2078. PHP_FUNCTION(pg_put_line)
  2079. {
  2080. zval **query, **pgsql_link = NULL;
  2081. int id = -1;
  2082. PGconn *pgsql;
  2083. int result = 0;
  2084. switch(ZEND_NUM_ARGS()) {
  2085. case 1:
  2086. if (zend_get_parameters_ex(1, &query)==FAILURE) {
  2087. RETURN_FALSE;
  2088. }
  2089. id = PGG(default_link);
  2090. CHECK_DEFAULT_LINK(id);
  2091. break;
  2092. case 2:
  2093. if (zend_get_parameters_ex(2, &pgsql_link, &query)==FAILURE) {
  2094. RETURN_FALSE;
  2095. }
  2096. break;
  2097. default:
  2098. WRONG_PARAM_COUNT;
  2099. break;
  2100. }
  2101. ZEND_FETCH_RESOURCE2(pgsql, PGconn *, pgsql_link, id, "PostgreSQL link", le_link, le_plink);
  2102. convert_to_string_ex(query);
  2103. result = PQputline(pgsql, Z_STRVAL_PP(query));
  2104. if (result==EOF) {
  2105. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Query failed: %s.", PQerrorMessage(pgsql));
  2106. RETURN_FALSE;
  2107. }
  2108. RETURN_TRUE;
  2109. }
  2110. /* }}} */
  2111. /* {{{ proto array pg_copy_to(resource connection, string table_name [, string delimiter [, string null_as]])
  2112. Copy table to array */
  2113. PHP_FUNCTION(pg_copy_to)
  2114. {
  2115. zval *pgsql_link;
  2116. char *table_name, *pg_delim = NULL, *pg_null_as = NULL;
  2117. int table_name_len, pg_delim_len, pg_null_as_len;
  2118. char *query;
  2119. char *query_template = "COPY \"\" TO STDOUT DELIMITERS ':' WITH NULL AS ''";
  2120. int id = -1;
  2121. PGconn *pgsql;
  2122. PGresult *pgsql_result;
  2123. ExecStatusType status;
  2124. int copydone = 0;
  2125. char copybuf[COPYBUFSIZ];
  2126. char *csv = (char *)NULL;
  2127. int ret;
  2128. int argc = ZEND_NUM_ARGS();
  2129. if (zend_parse_parameters(argc TSRMLS_CC, "rs|ss",
  2130. &pgsql_link, &table_name, &table_name_len,
  2131. &pg_delim, &pg_delim_len, &pg_null_as, &pg_null_as_len) == FAILURE) {
  2132. return;
  2133. }
  2134. if (!pg_delim) {
  2135. pg_delim = "\t";
  2136. }
  2137. if (!pg_null_as) {
  2138. pg_null_as = safe_estrdup("\\\\N");
  2139. }
  2140. ZEND_FETCH_RESOURCE2(pgsql, PGconn *, &pgsql_link, id, "PostgreSQL link", le_link, le_plink);
  2141. query = (char *)emalloc(strlen(query_template) + strlen(table_name) + strlen(pg_null_as) + 1);
  2142. sprintf(query, "COPY \"%s\" TO STDOUT DELIMITERS '%s' WITH NULL AS '%s'",
  2143. table_name, pg_delim, pg_null_as);
  2144. while ((pgsql_result = PQgetResult(pgsql))) {
  2145. PQclear(pgsql_result);
  2146. }
  2147. pgsql_result = PQexec(pgsql, query);
  2148. efree(pg_null_as);
  2149. efree(query);
  2150. if (pgsql_result) {
  2151. status = PQresultStatus(pgsql_result);
  2152. } else {
  2153. status = (ExecStatusType) PQstatus(pgsql);
  2154. }
  2155. switch (status) {
  2156. case PGRES_COPY_OUT:
  2157. if (pgsql_result) {
  2158. PQclear(pgsql_result);
  2159. if (array_init(return_value) == FAILURE) {
  2160. RETURN_FALSE;
  2161. }
  2162. while (!copydone)
  2163. {
  2164. if ((ret = PQgetline(pgsql, copybuf, COPYBUFSIZ))) {
  2165. php_error_docref(NULL TSRMLS_CC, E_WARNING, "getline failed: %s.", PQerrorMessage(pgsql));
  2166. RETURN_FALSE;
  2167. }
  2168. if (copybuf[0] == '\\' &&
  2169. copybuf[1] == '.' &&
  2170. copybuf[2] == '\0')
  2171. {
  2172. copydone = 1;
  2173. }
  2174. else
  2175. {
  2176. if (csv == (char *)NULL) {
  2177. csv = estrdup(copybuf);
  2178. } else {
  2179. csv = (char *)erealloc(csv, strlen(csv) + sizeof(char)*(COPYBUFSIZ+1));
  2180. strcat(csv, copybuf);
  2181. }
  2182. switch (ret)
  2183. {
  2184. case EOF:
  2185. copydone = 1;
  2186. case 0:
  2187. add_next_index_string(return_value, csv, 1);
  2188. efree(csv);
  2189. csv = (char *)NULL;
  2190. break;
  2191. case 1:
  2192. break;
  2193. }
  2194. }
  2195. }
  2196. if (PQendcopy(pgsql)) {
  2197. php_error_docref(NULL TSRMLS_CC, E_WARNING, "endcopy failed: %s.", PQerrorMessage(pgsql));
  2198. RETURN_FALSE;
  2199. }
  2200. while ((pgsql_result = PQgetResult(pgsql))) {
  2201. PQclear(pgsql_result);
  2202. }
  2203. } else {
  2204. PQclear(pgsql_result);
  2205. RETURN_FALSE;
  2206. }
  2207. break;
  2208. default:
  2209. PQclear(pgsql_result);
  2210. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Copy command failed: %s.", PQerrorMessage(pgsql));
  2211. RETURN_FALSE;
  2212. break;
  2213. }
  2214. }
  2215. /* }}} */
  2216. /* {{{ proto bool pg_copy_from(resource connection, string table_name , array rows [, string delimiter [, string null_as]])
  2217. Copy table from array */
  2218. PHP_FUNCTION(pg_copy_from)
  2219. {
  2220. zval *pgsql_link = NULL, *pg_rows;
  2221. zval **tmp;
  2222. char *table_name, *pg_delim = NULL, *pg_null_as = NULL;
  2223. int table_name_len, pg_delim_len, pg_null_as_len;
  2224. char *query;
  2225. char *query_template = "COPY \"\" FROM STDIN DELIMITERS ':' WITH NULL AS ''";
  2226. HashPosition pos;
  2227. int id = -1;
  2228. PGconn *pgsql;
  2229. PGresult *pgsql_result;
  2230. ExecStatusType status;
  2231. int argc = ZEND_NUM_ARGS();
  2232. if (zend_parse_parameters(argc TSRMLS_CC, "rs/a|ss",
  2233. &pgsql_link, &table_name, &table_name_len, &pg_rows,
  2234. &pg_delim, &pg_delim_len, &pg_null_as, &pg_null_as_len) == FAILURE) {
  2235. return;
  2236. }
  2237. if (!pg_delim) {
  2238. pg_delim = "\t";
  2239. }
  2240. if (!pg_null_as) {
  2241. pg_null_as = safe_estrdup("\\\\N");
  2242. }
  2243. ZEND_FETCH_RESOURCE2(pgsql, PGconn *, &pgsql_link, id, "PostgreSQL link", le_link, le_plink);
  2244. query = (char *)emalloc(strlen(query_template) + strlen(table_name) + strlen(pg_null_as) + 1);
  2245. sprintf(query, "COPY \"%s\" FROM STDIN DELIMITERS '%s' WITH NULL AS '%s'",
  2246. table_name, pg_delim, pg_null_as);
  2247. while ((pgsql_result = PQgetResult(pgsql))) {
  2248. PQclear(pgsql_result);
  2249. }
  2250. pgsql_result = PQexec(pgsql, query);
  2251. efree(pg_null_as);
  2252. efree(query);
  2253. if (pgsql_result) {
  2254. status = PQresultStatus(pgsql_result);
  2255. } else {
  2256. status = (ExecStatusType) PQstatus(pgsql);
  2257. }
  2258. switch (status) {
  2259. case PGRES_COPY_IN:
  2260. if (pgsql_result) {
  2261. PQclear(pgsql_result);
  2262. zend_hash_internal_pointer_reset_ex(Z_ARRVAL_P(pg_rows), &pos);
  2263. while (zend_hash_get_current_data_ex(Z_ARRVAL_P(pg_rows), (void **) &tmp, &pos) == SUCCESS) {
  2264. convert_to_string_ex(tmp);
  2265. query = (char *)emalloc(Z_STRLEN_PP(tmp) +2);
  2266. strcpy(query, Z_STRVAL_PP(tmp));
  2267. if(*(query+Z_STRLEN_PP(tmp)-1) != '\n')
  2268. strcat(query, "\n");
  2269. if (PQputline(pgsql, query)) {
  2270. efree(query);
  2271. php_error_docref(NULL TSRMLS_CC, E_WARNING, "copy failed: %s.", PQerrorMessage(pgsql));
  2272. RETURN_FALSE;
  2273. }
  2274. efree(query);
  2275. zend_hash_move_forward_ex(Z_ARRVAL_P(pg_rows), &pos);
  2276. }
  2277. if (PQputline(pgsql, "\\.\n") == EOF) {
  2278. php_error_docref(NULL TSRMLS_CC, E_WARNING, "putline failed: %s.", PQerrorMessage(pgsql));
  2279. RETURN_FALSE;
  2280. }
  2281. if (PQendcopy(pgsql)) {
  2282. php_error_docref(NULL TSRMLS_CC, E_WARNING, "endcopy failed: %s.", PQerrorMessage(pgsql));
  2283. RETURN_FALSE;
  2284. }
  2285. while ((pgsql_result = PQgetResult(pgsql))) {
  2286. PQclear(pgsql_result);
  2287. }
  2288. } else {
  2289. PQclear(pgsql_result);
  2290. RETURN_FALSE;
  2291. }
  2292. RETURN_TRUE;
  2293. break;
  2294. default:
  2295. PQclear(pgsql_result);
  2296. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Copy command failed: %s.", PQerrorMessage(pgsql));
  2297. RETURN_FALSE;
  2298. break;
  2299. }
  2300. }
  2301. /* }}} */
  2302. #ifdef HAVE_PQESCAPE
  2303. /* {{{ proto string pg_escape_string(string data)
  2304. Escape string for text/char type */
  2305. PHP_FUNCTION(pg_escape_string)
  2306. {
  2307. char *from = NULL, *to = NULL;
  2308. size_t from_len, to_len;
  2309. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s",
  2310. &from, &from_len) == FAILURE) {
  2311. return;
  2312. }
  2313. to = (char *)emalloc(from_len*2+1);
  2314. to_len = (int)PQescapeString(to, from, from_len);
  2315. RETURN_STRINGL(to, to_len, 0);
  2316. }
  2317. /* }}} */
  2318. /* {{{ proto string pg_escape_bytea(string data)
  2319. Escape binary for bytea type */
  2320. PHP_FUNCTION(pg_escape_bytea)
  2321. {
  2322. char *from = NULL, *to = NULL;
  2323. size_t from_len, to_len;
  2324. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s",
  2325. &from, &from_len) == FAILURE) {
  2326. return;
  2327. }
  2328. to = (char *)PQescapeBytea((unsigned char*)from, from_len, &to_len);
  2329. RETVAL_STRINGL(to, to_len-1, 1); /* to_len includes addtional '\0' */
  2330. free(to);
  2331. }
  2332. /* }}} */
  2333. /* PQunescapeBytea() from PostgreSQL 7.3 to provide bytea unescape feature to 7.2 users.
  2334. Renamed to php_pgsql_unescape_bytea() */
  2335. /*
  2336. * PQunescapeBytea - converts the null terminated string representation
  2337. * of a bytea, strtext, into binary, filling a buffer. It returns a
  2338. * pointer to the buffer which is NULL on error, and the size of the
  2339. * buffer in retbuflen. The pointer may subsequently be used as an
  2340. * argument to the function free(3). It is the reverse of PQescapeBytea.
  2341. *
  2342. * The following transformations are reversed:
  2343. * '\0' == ASCII 0 == \000
  2344. * '\'' == ASCII 39 == \'
  2345. * '\\' == ASCII 92 == \\
  2346. *
  2347. * States:
  2348. * 0 normal 0->1->2->3->4
  2349. * 1 \ 1->5
  2350. * 2 \0 1->6
  2351. * 3 \00
  2352. * 4 \000
  2353. * 5 \'
  2354. * 6 \\
  2355. */
  2356. static unsigned char * php_pgsql_unescape_bytea(unsigned char *strtext, size_t *retbuflen)
  2357. {
  2358. size_t buflen;
  2359. unsigned char *buffer,
  2360. *sp,
  2361. *bp;
  2362. unsigned int state = 0;
  2363. if (strtext == NULL)
  2364. return NULL;
  2365. buflen = strlen(strtext); /* will shrink, also we discover if
  2366. * strtext */
  2367. buffer = (unsigned char *) malloc(buflen); /* isn't NULL terminated */
  2368. if (buffer == NULL)
  2369. return NULL;
  2370. for (bp = buffer, sp = strtext; *sp != '\0'; bp++, sp++)
  2371. {
  2372. switch (state)
  2373. {
  2374. case 0:
  2375. if (*sp == '\\')
  2376. state = 1;
  2377. *bp = *sp;
  2378. break;
  2379. case 1:
  2380. if (*sp == '\'') /* state=5 */
  2381. { /* replace \' with 39 */
  2382. bp--;
  2383. *bp = '\'';
  2384. buflen--;
  2385. state = 0;
  2386. }
  2387. else if (*sp == '\\') /* state=6 */
  2388. { /* replace \\ with 92 */
  2389. bp--;
  2390. *bp = '\\';
  2391. buflen--;
  2392. state = 0;
  2393. }
  2394. else
  2395. {
  2396. if (isdigit(*sp))
  2397. state = 2;
  2398. else
  2399. state = 0;
  2400. *bp = *sp;
  2401. }
  2402. break;
  2403. case 2:
  2404. if (isdigit(*sp))
  2405. state = 3;
  2406. else
  2407. state = 0;
  2408. *bp = *sp;
  2409. break;
  2410. case 3:
  2411. if (isdigit(*sp)) /* state=4 */
  2412. {
  2413. int v;
  2414. bp -= 3;
  2415. sscanf(sp - 2, "%03o", &v);
  2416. *bp = v;
  2417. buflen -= 3;
  2418. state = 0;
  2419. }
  2420. else
  2421. {
  2422. *bp = *sp;
  2423. state = 0;
  2424. }
  2425. break;
  2426. }
  2427. }
  2428. buffer = realloc(buffer, buflen);
  2429. if (buffer == NULL)
  2430. return NULL;
  2431. *retbuflen = buflen;
  2432. return buffer;
  2433. }
  2434. /* {{{ proto string pg_unescape_bytea(string data)
  2435. Unescape binary for bytea type */
  2436. PHP_FUNCTION(pg_unescape_bytea)
  2437. {
  2438. char *from = NULL, *to = NULL;
  2439. size_t from_len, to_len;
  2440. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s",
  2441. &from, &from_len) == FAILURE) {
  2442. return;
  2443. }
  2444. to = (char *)php_pgsql_unescape_bytea((unsigned char*)from, &to_len);
  2445. if (!to) {
  2446. RETURN_FALSE;
  2447. }
  2448. RETVAL_STRINGL(to, to_len, 1);
  2449. free(to);
  2450. }
  2451. /* }}} */
  2452. #endif
  2453. /* {{{ proto string pg_result_error(resource result)
  2454. Get error message associated with result */
  2455. PHP_FUNCTION(pg_result_error)
  2456. {
  2457. zval *result;
  2458. PGresult *pgsql_result;
  2459. pgsql_result_handle *pg_result;
  2460. char *err = NULL;
  2461. if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS() TSRMLS_CC, "r",
  2462. &result) == FAILURE) {
  2463. RETURN_FALSE;
  2464. }
  2465. ZEND_FETCH_RESOURCE(pg_result, pgsql_result_handle *, &result, -1, "PostgreSQL result", le_result);
  2466. pgsql_result = pg_result->result;
  2467. if (!pgsql_result) {
  2468. RETURN_FALSE;
  2469. }
  2470. err = (char *)PQresultErrorMessage(pgsql_result);
  2471. RETURN_STRING(err,1);
  2472. }
  2473. /* }}} */
  2474. /* {{{ proto int pg_connection_status(resource connnection)
  2475. Get connection status */
  2476. PHP_FUNCTION(pg_connection_status)
  2477. {
  2478. zval *pgsql_link = NULL;
  2479. int id = -1;
  2480. PGconn *pgsql;
  2481. if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS() TSRMLS_CC, "r",
  2482. &pgsql_link) == FAILURE) {
  2483. RETURN_FALSE;
  2484. }
  2485. ZEND_FETCH_RESOURCE2(pgsql, PGconn *, &pgsql_link, id, "PostgreSQL link", le_link, le_plink);
  2486. RETURN_LONG(PQstatus(pgsql));
  2487. }
  2488. /* }}} */
  2489. /* {{{ proto bool pg_connection_reset(resource connection)
  2490. Reset connection (reconnect) */
  2491. PHP_FUNCTION(pg_connection_reset)
  2492. {
  2493. zval *pgsql_link;
  2494. int id = -1;
  2495. PGconn *pgsql;
  2496. if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS() TSRMLS_CC, "r",
  2497. &pgsql_link) == FAILURE) {
  2498. RETURN_FALSE;
  2499. }
  2500. ZEND_FETCH_RESOURCE2(pgsql, PGconn *, &pgsql_link, id, "PostgreSQL link", le_link, le_plink);
  2501. PQreset(pgsql);
  2502. if (PQstatus(pgsql) == CONNECTION_BAD) {
  2503. RETURN_FALSE;
  2504. }
  2505. RETURN_TRUE;
  2506. }
  2507. /* }}} */
  2508. #define PHP_PG_ASYNC_IS_BUSY 1
  2509. #define PHP_PG_ASYNC_REQUEST_CANCEL 2
  2510. /* {{{ php_pgsql_flush_query
  2511. */
  2512. static int php_pgsql_flush_query(PGconn *pgsql TSRMLS_DC)
  2513. {
  2514. PGresult *res;
  2515. int leftover = 0;
  2516. if (PQsetnonblocking(pgsql, 1)) {
  2517. php_error_docref(NULL TSRMLS_CC, E_NOTICE,"Cannot set connection to nonblocking mode.");
  2518. return -1;
  2519. }
  2520. while ((res = PQgetResult(pgsql))) {
  2521. PQclear(res);
  2522. leftover++;
  2523. }
  2524. PQsetnonblocking(pgsql, 0);
  2525. return leftover;
  2526. }
  2527. /* }}} */
  2528. /* {{{ php_pgsql_do_async
  2529. */
  2530. static void php_pgsql_do_async(INTERNAL_FUNCTION_PARAMETERS, int entry_type)
  2531. {
  2532. zval *pgsql_link;
  2533. int id = -1;
  2534. PGconn *pgsql;
  2535. PGresult *pgsql_result;
  2536. if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS() TSRMLS_CC, "r",
  2537. &pgsql_link) == FAILURE) {
  2538. RETURN_FALSE;
  2539. }
  2540. ZEND_FETCH_RESOURCE2(pgsql, PGconn *, &pgsql_link, id, "PostgreSQL link", le_link, le_plink);
  2541. if (PQ_SETNONBLOCKING(pgsql, 1)) {
  2542. php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Cannot set connection to nonblocking mode.");
  2543. RETURN_FALSE;
  2544. }
  2545. switch(entry_type) {
  2546. case PHP_PG_ASYNC_IS_BUSY:
  2547. PQconsumeInput(pgsql);
  2548. Z_LVAL_P(return_value) = PQisBusy(pgsql);
  2549. Z_TYPE_P(return_value) = IS_LONG;
  2550. break;
  2551. case PHP_PG_ASYNC_REQUEST_CANCEL:
  2552. Z_LVAL_P(return_value) = PQrequestCancel(pgsql);
  2553. Z_TYPE_P(return_value) = IS_LONG;
  2554. while ((pgsql_result = PQgetResult(pgsql))) {
  2555. PQclear(pgsql_result);
  2556. }
  2557. break;
  2558. default:
  2559. php_error_docref(NULL TSRMLS_CC, E_ERROR, "PostgreSQL module error, please report this error.");
  2560. break;
  2561. }
  2562. if (PQ_SETNONBLOCKING(pgsql, 0)) {
  2563. php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Cannot set connection to blocking mode.");
  2564. }
  2565. convert_to_boolean_ex(&return_value);
  2566. }
  2567. /* }}} */
  2568. /* {{{ proto bool pg_cancel_query(resource connection)
  2569. Cancel request */
  2570. PHP_FUNCTION(pg_cancel_query)
  2571. {
  2572. php_pgsql_do_async(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_PG_ASYNC_REQUEST_CANCEL);
  2573. }
  2574. /* }}} */
  2575. /* {{{ proto bool pg_connection_busy(resource connection)
  2576. Get connection is busy or not */
  2577. PHP_FUNCTION(pg_connection_busy)
  2578. {
  2579. php_pgsql_do_async(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_PG_ASYNC_IS_BUSY);
  2580. }
  2581. /* }}} */
  2582. /* {{{ proto bool pg_send_query(resource connection, string qeury)
  2583. Send asynchronous query */
  2584. PHP_FUNCTION(pg_send_query)
  2585. {
  2586. zval *pgsql_link;
  2587. char *query;
  2588. int len;
  2589. int id = -1;
  2590. PGconn *pgsql;
  2591. PGresult *res;
  2592. int leftover = 0;
  2593. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rs",
  2594. &pgsql_link, &query, &len) == FAILURE) {
  2595. return;
  2596. }
  2597. ZEND_FETCH_RESOURCE2(pgsql, PGconn *, &pgsql_link, id, "PostgreSQL link", le_link, le_plink);
  2598. if (PQ_SETNONBLOCKING(pgsql, 1)) {
  2599. php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Cannot set connection to nonblocking mode.");
  2600. RETURN_FALSE;
  2601. }
  2602. while ((res = PQgetResult(pgsql))) {
  2603. PQclear(res);
  2604. leftover = 1;
  2605. }
  2606. if (leftover) {
  2607. php_error_docref(NULL TSRMLS_CC, E_NOTICE, "There are results on this connection. Call pg_get_result() until it returns FALSE.");
  2608. }
  2609. if (!PQsendQuery(pgsql, query)) {
  2610. RETURN_FALSE;
  2611. }
  2612. if (PQ_SETNONBLOCKING(pgsql, 0)) {
  2613. php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Cannot set connection to blocking mode.");
  2614. }
  2615. RETURN_TRUE;
  2616. }
  2617. /* }}} */
  2618. /* {{{ proto resource pg_get_result([resource connection])
  2619. Get asynchronous query result */
  2620. PHP_FUNCTION(pg_get_result)
  2621. {
  2622. zval *pgsql_link;
  2623. int id = -1;
  2624. PGconn *pgsql;
  2625. PGresult *pgsql_result;
  2626. pgsql_result_handle *pg_result;
  2627. if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS() TSRMLS_CC, "r",
  2628. &pgsql_link) == FAILURE) {
  2629. RETURN_FALSE;
  2630. }
  2631. ZEND_FETCH_RESOURCE2(pgsql, PGconn *, &pgsql_link, id, "PostgreSQL link", le_link, le_plink);
  2632. pgsql_result = PQgetResult(pgsql);
  2633. if (!pgsql_result) {
  2634. /* no result */
  2635. RETURN_FALSE;
  2636. }
  2637. pg_result = (pgsql_result_handle *) emalloc(sizeof(pgsql_result_handle));
  2638. pg_result->conn = pgsql;
  2639. pg_result->result = pgsql_result;
  2640. pg_result->row = -1;
  2641. ZEND_REGISTER_RESOURCE(return_value, pg_result, le_result);
  2642. }
  2643. /* }}} */
  2644. /* {{{ proto int pg_result_status(resource result[, long result_type])
  2645. Get status of query result */
  2646. PHP_FUNCTION(pg_result_status)
  2647. {
  2648. zval *result;
  2649. long result_type = PGSQL_STATUS_LONG;
  2650. ExecStatusType status;
  2651. PGresult *pgsql_result;
  2652. pgsql_result_handle *pg_result;
  2653. if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS() TSRMLS_CC, "r|l",
  2654. &result, &result_type) == FAILURE) {
  2655. RETURN_FALSE;
  2656. }
  2657. ZEND_FETCH_RESOURCE(pg_result, pgsql_result_handle *, &result, -1, "PostgreSQL result", le_result);
  2658. pgsql_result = pg_result->result;
  2659. if (result_type == PGSQL_STATUS_LONG) {
  2660. status = PQresultStatus(pgsql_result);
  2661. RETURN_LONG((int)status);
  2662. }
  2663. else if (result_type == PGSQL_STATUS_STRING) {
  2664. RETURN_STRING(PQcmdStatus(pgsql_result), 1);
  2665. }
  2666. else {
  2667. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Optional 2nd parameter should be PGSQL_STATUS_LONG or PGSQL_STATUS_STRING.");
  2668. RETURN_FALSE;
  2669. }
  2670. }
  2671. /* }}} */
  2672. /* {{{ proto resource pg_get_notify([resource connection[, result_type]])
  2673. Get asynchronous notification */
  2674. PHP_FUNCTION(pg_get_notify)
  2675. {
  2676. zval *pgsql_link;
  2677. int id = -1, result_type = PGSQL_ASSOC;
  2678. PGconn *pgsql;
  2679. PGnotify *pgsql_notify;
  2680. if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS() TSRMLS_CC, "r|l",
  2681. &pgsql_link) == FAILURE) {
  2682. RETURN_FALSE;
  2683. }
  2684. ZEND_FETCH_RESOURCE2(pgsql, PGconn *, &pgsql_link, id, "PostgreSQL link", le_link, le_plink);
  2685. PQconsumeInput(pgsql);
  2686. pgsql_notify = PQnotifies(pgsql);
  2687. if (!pgsql_notify) {
  2688. /* no notify message */
  2689. RETURN_FALSE;
  2690. }
  2691. array_init(return_value);
  2692. if (result_type & (PGSQL_NUM|PGSQL_BOTH)) {
  2693. add_index_string(return_value, 0, pgsql_notify->relname, 1);
  2694. add_index_long(return_value, 1, pgsql_notify->be_pid);
  2695. }
  2696. if (result_type & (PGSQL_ASSOC|PGSQL_BOTH)) {
  2697. add_assoc_string(return_value, "message", pgsql_notify->relname, 1);
  2698. add_assoc_long(return_value, "pid", pgsql_notify->be_pid);
  2699. }
  2700. }
  2701. /* }}} */
  2702. /* {{{ proto resource pg_get_pid([resource connection)
  2703. Get backend(server) pid */
  2704. PHP_FUNCTION(pg_get_pid)
  2705. {
  2706. zval *pgsql_link;
  2707. int id = -1, pid;
  2708. PGconn *pgsql;
  2709. if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS() TSRMLS_CC, "r",
  2710. &pgsql_link) == FAILURE) {
  2711. RETURN_FALSE;
  2712. }
  2713. ZEND_FETCH_RESOURCE2(pgsql, PGconn *, &pgsql_link, id, "PostgreSQL link", le_link, le_plink);
  2714. RETURN_LONG(PQbackendPID(pgsql));
  2715. }
  2716. /* }}} */
  2717. /* {{{ php_pgsql_meta_data
  2718. * TODO: Add meta_data cache for better performance
  2719. */
  2720. PHPAPI int php_pgsql_meta_data(PGconn *pg_link, const char *table_name, zval *meta TSRMLS_DC)
  2721. {
  2722. PGresult *pg_result;
  2723. char *tmp_name;
  2724. smart_str querystr = {0};
  2725. size_t new_len;
  2726. int i, num_rows;
  2727. zval *elem;
  2728. smart_str_appends(&querystr,
  2729. "SELECT a.attname, a.attnum, t.typname, a.attlen, a.attnotNULL, a.atthasdef "
  2730. "FROM pg_class as c, pg_attribute a, pg_type t "
  2731. "WHERE a.attnum > 0 AND a.attrelid = c.oid AND c.relname = '");
  2732. tmp_name = php_addslashes((char *)table_name, strlen(table_name), &new_len, 0 TSRMLS_CC);
  2733. smart_str_appendl(&querystr, tmp_name, new_len);
  2734. efree(tmp_name);
  2735. smart_str_appends(&querystr, "' AND a.atttypid = t.oid ORDER BY a.attnum;");
  2736. smart_str_0(&querystr);
  2737. pg_result = PQexec(pg_link, querystr.c);
  2738. if (PQresultStatus(pg_result) != PGRES_TUPLES_OK || (num_rows = PQntuples(pg_result)) == 0) {
  2739. php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Failed to query meta_data for '%s' table %s.", table_name, querystr.c);
  2740. smart_str_free(&querystr);
  2741. PQclear(pg_result);
  2742. return FAILURE;
  2743. }
  2744. smart_str_free(&querystr);
  2745. for (i = 0; i < num_rows; i++) {
  2746. char *name;
  2747. MAKE_STD_ZVAL(elem);
  2748. array_init(elem);
  2749. add_assoc_long(elem, "num", atoi(PQgetvalue(pg_result,i,1)));
  2750. add_assoc_string(elem, "type", PQgetvalue(pg_result,i,2), 1);
  2751. add_assoc_long(elem, "len", atoi(PQgetvalue(pg_result,i,3)));
  2752. if (!strcmp(PQgetvalue(pg_result,i,4), "t")) {
  2753. add_assoc_bool(elem, "not null", 1);
  2754. }
  2755. else {
  2756. add_assoc_bool(elem, "not null", 0);
  2757. }
  2758. if (!strcmp(PQgetvalue(pg_result,i,5), "t")) {
  2759. add_assoc_bool(elem, "has default", 1);
  2760. }
  2761. else {
  2762. add_assoc_bool(elem, "has default", 0);
  2763. }
  2764. name = PQgetvalue(pg_result,i,0);
  2765. add_assoc_zval(meta, name, elem);
  2766. }
  2767. return SUCCESS;
  2768. }
  2769. /* }}} */
  2770. /* {{{ proto array pg_meta_data(resource db, string table)
  2771. Get meta_data */
  2772. PHP_FUNCTION(pg_meta_data)
  2773. {
  2774. zval *pgsql_link;
  2775. char *table_name;
  2776. uint table_name_len;
  2777. PGconn *pgsql;
  2778. int id = -1;
  2779. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rs",
  2780. &pgsql_link, &table_name, &table_name_len) == FAILURE) {
  2781. return;
  2782. }
  2783. ZEND_FETCH_RESOURCE2(pgsql, PGconn *, &pgsql_link, id, "PostgreSQL link", le_link, le_plink);
  2784. array_init(return_value);
  2785. if (php_pgsql_meta_data(pgsql, table_name, return_value TSRMLS_CC) == FAILURE) {
  2786. zval_dtor(return_value); /* destroy array */
  2787. RETURN_FALSE;
  2788. }
  2789. }
  2790. /* }}} */
  2791. /* {{{ php_pgsql_get_data_type
  2792. */
  2793. static php_pgsql_data_type php_pgsql_get_data_type(const char *type_name, size_t len)
  2794. {
  2795. /* This is stupid way to do. I'll fix it when I decied how to support
  2796. user defined types. (Yasuo) */
  2797. /* boolean */
  2798. if (!strcmp(type_name, "bool")|| !strcmp(type_name, "boolean"))
  2799. return PG_BOOL;
  2800. /* object id */
  2801. if (!strcmp(type_name, "oid"))
  2802. return PG_OID;
  2803. /* integer */
  2804. if (!strcmp(type_name, "int2") || !strcmp(type_name, "smallint"))
  2805. return PG_INT2;
  2806. if (!strcmp(type_name, "int4") || !strcmp(type_name, "integer"))
  2807. return PG_INT4;
  2808. if (!strcmp(type_name, "int8") || !strcmp(type_name, "bigint"))
  2809. return PG_INT8;
  2810. /* real and other */
  2811. if (!strcmp(type_name, "float4") || !strcmp(type_name, "real"))
  2812. return PG_FLOAT4;
  2813. if (!strcmp(type_name, "float8") || !strcmp(type_name, "double precision"))
  2814. return PG_FLOAT8;
  2815. if (!strcmp(type_name, "numeric"))
  2816. return PG_NUMERIC;
  2817. if (!strcmp(type_name, "money"))
  2818. return PG_MONEY;
  2819. /* character */
  2820. if (!strcmp(type_name, "text"))
  2821. return PG_TEXT;
  2822. if (!strcmp(type_name, "bpchar") || !strcmp(type_name, "character"))
  2823. return PG_CHAR;
  2824. if (!strcmp(type_name, "varchar") || !strcmp(type_name, "character varying"))
  2825. return PG_VARCHAR;
  2826. /* time and interval */
  2827. if (!strcmp(type_name, "abstime"))
  2828. return PG_UNIX_TIME;
  2829. if (!strcmp(type_name, "reltime"))
  2830. return PG_UNIX_TIME_INTERVAL;
  2831. if (!strcmp(type_name, "tinterval"))
  2832. return PG_UNIX_TIME_INTERVAL;
  2833. if (!strcmp(type_name, "date"))
  2834. return PG_DATE;
  2835. if (!strcmp(type_name, "time"))
  2836. return PG_TIME;
  2837. if (!strcmp(type_name, "timestamp") || !strcmp(type_name, "time with time zone"))
  2838. return PG_TIME_WITH_TIMEZONE;
  2839. if (!strcmp(type_name, "timestamp with time zone"))
  2840. return PG_TIMESTAMP_WITH_TIMEZONE;
  2841. if (!strcmp(type_name, "interval"))
  2842. return PG_INTERVAL;
  2843. /* binary */
  2844. if (!strcmp(type_name, "bytea"))
  2845. return PG_BYTEA;
  2846. /* network */
  2847. if (!strcmp(type_name, "cidr"))
  2848. return PG_CIDR;
  2849. if (!strcmp(type_name, "inet"))
  2850. return PG_INET;
  2851. if (!strcmp(type_name, "macaddr"))
  2852. return PG_MACADDR;
  2853. /* bit */
  2854. if (!strcmp(type_name, "bit"))
  2855. return PG_BIT;
  2856. if (!strcmp(type_name, "bit varying"))
  2857. return PG_VARBIT;
  2858. /* geometric */
  2859. if (!strcmp(type_name, "line"))
  2860. return PG_LINE;
  2861. if (!strcmp(type_name, "lseg"))
  2862. return PG_LSEG;
  2863. if (!strcmp(type_name, "box"))
  2864. return PG_BOX;
  2865. if (!strcmp(type_name, "path"))
  2866. return PG_PATH;
  2867. if (!strcmp(type_name, "point"))
  2868. return PG_POINT;
  2869. if (!strcmp(type_name, "polygon"))
  2870. return PG_POLYGON;
  2871. if (!strcmp(type_name, "circle"))
  2872. return PG_CIRCLE;
  2873. return PG_UNKNOWN;
  2874. }
  2875. /* }}} */
  2876. /* {{{ php_pgsql_convert_match
  2877. * test field value with regular expression specified.
  2878. */
  2879. static int php_pgsql_convert_match(const char *str, const char *regex , int icase TSRMLS_DC)
  2880. {
  2881. regex_t re;
  2882. regmatch_t *subs;
  2883. int regopt = REG_EXTENDED;
  2884. int regerr, ret = SUCCESS;
  2885. if (icase) {
  2886. regopt |= REG_ICASE;
  2887. }
  2888. regerr = regcomp(&re, regex, regopt);
  2889. if (regerr) {
  2890. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Cannot compile regex.");
  2891. regfree(&re);
  2892. return FAILURE;
  2893. }
  2894. subs = (regmatch_t *)ecalloc(sizeof(regmatch_t), re.re_nsub+1);
  2895. if (!subs) {
  2896. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Cannot allocate memory.");
  2897. regfree(&re);
  2898. return FAILURE;
  2899. }
  2900. regerr = regexec(&re, str, re.re_nsub+1, subs, 0);
  2901. if (regerr == REG_NOMATCH) {
  2902. #ifdef PHP_DEBUG
  2903. php_error_docref(NULL TSRMLS_CC, E_NOTICE, "'%s' does not match with '%s'.", str, regex);
  2904. #endif
  2905. ret = FAILURE;
  2906. }
  2907. else if (regerr) {
  2908. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Cannot exec regex.");
  2909. ret = FAILURE;
  2910. }
  2911. regfree(&re);
  2912. efree(subs);
  2913. return ret;
  2914. }
  2915. /* }}} */
  2916. /* {{{ php_pgsql_add_quote
  2917. * add quotes around string.
  2918. */
  2919. static int php_pgsql_add_quotes(zval *src, zend_bool should_free TSRMLS_DC)
  2920. {
  2921. smart_str str = {0};
  2922. assert(Z_TYPE_P(src) == IS_STRING);
  2923. assert(should_free == 1 || should_free == 0);
  2924. smart_str_appendc(&str, '\'');
  2925. smart_str_appendl(&str, Z_STRVAL_P(src), Z_STRLEN_P(src));
  2926. smart_str_appendc(&str, '\'');
  2927. smart_str_0(&str);
  2928. if (should_free) {
  2929. efree(Z_STRVAL_P(src));
  2930. }
  2931. Z_STRVAL_P(src) = str.c;
  2932. Z_STRLEN_P(src) = str.len;
  2933. return SUCCESS;
  2934. }
  2935. /* }}} */
  2936. #define PGSQL_CONV_CHECK_IGNORE() \
  2937. if (!err && Z_TYPE_P(new_val) == IS_STRING && !strcmp(Z_STRVAL_P(new_val), "NULL")) { \
  2938. /* if new_value is string "NULL" and field has default value, remove element to use default value */ \
  2939. if (!(opt & PGSQL_CONV_IGNORE_DEFAULT) && Z_BVAL_PP(has_default)) { \
  2940. zval_dtor(new_val); \
  2941. FREE_ZVAL(new_val); \
  2942. skip_field = 1; \
  2943. } \
  2944. /* raise error if it's not null and cannot be ignored */ \
  2945. else if (!(opt & PGSQL_CONV_IGNORE_NOT_NULL) && Z_BVAL_PP(not_null)) { \
  2946. php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Detected NULL for 'NOT NULL' field '%s'.", field ); \
  2947. err = 1; \
  2948. } \
  2949. }
  2950. /* {{{ php_pgsql_convert
  2951. * check and convert array values (fieldname=>vlaue pair) for sql
  2952. */
  2953. PHPAPI int php_pgsql_convert(PGconn *pg_link, const char *table_name, const zval *values, zval *result, ulong opt TSRMLS_DC)
  2954. {
  2955. HashPosition pos;
  2956. char *field = NULL;
  2957. uint field_len = -1;
  2958. ulong num_idx = -1;
  2959. zval *meta, **def, **type, **not_null, **has_default, **val, *new_val;
  2960. int new_len, key_type, err = 0, skip_field;
  2961. assert(pg_link != NULL);
  2962. assert(Z_TYPE_P(values) == IS_ARRAY);
  2963. assert(Z_TYPE_P(result) == IS_ARRAY);
  2964. assert(!(opt & ~PGSQL_CONV_OPTS));
  2965. if (!table_name) {
  2966. return FAILURE;
  2967. }
  2968. MAKE_STD_ZVAL(meta);
  2969. if (array_init(meta) == FAILURE) {
  2970. zval_dtor(meta);
  2971. FREE_ZVAL(meta);
  2972. return FAILURE;
  2973. }
  2974. if (php_pgsql_meta_data(pg_link, table_name, meta TSRMLS_CC) == FAILURE) {
  2975. zval_dtor(meta);
  2976. FREE_ZVAL(meta);
  2977. return FAILURE;
  2978. }
  2979. for (zend_hash_internal_pointer_reset_ex(Z_ARRVAL_P(values), &pos);
  2980. zend_hash_get_current_data_ex(Z_ARRVAL_P(values), (void **)&val, &pos) == SUCCESS;
  2981. zend_hash_move_forward_ex(Z_ARRVAL_P(values), &pos)) {
  2982. skip_field = 0;
  2983. if ((key_type = zend_hash_get_current_key_ex(Z_ARRVAL_P(values), &field, &field_len, &num_idx, 0, &pos)) == HASH_KEY_NON_EXISTANT) {
  2984. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to get array key type.");
  2985. err = 1;
  2986. }
  2987. if (!err && key_type == HASH_KEY_IS_LONG) {
  2988. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Accepts only string key for values.");
  2989. err = 1;
  2990. }
  2991. if (!err && key_type == HASH_KEY_NON_EXISTANT) {
  2992. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Accepts only string key for values.");
  2993. err = 1;
  2994. }
  2995. if (!err && zend_hash_find(Z_ARRVAL_P(meta), field, field_len, (void **)&def) == FAILURE) {
  2996. php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Invalid field name (%s) in values.", field);
  2997. err = 1;
  2998. }
  2999. if (!err && zend_hash_find(Z_ARRVAL_PP(def), "type", sizeof("type"), (void **)&type) == FAILURE) {
  3000. php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Detected broken meta data. Missing 'type'.");
  3001. err = 1;
  3002. }
  3003. if (!err && zend_hash_find(Z_ARRVAL_PP(def), "not null", sizeof("not null"), (void **)&not_null) == FAILURE) {
  3004. php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Detected broken meta data. Missing 'not null'.");
  3005. err = 1;
  3006. }
  3007. if (!err && zend_hash_find(Z_ARRVAL_PP(def), "has default", sizeof("has default"), (void **)&has_default) == FAILURE) {
  3008. php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Detected broken meta data. Missing 'has default'.");
  3009. err = 1;
  3010. }
  3011. if (!err && (Z_TYPE_PP(val) == IS_ARRAY ||
  3012. Z_TYPE_PP(val) == IS_OBJECT ||
  3013. Z_TYPE_PP(val) == IS_CONSTANT_ARRAY)) {
  3014. php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Expects scaler values as field values.");
  3015. err = 1;
  3016. }
  3017. if (err) {
  3018. break; /* break out for() */
  3019. }
  3020. MAKE_STD_ZVAL(new_val);
  3021. switch(php_pgsql_get_data_type(Z_STRVAL_PP(type), Z_STRLEN_PP(type)))
  3022. {
  3023. case PG_BOOL:
  3024. switch (Z_TYPE_PP(val)) {
  3025. case IS_STRING:
  3026. if (Z_STRLEN_PP(val) == 0) {
  3027. ZVAL_STRING(new_val, "NULL", 1);
  3028. }
  3029. else {
  3030. if (!strcmp(Z_STRVAL_PP(val), "t") || !strcmp(Z_STRVAL_PP(val), "T") ||
  3031. !strcmp(Z_STRVAL_PP(val), "y") || !strcmp(Z_STRVAL_PP(val), "Y") ||
  3032. !strcmp(Z_STRVAL_PP(val), "true") || !strcmp(Z_STRVAL_PP(val), "True") ||
  3033. !strcmp(Z_STRVAL_PP(val), "yes") || !strcmp(Z_STRVAL_PP(val), "Yes") ||
  3034. !strcmp(Z_STRVAL_PP(val), "1")) {
  3035. ZVAL_STRING(new_val, "'t'", 1);
  3036. }
  3037. else if (!strcmp(Z_STRVAL_PP(val), "f") || !strcmp(Z_STRVAL_PP(val), "F") ||
  3038. !strcmp(Z_STRVAL_PP(val), "n") || !strcmp(Z_STRVAL_PP(val), "N") ||
  3039. !strcmp(Z_STRVAL_PP(val), "false") || !strcmp(Z_STRVAL_PP(val), "False") ||
  3040. !strcmp(Z_STRVAL_PP(val), "no") || !strcmp(Z_STRVAL_PP(val), "No") ||
  3041. !strcmp(Z_STRVAL_PP(val), "0")) {
  3042. ZVAL_STRING(new_val, "'f'", 1);
  3043. }
  3044. else {
  3045. 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);
  3046. err = 1;
  3047. }
  3048. }
  3049. break;
  3050. case IS_LONG:
  3051. case IS_BOOL:
  3052. if (Z_LVAL_PP(val)) {
  3053. ZVAL_STRING(new_val, "'t'", 1);
  3054. }
  3055. else {
  3056. ZVAL_STRING(new_val, "'f'", 1);
  3057. }
  3058. break;
  3059. case IS_NULL:
  3060. ZVAL_STRING(new_val, "NULL", 1);
  3061. break;
  3062. default:
  3063. err = 1;
  3064. }
  3065. PGSQL_CONV_CHECK_IGNORE();
  3066. if (err) {
  3067. php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Expects string, null, long or boolelan value for PostgreSQL '%s' (%s).", Z_STRVAL_PP(type), field);
  3068. }
  3069. break;
  3070. case PG_OID:
  3071. case PG_INT2:
  3072. case PG_INT4:
  3073. case PG_INT8:
  3074. switch (Z_TYPE_PP(val)) {
  3075. case IS_STRING:
  3076. if (Z_STRLEN_PP(val) == 0) {
  3077. ZVAL_STRING(new_val, "NULL", 1);
  3078. }
  3079. else {
  3080. /* FIXME: better regex must be used */
  3081. if (php_pgsql_convert_match(Z_STRVAL_PP(val), "^([+-]{0,1}[0-9]+)$", 0 TSRMLS_CC) == FAILURE) {
  3082. err = 1;
  3083. }
  3084. else {
  3085. ZVAL_STRING(new_val, Z_STRVAL_PP(val), 1);
  3086. }
  3087. }
  3088. break;
  3089. case IS_DOUBLE:
  3090. ZVAL_DOUBLE(new_val, Z_DVAL_PP(val));
  3091. convert_to_long_ex(&new_val);
  3092. break;
  3093. case IS_LONG:
  3094. ZVAL_LONG(new_val, Z_LVAL_PP(val));
  3095. break;
  3096. case IS_NULL:
  3097. ZVAL_STRING(new_val, "NULL", 1);
  3098. break;
  3099. default:
  3100. err = 1;
  3101. }
  3102. PGSQL_CONV_CHECK_IGNORE();
  3103. if (err) {
  3104. php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Expects NULL, string, long or double value for pgsql '%s' (%s).", Z_STRVAL_PP(type), field);
  3105. }
  3106. break;
  3107. case PG_NUMERIC:
  3108. case PG_MONEY:
  3109. case PG_FLOAT4:
  3110. case PG_FLOAT8:
  3111. switch (Z_TYPE_PP(val)) {
  3112. case IS_STRING:
  3113. if (Z_STRLEN_PP(val) == 0) {
  3114. ZVAL_STRING(new_val, "NULL", 1);
  3115. }
  3116. else {
  3117. /* FIXME: better regex must be used */
  3118. 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) {
  3119. err = 1;
  3120. }
  3121. else {
  3122. ZVAL_STRING(new_val, Z_STRVAL_PP(val), 1);
  3123. }
  3124. }
  3125. break;
  3126. case IS_LONG:
  3127. ZVAL_LONG(new_val, Z_LVAL_PP(val));
  3128. break;
  3129. case IS_DOUBLE:
  3130. ZVAL_DOUBLE(new_val, Z_DVAL_PP(val));
  3131. break;
  3132. case IS_NULL:
  3133. ZVAL_STRING(new_val, "NULL", 1);
  3134. break;
  3135. default:
  3136. err = 1;
  3137. }
  3138. PGSQL_CONV_CHECK_IGNORE();
  3139. if (err) {
  3140. php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Expects NULL, string, long or double value for PostgreSQL '%s' (%s).", Z_STRVAL_PP(type), field);
  3141. }
  3142. break;
  3143. case PG_TEXT:
  3144. case PG_CHAR:
  3145. case PG_VARCHAR:
  3146. switch (Z_TYPE_PP(val)) {
  3147. case IS_STRING:
  3148. if (Z_STRLEN_PP(val) == 0) {
  3149. if (opt & PGSQL_CONV_FORCE_NULL) {
  3150. ZVAL_STRING(new_val, "NULL", 1);
  3151. }
  3152. else {
  3153. ZVAL_STRING(new_val, empty_string, 1);
  3154. }
  3155. }
  3156. else {
  3157. Z_TYPE_P(new_val) = IS_STRING;
  3158. #if HAVE_PQESCAPE
  3159. {
  3160. char *tmp;
  3161. tmp = (char *)emalloc(Z_STRLEN_PP(val)*2+1);
  3162. Z_STRLEN_P(new_val) = (int)PQescapeString(tmp, Z_STRVAL_PP(val), Z_STRLEN_PP(val));
  3163. Z_STRVAL_P(new_val) = tmp;
  3164. }
  3165. #else
  3166. Z_STRVAL_P(new_val) = php_addslashes(Z_STRVAL_PP(val), Z_STRLEN_PP(val), &Z_STRLEN_P(new_val), 0 TSRMLS_CC);
  3167. #endif
  3168. }
  3169. php_pgsql_add_quotes(new_val, 1 TSRMLS_CC);
  3170. break;
  3171. case IS_LONG:
  3172. ZVAL_LONG(new_val, Z_LVAL_PP(val));
  3173. convert_to_string_ex(&new_val);
  3174. break;
  3175. case IS_DOUBLE:
  3176. ZVAL_DOUBLE(new_val, Z_DVAL_PP(val));
  3177. convert_to_string_ex(&new_val);
  3178. break;
  3179. case IS_NULL:
  3180. ZVAL_STRING(new_val, "NULL", 1);
  3181. break;
  3182. default:
  3183. err = 1;
  3184. }
  3185. PGSQL_CONV_CHECK_IGNORE();
  3186. if (err) {
  3187. php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Expects NULL, string, long or double value for PostgreSQL '%s' (%s).", Z_STRVAL_PP(type), field);
  3188. }
  3189. break;
  3190. case PG_UNIX_TIME:
  3191. case PG_UNIX_TIME_INTERVAL:
  3192. /* these are the actallay a integer */
  3193. switch (Z_TYPE_PP(val)) {
  3194. case IS_STRING:
  3195. if (Z_STRLEN_PP(val) == 0) {
  3196. ZVAL_STRING(new_val, "NULL", 1);
  3197. }
  3198. else {
  3199. /* FIXME: Better regex must be used */
  3200. if (php_pgsql_convert_match(Z_STRVAL_PP(val), "^[0-9]+$", 0 TSRMLS_CC) == FAILURE) {
  3201. err = 1;
  3202. }
  3203. else {
  3204. ZVAL_STRING(new_val, Z_STRVAL_PP(val), 1);
  3205. convert_to_long_ex(&new_val);
  3206. }
  3207. }
  3208. break;
  3209. case IS_DOUBLE:
  3210. ZVAL_DOUBLE(new_val, Z_DVAL_PP(val));
  3211. convert_to_long_ex(&new_val);
  3212. break;
  3213. case IS_LONG:
  3214. ZVAL_LONG(new_val, Z_LVAL_PP(val));
  3215. break;
  3216. case IS_NULL:
  3217. ZVAL_STRING(new_val, "NULL", 1);
  3218. break;
  3219. default:
  3220. err = 1;
  3221. }
  3222. PGSQL_CONV_CHECK_IGNORE();
  3223. if (err) {
  3224. php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Expects NULL, string, long or double value for '%s' (%s).", Z_STRVAL_PP(type), field);
  3225. }
  3226. break;
  3227. case PG_CIDR:
  3228. case PG_INET:
  3229. switch (Z_TYPE_PP(val)) {
  3230. case IS_STRING:
  3231. if (Z_STRLEN_PP(val) == 0) {
  3232. ZVAL_STRING(new_val, "NULL", 1);
  3233. }
  3234. else {
  3235. /* FIXME: Better regex must be used */
  3236. 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) {
  3237. err = 1;
  3238. }
  3239. else {
  3240. ZVAL_STRING(new_val, Z_STRVAL_PP(val), 1);
  3241. php_pgsql_add_quotes(new_val, 1 TSRMLS_CC);
  3242. }
  3243. }
  3244. break;
  3245. case IS_NULL:
  3246. ZVAL_STRING(new_val, "NULL", 1);
  3247. break;
  3248. default:
  3249. err = 1;
  3250. }
  3251. PGSQL_CONV_CHECK_IGNORE();
  3252. if (err) {
  3253. php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Expects NULL or string for '%s' (%s).", Z_STRVAL_PP(type), field);
  3254. }
  3255. break;
  3256. case PG_TIME_WITH_TIMEZONE:
  3257. case PG_TIMESTAMP_WITH_TIMEZONE:
  3258. switch(Z_TYPE_PP(val)) {
  3259. case IS_STRING:
  3260. if (Z_STRLEN_PP(val) == 0) {
  3261. ZVAL_STRING(new_val, "NULL", 1);
  3262. }
  3263. else {
  3264. /* FIXME: better regex must be used */
  3265. 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,1}$", 1 TSRMLS_CC) == FAILURE) {
  3266. err = 1;
  3267. }
  3268. else {
  3269. ZVAL_STRING(new_val, Z_STRVAL_PP(val), 1);
  3270. php_pgsql_add_quotes(new_val, 1 TSRMLS_CC);
  3271. }
  3272. }
  3273. break;
  3274. case IS_NULL:
  3275. ZVAL_STRING(new_val, "NULL", 1);
  3276. break;
  3277. default:
  3278. err = 1;
  3279. }
  3280. PGSQL_CONV_CHECK_IGNORE();
  3281. if (err) {
  3282. php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Expects NULL or string for PostgreSQL %s field (%s).", Z_STRVAL_PP(type), field);
  3283. }
  3284. break;
  3285. case PG_DATE:
  3286. switch(Z_TYPE_PP(val)) {
  3287. case IS_STRING:
  3288. if (Z_STRLEN_PP(val) == 0) {
  3289. ZVAL_STRING(new_val, "NULL", 1);
  3290. }
  3291. else {
  3292. /* FIXME: better regex must be used */
  3293. if (php_pgsql_convert_match(Z_STRVAL_PP(val), "^([0-9]{4}[/-][0-9]{1,2}[/-][0-9]{1,2})$", 1 TSRMLS_CC) == FAILURE) {
  3294. err = 1;
  3295. }
  3296. else {
  3297. ZVAL_STRING(new_val, Z_STRVAL_PP(val), 1);
  3298. php_pgsql_add_quotes(new_val, 1 TSRMLS_CC);
  3299. }
  3300. }
  3301. break;
  3302. case IS_NULL:
  3303. ZVAL_STRING(new_val, "NULL", 1);
  3304. break;
  3305. default:
  3306. err = 1;
  3307. }
  3308. PGSQL_CONV_CHECK_IGNORE();
  3309. if (err) {
  3310. php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Expects NULL or string for PostgreSQL %s field (%s).", Z_STRVAL_PP(type), field);
  3311. }
  3312. break;
  3313. case PG_TIME:
  3314. switch(Z_TYPE_PP(val)) {
  3315. case IS_STRING:
  3316. if (Z_STRLEN_PP(val) == 0) {
  3317. ZVAL_STRING(new_val, "NULL", 1);
  3318. }
  3319. else {
  3320. /* FIXME: better regex must be used */
  3321. 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) {
  3322. err = 1;
  3323. }
  3324. else {
  3325. ZVAL_STRING(new_val, Z_STRVAL_PP(val), 1);
  3326. php_pgsql_add_quotes(new_val, 1 TSRMLS_CC);
  3327. }
  3328. }
  3329. break;
  3330. case IS_NULL:
  3331. ZVAL_STRING(new_val, "NULL", 1);
  3332. break;
  3333. default:
  3334. err = 1;
  3335. }
  3336. PGSQL_CONV_CHECK_IGNORE();
  3337. if (err) {
  3338. php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Expects NULL or string for PostgreSQL %s field (%s).", Z_STRVAL_PP(type), field);
  3339. }
  3340. break;
  3341. case PG_INTERVAL:
  3342. switch(Z_TYPE_PP(val)) {
  3343. case IS_STRING:
  3344. if (Z_STRLEN_PP(val) == 0) {
  3345. ZVAL_STRING(new_val, "NULL", 1);
  3346. }
  3347. else {
  3348. /* FIXME: better regex must be used */
  3349. if (php_pgsql_convert_match(Z_STRVAL_PP(val), "^[+-]{0,1}[ \\t]+((second|seconds|minute|minute|hour|hour|day|days|week|weeks|month|monthes|year|years|decade|decades|century|centuries|millennium|millenniums){1,1}[ \\t]+)+([ \\t]+ago){0,1}$", 1 TSRMLS_CC) == FAILURE &&
  3350. php_pgsql_convert_match(Z_STRVAL_PP(val), "^@[ \\t]+[+-]{0,1}[ \\t]+(second|seconds|minute|minute|hour|hour|day|days|week|weeks|month|monthes|year|years|decade|decades|century|centuries|millennium|millenniums){1,1}[ \\t]+)+([ \\t]+ago$", 1 TSRMLS_CC) == FAILURE) {
  3351. err = 1;
  3352. }
  3353. else {
  3354. ZVAL_STRING(new_val, Z_STRVAL_PP(val), 1);
  3355. php_pgsql_add_quotes(new_val, 1 TSRMLS_CC);
  3356. }
  3357. }
  3358. break;
  3359. case IS_NULL:
  3360. ZVAL_STRING(new_val, "NULL", 1);
  3361. break;
  3362. default:
  3363. err = 1;
  3364. }
  3365. PGSQL_CONV_CHECK_IGNORE();
  3366. if (err) {
  3367. php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Expects NULL or string for PostgreSQL %s field (%s).", Z_STRVAL_PP(type), field);
  3368. }
  3369. break;
  3370. #ifdef HAVE_PQESCAPE
  3371. case PG_BYTEA:
  3372. switch (Z_TYPE_PP(val)) {
  3373. case IS_STRING:
  3374. if (Z_STRLEN_PP(val) == 0) {
  3375. ZVAL_STRING(new_val, "NULL", 1);
  3376. }
  3377. else {
  3378. unsigned char *tmp;
  3379. size_t to_len;
  3380. tmp = PQescapeBytea(Z_STRVAL_PP(val), Z_STRLEN_PP(val), &to_len);
  3381. Z_TYPE_P(new_val) = IS_STRING;
  3382. Z_STRLEN_P(new_val) = to_len-1; /* PQescapeBytea's to_len includes additional '\0' */
  3383. Z_STRVAL_P(new_val) = emalloc(to_len);
  3384. memcpy(Z_STRVAL_P(new_val), tmp, to_len);
  3385. free(tmp);
  3386. php_pgsql_add_quotes(new_val, 1 TSRMLS_CC);
  3387. }
  3388. break;
  3389. case IS_LONG:
  3390. ZVAL_LONG(new_val, Z_LVAL_PP(val));
  3391. convert_to_string_ex(&new_val);
  3392. break;
  3393. case IS_DOUBLE:
  3394. ZVAL_DOUBLE(new_val, Z_DVAL_PP(val));
  3395. convert_to_string_ex(&new_val);
  3396. break;
  3397. case IS_NULL:
  3398. ZVAL_STRING(new_val, "NULL", 1);
  3399. break;
  3400. default:
  3401. err = 1;
  3402. }
  3403. PGSQL_CONV_CHECK_IGNORE();
  3404. if (err) {
  3405. php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Expects NULL, string, long or double value for PostgreSQL '%s' (%s).", Z_STRVAL_PP(type), field);
  3406. }
  3407. break;
  3408. #endif
  3409. case PG_MACADDR:
  3410. switch(Z_TYPE_PP(val)) {
  3411. case IS_STRING:
  3412. if (Z_STRLEN_PP(val) == 0) {
  3413. ZVAL_STRING(new_val, "NULL", 1);
  3414. }
  3415. else {
  3416. 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) {
  3417. err = 1;
  3418. }
  3419. else {
  3420. ZVAL_STRING(new_val, Z_STRVAL_PP(val), 1);
  3421. php_pgsql_add_quotes(new_val, 1 TSRMLS_CC);
  3422. }
  3423. }
  3424. break;
  3425. case IS_NULL:
  3426. ZVAL_STRING(new_val, "NULL", 1);
  3427. break;
  3428. default:
  3429. err = 1;
  3430. }
  3431. PGSQL_CONV_CHECK_IGNORE();
  3432. if (err) {
  3433. php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Expects NULL or string for PostgreSQL %s field (%s).", Z_STRVAL_PP(type), field);
  3434. }
  3435. break;
  3436. /* bit */
  3437. case PG_BIT:
  3438. case PG_VARBIT:
  3439. /* geometric */
  3440. case PG_LINE:
  3441. case PG_LSEG:
  3442. case PG_POINT:
  3443. case PG_BOX:
  3444. case PG_PATH:
  3445. case PG_POLYGON:
  3446. case PG_CIRCLE:
  3447. php_error_docref(NULL TSRMLS_CC, E_NOTICE, "PostgreSQL '%s' type (%s) is not supported.", Z_STRVAL_PP(type), field);
  3448. err = 1;
  3449. break;
  3450. case PG_UNKNOWN:
  3451. default:
  3452. php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Unknown or system data type '%s' for '%s'.", Z_STRVAL_PP(type), field);
  3453. err = 1;
  3454. break;
  3455. } /* switch */
  3456. if (err) {
  3457. zval_dtor(new_val);
  3458. FREE_ZVAL(new_val);
  3459. break; /* break out for() */
  3460. }
  3461. if (!skip_field) {
  3462. /* If field is NULL and HAS DEFAULT, should be skipped */
  3463. field = php_addslashes(field, strlen(field), &new_len, 0 TSRMLS_CC);
  3464. add_assoc_zval(result, field, new_val);
  3465. efree(field);
  3466. }
  3467. } /* for */
  3468. zval_dtor(meta);
  3469. FREE_ZVAL(meta);
  3470. if (err) {
  3471. /* shouldn't destroy & free zval here */
  3472. return FAILURE;
  3473. }
  3474. return SUCCESS;
  3475. }
  3476. /* }}} */
  3477. /* {{{ proto array pg_convert(resource db, string table, array values[, int options])
  3478. Check and convert values for PostgreSQL SQL statement */
  3479. PHP_FUNCTION(pg_convert)
  3480. {
  3481. zval *pgsql_link, *values;
  3482. char *table_name;
  3483. size_t table_name_len;
  3484. ulong option = 0;
  3485. PGconn *pg_link;
  3486. int id = -1;
  3487. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,
  3488. "rsa|l", &pgsql_link, &table_name, &table_name_len, &values, &option) == FAILURE) {
  3489. return;
  3490. }
  3491. if (option & ~PGSQL_CONV_OPTS) {
  3492. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid option is specified.");
  3493. RETURN_FALSE;
  3494. }
  3495. if (!table_name_len) {
  3496. php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Table name is invalid.");
  3497. RETURN_FALSE;
  3498. }
  3499. ZEND_FETCH_RESOURCE2(pg_link, PGconn *, &pgsql_link, id, "PostgreSQL link", le_link, le_plink);
  3500. if (php_pgsql_flush_query(pg_link TSRMLS_CC)) {
  3501. php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Detected unhandled result(s) in connection.");
  3502. }
  3503. array_init(return_value);
  3504. if (php_pgsql_convert(pg_link, table_name, values, return_value, option TSRMLS_CC) == FAILURE) {
  3505. zval_dtor(return_value);
  3506. RETURN_FALSE;
  3507. }
  3508. }
  3509. /* }}} */
  3510. static int do_exec(smart_str *querystr, int expect, PGconn *pg_link, ulong opt TSRMLS_DC)
  3511. {
  3512. if (opt & PGSQL_DML_ASYNC) {
  3513. if (PQsendQuery(pg_link, querystr->c)) {
  3514. return 0;
  3515. }
  3516. }
  3517. else {
  3518. PGresult *pg_result;
  3519. pg_result = PQexec(pg_link, querystr->c);
  3520. if (PQresultStatus(pg_result) == expect) {
  3521. return 0;
  3522. } else {
  3523. php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Failed to execute '%s'.", querystr->c);
  3524. PQclear(pg_result);
  3525. }
  3526. }
  3527. return -1;
  3528. }
  3529. /* {{{ php_pgsql_insert
  3530. */
  3531. PHPAPI int php_pgsql_insert(PGconn *pg_link, const char *table, zval *var_array, ulong opt, char **sql TSRMLS_DC)
  3532. {
  3533. zval **val, *converted = NULL;
  3534. char buf[256];
  3535. char *fld;
  3536. smart_str querystr = {0};
  3537. int key_type, fld_len, ret = FAILURE;
  3538. ulong num_idx;
  3539. HashPosition pos;
  3540. assert(pg_link != NULL);
  3541. assert(table != NULL);
  3542. assert(Z_TYPE_P(var_array) == IS_ARRAY);
  3543. if (zend_hash_num_elements(Z_ARRVAL_P(var_array)) == 0) {
  3544. return FAILURE;
  3545. }
  3546. /* convert input array if needed */
  3547. if (!(opt & PGSQL_DML_NO_CONV)) {
  3548. MAKE_STD_ZVAL(converted);
  3549. array_init(converted);
  3550. if (php_pgsql_convert(pg_link, table, var_array, converted, (opt & PGSQL_CONV_OPTS) TSRMLS_CC) == FAILURE) {
  3551. goto cleanup;
  3552. }
  3553. var_array = converted;
  3554. }
  3555. smart_str_appends(&querystr, "INSERT INTO ");
  3556. smart_str_appends(&querystr, table);
  3557. smart_str_appends(&querystr, " (");
  3558. zend_hash_internal_pointer_reset_ex(Z_ARRVAL_P(var_array), &pos);
  3559. while ((key_type = zend_hash_get_current_key_ex(Z_ARRVAL_P(var_array), &fld,
  3560. &fld_len, &num_idx, 0, &pos)) != HASH_KEY_NON_EXISTANT) {
  3561. if (key_type == HASH_KEY_IS_LONG) {
  3562. php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Expects associative array for values to be inserted.");
  3563. goto cleanup;
  3564. }
  3565. smart_str_appendl(&querystr, fld, fld_len - 1);
  3566. smart_str_appendc(&querystr, ',');
  3567. zend_hash_move_forward_ex(Z_ARRVAL_P(var_array), &pos);
  3568. }
  3569. querystr.len--;
  3570. smart_str_appends(&querystr, ") VALUES (");
  3571. /* make values string */
  3572. for (zend_hash_internal_pointer_reset_ex(Z_ARRVAL_P(var_array), &pos);
  3573. zend_hash_get_current_data_ex(Z_ARRVAL_P(var_array), (void **)&val, &pos) == SUCCESS;
  3574. zend_hash_move_forward_ex(Z_ARRVAL_P(var_array), &pos)) {
  3575. /* we can avoid the key_type check here, because we tested it in the other loop */
  3576. switch(Z_TYPE_PP(val)) {
  3577. case IS_STRING:
  3578. smart_str_appendl(&querystr, Z_STRVAL_PP(val), Z_STRLEN_PP(val));
  3579. break;
  3580. case IS_LONG:
  3581. smart_str_append_long(&querystr, Z_LVAL_PP(val));
  3582. break;
  3583. case IS_DOUBLE:
  3584. smart_str_appendl(&querystr, buf, sprintf(buf, "%f", Z_DVAL_PP(val)));
  3585. break;
  3586. default:
  3587. /* should not happen */
  3588. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Report this error to php-dev@lists.php.net, type = %d.", Z_TYPE_PP(val));
  3589. goto cleanup;
  3590. break;
  3591. }
  3592. smart_str_appendc(&querystr, ',');
  3593. }
  3594. /* Remove the trailing "," */
  3595. querystr.len--;
  3596. smart_str_appends(&querystr, ");");
  3597. smart_str_0(&querystr);
  3598. if ((opt & (PGSQL_DML_EXEC|PGSQL_DML_ASYNC)) &&
  3599. do_exec(&querystr, PGRES_COMMAND_OK, pg_link, (opt & PGSQL_CONV_OPTS) TSRMLS_CC) == 0) {
  3600. ret = SUCCESS;
  3601. }
  3602. else if (opt & PGSQL_DML_STRING) {
  3603. ret = SUCCESS;
  3604. }
  3605. cleanup:
  3606. if (!(opt & PGSQL_DML_NO_CONV)) {
  3607. zval_dtor(converted);
  3608. FREE_ZVAL(converted);
  3609. }
  3610. if (ret == SUCCESS && (opt & PGSQL_DML_STRING)) {
  3611. *sql = querystr.c;
  3612. }
  3613. else {
  3614. smart_str_free(&querystr);
  3615. }
  3616. return ret;
  3617. }
  3618. /* }}} */
  3619. /* {{{ proto mixed pg_insert(resource db, string table, array values[, int options])
  3620. Insert values (filed=>value) to table */
  3621. PHP_FUNCTION(pg_insert)
  3622. {
  3623. zval *pgsql_link, *values;
  3624. char *table, *sql = NULL;
  3625. ulong table_len;
  3626. ulong option = PGSQL_DML_EXEC;
  3627. PGconn *pg_link;
  3628. int id = -1, argc = ZEND_NUM_ARGS();
  3629. if (zend_parse_parameters(argc TSRMLS_CC, "rsa|l",
  3630. &pgsql_link, &table, &table_len, &values, &option) == FAILURE) {
  3631. return;
  3632. }
  3633. if (option & ~(PGSQL_CONV_OPTS|PGSQL_DML_NO_CONV|PGSQL_DML_EXEC|PGSQL_DML_ASYNC|PGSQL_DML_STRING)) {
  3634. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid option is specified.");
  3635. RETURN_FALSE;
  3636. }
  3637. ZEND_FETCH_RESOURCE2(pg_link, PGconn *, &pgsql_link, id, "PostgreSQL link", le_link, le_plink);
  3638. if (php_pgsql_flush_query(pg_link TSRMLS_CC)) {
  3639. php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Detected unhandled result(s) in connection.");
  3640. }
  3641. if (php_pgsql_insert(pg_link, table, values, option, &sql TSRMLS_CC) == FAILURE) {
  3642. RETURN_FALSE;
  3643. }
  3644. if (option & PGSQL_DML_STRING) {
  3645. RETURN_STRING(sql, 0);
  3646. }
  3647. RETURN_TRUE;
  3648. }
  3649. /* }}} */
  3650. static inline int build_assignment_string(smart_str *querystr, HashTable *ht, const char *pad, int pad_len TSRMLS_DC)
  3651. {
  3652. HashPosition pos;
  3653. size_t fld_len;
  3654. int key_type;
  3655. ulong num_idx;
  3656. char *fld;
  3657. char buf[256];
  3658. zval **val;
  3659. for (zend_hash_internal_pointer_reset_ex(ht, &pos);
  3660. zend_hash_get_current_data_ex(ht, (void **)&val, &pos) == SUCCESS;
  3661. zend_hash_move_forward_ex(ht, &pos)) {
  3662. key_type = zend_hash_get_current_key_ex(ht, &fld, &fld_len, &num_idx, 0, &pos);
  3663. if (key_type == HASH_KEY_IS_LONG) {
  3664. php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Expects associative array for values to be inserted.");
  3665. return -1;
  3666. }
  3667. smart_str_appendl(querystr, fld, fld_len - 1);
  3668. smart_str_appendc(querystr, '=');
  3669. switch(Z_TYPE_PP(val)) {
  3670. case IS_STRING:
  3671. smart_str_appendl(querystr, Z_STRVAL_PP(val), Z_STRLEN_PP(val));
  3672. break;
  3673. case IS_LONG:
  3674. smart_str_append_long(querystr, Z_LVAL_PP(val));
  3675. break;
  3676. case IS_DOUBLE:
  3677. smart_str_appendl(querystr, buf, sprintf(buf, "%f", Z_DVAL_PP(val)));
  3678. break;
  3679. default:
  3680. /* should not happen */
  3681. php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Expects scaler values other than NULL. Need to convert?");
  3682. return -1;
  3683. }
  3684. smart_str_appendl(querystr, pad, pad_len);
  3685. }
  3686. querystr->len -= pad_len;
  3687. return 0;
  3688. }
  3689. /* {{{ php_pgsql_update
  3690. */
  3691. PHPAPI int php_pgsql_update(PGconn *pg_link, const char *table, zval *var_array, zval *ids_array, ulong opt, char **sql TSRMLS_DC)
  3692. {
  3693. zval *var_converted = NULL, *ids_converted = NULL;
  3694. smart_str querystr = {0};
  3695. int ret = FAILURE;
  3696. assert(pg_link != NULL);
  3697. assert(table != NULL);
  3698. assert(Z_TYPE_P(var_array) == IS_ARRAY);
  3699. assert(Z_TYPE_P(ids_array) == IS_ARRAY);
  3700. assert(!(opt & ~(PGSQL_CONV_OPTS|PGSQL_DML_NO_CONV|PGSQL_DML_EXEC|PGSQL_DML_STRING)));
  3701. if (zend_hash_num_elements(Z_ARRVAL_P(var_array)) == 0
  3702. || zend_hash_num_elements(Z_ARRVAL_P(ids_array)) == 0) {
  3703. return FAILURE;
  3704. }
  3705. if (!(opt & PGSQL_DML_NO_CONV)) {
  3706. MAKE_STD_ZVAL(var_converted);
  3707. array_init(var_converted);
  3708. if (php_pgsql_convert(pg_link, table, var_array, var_converted, (opt & PGSQL_CONV_OPTS) TSRMLS_CC) == FAILURE) {
  3709. goto cleanup;
  3710. }
  3711. var_array = var_converted;
  3712. MAKE_STD_ZVAL(ids_converted);
  3713. array_init(ids_converted);
  3714. if (php_pgsql_convert(pg_link, table, ids_array, ids_converted, (opt & PGSQL_CONV_OPTS) TSRMLS_CC) == FAILURE) {
  3715. goto cleanup;
  3716. }
  3717. ids_array = ids_converted;
  3718. }
  3719. smart_str_appends(&querystr, "UPDATE ");
  3720. smart_str_appends(&querystr, table);
  3721. smart_str_appends(&querystr, " SET ");
  3722. if (build_assignment_string(&querystr, Z_ARRVAL_P(var_array), ",", 1 TSRMLS_CC))
  3723. goto cleanup;
  3724. smart_str_appends(&querystr, " WHERE ");
  3725. if (build_assignment_string(&querystr, Z_ARRVAL_P(ids_array), " AND ", sizeof(" AND ")-1 TSRMLS_CC))
  3726. goto cleanup;
  3727. smart_str_appendc(&querystr, ';');
  3728. smart_str_0(&querystr);
  3729. if (do_exec(&querystr, PGRES_COMMAND_OK, pg_link, opt TSRMLS_CC) == 0)
  3730. ret = SUCCESS;
  3731. cleanup:
  3732. if (var_converted) {
  3733. zval_dtor(var_converted);
  3734. FREE_ZVAL(var_converted);
  3735. }
  3736. if (ids_converted) {
  3737. zval_dtor(ids_converted);
  3738. FREE_ZVAL(ids_converted);
  3739. }
  3740. if (ret == SUCCESS && (opt & PGSQL_DML_STRING)) {
  3741. *sql = querystr.c;
  3742. }
  3743. else {
  3744. smart_str_free(&querystr);
  3745. }
  3746. return ret;
  3747. }
  3748. /* }}} */
  3749. /* {{{ proto mixed pg_update(resource db, string table, array fields, array ids[, int options])
  3750. Update table using values (field=>value) and ids (id=>value) */
  3751. PHP_FUNCTION(pg_update)
  3752. {
  3753. zval *pgsql_link, *values, *ids;
  3754. char *table, *sql = NULL;
  3755. ulong table_len;
  3756. ulong option = PGSQL_DML_EXEC;
  3757. PGconn *pg_link;
  3758. int id = -1, argc = ZEND_NUM_ARGS();
  3759. if (zend_parse_parameters(argc TSRMLS_CC, "rsaa|l",
  3760. &pgsql_link, &table, &table_len, &values, &ids, &option) == FAILURE) {
  3761. return;
  3762. }
  3763. if (option & ~(PGSQL_CONV_OPTS|PGSQL_DML_NO_CONV|PGSQL_DML_EXEC|PGSQL_DML_STRING)) {
  3764. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid option is specified.");
  3765. RETURN_FALSE;
  3766. }
  3767. ZEND_FETCH_RESOURCE2(pg_link, PGconn *, &pgsql_link, id, "PostgreSQL link", le_link, le_plink);
  3768. if (php_pgsql_flush_query(pg_link TSRMLS_CC)) {
  3769. php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Detected unhandled result(s) in connection.");
  3770. }
  3771. if (php_pgsql_update(pg_link, table, values, ids, option, &sql TSRMLS_CC) == FAILURE) {
  3772. RETURN_FALSE;
  3773. }
  3774. if (option & PGSQL_DML_STRING) {
  3775. RETURN_STRING(sql, 0);
  3776. }
  3777. RETURN_TRUE;
  3778. }
  3779. /* }}} */
  3780. /* {{{ php_pgsql_delete
  3781. */
  3782. PHPAPI int php_pgsql_delete(PGconn *pg_link, const char *table, zval *ids_array, ulong opt, char **sql TSRMLS_DC)
  3783. {
  3784. zval *ids_converted = NULL;
  3785. smart_str querystr = {0};
  3786. int ret = FAILURE;
  3787. assert(pg_link != NULL);
  3788. assert(table != NULL);
  3789. assert(Z_TYPE_P(ids_array) == IS_ARRAY);
  3790. assert(!(opt & ~(PGSQL_CONV_FORCE_NULL|PGSQL_DML_EXEC|PGSQL_DML_STRING)));
  3791. if (zend_hash_num_elements(Z_ARRVAL_P(ids_array)) == 0) {
  3792. return FAILURE;
  3793. }
  3794. if (!(opt & PGSQL_DML_NO_CONV)) {
  3795. MAKE_STD_ZVAL(ids_converted);
  3796. array_init(ids_converted);
  3797. if (php_pgsql_convert(pg_link, table, ids_array, ids_converted, (opt & PGSQL_CONV_OPTS) TSRMLS_CC) == FAILURE) {
  3798. goto cleanup;
  3799. }
  3800. ids_array = ids_converted;
  3801. }
  3802. smart_str_appends(&querystr, "DELETE FROM ");
  3803. smart_str_appends(&querystr, table);
  3804. smart_str_appends(&querystr, " WHERE ");
  3805. if (build_assignment_string(&querystr, Z_ARRVAL_P(ids_array), " AND ", sizeof(" AND ")-1 TSRMLS_CC))
  3806. goto cleanup;
  3807. smart_str_appendc(&querystr, ';');
  3808. smart_str_0(&querystr);
  3809. if (do_exec(&querystr, PGRES_COMMAND_OK, pg_link, opt TSRMLS_CC) == 0)
  3810. ret = SUCCESS;
  3811. cleanup:
  3812. if (!(opt & PGSQL_DML_NO_CONV)) {
  3813. zval_dtor(ids_converted);
  3814. FREE_ZVAL(ids_converted);
  3815. }
  3816. if (ret == SUCCESS && (opt & PGSQL_DML_STRING)) {
  3817. *sql = estrdup(querystr.c);
  3818. }
  3819. else {
  3820. smart_str_free(&querystr);
  3821. }
  3822. return ret;
  3823. }
  3824. /* }}} */
  3825. /* {{{ proto mixed pg_delete(resource db, string table, array ids[, int options])
  3826. Delete records has ids (id=>value) */
  3827. PHP_FUNCTION(pg_delete)
  3828. {
  3829. zval *pgsql_link, *ids;
  3830. char *table, *sql = NULL;
  3831. ulong table_len, option = PGSQL_DML_EXEC;
  3832. PGconn *pg_link;
  3833. int id = -1, argc = ZEND_NUM_ARGS();
  3834. if (zend_parse_parameters(argc TSRMLS_CC, "rsa|l",
  3835. &pgsql_link, &table, &table_len, &ids, &option) == FAILURE) {
  3836. return;
  3837. }
  3838. if (option & ~(PGSQL_CONV_FORCE_NULL|PGSQL_DML_NO_CONV|PGSQL_DML_EXEC|PGSQL_DML_STRING)) {
  3839. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid option is specified.");
  3840. RETURN_FALSE;
  3841. }
  3842. ZEND_FETCH_RESOURCE2(pg_link, PGconn *, &pgsql_link, id, "PostgreSQL link", le_link, le_plink);
  3843. if (php_pgsql_flush_query(pg_link TSRMLS_CC)) {
  3844. php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Detected unhandled result(s) in connection.");
  3845. }
  3846. if (php_pgsql_delete(pg_link, table, ids, option, &sql TSRMLS_CC) == FAILURE) {
  3847. RETURN_FALSE;
  3848. }
  3849. if (option & PGSQL_DML_STRING) {
  3850. RETURN_STRING(sql, 0);
  3851. }
  3852. RETURN_TRUE;
  3853. }
  3854. /* }}} */
  3855. /* {{{ php_pgsql_result2array
  3856. */
  3857. PHPAPI int php_pgsql_result2array(PGresult *pg_result, zval *ret_array TSRMLS_DC)
  3858. {
  3859. zval *row;
  3860. char *field_name, *element, *data;
  3861. size_t num_fields, element_len, data_len;
  3862. int pg_numrows, pg_row;
  3863. uint i;
  3864. assert(Z_TYPE_P(ret_array) == IS_ARRAY);
  3865. if ((pg_numrows = PQntuples(pg_result)) <= 0) {
  3866. return FAILURE;
  3867. }
  3868. for (pg_row = 0; pg_row < pg_numrows; pg_row++) {
  3869. MAKE_STD_ZVAL(row);
  3870. array_init(row);
  3871. add_index_zval(ret_array, pg_row, row);
  3872. for (i = 0, num_fields = PQnfields(pg_result); i < num_fields; i++) {
  3873. if (PQgetisnull(pg_result, pg_row, i)) {
  3874. field_name = PQfname(pg_result, i);
  3875. add_assoc_null(row, field_name);
  3876. } else {
  3877. element = PQgetvalue(pg_result, pg_row, i);
  3878. element_len = (element ? strlen(element) : 0);
  3879. if (element) {
  3880. if (PG(magic_quotes_runtime)) {
  3881. data = php_addslashes(element, element_len, &data_len, 0 TSRMLS_CC);
  3882. } else {
  3883. data = safe_estrndup(element, element_len);
  3884. data_len = element_len;
  3885. }
  3886. field_name = PQfname(pg_result, i);
  3887. add_assoc_stringl(row, field_name, data, data_len, 0);
  3888. }
  3889. }
  3890. }
  3891. }
  3892. return SUCCESS;
  3893. }
  3894. /* }}} */
  3895. /* {{{ php_pgsql_select
  3896. */
  3897. PHPAPI int php_pgsql_select(PGconn *pg_link, const char *table, zval *ids_array, zval *ret_array, ulong opt, char **sql TSRMLS_DC)
  3898. {
  3899. zval *ids_converted = NULL;
  3900. smart_str querystr = {0};
  3901. int ret = FAILURE;
  3902. PGresult *pg_result;
  3903. assert(pg_link != NULL);
  3904. assert(table != NULL);
  3905. assert(Z_TYPE_P(ids_array) == IS_ARRAY);
  3906. assert(Z_TYPE_P(ret_array) == IS_ARRAY);
  3907. assert(!(opt & ~(PGSQL_CONV_OPTS|PGSQL_DML_NO_CONV|PGSQL_DML_EXEC|PGSQL_DML_ASYNC|PGSQL_DML_STRING)));
  3908. if (zend_hash_num_elements(Z_ARRVAL_P(ids_array)) == 0) {
  3909. return FAILURE;
  3910. }
  3911. if (!(opt & PGSQL_DML_NO_CONV)) {
  3912. MAKE_STD_ZVAL(ids_converted);
  3913. array_init(ids_converted);
  3914. if (php_pgsql_convert(pg_link, table, ids_array, ids_converted, (opt & PGSQL_CONV_OPTS) TSRMLS_CC) == FAILURE) {
  3915. goto cleanup;
  3916. }
  3917. ids_array = ids_converted;
  3918. }
  3919. smart_str_appends(&querystr, "SELECT * FROM ");
  3920. smart_str_appends(&querystr, table);
  3921. smart_str_appends(&querystr, " WHERE ");
  3922. if (build_assignment_string(&querystr, Z_ARRVAL_P(ids_array), " AND ", sizeof(" AND ")-1 TSRMLS_CC))
  3923. goto cleanup;
  3924. smart_str_appendc(&querystr, ';');
  3925. smart_str_0(&querystr);
  3926. pg_result = PQexec(pg_link, querystr.c);
  3927. if (PQresultStatus(pg_result) == PGRES_TUPLES_OK) {
  3928. ret = php_pgsql_result2array(pg_result, ret_array TSRMLS_CC);
  3929. } else {
  3930. php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Failed to execute '%s'.", querystr.c);
  3931. PQclear(pg_result);
  3932. }
  3933. cleanup:
  3934. if (!(opt & PGSQL_DML_NO_CONV)) {
  3935. zval_dtor(ids_converted);
  3936. FREE_ZVAL(ids_converted);
  3937. }
  3938. if (ret == SUCCESS && (opt & PGSQL_DML_STRING)) {
  3939. *sql = querystr.c;
  3940. }
  3941. else {
  3942. smart_str_free(&querystr);
  3943. }
  3944. return ret;
  3945. }
  3946. /* }}} */
  3947. /* {{{ proto mixed pg_select(resource db, string table, array ids[, int options])
  3948. Select records that has ids (id=>value) */
  3949. PHP_FUNCTION(pg_select)
  3950. {
  3951. zval *pgsql_link, *ids;
  3952. char *table, *sql = NULL;
  3953. ulong table_len, option = PGSQL_DML_EXEC;
  3954. PGconn *pg_link;
  3955. int id = -1, argc = ZEND_NUM_ARGS();
  3956. if (zend_parse_parameters(argc TSRMLS_CC, "rsa|l",
  3957. &pgsql_link, &table, &table_len, &ids, &option) == FAILURE) {
  3958. return;
  3959. }
  3960. if (option & ~(PGSQL_CONV_FORCE_NULL|PGSQL_DML_NO_CONV|PGSQL_DML_EXEC|PGSQL_DML_ASYNC|PGSQL_DML_STRING)) {
  3961. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid option is specified.");
  3962. RETURN_FALSE;
  3963. }
  3964. ZEND_FETCH_RESOURCE2(pg_link, PGconn *, &pgsql_link, id, "PostgreSQL link", le_link, le_plink);
  3965. if (php_pgsql_flush_query(pg_link TSRMLS_CC)) {
  3966. php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Detected unhandled result(s) in connection.");
  3967. }
  3968. array_init(return_value);
  3969. if (php_pgsql_select(pg_link, table, ids, return_value, option, &sql TSRMLS_CC) == FAILURE) {
  3970. zval_dtor(return_value);
  3971. RETURN_FALSE;
  3972. }
  3973. if (option & PGSQL_DML_STRING) {
  3974. zval_dtor(return_value);
  3975. RETURN_STRING(sql, 0);
  3976. }
  3977. return;
  3978. }
  3979. /* }}} */
  3980. #endif
  3981. /*
  3982. * Local variables:
  3983. * tab-width: 4
  3984. * c-basic-offset: 4
  3985. * End:
  3986. * vim600: sw=4 ts=4 fdm=marker
  3987. * vim<600: sw=4 ts=4
  3988. */