PageRenderTime 57ms CodeModel.GetById 1ms RepoModel.GetById 0ms app.codeStats 1ms

/ext/pgsql/pgsql.c

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