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 | http://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 /* connection functions */ 61 PHP_FUNCTION(pg_connect); 62 PHP_FUNCTION(pg_pconnect); 63 PHP_FUNCTION(pg_connect_poll); 64 PHP_FUNCTION(pg_close); 65 PHP_FUNCTION(pg_connection_reset); 66 PHP_FUNCTION(pg_connection_status); 67 PHP_FUNCTION(pg_connection_busy); 68 PHP_FUNCTION(pg_host); 69 PHP_FUNCTION(pg_dbname); 70 PHP_FUNCTION(pg_port); 71 PHP_FUNCTION(pg_tty); 72 PHP_FUNCTION(pg_options); 73 PHP_FUNCTION(pg_version); 74 PHP_FUNCTION(pg_ping); 75 PHP_FUNCTION(pg_parameter_status); 76 PHP_FUNCTION(pg_transaction_status); 77 /* query functions */ 78 PHP_FUNCTION(pg_query); 79 PHP_FUNCTION(pg_query_params); 80 PHP_FUNCTION(pg_prepare); 81 PHP_FUNCTION(pg_execute); 82 PHP_FUNCTION(pg_send_query); 83 PHP_FUNCTION(pg_send_query_params); 84 PHP_FUNCTION(pg_send_prepare); 85 PHP_FUNCTION(pg_send_execute); 86 PHP_FUNCTION(pg_cancel_query); 87 /* result functions */ 88 PHP_FUNCTION(pg_fetch_assoc); 89 PHP_FUNCTION(pg_fetch_array); 90 PHP_FUNCTION(pg_fetch_object); 91 PHP_FUNCTION(pg_fetch_result); 92 PHP_FUNCTION(pg_fetch_row); 93 PHP_FUNCTION(pg_fetch_all); 94 PHP_FUNCTION(pg_fetch_all_columns); 95 PHP_FUNCTION(pg_affected_rows); 96 PHP_FUNCTION(pg_get_result); 97 PHP_FUNCTION(pg_result_seek); 98 PHP_FUNCTION(pg_result_status); 99 PHP_FUNCTION(pg_free_result); 100 PHP_FUNCTION(pg_last_oid); 101 PHP_FUNCTION(pg_num_rows); 102 PHP_FUNCTION(pg_num_fields); 103 PHP_FUNCTION(pg_field_name); 104 PHP_FUNCTION(pg_field_num); 105 PHP_FUNCTION(pg_field_size); 106 PHP_FUNCTION(pg_field_type); 107 PHP_FUNCTION(pg_field_type_oid); 108 PHP_FUNCTION(pg_field_prtlen); 109 PHP_FUNCTION(pg_field_is_null); 110 PHP_FUNCTION(pg_field_table); 111 /* async message functions */ 112 PHP_FUNCTION(pg_get_notify); 113 PHP_FUNCTION(pg_socket); 114 PHP_FUNCTION(pg_consume_input); 115 PHP_FUNCTION(pg_flush); 116 PHP_FUNCTION(pg_get_pid); 117 /* error message functions */ 118 PHP_FUNCTION(pg_result_error); 119 PHP_FUNCTION(pg_result_error_field); 120 PHP_FUNCTION(pg_last_error); 121 PHP_FUNCTION(pg_last_notice); 122 /* copy functions */ 123 PHP_FUNCTION(pg_put_line); 124 PHP_FUNCTION(pg_end_copy); 125 PHP_FUNCTION(pg_copy_to); 126 PHP_FUNCTION(pg_copy_from); 127 /* large object functions */ 128 PHP_FUNCTION(pg_lo_create); 129 PHP_FUNCTION(pg_lo_unlink); 130 PHP_FUNCTION(pg_lo_open); 131 PHP_FUNCTION(pg_lo_close); 132 PHP_FUNCTION(pg_lo_read); 133 PHP_FUNCTION(pg_lo_write); 134 PHP_FUNCTION(pg_lo_read_all); 135 PHP_FUNCTION(pg_lo_import); 136 PHP_FUNCTION(pg_lo_export); 137 PHP_FUNCTION(pg_lo_seek); 138 PHP_FUNCTION(pg_lo_tell); 139 PHP_FUNCTION(pg_lo_truncate); 140 141 /* debugging functions */ 142 PHP_FUNCTION(pg_trace); 143 PHP_FUNCTION(pg_untrace); 144 145 /* utility functions */ 146 PHP_FUNCTION(pg_client_encoding); 147 PHP_FUNCTION(pg_set_client_encoding); 148 PHP_FUNCTION(pg_set_error_verbosity); 149 PHP_FUNCTION(pg_escape_string); 150 PHP_FUNCTION(pg_escape_bytea); 151 PHP_FUNCTION(pg_unescape_bytea); 152 PHP_FUNCTION(pg_escape_literal); 153 PHP_FUNCTION(pg_escape_identifier); 154 155 /* misc functions */ 156 PHP_FUNCTION(pg_meta_data); 157 PHP_FUNCTION(pg_convert); 158 PHP_FUNCTION(pg_insert); 159 PHP_FUNCTION(pg_update); 160 PHP_FUNCTION(pg_delete); 161 PHP_FUNCTION(pg_select); 162 163 /* connection options - ToDo: Add async connection option */ 164 #define PGSQL_CONNECT_FORCE_NEW (1<<1) 165 #define PGSQL_CONNECT_ASYNC (1<<2) 166 /* php_pgsql_convert options */ 167 #define PGSQL_CONV_IGNORE_DEFAULT (1<<1) /* Do not use DEFAULT value by removing field from returned array */ 168 #define PGSQL_CONV_FORCE_NULL (1<<2) /* Convert to NULL if string is null string */ 169 #define PGSQL_CONV_IGNORE_NOT_NULL (1<<3) /* Ignore NOT NULL constraints */ 170 #define PGSQL_CONV_OPTS (PGSQL_CONV_IGNORE_DEFAULT|PGSQL_CONV_FORCE_NULL|PGSQL_CONV_IGNORE_NOT_NULL) 171 /* php_pgsql_insert/update/select/delete options */ 172 #define PGSQL_DML_NO_CONV (1<<8) /* Do not call php_pgsql_convert() */ 173 #define PGSQL_DML_EXEC (1<<9) /* Execute query */ 174 #define PGSQL_DML_ASYNC (1<<10) /* Do async query */ 175 #define PGSQL_DML_STRING (1<<11) /* Return query string */ 176 #define PGSQL_DML_ESCAPE (1<<12) /* No convert, but escape only */ 177 178 /* exported functions */ 179 PHP_PGSQL_API int php_pgsql_meta_data(PGconn *pg_link, const char *table_name, zval *meta, zend_bool extended); 180 PHP_PGSQL_API int php_pgsql_convert(PGconn *pg_link, const char *table_name, const zval *values, zval *result, zend_ulong opt); 181 PHP_PGSQL_API int php_pgsql_insert(PGconn *pg_link, const char *table, zval *values, zend_ulong opt, zend_string **sql); 182 PHP_PGSQL_API int php_pgsql_update(PGconn *pg_link, const char *table, zval *values, zval *ids, zend_ulong opt , zend_string **sql); 183 PHP_PGSQL_API int php_pgsql_delete(PGconn *pg_link, const char *table, zval *ids, zend_ulong opt, zend_string **sql); 184 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 ); 185 PHP_PGSQL_API void php_pgsql_result2array(PGresult *pg_result, zval *ret_array, long fetch_option); 186 187 /* internal functions */ 188 static void php_pgsql_do_connect(INTERNAL_FUNCTION_PARAMETERS,int persistent); 189 static void php_pgsql_get_link_info(INTERNAL_FUNCTION_PARAMETERS, int entry_type); 190 static void php_pgsql_get_result_info(INTERNAL_FUNCTION_PARAMETERS, int entry_type); 191 static char *get_field_name(PGconn *pgsql, Oid oid, HashTable *list); 192 static void php_pgsql_get_field_info(INTERNAL_FUNCTION_PARAMETERS, int entry_type); 193 static void php_pgsql_data_info(INTERNAL_FUNCTION_PARAMETERS, int entry_type); 194 static void php_pgsql_do_async(INTERNAL_FUNCTION_PARAMETERS,int entry_type); 195 196 static ssize_t php_pgsql_fd_write(php_stream *stream, const char *buf, size_t count); 197 static ssize_t php_pgsql_fd_read(php_stream *stream, char *buf, size_t count); 198 static int php_pgsql_fd_close(php_stream *stream, int close_handle); 199 static int php_pgsql_fd_flush(php_stream *stream); 200 static int php_pgsql_fd_set_option(php_stream *stream, int option, int value, void *ptrparam); 201 static int php_pgsql_fd_cast(php_stream *stream, int cast_as, void **ret); 202 203 typedef enum _php_pgsql_data_type { 204 /* boolean */ 205 PG_BOOL, 206 /* number */ 207 PG_OID, 208 PG_INT2, 209 PG_INT4, 210 PG_INT8, 211 PG_FLOAT4, 212 PG_FLOAT8, 213 PG_NUMERIC, 214 PG_MONEY, 215 /* character */ 216 PG_TEXT, 217 PG_CHAR, 218 PG_VARCHAR, 219 /* time and interval */ 220 PG_UNIX_TIME, 221 PG_UNIX_TIME_INTERVAL, 222 PG_DATE, 223 PG_TIME, 224 PG_TIME_WITH_TIMEZONE, 225 PG_TIMESTAMP, 226 PG_TIMESTAMP_WITH_TIMEZONE, 227 PG_INTERVAL, 228 /* binary */ 229 PG_BYTEA, 230 /* network */ 231 PG_CIDR, 232 PG_INET, 233 PG_MACADDR, 234 /* bit */ 235 PG_BIT, 236 PG_VARBIT, 237 /* geometoric */ 238 PG_LINE, 239 PG_LSEG, 240 PG_POINT, 241 PG_BOX, 242 PG_PATH, 243 PG_POLYGON, 244 PG_CIRCLE, 245 /* unknown and system */ 246 PG_UNKNOWN 247 } php_pgsql_data_type; 248 249 typedef struct pgLofp { 250 PGconn *conn; 251 int lofd; 252 } pgLofp; 253 254 typedef struct _php_pgsql_result_handle { 255 PGconn *conn; 256 PGresult *result; 257 int row; 258 } pgsql_result_handle; 259 260 typedef struct _php_pgsql_notice { 261 char *message; 262 size_t len; 263 } php_pgsql_notice; 264 265 static const php_stream_ops php_stream_pgsql_fd_ops = { 266 php_pgsql_fd_write, 267 php_pgsql_fd_read, 268 php_pgsql_fd_close, 269 php_pgsql_fd_flush, 270 "PostgreSQL link", 271 NULL, /* seek */ 272 php_pgsql_fd_cast, /* cast */ 273 NULL, /* stat */ 274 php_pgsql_fd_set_option 275 }; 276 277 ZEND_BEGIN_MODULE_GLOBALS(pgsql) 278 zend_long num_links,num_persistent; 279 zend_long max_links,max_persistent; 280 zend_long allow_persistent; 281 zend_long auto_reset_persistent; 282 int le_lofp,le_string; 283 int ignore_notices,log_notices; 284 HashTable notices; /* notice message for each connection */ 285 zend_resource *default_link; /* default link when connection is omitted */ 286 HashTable hashes; /* hashes for each connection */ 287 ZEND_END_MODULE_GLOBALS(pgsql) 288 289 ZEND_EXTERN_MODULE_GLOBALS(pgsql) 290 # define PGG(v) ZEND_MODULE_GLOBALS_ACCESSOR(pgsql, v) 291 292 #if defined(ZTS) && defined(COMPILE_DL_PGSQL) 293 ZEND_TSRMLS_CACHE_EXTERN() 294 #endif 295 296 #endif 297 298 #else 299 300 #define pgsql_module_ptr NULL 301 302 #endif 303 304 #define phpext_pgsql_ptr pgsql_module_ptr 305 306 #endif /* PHP_PGSQL_H */ 307