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.

4275 lines
117 KiB

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