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

6711 lines
185 KiB

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