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