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.

7198 lines
197 KiB

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