1 /* 2 +----------------------------------------------------------------------+ 3 | PHP Version 7 | 4 +----------------------------------------------------------------------+ 5 | Copyright (c) 1997-2017 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 215 /* exported functions */ 216 PHP_PGSQL_API int php_pgsql_meta_data(PGconn *pg_link, const char *table_name, zval *meta, zend_bool extended); 217 PHP_PGSQL_API int php_pgsql_convert(PGconn *pg_link, const char *table_name, const zval *values, zval *result, zend_ulong opt); 218 PHP_PGSQL_API int php_pgsql_insert(PGconn *pg_link, const char *table, zval *values, zend_ulong opt, zend_string **sql); 219 PHP_PGSQL_API int php_pgsql_update(PGconn *pg_link, const char *table, zval *values, zval *ids, zend_ulong opt , zend_string **sql); 220 PHP_PGSQL_API int php_pgsql_delete(PGconn *pg_link, const char *table, zval *ids, zend_ulong opt, zend_string **sql); 221 PHP_PGSQL_API int php_pgsql_select(PGconn *pg_link, const char *table, zval *ids, zval *ret_array, zend_ulong opt, zend_string **sql ); 222 PHP_PGSQL_API int php_pgsql_result2array(PGresult *pg_result, zval *ret_array); 223 224 /* internal functions */ 225 static void php_pgsql_do_connect(INTERNAL_FUNCTION_PARAMETERS,int persistent); 226 static void php_pgsql_get_link_info(INTERNAL_FUNCTION_PARAMETERS, int entry_type); 227 static void php_pgsql_get_result_info(INTERNAL_FUNCTION_PARAMETERS, int entry_type); 228 static char *get_field_name(PGconn *pgsql, Oid oid, HashTable *list); 229 static void php_pgsql_get_field_info(INTERNAL_FUNCTION_PARAMETERS, int entry_type); 230 static void php_pgsql_data_info(INTERNAL_FUNCTION_PARAMETERS, int entry_type); 231 static void php_pgsql_do_async(INTERNAL_FUNCTION_PARAMETERS,int entry_type); 232 233 static size_t php_pgsql_fd_write(php_stream *stream, const char *buf, size_t count); 234 static size_t php_pgsql_fd_read(php_stream *stream, char *buf, size_t count); 235 static int php_pgsql_fd_close(php_stream *stream, int close_handle); 236 static int php_pgsql_fd_flush(php_stream *stream); 237 static int php_pgsql_fd_set_option(php_stream *stream, int option, int value, void *ptrparam); 238 static int php_pgsql_fd_cast(php_stream *stream, int cast_as, void **ret); 239 240 typedef enum _php_pgsql_data_type { 241 /* boolean */ 242 PG_BOOL, 243 /* number */ 244 PG_OID, 245 PG_INT2, 246 PG_INT4, 247 PG_INT8, 248 PG_FLOAT4, 249 PG_FLOAT8, 250 PG_NUMERIC, 251 PG_MONEY, 252 /* character */ 253 PG_TEXT, 254 PG_CHAR, 255 PG_VARCHAR, 256 /* time and interval */ 257 PG_UNIX_TIME, 258 PG_UNIX_TIME_INTERVAL, 259 PG_DATE, 260 PG_TIME, 261 PG_TIME_WITH_TIMEZONE, 262 PG_TIMESTAMP, 263 PG_TIMESTAMP_WITH_TIMEZONE, 264 PG_INTERVAL, 265 /* binary */ 266 PG_BYTEA, 267 /* network */ 268 PG_CIDR, 269 PG_INET, 270 PG_MACADDR, 271 /* bit */ 272 PG_BIT, 273 PG_VARBIT, 274 /* geometoric */ 275 PG_LINE, 276 PG_LSEG, 277 PG_POINT, 278 PG_BOX, 279 PG_PATH, 280 PG_POLYGON, 281 PG_CIRCLE, 282 /* unknown and system */ 283 PG_UNKNOWN 284 } php_pgsql_data_type; 285 286 typedef struct pgLofp { 287 PGconn *conn; 288 int lofd; 289 } pgLofp; 290 291 typedef struct _php_pgsql_result_handle { 292 PGconn *conn; 293 PGresult *result; 294 int row; 295 } pgsql_result_handle; 296 297 typedef struct _php_pgsql_notice { 298 char *message; 299 size_t len; 300 } php_pgsql_notice; 301 302 static php_stream_ops php_stream_pgsql_fd_ops = { 303 php_pgsql_fd_write, 304 php_pgsql_fd_read, 305 php_pgsql_fd_close, 306 php_pgsql_fd_flush, 307 "PostgreSQL link", 308 NULL, /* seek */ 309 php_pgsql_fd_cast, /* cast */ 310 NULL, /* stat */ 311 php_pgsql_fd_set_option 312 }; 313 314 ZEND_BEGIN_MODULE_GLOBALS(pgsql) 315 zend_long num_links,num_persistent; 316 zend_long max_links,max_persistent; 317 zend_long allow_persistent; 318 zend_long auto_reset_persistent; 319 int le_lofp,le_string; 320 int ignore_notices,log_notices; 321 HashTable notices; /* notice message for each connection */ 322 zend_resource *default_link; /* default link when connection is omitted */ 323 ZEND_END_MODULE_GLOBALS(pgsql) 324 325 ZEND_EXTERN_MODULE_GLOBALS(pgsql) 326 # define PGG(v) ZEND_MODULE_GLOBALS_ACCESSOR(pgsql, v) 327 328 #if defined(ZTS) && defined(COMPILE_DL_PGSQL) 329 ZEND_TSRMLS_CACHE_EXTERN() 330 #endif 331 332 #endif 333 334 #else 335 336 #define pgsql_module_ptr NULL 337 338 #endif 339 340 #define phpext_pgsql_ptr pgsql_module_ptr 341 342 #endif /* PHP_PGSQL_H */ 343