php_pgsql.h 10 KB

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