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.

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