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.

341 lines
10 KiB

12 years ago
24 years ago
24 years ago
24 years ago
24 years ago
12 years ago
  1. /*
  2. +----------------------------------------------------------------------+
  3. | PHP Version 7 |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1997-2017 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. +----------------------------------------------------------------------+
  18. */
  19. /* $Id$ */
  20. #ifndef PHP_PGSQL_H
  21. #define PHP_PGSQL_H
  22. #if HAVE_PGSQL
  23. #define PHP_PGSQL_API_VERSION 20140217
  24. extern zend_module_entry pgsql_module_entry;
  25. #define pgsql_module_ptr &pgsql_module_entry
  26. #include "php_version.h"
  27. #define PHP_PGSQL_VERSION PHP_VERSION
  28. #ifdef PHP_PGSQL_PRIVATE
  29. #undef SOCKET_SIZE_TYPE
  30. #include <libpq-fe.h>
  31. #ifdef PHP_WIN32
  32. #define INV_WRITE 0x00020000
  33. #define INV_READ 0x00040000
  34. #undef PHP_PGSQL_API
  35. #ifdef PGSQL_EXPORTS
  36. #define PHP_PGSQL_API __declspec(dllexport)
  37. #else
  38. #define PHP_PGSQL_API __declspec(dllimport)
  39. #endif
  40. #else
  41. #include <libpq/libpq-fs.h>
  42. # if defined(__GNUC__) && __GNUC__ >= 4
  43. # define PHP_PGSQL_API __attribute__ ((visibility("default")))
  44. # else
  45. # define PHP_PGSQL_API
  46. # endif
  47. #endif
  48. #ifdef HAVE_PG_CONFIG_H
  49. #include <pg_config.h>
  50. #endif
  51. #ifdef HAVE_PGSQL_WITH_MULTIBYTE_SUPPORT
  52. const char * pg_encoding_to_char(int encoding);
  53. #endif
  54. PHP_MINIT_FUNCTION(pgsql);
  55. PHP_MSHUTDOWN_FUNCTION(pgsql);
  56. PHP_RINIT_FUNCTION(pgsql);
  57. PHP_RSHUTDOWN_FUNCTION(pgsql);
  58. PHP_MINFO_FUNCTION(pgsql);
  59. /* connection functions */
  60. PHP_FUNCTION(pg_connect);
  61. PHP_FUNCTION(pg_pconnect);
  62. PHP_FUNCTION(pg_connect_poll);
  63. PHP_FUNCTION(pg_close);
  64. PHP_FUNCTION(pg_connection_reset);
  65. PHP_FUNCTION(pg_connection_status);
  66. PHP_FUNCTION(pg_connection_busy);
  67. PHP_FUNCTION(pg_host);
  68. PHP_FUNCTION(pg_dbname);
  69. PHP_FUNCTION(pg_port);
  70. PHP_FUNCTION(pg_tty);
  71. PHP_FUNCTION(pg_options);
  72. PHP_FUNCTION(pg_version);
  73. PHP_FUNCTION(pg_ping);
  74. #if HAVE_PQPARAMETERSTATUS
  75. PHP_FUNCTION(pg_parameter_status);
  76. #endif
  77. #if HAVE_PGTRANSACTIONSTATUS
  78. PHP_FUNCTION(pg_transaction_status);
  79. #endif
  80. /* query functions */
  81. PHP_FUNCTION(pg_query);
  82. #if HAVE_PQEXECPARAMS
  83. PHP_FUNCTION(pg_query_params);
  84. #endif
  85. #if HAVE_PQPREPARE
  86. PHP_FUNCTION(pg_prepare);
  87. #endif
  88. #if HAVE_PQEXECPREPARED
  89. PHP_FUNCTION(pg_execute);
  90. #endif
  91. PHP_FUNCTION(pg_send_query);
  92. #if HAVE_PQSENDQUERYPARAMS
  93. PHP_FUNCTION(pg_send_query_params);
  94. #endif
  95. #if HAVE_PQSENDPREPARE
  96. PHP_FUNCTION(pg_send_prepare);
  97. #endif
  98. #if HAVE_PQSENDQUERYPREPARED
  99. PHP_FUNCTION(pg_send_execute);
  100. #endif
  101. PHP_FUNCTION(pg_cancel_query);
  102. /* result functions */
  103. PHP_FUNCTION(pg_fetch_assoc);
  104. PHP_FUNCTION(pg_fetch_array);
  105. PHP_FUNCTION(pg_fetch_object);
  106. PHP_FUNCTION(pg_fetch_result);
  107. PHP_FUNCTION(pg_fetch_row);
  108. PHP_FUNCTION(pg_fetch_all);
  109. PHP_FUNCTION(pg_fetch_all_columns);
  110. #if HAVE_PQCMDTUPLES
  111. PHP_FUNCTION(pg_affected_rows);
  112. #endif
  113. PHP_FUNCTION(pg_get_result);
  114. PHP_FUNCTION(pg_result_seek);
  115. PHP_FUNCTION(pg_result_status);
  116. PHP_FUNCTION(pg_free_result);
  117. PHP_FUNCTION(pg_last_oid);
  118. PHP_FUNCTION(pg_num_rows);
  119. PHP_FUNCTION(pg_num_fields);
  120. PHP_FUNCTION(pg_field_name);
  121. PHP_FUNCTION(pg_field_num);
  122. PHP_FUNCTION(pg_field_size);
  123. PHP_FUNCTION(pg_field_type);
  124. PHP_FUNCTION(pg_field_type_oid);
  125. PHP_FUNCTION(pg_field_prtlen);
  126. PHP_FUNCTION(pg_field_is_null);
  127. PHP_FUNCTION(pg_field_table);
  128. /* async message functions */
  129. PHP_FUNCTION(pg_get_notify);
  130. PHP_FUNCTION(pg_socket);
  131. PHP_FUNCTION(pg_consume_input);
  132. PHP_FUNCTION(pg_flush);
  133. PHP_FUNCTION(pg_get_pid);
  134. /* error message functions */
  135. PHP_FUNCTION(pg_result_error);
  136. #if HAVE_PQRESULTERRORFIELD
  137. PHP_FUNCTION(pg_result_error_field);
  138. #endif
  139. PHP_FUNCTION(pg_last_error);
  140. PHP_FUNCTION(pg_last_notice);
  141. /* copy functions */
  142. PHP_FUNCTION(pg_put_line);
  143. PHP_FUNCTION(pg_end_copy);
  144. PHP_FUNCTION(pg_copy_to);
  145. PHP_FUNCTION(pg_copy_from);
  146. /* large object functions */
  147. PHP_FUNCTION(pg_lo_create);
  148. PHP_FUNCTION(pg_lo_unlink);
  149. PHP_FUNCTION(pg_lo_open);
  150. PHP_FUNCTION(pg_lo_close);
  151. PHP_FUNCTION(pg_lo_read);
  152. PHP_FUNCTION(pg_lo_write);
  153. PHP_FUNCTION(pg_lo_read_all);
  154. PHP_FUNCTION(pg_lo_import);
  155. PHP_FUNCTION(pg_lo_export);
  156. PHP_FUNCTION(pg_lo_seek);
  157. PHP_FUNCTION(pg_lo_tell);
  158. #if HAVE_PG_LO_TRUNCATE
  159. PHP_FUNCTION(pg_lo_truncate);
  160. #endif
  161. /* debugging functions */
  162. PHP_FUNCTION(pg_trace);
  163. PHP_FUNCTION(pg_untrace);
  164. /* utility functions */
  165. PHP_FUNCTION(pg_client_encoding);
  166. PHP_FUNCTION(pg_set_client_encoding);
  167. #if HAVE_PQSETERRORVERBOSITY
  168. PHP_FUNCTION(pg_set_error_verbosity);
  169. #endif
  170. #if HAVE_PQESCAPE
  171. PHP_FUNCTION(pg_escape_string);
  172. PHP_FUNCTION(pg_escape_bytea);
  173. PHP_FUNCTION(pg_unescape_bytea);
  174. PHP_FUNCTION(pg_escape_literal);
  175. PHP_FUNCTION(pg_escape_identifier);
  176. #endif
  177. /* misc functions */
  178. PHP_FUNCTION(pg_meta_data);
  179. PHP_FUNCTION(pg_convert);
  180. PHP_FUNCTION(pg_insert);
  181. PHP_FUNCTION(pg_update);
  182. PHP_FUNCTION(pg_delete);
  183. PHP_FUNCTION(pg_select);
  184. /* connection options - ToDo: Add async connection option */
  185. #define PGSQL_CONNECT_FORCE_NEW (1<<1)
  186. #define PGSQL_CONNECT_ASYNC (1<<2)
  187. /* php_pgsql_convert options */
  188. #define PGSQL_CONV_IGNORE_DEFAULT (1<<1) /* Do not use DEAFULT value by removing field from returned array */
  189. #define PGSQL_CONV_FORCE_NULL (1<<2) /* Convert to NULL if string is null string */
  190. #define PGSQL_CONV_IGNORE_NOT_NULL (1<<3) /* Ignore NOT NULL constraints */
  191. #define PGSQL_CONV_OPTS (PGSQL_CONV_IGNORE_DEFAULT|PGSQL_CONV_FORCE_NULL|PGSQL_CONV_IGNORE_NOT_NULL)
  192. /* php_pgsql_insert/update/select/delete options */
  193. #define PGSQL_DML_NO_CONV (1<<8) /* Do not call php_pgsql_convert() */
  194. #define PGSQL_DML_EXEC (1<<9) /* Execute query */
  195. #define PGSQL_DML_ASYNC (1<<10) /* Do async query */
  196. #define PGSQL_DML_STRING (1<<11) /* Return query string */
  197. #define PGSQL_DML_ESCAPE (1<<12) /* No convert, but escape only */
  198. /* exported functions */
  199. PHP_PGSQL_API int php_pgsql_meta_data(PGconn *pg_link, const char *table_name, zval *meta, zend_bool extended);
  200. PHP_PGSQL_API int php_pgsql_convert(PGconn *pg_link, const char *table_name, const zval *values, zval *result, zend_ulong opt);
  201. PHP_PGSQL_API int php_pgsql_insert(PGconn *pg_link, const char *table, zval *values, zend_ulong opt, zend_string **sql);
  202. PHP_PGSQL_API int php_pgsql_update(PGconn *pg_link, const char *table, zval *values, zval *ids, zend_ulong opt , zend_string **sql);
  203. PHP_PGSQL_API int php_pgsql_delete(PGconn *pg_link, const char *table, zval *ids, zend_ulong opt, zend_string **sql);
  204. PHP_PGSQL_API int php_pgsql_select(PGconn *pg_link, const char *table, zval *ids, zval *ret_array, zend_ulong opt, long fetch_option, zend_string **sql );
  205. PHP_PGSQL_API int php_pgsql_result2array(PGresult *pg_result, zval *ret_array, long fetch_option);
  206. /* internal functions */
  207. static void php_pgsql_do_connect(INTERNAL_FUNCTION_PARAMETERS,int persistent);
  208. static void php_pgsql_get_link_info(INTERNAL_FUNCTION_PARAMETERS, int entry_type);
  209. static void php_pgsql_get_result_info(INTERNAL_FUNCTION_PARAMETERS, int entry_type);
  210. static char *get_field_name(PGconn *pgsql, Oid oid, HashTable *list);
  211. static void php_pgsql_get_field_info(INTERNAL_FUNCTION_PARAMETERS, int entry_type);
  212. static void php_pgsql_data_info(INTERNAL_FUNCTION_PARAMETERS, int entry_type);
  213. static void php_pgsql_do_async(INTERNAL_FUNCTION_PARAMETERS,int entry_type);
  214. static size_t php_pgsql_fd_write(php_stream *stream, const char *buf, size_t count);
  215. static size_t php_pgsql_fd_read(php_stream *stream, char *buf, size_t count);
  216. static int php_pgsql_fd_close(php_stream *stream, int close_handle);
  217. static int php_pgsql_fd_flush(php_stream *stream);
  218. static int php_pgsql_fd_set_option(php_stream *stream, int option, int value, void *ptrparam);
  219. static int php_pgsql_fd_cast(php_stream *stream, int cast_as, void **ret);
  220. typedef enum _php_pgsql_data_type {
  221. /* boolean */
  222. PG_BOOL,
  223. /* number */
  224. PG_OID,
  225. PG_INT2,
  226. PG_INT4,
  227. PG_INT8,
  228. PG_FLOAT4,
  229. PG_FLOAT8,
  230. PG_NUMERIC,
  231. PG_MONEY,
  232. /* character */
  233. PG_TEXT,
  234. PG_CHAR,
  235. PG_VARCHAR,
  236. /* time and interval */
  237. PG_UNIX_TIME,
  238. PG_UNIX_TIME_INTERVAL,
  239. PG_DATE,
  240. PG_TIME,
  241. PG_TIME_WITH_TIMEZONE,
  242. PG_TIMESTAMP,
  243. PG_TIMESTAMP_WITH_TIMEZONE,
  244. PG_INTERVAL,
  245. /* binary */
  246. PG_BYTEA,
  247. /* network */
  248. PG_CIDR,
  249. PG_INET,
  250. PG_MACADDR,
  251. /* bit */
  252. PG_BIT,
  253. PG_VARBIT,
  254. /* geometoric */
  255. PG_LINE,
  256. PG_LSEG,
  257. PG_POINT,
  258. PG_BOX,
  259. PG_PATH,
  260. PG_POLYGON,
  261. PG_CIRCLE,
  262. /* unknown and system */
  263. PG_UNKNOWN
  264. } php_pgsql_data_type;
  265. typedef struct pgLofp {
  266. PGconn *conn;
  267. int lofd;
  268. } pgLofp;
  269. typedef struct _php_pgsql_result_handle {
  270. PGconn *conn;
  271. PGresult *result;
  272. int row;
  273. } pgsql_result_handle;
  274. typedef struct _php_pgsql_notice {
  275. char *message;
  276. size_t len;
  277. } php_pgsql_notice;
  278. static const php_stream_ops php_stream_pgsql_fd_ops = {
  279. php_pgsql_fd_write,
  280. php_pgsql_fd_read,
  281. php_pgsql_fd_close,
  282. php_pgsql_fd_flush,
  283. "PostgreSQL link",
  284. NULL, /* seek */
  285. php_pgsql_fd_cast, /* cast */
  286. NULL, /* stat */
  287. php_pgsql_fd_set_option
  288. };
  289. ZEND_BEGIN_MODULE_GLOBALS(pgsql)
  290. zend_long num_links,num_persistent;
  291. zend_long max_links,max_persistent;
  292. zend_long allow_persistent;
  293. zend_long auto_reset_persistent;
  294. int le_lofp,le_string;
  295. int ignore_notices,log_notices;
  296. HashTable notices; /* notice message for each connection */
  297. zend_resource *default_link; /* default link when connection is omitted */
  298. ZEND_END_MODULE_GLOBALS(pgsql)
  299. ZEND_EXTERN_MODULE_GLOBALS(pgsql)
  300. # define PGG(v) ZEND_MODULE_GLOBALS_ACCESSOR(pgsql, v)
  301. #if defined(ZTS) && defined(COMPILE_DL_PGSQL)
  302. ZEND_TSRMLS_CACHE_EXTERN()
  303. #endif
  304. #endif
  305. #else
  306. #define pgsql_module_ptr NULL
  307. #endif
  308. #define phpext_pgsql_ptr pgsql_module_ptr
  309. #endif /* PHP_PGSQL_H */