xref: /PHP-5.6/ext/pgsql/php_pgsql.h (revision 49493a2d)
1 /*
2    +----------------------------------------------------------------------+
3    | PHP Version 5                                                        |
4    +----------------------------------------------------------------------+
5    | Copyright (c) 1997-2016 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 #ifdef PHP_PGSQL_PRIVATE
33 #undef SOCKET_SIZE_TYPE
34 #include <libpq-fe.h>
35 
36 #ifdef PHP_WIN32
37 #define INV_WRITE            0x00020000
38 #define INV_READ             0x00040000
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 #include <libpq/libpq-fs.h>
47 # if defined(__GNUC__) && __GNUC__ >= 4
48 #  define PHP_PGSQL_API __attribute__ ((visibility("default")))
49 # else
50 #  define PHP_PGSQL_API
51 # endif
52 #endif
53 
54 #ifdef HAVE_PG_CONFIG_H
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 DEAFULT 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 
212 /* exported functions */
213 PHP_PGSQL_API int php_pgsql_meta_data(PGconn *pg_link, const char *table_name, zval *meta, zend_bool extended TSRMLS_DC);
214 PHP_PGSQL_API int php_pgsql_convert(PGconn *pg_link, const char *table_name, const zval *values, zval *result, ulong opt TSRMLS_DC);
215 PHP_PGSQL_API int php_pgsql_insert(PGconn *pg_link, const char *table, zval *values, ulong opt, char **sql TSRMLS_DC);
216 PHP_PGSQL_API int php_pgsql_update(PGconn *pg_link, const char *table, zval *values, zval *ids, ulong opt , char **sql TSRMLS_DC);
217 PHP_PGSQL_API int php_pgsql_delete(PGconn *pg_link, const char *table, zval *ids, ulong opt, char **sql TSRMLS_DC);
218 PHP_PGSQL_API int php_pgsql_select(PGconn *pg_link, const char *table, zval *ids, zval *ret_array, ulong opt, char **sql  TSRMLS_DC);
219 PHP_PGSQL_API int php_pgsql_result2array(PGresult *pg_result, zval *ret_array TSRMLS_DC);
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 TSRMLS_DC);
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 TSRMLS_DC);
231 static size_t php_pgsql_fd_read(php_stream *stream, char *buf, size_t count TSRMLS_DC);
232 static int php_pgsql_fd_close(php_stream *stream, int close_handle TSRMLS_DC);
233 static int php_pgsql_fd_flush(php_stream *stream TSRMLS_DC);
234 static int php_pgsql_fd_set_option(php_stream *stream, int option, int value, void *ptrparam TSRMLS_DC);
235 static int php_pgsql_fd_cast(php_stream *stream, int cast_as, void **ret TSRMLS_DC);
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 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 	long default_link; /* default link when connection is omitted */
313 	long num_links,num_persistent;
314 	long max_links,max_persistent;
315 	long allow_persistent;
316 	long auto_reset_persistent;
317 	int le_lofp,le_string;
318 	int ignore_notices,log_notices;
319 	HashTable notices;  /* notice message for each connection */
320 ZEND_END_MODULE_GLOBALS(pgsql)
321 
322 ZEND_EXTERN_MODULE_GLOBALS(pgsql)
323 
324 #ifdef ZTS
325 # define PGG(v) TSRMG(pgsql_globals_id, zend_pgsql_globals *, v)
326 #else
327 # define PGG(v) (pgsql_globals.v)
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