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