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