xref: /php-src/ext/pdo_pgsql/pdo_pgsql.c (revision 51dafc60)
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   | Author: Edin Kadribasic <edink@emini.dk>                             |
14   +----------------------------------------------------------------------+
15 */
16 
17 #ifdef HAVE_CONFIG_H
18 #include "config.h"
19 #endif
20 
21 #include "php.h"
22 #include "php_ini.h"
23 #include "ext/standard/info.h"
24 #include "pdo/php_pdo.h"
25 #include "pdo/php_pdo_error.h"
26 #include "pdo/php_pdo_driver.h"
27 #include "php_pdo_pgsql.h"
28 #include "php_pdo_pgsql_int.h"
29 #include "pdo_pgsql_arginfo.h"
30 
31 static zend_class_entry *PdoPgsql_ce;
32 
33 /* {{{ pdo_sqlite_deps */
34 static const zend_module_dep pdo_pgsql_deps[] = {
35 	ZEND_MOD_REQUIRED("pdo")
36 	ZEND_MOD_END
37 };
38 /* }}} */
39 
40 /* {{{ pdo_pgsql_module_entry */
41 zend_module_entry pdo_pgsql_module_entry = {
42 	STANDARD_MODULE_HEADER_EX, NULL,
43 	pdo_pgsql_deps,
44 	"pdo_pgsql",
45 	NULL,
46 	PHP_MINIT(pdo_pgsql),
47 	PHP_MSHUTDOWN(pdo_pgsql),
48 	NULL,
49 	NULL,
50 	PHP_MINFO(pdo_pgsql),
51 	PHP_PDO_PGSQL_VERSION,
52 	STANDARD_MODULE_PROPERTIES
53 };
54 /* }}} */
55 
56 #ifdef COMPILE_DL_PDO_PGSQL
57 ZEND_GET_MODULE(pdo_pgsql)
58 #endif
59 
60 /* Escape an identifier for insertion into a text field	*/
PHP_METHOD(PdoPgsql,escapeIdentifier)61 PHP_METHOD(PdoPgsql, escapeIdentifier)
62 {
63 	zend_string *from = NULL;
64 	char *tmp;
65 	pdo_dbh_t *dbh;
66 	pdo_pgsql_db_handle *H;
67 
68 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "S", &from) == FAILURE) {
69 		RETURN_THROWS();
70 	}
71 
72 	dbh = Z_PDO_DBH_P(ZEND_THIS);
73 	PDO_CONSTRUCT_CHECK;
74 	PDO_DBH_CLEAR_ERR();
75 
76 	/* Obtain db Handle */
77 	H = (pdo_pgsql_db_handle *)dbh->driver_data;
78 	if (H->server == NULL) {
79 		zend_throw_error(NULL, "PostgreSQL connection has already been closed");
80 		RETURN_THROWS();
81 	}
82 
83 	tmp = PQescapeIdentifier(H->server, ZSTR_VAL(from), ZSTR_LEN(from));
84 	if (!tmp) {
85 		pdo_pgsql_error(dbh, PGRES_FATAL_ERROR, NULL);
86 		PDO_HANDLE_DBH_ERR();
87 		RETURN_THROWS();
88 	}
89 
90 	RETVAL_STRING(tmp);
91 	PQfreemem(tmp);
92 }
93 
94 /* Returns true if the copy worked fine or false if error */
PHP_METHOD(PdoPgsql,copyFromArray)95 PHP_METHOD(PdoPgsql, copyFromArray)
96 {
97 	pgsqlCopyFromArray_internal(INTERNAL_FUNCTION_PARAM_PASSTHRU);
98 }
99 
100 /* Returns true if the copy worked fine or false if error */
PHP_METHOD(PdoPgsql,copyFromFile)101 PHP_METHOD(PdoPgsql, copyFromFile)
102 {
103 	pgsqlCopyFromFile_internal(INTERNAL_FUNCTION_PARAM_PASSTHRU);
104 }
105 
106 /* Returns true if the copy worked fine or false if error */
PHP_METHOD(PdoPgsql,copyToFile)107 PHP_METHOD(PdoPgsql, copyToFile)
108 {
109 	pgsqlCopyToFile_internal(INTERNAL_FUNCTION_PARAM_PASSTHRU);
110 }
111 
112 /* Returns true if the copy worked fine or false if error */
PHP_METHOD(PdoPgsql,copyToArray)113 PHP_METHOD(PdoPgsql, copyToArray)
114 {
115 	pgsqlCopyToArray_internal(INTERNAL_FUNCTION_PARAM_PASSTHRU);
116 }
117 
118 /* Creates a new large object, returning its identifier.  Must be called inside a transaction. */
PHP_METHOD(PdoPgsql,lobCreate)119 PHP_METHOD(PdoPgsql, lobCreate)
120 {
121 	pgsqlLOBCreate_internal(INTERNAL_FUNCTION_PARAM_PASSTHRU);
122 }
123 
124 /* Opens an existing large object stream.  Must be called inside a transaction. */
PHP_METHOD(PdoPgsql,lobOpen)125 PHP_METHOD(PdoPgsql, lobOpen)
126 {
127 	pgsqlLOBOpen_internal(INTERNAL_FUNCTION_PARAM_PASSTHRU);
128 }
129 
130 /* Deletes the large object identified by oid.  Must be called inside a transaction. */
PHP_METHOD(PdoPgsql,lobUnlink)131 PHP_METHOD(PdoPgsql, lobUnlink)
132 {
133 	pgsqlLOBUnlink_internal(INTERNAL_FUNCTION_PARAM_PASSTHRU);
134 }
135 
136 /* Get asynchronous notification */
PHP_METHOD(PdoPgsql,getNotify)137 PHP_METHOD(PdoPgsql, getNotify)
138 {
139 	pgsqlGetNotify_internal(INTERNAL_FUNCTION_PARAM_PASSTHRU);
140 }
141 
142 /* Get backend(server) pid */
PHP_METHOD(PdoPgsql,getPid)143 PHP_METHOD(PdoPgsql, getPid)
144 {
145 	pgsqlGetPid_internal(INTERNAL_FUNCTION_PARAM_PASSTHRU);
146 }
147 
148 /* true global environment */
149 
150 /* {{{ PHP_MINIT_FUNCTION */
PHP_MINIT_FUNCTION(pdo_pgsql)151 PHP_MINIT_FUNCTION(pdo_pgsql)
152 {
153 	REGISTER_PDO_CLASS_CONST_LONG("PGSQL_ATTR_DISABLE_PREPARES", PDO_PGSQL_ATTR_DISABLE_PREPARES);
154 	REGISTER_PDO_CLASS_CONST_LONG("PGSQL_TRANSACTION_IDLE", (zend_long)PGSQL_TRANSACTION_IDLE);
155 	REGISTER_PDO_CLASS_CONST_LONG("PGSQL_TRANSACTION_ACTIVE", (zend_long)PGSQL_TRANSACTION_ACTIVE);
156 	REGISTER_PDO_CLASS_CONST_LONG("PGSQL_TRANSACTION_INTRANS", (zend_long)PGSQL_TRANSACTION_INTRANS);
157 	REGISTER_PDO_CLASS_CONST_LONG("PGSQL_TRANSACTION_INERROR", (zend_long)PGSQL_TRANSACTION_INERROR);
158 	REGISTER_PDO_CLASS_CONST_LONG("PGSQL_TRANSACTION_UNKNOWN", (zend_long)PGSQL_TRANSACTION_UNKNOWN);
159 
160 	PdoPgsql_ce = register_class_PdoPgsql(pdo_dbh_ce);
161 	PdoPgsql_ce->create_object = pdo_dbh_new;
162 
163 	if (php_pdo_register_driver(&pdo_pgsql_driver) == FAILURE) {
164 		return FAILURE;
165 	}
166 
167 	return php_pdo_register_driver_specific_ce(&pdo_pgsql_driver, PdoPgsql_ce);
168 }
169 /* }}} */
170 
171 /* {{{ PHP_MSHUTDOWN_FUNCTION */
PHP_MSHUTDOWN_FUNCTION(pdo_pgsql)172 PHP_MSHUTDOWN_FUNCTION(pdo_pgsql)
173 {
174 	php_pdo_unregister_driver(&pdo_pgsql_driver);
175 	return SUCCESS;
176 }
177 /* }}} */
178 
179 /* {{{ PHP_MINFO_FUNCTION */
PHP_MINFO_FUNCTION(pdo_pgsql)180 PHP_MINFO_FUNCTION(pdo_pgsql)
181 {
182 	char buf[16];
183 
184 	php_info_print_table_start();
185 	php_info_print_table_row(2, "PDO Driver for PostgreSQL", "enabled");
186 	pdo_libpq_version(buf, sizeof(buf));
187 	php_info_print_table_row(2, "PostgreSQL(libpq) Version", buf);
188 	php_info_print_table_end();
189 }
190 /* }}} */
191