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