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 "ext/pdo/php_pdo.h"
25 #include "ext/pdo/php_pdo_error.h"
26 #include "ext/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_pgsql_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(Pdo_Pgsql,escapeIdentifier)61 PHP_METHOD(Pdo_Pgsql, 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(Pdo_Pgsql,copyFromArray)95 PHP_METHOD(Pdo_Pgsql, 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(Pdo_Pgsql,copyFromFile)101 PHP_METHOD(Pdo_Pgsql, 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(Pdo_Pgsql,copyToFile)107 PHP_METHOD(Pdo_Pgsql, 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(Pdo_Pgsql,copyToArray)113 PHP_METHOD(Pdo_Pgsql, 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(Pdo_Pgsql,lobCreate)119 PHP_METHOD(Pdo_Pgsql, 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(Pdo_Pgsql,lobOpen)125 PHP_METHOD(Pdo_Pgsql, 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(Pdo_Pgsql,lobUnlink)131 PHP_METHOD(Pdo_Pgsql, lobUnlink)
132 {
133 pgsqlLOBUnlink_internal(INTERNAL_FUNCTION_PARAM_PASSTHRU);
134 }
135
136 /* Get asynchronous notification */
PHP_METHOD(Pdo_Pgsql,getNotify)137 PHP_METHOD(Pdo_Pgsql, getNotify)
138 {
139 pgsqlGetNotify_internal(INTERNAL_FUNCTION_PARAM_PASSTHRU);
140 }
141
142 /* Get backend(server) pid */
PHP_METHOD(Pdo_Pgsql,getPid)143 PHP_METHOD(Pdo_Pgsql, getPid)
144 {
145 pgsqlGetPid_internal(INTERNAL_FUNCTION_PARAM_PASSTHRU);
146 }
147
148 /* Sets a callback to receive DB notices (after client_min_messages has been set */
PHP_METHOD(Pdo_Pgsql,setNoticeCallback)149 PHP_METHOD(Pdo_Pgsql, setNoticeCallback)
150 {
151 zend_fcall_info fci = empty_fcall_info;
152 zend_fcall_info_cache fcc = empty_fcall_info_cache;
153 if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS(), "F!", &fci, &fcc)) {
154 RETURN_THROWS();
155 }
156
157 pdo_dbh_t *dbh = Z_PDO_DBH_P(ZEND_THIS);
158 PDO_CONSTRUCT_CHECK_WITH_CLEANUP(cleanup);
159
160 pdo_pgsql_db_handle *H = (pdo_pgsql_db_handle *)dbh->driver_data;
161
162 pdo_pgsql_cleanup_notice_callback(H);
163
164 if (ZEND_FCC_INITIALIZED(fcc)) {
165 H->notice_callback = emalloc(sizeof(zend_fcall_info_cache));
166 zend_fcc_dup(H->notice_callback, &fcc);
167 }
168
169 return;
170
171 cleanup:
172 zend_release_fcall_info_cache(&fcc);
173 RETURN_THROWS();
174 }
175
176 /* true global environment */
177
178 /* {{{ PHP_MINIT_FUNCTION */
PHP_MINIT_FUNCTION(pdo_pgsql)179 PHP_MINIT_FUNCTION(pdo_pgsql)
180 {
181 REGISTER_PDO_CLASS_CONST_LONG("PGSQL_ATTR_DISABLE_PREPARES", PDO_PGSQL_ATTR_DISABLE_PREPARES);
182 REGISTER_PDO_CLASS_CONST_LONG("PGSQL_TRANSACTION_IDLE", (zend_long)PGSQL_TRANSACTION_IDLE);
183 REGISTER_PDO_CLASS_CONST_LONG("PGSQL_TRANSACTION_ACTIVE", (zend_long)PGSQL_TRANSACTION_ACTIVE);
184 REGISTER_PDO_CLASS_CONST_LONG("PGSQL_TRANSACTION_INTRANS", (zend_long)PGSQL_TRANSACTION_INTRANS);
185 REGISTER_PDO_CLASS_CONST_LONG("PGSQL_TRANSACTION_INERROR", (zend_long)PGSQL_TRANSACTION_INERROR);
186 REGISTER_PDO_CLASS_CONST_LONG("PGSQL_TRANSACTION_UNKNOWN", (zend_long)PGSQL_TRANSACTION_UNKNOWN);
187 #ifdef HAVE_PG_RESULT_MEMORY_SIZE
188 REGISTER_PDO_CLASS_CONST_LONG("PGSQL_ATTR_RESULT_MEMORY_SIZE", (zend_long)PDO_PGSQL_ATTR_RESULT_MEMORY_SIZE);
189 #endif
190
191 PdoPgsql_ce = register_class_Pdo_Pgsql(pdo_dbh_ce);
192 PdoPgsql_ce->create_object = pdo_dbh_new;
193
194 if (php_pdo_register_driver(&pdo_pgsql_driver) == FAILURE) {
195 return FAILURE;
196 }
197
198 return php_pdo_register_driver_specific_ce(&pdo_pgsql_driver, PdoPgsql_ce);
199 }
200 /* }}} */
201
202 /* {{{ PHP_MSHUTDOWN_FUNCTION */
PHP_MSHUTDOWN_FUNCTION(pdo_pgsql)203 PHP_MSHUTDOWN_FUNCTION(pdo_pgsql)
204 {
205 php_pdo_unregister_driver(&pdo_pgsql_driver);
206 return SUCCESS;
207 }
208 /* }}} */
209
210 /* {{{ PHP_MINFO_FUNCTION */
PHP_MINFO_FUNCTION(pdo_pgsql)211 PHP_MINFO_FUNCTION(pdo_pgsql)
212 {
213 char buf[16];
214
215 php_info_print_table_start();
216 php_info_print_table_row(2, "PDO Driver for PostgreSQL", "enabled");
217 pdo_libpq_version(buf, sizeof(buf));
218 php_info_print_table_row(2, "PostgreSQL(libpq) Version", buf);
219 php_info_print_table_end();
220 }
221 /* }}} */
222