xref: /php-src/ext/pgsql/php_pgsql.h (revision ba54cebb)
1 /*
2    +----------------------------------------------------------------------+
3    | Copyright (c) The PHP Group                                          |
4    +----------------------------------------------------------------------+
5    | This source file is subject to version 3.01 of the PHP license,      |
6    | that is bundled with this package in the file LICENSE, and is        |
7    | available through the world-wide-web at the following url:           |
8    | https://www.php.net/license/3_01.txt                                 |
9    | If you did not receive a copy of the PHP license and are unable to   |
10    | obtain it through the world-wide-web, please send a note to          |
11    | license@php.net so we can mail you a copy immediately.               |
12    +----------------------------------------------------------------------+
13    | Authors: Zeev Suraski <zeev@php.net>                                 |
14    |          Jouni Ahto <jouni.ahto@exdec.fi>                            |
15    +----------------------------------------------------------------------+
16  */
17 
18 #ifndef PHP_PGSQL_H
19 #define PHP_PGSQL_H
20 
21 #ifdef HAVE_PGSQL
22 
23 extern zend_module_entry pgsql_module_entry;
24 #define pgsql_module_ptr &pgsql_module_entry
25 
26 #include "php_version.h"
27 #define PHP_PGSQL_VERSION PHP_VERSION
28 
29 #ifdef PHP_PGSQL_PRIVATE
30 #undef SOCKET_SIZE_TYPE
31 #include <libpq-fe.h>
32 
33 #include <libpq/libpq-fs.h>
34 #ifdef PHP_WIN32
35 #undef PHP_PGSQL_API
36 #ifdef PGSQL_EXPORTS
37 #define PHP_PGSQL_API __declspec(dllexport)
38 #else
39 #define PHP_PGSQL_API __declspec(dllimport)
40 #endif
41 #else
42 # if defined(__GNUC__) && __GNUC__ >= 4
43 #  define PHP_PGSQL_API __attribute__ ((visibility("default")))
44 # else
45 #  define PHP_PGSQL_API
46 # endif
47 #endif
48 
49 PHP_MINIT_FUNCTION(pgsql);
50 PHP_MSHUTDOWN_FUNCTION(pgsql);
51 PHP_RINIT_FUNCTION(pgsql);
52 PHP_RSHUTDOWN_FUNCTION(pgsql);
53 PHP_MINFO_FUNCTION(pgsql);
54 
55 /* connection options - ToDo: Add async connection option */
56 #define PGSQL_CONNECT_FORCE_NEW     (1<<1)
57 #define PGSQL_CONNECT_ASYNC         (1<<2)
58 /* php_pgsql_convert options */
59 #define PGSQL_CONV_IGNORE_DEFAULT   (1<<1)     /* Do not use DEFAULT value by removing field from returned array */
60 #define PGSQL_CONV_FORCE_NULL       (1<<2)     /* Convert to NULL if string is null string */
61 #define PGSQL_CONV_IGNORE_NOT_NULL  (1<<3)     /* Ignore NOT NULL constraints */
62 #define PGSQL_CONV_OPTS             (PGSQL_CONV_IGNORE_DEFAULT|PGSQL_CONV_FORCE_NULL|PGSQL_CONV_IGNORE_NOT_NULL)
63 /* php_pgsql_insert/update/select/delete options */
64 #define PGSQL_DML_NO_CONV           (1<<8)     /* Do not call php_pgsql_convert() */
65 #define PGSQL_DML_EXEC              (1<<9)     /* Execute query */
66 #define PGSQL_DML_ASYNC             (1<<10)    /* Do async query */
67 #define PGSQL_DML_STRING            (1<<11)    /* Return query string */
68 #define PGSQL_DML_ESCAPE            (1<<12)    /* No convert, but escape only */
69 
70 /* exported functions */
71 PHP_PGSQL_API zend_result php_pgsql_meta_data(PGconn *pg_link, const zend_string *table_name, zval *meta, bool extended);
72 PHP_PGSQL_API zend_result php_pgsql_convert(PGconn *pg_link, const zend_string *table_name, const zval *values, zval *result, zend_ulong opt);
73 PHP_PGSQL_API zend_result php_pgsql_insert(PGconn *pg_link, const zend_string *table, zval *values, zend_ulong opt, zend_string **sql);
74 PHP_PGSQL_API zend_result php_pgsql_update(PGconn *pg_link, const zend_string *table, zval *values, zval *ids, zend_ulong opt , zend_string **sql);
75 PHP_PGSQL_API zend_result php_pgsql_delete(PGconn *pg_link, const zend_string *table, zval *ids, zend_ulong opt, zend_string **sql);
76 PHP_PGSQL_API zend_result php_pgsql_select(PGconn *pg_link, const zend_string *table, zval *ids, zval *ret_array, zend_ulong opt, long fetch_option, zend_string **sql );
77 PHP_PGSQL_API void php_pgsql_result2array(PGresult *pg_result, zval *ret_array, long fetch_option);
78 
79 /* internal functions */
80 static void php_pgsql_do_connect(INTERNAL_FUNCTION_PARAMETERS,int persistent);
81 static void php_pgsql_get_link_info(INTERNAL_FUNCTION_PARAMETERS, int entry_type);
82 static void php_pgsql_get_result_info(INTERNAL_FUNCTION_PARAMETERS, int entry_type);
83 static void php_pgsql_get_field_info(INTERNAL_FUNCTION_PARAMETERS, int entry_type);
84 static void php_pgsql_data_info(INTERNAL_FUNCTION_PARAMETERS, int entry_type, bool nullable_row);
85 static void php_pgsql_do_async(INTERNAL_FUNCTION_PARAMETERS,int entry_type);
86 
87 static ssize_t php_pgsql_fd_write(php_stream *stream, const char *buf, size_t count);
88 static ssize_t php_pgsql_fd_read(php_stream *stream, char *buf, size_t count);
89 static int php_pgsql_fd_close(php_stream *stream, int close_handle);
90 static int php_pgsql_fd_flush(php_stream *stream);
91 static int php_pgsql_fd_set_option(php_stream *stream, int option, int value, void *ptrparam);
92 static int php_pgsql_fd_cast(php_stream *stream, int cast_as, void **ret);
93 
94 typedef enum _php_pgsql_data_type {
95 	/* boolean */
96 	PG_BOOL,
97 	/* number */
98 	PG_OID,
99 	PG_INT2,
100 	PG_INT4,
101 	PG_INT8,
102 	PG_FLOAT4,
103 	PG_FLOAT8,
104 	PG_NUMERIC,
105 	PG_MONEY,
106 	/* character */
107 	PG_TEXT,
108 	PG_CHAR,
109 	PG_VARCHAR,
110 	/* time and interval */
111 	PG_UNIX_TIME,
112 	PG_UNIX_TIME_INTERVAL,
113 	PG_DATE,
114 	PG_TIME,
115 	PG_TIME_WITH_TIMEZONE,
116 	PG_TIMESTAMP,
117 	PG_TIMESTAMP_WITH_TIMEZONE,
118 	PG_INTERVAL,
119 	/* binary */
120 	PG_BYTEA,
121 	/* network */
122 	PG_CIDR,
123 	PG_INET,
124 	PG_MACADDR,
125 	/* bit */
126 	PG_BIT,
127 	PG_VARBIT,
128 	/* geometoric */
129 	PG_LINE,
130 	PG_LSEG,
131 	PG_POINT,
132 	PG_BOX,
133 	PG_PATH,
134 	PG_POLYGON,
135 	PG_CIRCLE,
136 	/* unknown and system */
137 	PG_UNKNOWN
138 } php_pgsql_data_type;
139 
140 typedef struct pgsql_link_handle {
141 	PGconn *conn;
142 	zend_string *hash;
143 	HashTable *notices;
144 	bool persistent;
145 	zend_object std;
146 } pgsql_link_handle;
147 
148 typedef struct pgLofp {
149 	PGconn *conn;
150 	int lofd;
151 	zend_object std;
152 } pgLofp;
153 
154 typedef struct _php_pgsql_result_handle {
155 	PGconn *conn;
156 	PGresult *result;
157 	int row;
158 	zend_object std;
159 } pgsql_result_handle;
160 
161 typedef struct _php_pgsql_notice {
162 	char *message;
163 	size_t len;
164 } php_pgsql_notice;
165 
166 static const php_stream_ops php_stream_pgsql_fd_ops = {
167 	php_pgsql_fd_write,
168 	php_pgsql_fd_read,
169 	php_pgsql_fd_close,
170 	php_pgsql_fd_flush,
171 	"PostgreSQL link",
172 	NULL, /* seek */
173 	php_pgsql_fd_cast, /* cast */
174 	NULL, /* stat */
175 	php_pgsql_fd_set_option
176 };
177 
178 #define PGSQL_MAX_REGEXES 11
179 
180 ZEND_BEGIN_MODULE_GLOBALS(pgsql)
181 	zend_long num_links,num_persistent;
182 	zend_long max_links,max_persistent;
183 	bool allow_persistent;
184 	int ignore_notices;
185 	zend_long auto_reset_persistent;
186 	int log_notices;
187 	zend_object *default_link; /* default link when connection is omitted */
188 	zend_string *regexes[PGSQL_MAX_REGEXES];
189 	HashTable field_oids;
190 	HashTable table_oids;
191 	HashTable connections;
192 ZEND_END_MODULE_GLOBALS(pgsql)
193 
194 ZEND_EXTERN_MODULE_GLOBALS(pgsql)
195 # define PGG(v) ZEND_MODULE_GLOBALS_ACCESSOR(pgsql, v)
196 
197 #if defined(ZTS) && defined(COMPILE_DL_PGSQL)
198 ZEND_TSRMLS_CACHE_EXTERN()
199 #endif
200 
201 #endif
202 
203 #else
204 
205 #define pgsql_module_ptr NULL
206 
207 #endif
208 
209 #define phpext_pgsql_ptr pgsql_module_ptr
210 
211 #endif /* PHP_PGSQL_H */
212