pgsql_driver.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284
  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: Edin Kadribasic <edink@emini.dk> |
  14. | Ilia Alshanestsky <ilia@prohost.org> |
  15. | Wez Furlong <wez@php.net> |
  16. +----------------------------------------------------------------------+
  17. */
  18. #ifdef HAVE_CONFIG_H
  19. #include "config.h"
  20. #endif
  21. #include "php.h"
  22. #include "php_ini.h"
  23. #include "ext/standard/info.h"
  24. #include "ext/standard/php_string.h"
  25. #include "main/php_network.h"
  26. #include "pdo/php_pdo.h"
  27. #include "pdo/php_pdo_driver.h"
  28. #include "pdo/php_pdo_error.h"
  29. #include "ext/standard/file.h"
  30. #undef SIZEOF_OFF_T
  31. #include "php_pdo_pgsql.h"
  32. #include "php_pdo_pgsql_int.h"
  33. #include "zend_exceptions.h"
  34. #include "pgsql_driver_arginfo.h"
  35. static char * _pdo_pgsql_trim_message(const char *message, int persistent)
  36. {
  37. size_t i = strlen(message)-1;
  38. char *tmp;
  39. if (i>1 && (message[i-1] == '\r' || message[i-1] == '\n') && message[i] == '.') {
  40. --i;
  41. }
  42. while (i>0 && (message[i] == '\r' || message[i] == '\n')) {
  43. --i;
  44. }
  45. ++i;
  46. tmp = pemalloc(i + 1, persistent);
  47. memcpy(tmp, message, i);
  48. tmp[i] = '\0';
  49. return tmp;
  50. }
  51. static zend_string* _pdo_pgsql_escape_credentials(char *str)
  52. {
  53. if (str) {
  54. return php_addcslashes_str(str, strlen(str), "\\'", sizeof("\\'"));
  55. }
  56. return NULL;
  57. }
  58. int _pdo_pgsql_error(pdo_dbh_t *dbh, pdo_stmt_t *stmt, int errcode, const char *sqlstate, const char *msg, const char *file, int line) /* {{{ */
  59. {
  60. pdo_pgsql_db_handle *H = (pdo_pgsql_db_handle *)dbh->driver_data;
  61. pdo_error_type *pdo_err = stmt ? &stmt->error_code : &dbh->error_code;
  62. pdo_pgsql_error_info *einfo = &H->einfo;
  63. char *errmsg = PQerrorMessage(H->server);
  64. einfo->errcode = errcode;
  65. einfo->file = file;
  66. einfo->line = line;
  67. if (einfo->errmsg) {
  68. pefree(einfo->errmsg, dbh->is_persistent);
  69. einfo->errmsg = NULL;
  70. }
  71. if (sqlstate == NULL || strlen(sqlstate) >= sizeof(pdo_error_type)) {
  72. strcpy(*pdo_err, "HY000");
  73. }
  74. else {
  75. strcpy(*pdo_err, sqlstate);
  76. }
  77. if (msg) {
  78. einfo->errmsg = pestrdup(msg, dbh->is_persistent);
  79. }
  80. else if (errmsg) {
  81. einfo->errmsg = _pdo_pgsql_trim_message(errmsg, dbh->is_persistent);
  82. }
  83. if (!dbh->methods) {
  84. pdo_throw_exception(einfo->errcode, einfo->errmsg, pdo_err);
  85. }
  86. return errcode;
  87. }
  88. /* }}} */
  89. static void _pdo_pgsql_notice(pdo_dbh_t *dbh, const char *message) /* {{{ */
  90. {
  91. /* pdo_pgsql_db_handle *H = (pdo_pgsql_db_handle *)dbh->driver_data; */
  92. }
  93. /* }}} */
  94. static void pdo_pgsql_fetch_error_func(pdo_dbh_t *dbh, pdo_stmt_t *stmt, zval *info) /* {{{ */
  95. {
  96. pdo_pgsql_db_handle *H = (pdo_pgsql_db_handle *)dbh->driver_data;
  97. pdo_pgsql_error_info *einfo = &H->einfo;
  98. if (einfo->errcode) {
  99. add_next_index_long(info, einfo->errcode);
  100. } else {
  101. /* Add null to respect expected info array structure */
  102. add_next_index_null(info);
  103. }
  104. if (einfo->errmsg) {
  105. add_next_index_string(info, einfo->errmsg);
  106. }
  107. }
  108. /* }}} */
  109. /* {{{ pdo_pgsql_create_lob_stream */
  110. static ssize_t pgsql_lob_write(php_stream *stream, const char *buf, size_t count)
  111. {
  112. struct pdo_pgsql_lob_self *self = (struct pdo_pgsql_lob_self*)stream->abstract;
  113. return lo_write(self->conn, self->lfd, (char*)buf, count);
  114. }
  115. static ssize_t pgsql_lob_read(php_stream *stream, char *buf, size_t count)
  116. {
  117. struct pdo_pgsql_lob_self *self = (struct pdo_pgsql_lob_self*)stream->abstract;
  118. return lo_read(self->conn, self->lfd, buf, count);
  119. }
  120. static int pgsql_lob_close(php_stream *stream, int close_handle)
  121. {
  122. struct pdo_pgsql_lob_self *self = (struct pdo_pgsql_lob_self*)stream->abstract;
  123. if (close_handle) {
  124. lo_close(self->conn, self->lfd);
  125. }
  126. zval_ptr_dtor(&self->dbh);
  127. efree(self);
  128. return 0;
  129. }
  130. static int pgsql_lob_flush(php_stream *stream)
  131. {
  132. return 0;
  133. }
  134. static int pgsql_lob_seek(php_stream *stream, zend_off_t offset, int whence,
  135. zend_off_t *newoffset)
  136. {
  137. struct pdo_pgsql_lob_self *self = (struct pdo_pgsql_lob_self*)stream->abstract;
  138. #if defined(HAVE_PG_LO64) && defined(ZEND_ENABLE_ZVAL_LONG64)
  139. zend_off_t pos = lo_lseek64(self->conn, self->lfd, offset, whence);
  140. #else
  141. zend_off_t pos = lo_lseek(self->conn, self->lfd, offset, whence);
  142. #endif
  143. *newoffset = pos;
  144. return pos >= 0 ? 0 : -1;
  145. }
  146. const php_stream_ops pdo_pgsql_lob_stream_ops = {
  147. pgsql_lob_write,
  148. pgsql_lob_read,
  149. pgsql_lob_close,
  150. pgsql_lob_flush,
  151. "pdo_pgsql lob stream",
  152. pgsql_lob_seek,
  153. NULL,
  154. NULL,
  155. NULL
  156. };
  157. php_stream *pdo_pgsql_create_lob_stream(zval *dbh, int lfd, Oid oid)
  158. {
  159. php_stream *stm;
  160. struct pdo_pgsql_lob_self *self = ecalloc(1, sizeof(*self));
  161. pdo_pgsql_db_handle *H = (pdo_pgsql_db_handle *)(Z_PDO_DBH_P(dbh))->driver_data;
  162. ZVAL_COPY_VALUE(&self->dbh, dbh);
  163. self->lfd = lfd;
  164. self->oid = oid;
  165. self->conn = H->server;
  166. stm = php_stream_alloc(&pdo_pgsql_lob_stream_ops, self, 0, "r+b");
  167. if (stm) {
  168. Z_ADDREF_P(dbh);
  169. return stm;
  170. }
  171. efree(self);
  172. return NULL;
  173. }
  174. /* }}} */
  175. static void pgsql_handle_closer(pdo_dbh_t *dbh) /* {{{ */
  176. {
  177. pdo_pgsql_db_handle *H = (pdo_pgsql_db_handle *)dbh->driver_data;
  178. if (H) {
  179. if (H->server) {
  180. PQfinish(H->server);
  181. H->server = NULL;
  182. }
  183. if (H->einfo.errmsg) {
  184. pefree(H->einfo.errmsg, dbh->is_persistent);
  185. H->einfo.errmsg = NULL;
  186. }
  187. pefree(H, dbh->is_persistent);
  188. dbh->driver_data = NULL;
  189. }
  190. }
  191. /* }}} */
  192. static bool pgsql_handle_preparer(pdo_dbh_t *dbh, zend_string *sql, pdo_stmt_t *stmt, zval *driver_options)
  193. {
  194. pdo_pgsql_db_handle *H = (pdo_pgsql_db_handle *)dbh->driver_data;
  195. pdo_pgsql_stmt *S = ecalloc(1, sizeof(pdo_pgsql_stmt));
  196. int scrollable;
  197. int ret;
  198. zend_string *nsql = NULL;
  199. int emulate = 0;
  200. int execute_only = 0;
  201. S->H = H;
  202. stmt->driver_data = S;
  203. stmt->methods = &pgsql_stmt_methods;
  204. scrollable = pdo_attr_lval(driver_options, PDO_ATTR_CURSOR,
  205. PDO_CURSOR_FWDONLY) == PDO_CURSOR_SCROLL;
  206. if (scrollable) {
  207. if (S->cursor_name) {
  208. efree(S->cursor_name);
  209. }
  210. spprintf(&S->cursor_name, 0, "pdo_crsr_%08x", ++H->stmt_counter);
  211. emulate = 1;
  212. } else if (driver_options) {
  213. if (pdo_attr_lval(driver_options, PDO_ATTR_EMULATE_PREPARES, H->emulate_prepares) == 1) {
  214. emulate = 1;
  215. }
  216. if (pdo_attr_lval(driver_options, PDO_PGSQL_ATTR_DISABLE_PREPARES, H->disable_prepares) == 1) {
  217. execute_only = 1;
  218. }
  219. } else {
  220. emulate = H->disable_native_prepares || H->emulate_prepares;
  221. execute_only = H->disable_prepares;
  222. }
  223. if (!emulate && PQprotocolVersion(H->server) <= 2) {
  224. emulate = 1;
  225. }
  226. if (emulate) {
  227. stmt->supports_placeholders = PDO_PLACEHOLDER_NONE;
  228. } else {
  229. stmt->supports_placeholders = PDO_PLACEHOLDER_NAMED;
  230. stmt->named_rewrite_template = "$%d";
  231. }
  232. ret = pdo_parse_params(stmt, sql, &nsql);
  233. if (ret == -1) {
  234. /* couldn't grok it */
  235. strcpy(dbh->error_code, stmt->error_code);
  236. return false;
  237. } else if (ret == 1) {
  238. /* query was re-written */
  239. S->query = nsql;
  240. } else {
  241. S->query = zend_string_copy(sql);
  242. }
  243. if (!emulate && !execute_only) {
  244. /* prepared query: set the query name and defer the
  245. actual prepare until the first execute call */
  246. spprintf(&S->stmt_name, 0, "pdo_stmt_%08x", ++H->stmt_counter);
  247. }
  248. return true;
  249. }
  250. static zend_long pgsql_handle_doer(pdo_dbh_t *dbh, const zend_string *sql)
  251. {
  252. pdo_pgsql_db_handle *H = (pdo_pgsql_db_handle *)dbh->driver_data;
  253. PGresult *res;
  254. zend_long ret = 1;
  255. ExecStatusType qs;
  256. if (!(res = PQexec(H->server, ZSTR_VAL(sql)))) {
  257. /* fatal error */
  258. pdo_pgsql_error(dbh, PGRES_FATAL_ERROR, NULL);
  259. return -1;
  260. }
  261. qs = PQresultStatus(res);
  262. if (qs != PGRES_COMMAND_OK && qs != PGRES_TUPLES_OK) {
  263. pdo_pgsql_error(dbh, qs, pdo_pgsql_sqlstate(res));
  264. PQclear(res);
  265. return -1;
  266. }
  267. H->pgoid = PQoidValue(res);
  268. if (qs == PGRES_COMMAND_OK) {
  269. ret = ZEND_ATOL(PQcmdTuples(res));
  270. } else {
  271. ret = Z_L(0);
  272. }
  273. PQclear(res);
  274. return ret;
  275. }
  276. static zend_string* pgsql_handle_quoter(pdo_dbh_t *dbh, const zend_string *unquoted, enum pdo_param_type paramtype)
  277. {
  278. unsigned char *escaped;
  279. char *quoted;
  280. size_t quotedlen;
  281. zend_string *quoted_str;
  282. pdo_pgsql_db_handle *H = (pdo_pgsql_db_handle *)dbh->driver_data;
  283. size_t tmp_len;
  284. switch (paramtype) {
  285. case PDO_PARAM_LOB:
  286. /* escapedlen returned by PQescapeBytea() accounts for trailing 0 */
  287. escaped = PQescapeByteaConn(H->server, (unsigned char *)ZSTR_VAL(unquoted), ZSTR_LEN(unquoted), &tmp_len);
  288. quotedlen = tmp_len + 1;
  289. quoted = emalloc(quotedlen + 1);
  290. memcpy(quoted+1, escaped, quotedlen-2);
  291. quoted[0] = '\'';
  292. quoted[quotedlen-1] = '\'';
  293. quoted[quotedlen] = '\0';
  294. PQfreemem(escaped);
  295. break;
  296. default:
  297. quoted = safe_emalloc(2, ZSTR_LEN(unquoted), 3);
  298. quoted[0] = '\'';
  299. quotedlen = PQescapeStringConn(H->server, quoted + 1, ZSTR_VAL(unquoted), ZSTR_LEN(unquoted), NULL);
  300. quoted[quotedlen + 1] = '\'';
  301. quoted[quotedlen + 2] = '\0';
  302. quotedlen += 2;
  303. }
  304. quoted_str = zend_string_init(quoted, quotedlen, 0);
  305. efree(quoted);
  306. return quoted_str;
  307. }
  308. static zend_string *pdo_pgsql_last_insert_id(pdo_dbh_t *dbh, const zend_string *name)
  309. {
  310. pdo_pgsql_db_handle *H = (pdo_pgsql_db_handle *)dbh->driver_data;
  311. zend_string *id = NULL;
  312. PGresult *res;
  313. ExecStatusType status;
  314. if (name == NULL) {
  315. res = PQexec(H->server, "SELECT LASTVAL()");
  316. } else {
  317. const char *q[1];
  318. q[0] = ZSTR_VAL(name);
  319. res = PQexecParams(H->server, "SELECT CURRVAL($1)", 1, NULL, q, NULL, NULL, 0);
  320. }
  321. status = PQresultStatus(res);
  322. if (res && (status == PGRES_TUPLES_OK)) {
  323. id = zend_string_init((char *)PQgetvalue(res, 0, 0), PQgetlength(res, 0, 0), 0);
  324. } else {
  325. pdo_pgsql_error(dbh, status, pdo_pgsql_sqlstate(res));
  326. }
  327. if (res) {
  328. PQclear(res);
  329. }
  330. return id;
  331. }
  332. void pdo_libpq_version(char *buf, size_t len)
  333. {
  334. int version = PQlibVersion();
  335. int major = version / 10000;
  336. if (major >= 10) {
  337. int minor = version % 10000;
  338. snprintf(buf, len, "%d.%d", major, minor);
  339. } else {
  340. int minor = version / 100 % 100;
  341. int revision = version % 100;
  342. snprintf(buf, len, "%d.%d.%d", major, minor, revision);
  343. }
  344. }
  345. static int pdo_pgsql_get_attribute(pdo_dbh_t *dbh, zend_long attr, zval *return_value)
  346. {
  347. pdo_pgsql_db_handle *H = (pdo_pgsql_db_handle *)dbh->driver_data;
  348. switch (attr) {
  349. case PDO_ATTR_EMULATE_PREPARES:
  350. ZVAL_BOOL(return_value, H->emulate_prepares);
  351. break;
  352. case PDO_PGSQL_ATTR_DISABLE_PREPARES:
  353. ZVAL_BOOL(return_value, H->disable_prepares);
  354. break;
  355. case PDO_ATTR_CLIENT_VERSION: {
  356. char buf[16];
  357. pdo_libpq_version(buf, sizeof(buf));
  358. ZVAL_STRING(return_value, buf);
  359. break;
  360. }
  361. case PDO_ATTR_SERVER_VERSION:
  362. if (PQprotocolVersion(H->server) >= 3) { /* PostgreSQL 7.4 or later */
  363. ZVAL_STRING(return_value, (char*)PQparameterStatus(H->server, "server_version"));
  364. } else /* emulate above via a query */
  365. {
  366. PGresult *res = PQexec(H->server, "SELECT VERSION()");
  367. if (res && PQresultStatus(res) == PGRES_TUPLES_OK) {
  368. ZVAL_STRING(return_value, (char *)PQgetvalue(res, 0, 0));
  369. }
  370. if (res) {
  371. PQclear(res);
  372. }
  373. }
  374. break;
  375. case PDO_ATTR_CONNECTION_STATUS:
  376. switch (PQstatus(H->server)) {
  377. case CONNECTION_STARTED:
  378. ZVAL_STRINGL(return_value, "Waiting for connection to be made.", sizeof("Waiting for connection to be made.")-1);
  379. break;
  380. case CONNECTION_MADE:
  381. case CONNECTION_OK:
  382. ZVAL_STRINGL(return_value, "Connection OK; waiting to send.", sizeof("Connection OK; waiting to send.")-1);
  383. break;
  384. case CONNECTION_AWAITING_RESPONSE:
  385. ZVAL_STRINGL(return_value, "Waiting for a response from the server.", sizeof("Waiting for a response from the server.")-1);
  386. break;
  387. case CONNECTION_AUTH_OK:
  388. ZVAL_STRINGL(return_value, "Received authentication; waiting for backend start-up to finish.", sizeof("Received authentication; waiting for backend start-up to finish.")-1);
  389. break;
  390. #ifdef CONNECTION_SSL_STARTUP
  391. case CONNECTION_SSL_STARTUP:
  392. ZVAL_STRINGL(return_value, "Negotiating SSL encryption.", sizeof("Negotiating SSL encryption.")-1);
  393. break;
  394. #endif
  395. case CONNECTION_SETENV:
  396. ZVAL_STRINGL(return_value, "Negotiating environment-driven parameter settings.", sizeof("Negotiating environment-driven parameter settings.")-1);
  397. break;
  398. case CONNECTION_BAD:
  399. default:
  400. ZVAL_STRINGL(return_value, "Bad connection.", sizeof("Bad connection.")-1);
  401. break;
  402. }
  403. break;
  404. case PDO_ATTR_SERVER_INFO: {
  405. int spid = PQbackendPID(H->server);
  406. zend_string *str_info =
  407. strpprintf(0,
  408. "PID: %d; Client Encoding: %s; Is Superuser: %s; Session Authorization: %s; Date Style: %s",
  409. spid,
  410. (char*)PQparameterStatus(H->server, "client_encoding"),
  411. (char*)PQparameterStatus(H->server, "is_superuser"),
  412. (char*)PQparameterStatus(H->server, "session_authorization"),
  413. (char*)PQparameterStatus(H->server, "DateStyle"));
  414. ZVAL_STR(return_value, str_info);
  415. break;
  416. }
  417. default:
  418. return 0;
  419. }
  420. return 1;
  421. }
  422. /* {{{ */
  423. static zend_result pdo_pgsql_check_liveness(pdo_dbh_t *dbh)
  424. {
  425. pdo_pgsql_db_handle *H = (pdo_pgsql_db_handle *)dbh->driver_data;
  426. if (!PQconsumeInput(H->server) || PQstatus(H->server) == CONNECTION_BAD) {
  427. PQreset(H->server);
  428. }
  429. return (PQstatus(H->server) == CONNECTION_OK) ? SUCCESS : FAILURE;
  430. }
  431. /* }}} */
  432. static bool pgsql_handle_in_transaction(pdo_dbh_t *dbh)
  433. {
  434. pdo_pgsql_db_handle *H;
  435. H = (pdo_pgsql_db_handle *)dbh->driver_data;
  436. return PQtransactionStatus(H->server) > PQTRANS_IDLE;
  437. }
  438. static bool pdo_pgsql_transaction_cmd(const char *cmd, pdo_dbh_t *dbh)
  439. {
  440. pdo_pgsql_db_handle *H = (pdo_pgsql_db_handle *)dbh->driver_data;
  441. PGresult *res;
  442. bool ret = true;
  443. res = PQexec(H->server, cmd);
  444. if (PQresultStatus(res) != PGRES_COMMAND_OK) {
  445. pdo_pgsql_error(dbh, PQresultStatus(res), pdo_pgsql_sqlstate(res));
  446. ret = false;
  447. }
  448. PQclear(res);
  449. return ret;
  450. }
  451. static bool pgsql_handle_begin(pdo_dbh_t *dbh)
  452. {
  453. return pdo_pgsql_transaction_cmd("BEGIN", dbh);
  454. }
  455. static bool pgsql_handle_commit(pdo_dbh_t *dbh)
  456. {
  457. bool ret = pdo_pgsql_transaction_cmd("COMMIT", dbh);
  458. /* When deferred constraints are used the commit could
  459. fail, and a ROLLBACK implicitly ran. See bug #67462 */
  460. if (!ret) {
  461. dbh->in_txn = pgsql_handle_in_transaction(dbh);
  462. }
  463. return ret;
  464. }
  465. static bool pgsql_handle_rollback(pdo_dbh_t *dbh)
  466. {
  467. return pdo_pgsql_transaction_cmd("ROLLBACK", dbh);
  468. }
  469. /* {{{ Returns true if the copy worked fine or false if error */
  470. PHP_METHOD(PDO_PGSql_Ext, pgsqlCopyFromArray)
  471. {
  472. pdo_dbh_t *dbh;
  473. pdo_pgsql_db_handle *H;
  474. zval *pg_rows;
  475. char *table_name, *pg_delim = NULL, *pg_null_as = NULL, *pg_fields = NULL;
  476. size_t table_name_len, pg_delim_len = 0, pg_null_as_len = 0, pg_fields_len;
  477. char *query;
  478. PGresult *pgsql_result;
  479. ExecStatusType status;
  480. if (zend_parse_parameters(ZEND_NUM_ARGS(), "sa|sss!",
  481. &table_name, &table_name_len, &pg_rows,
  482. &pg_delim, &pg_delim_len, &pg_null_as, &pg_null_as_len, &pg_fields, &pg_fields_len) == FAILURE) {
  483. RETURN_THROWS();
  484. }
  485. if (!zend_hash_num_elements(Z_ARRVAL_P(pg_rows))) {
  486. zend_argument_value_error(2, "cannot be empty");
  487. RETURN_THROWS();
  488. }
  489. dbh = Z_PDO_DBH_P(ZEND_THIS);
  490. PDO_CONSTRUCT_CHECK;
  491. PDO_DBH_CLEAR_ERR();
  492. /* using pre-9.0 syntax as PDO_pgsql is 7.4+ compatible */
  493. if (pg_fields) {
  494. spprintf(&query, 0, "COPY %s (%s) FROM STDIN WITH DELIMITER E'%c' NULL AS E'%s'", table_name, pg_fields, (pg_delim_len ? *pg_delim : '\t'), (pg_null_as_len ? pg_null_as : "\\\\N"));
  495. } else {
  496. spprintf(&query, 0, "COPY %s FROM STDIN WITH DELIMITER E'%c' NULL AS E'%s'", table_name, (pg_delim_len ? *pg_delim : '\t'), (pg_null_as_len ? pg_null_as : "\\\\N"));
  497. }
  498. /* Obtain db Handle */
  499. H = (pdo_pgsql_db_handle *)dbh->driver_data;
  500. while ((pgsql_result = PQgetResult(H->server))) {
  501. PQclear(pgsql_result);
  502. }
  503. pgsql_result = PQexec(H->server, query);
  504. efree(query);
  505. query = NULL;
  506. if (pgsql_result) {
  507. status = PQresultStatus(pgsql_result);
  508. } else {
  509. status = (ExecStatusType) PQstatus(H->server);
  510. }
  511. if (status == PGRES_COPY_IN && pgsql_result) {
  512. int command_failed = 0;
  513. size_t buffer_len = 0;
  514. zval *tmp;
  515. PQclear(pgsql_result);
  516. ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(pg_rows), tmp) {
  517. size_t query_len;
  518. if (!try_convert_to_string(tmp)) {
  519. efree(query);
  520. RETURN_THROWS();
  521. }
  522. if (buffer_len < Z_STRLEN_P(tmp)) {
  523. buffer_len = Z_STRLEN_P(tmp);
  524. query = erealloc(query, buffer_len + 2); /* room for \n\0 */
  525. }
  526. memcpy(query, Z_STRVAL_P(tmp), Z_STRLEN_P(tmp));
  527. query_len = Z_STRLEN_P(tmp);
  528. if (query[query_len - 1] != '\n') {
  529. query[query_len++] = '\n';
  530. }
  531. query[query_len] = '\0';
  532. if (PQputCopyData(H->server, query, query_len) != 1) {
  533. efree(query);
  534. pdo_pgsql_error(dbh, PGRES_FATAL_ERROR, NULL);
  535. PDO_HANDLE_DBH_ERR();
  536. RETURN_FALSE;
  537. }
  538. } ZEND_HASH_FOREACH_END();
  539. if (query) {
  540. efree(query);
  541. }
  542. if (PQputCopyEnd(H->server, NULL) != 1) {
  543. pdo_pgsql_error(dbh, PGRES_FATAL_ERROR, NULL);
  544. PDO_HANDLE_DBH_ERR();
  545. RETURN_FALSE;
  546. }
  547. while ((pgsql_result = PQgetResult(H->server))) {
  548. if (PGRES_COMMAND_OK != PQresultStatus(pgsql_result)) {
  549. pdo_pgsql_error(dbh, PGRES_FATAL_ERROR, pdo_pgsql_sqlstate(pgsql_result));
  550. command_failed = 1;
  551. }
  552. PQclear(pgsql_result);
  553. }
  554. PDO_HANDLE_DBH_ERR();
  555. RETURN_BOOL(!command_failed);
  556. } else {
  557. pdo_pgsql_error(dbh, PGRES_FATAL_ERROR, pdo_pgsql_sqlstate(pgsql_result));
  558. PQclear(pgsql_result);
  559. PDO_HANDLE_DBH_ERR();
  560. RETURN_FALSE;
  561. }
  562. }
  563. /* }}} */
  564. /* {{{ Returns true if the copy worked fine or false if error */
  565. PHP_METHOD(PDO_PGSql_Ext, pgsqlCopyFromFile)
  566. {
  567. pdo_dbh_t *dbh;
  568. pdo_pgsql_db_handle *H;
  569. char *table_name, *filename, *pg_delim = NULL, *pg_null_as = NULL, *pg_fields = NULL;
  570. size_t table_name_len, filename_len, pg_delim_len = 0, pg_null_as_len = 0, pg_fields_len;
  571. char *query;
  572. PGresult *pgsql_result;
  573. ExecStatusType status;
  574. php_stream *stream;
  575. if (zend_parse_parameters(ZEND_NUM_ARGS(), "sp|sss!",
  576. &table_name, &table_name_len, &filename, &filename_len,
  577. &pg_delim, &pg_delim_len, &pg_null_as, &pg_null_as_len, &pg_fields, &pg_fields_len) == FAILURE) {
  578. RETURN_THROWS();
  579. }
  580. /* Obtain db Handler */
  581. dbh = Z_PDO_DBH_P(ZEND_THIS);
  582. PDO_CONSTRUCT_CHECK;
  583. PDO_DBH_CLEAR_ERR();
  584. stream = php_stream_open_wrapper_ex(filename, "rb", 0, NULL, FG(default_context));
  585. if (!stream) {
  586. pdo_pgsql_error_msg(dbh, PGRES_FATAL_ERROR, "Unable to open the file");
  587. PDO_HANDLE_DBH_ERR();
  588. RETURN_FALSE;
  589. }
  590. /* using pre-9.0 syntax as PDO_pgsql is 7.4+ compatible */
  591. if (pg_fields) {
  592. spprintf(&query, 0, "COPY %s (%s) FROM STDIN WITH DELIMITER E'%c' NULL AS E'%s'", table_name, pg_fields, (pg_delim_len ? *pg_delim : '\t'), (pg_null_as_len ? pg_null_as : "\\\\N"));
  593. } else {
  594. spprintf(&query, 0, "COPY %s FROM STDIN WITH DELIMITER E'%c' NULL AS E'%s'", table_name, (pg_delim_len ? *pg_delim : '\t'), (pg_null_as_len ? pg_null_as : "\\\\N"));
  595. }
  596. H = (pdo_pgsql_db_handle *)dbh->driver_data;
  597. while ((pgsql_result = PQgetResult(H->server))) {
  598. PQclear(pgsql_result);
  599. }
  600. pgsql_result = PQexec(H->server, query);
  601. efree(query);
  602. if (pgsql_result) {
  603. status = PQresultStatus(pgsql_result);
  604. } else {
  605. status = (ExecStatusType) PQstatus(H->server);
  606. }
  607. if (status == PGRES_COPY_IN && pgsql_result) {
  608. char *buf;
  609. int command_failed = 0;
  610. size_t line_len = 0;
  611. PQclear(pgsql_result);
  612. while ((buf = php_stream_get_line(stream, NULL, 0, &line_len)) != NULL) {
  613. if (PQputCopyData(H->server, buf, line_len) != 1) {
  614. efree(buf);
  615. pdo_pgsql_error(dbh, PGRES_FATAL_ERROR, NULL);
  616. php_stream_close(stream);
  617. PDO_HANDLE_DBH_ERR();
  618. RETURN_FALSE;
  619. }
  620. efree(buf);
  621. }
  622. php_stream_close(stream);
  623. if (PQputCopyEnd(H->server, NULL) != 1) {
  624. pdo_pgsql_error(dbh, PGRES_FATAL_ERROR, NULL);
  625. PDO_HANDLE_DBH_ERR();
  626. RETURN_FALSE;
  627. }
  628. while ((pgsql_result = PQgetResult(H->server))) {
  629. if (PGRES_COMMAND_OK != PQresultStatus(pgsql_result)) {
  630. pdo_pgsql_error(dbh, PGRES_FATAL_ERROR, pdo_pgsql_sqlstate(pgsql_result));
  631. command_failed = 1;
  632. }
  633. PQclear(pgsql_result);
  634. }
  635. PDO_HANDLE_DBH_ERR();
  636. RETURN_BOOL(!command_failed);
  637. } else {
  638. php_stream_close(stream);
  639. pdo_pgsql_error(dbh, PGRES_FATAL_ERROR, pdo_pgsql_sqlstate(pgsql_result));
  640. PQclear(pgsql_result);
  641. PDO_HANDLE_DBH_ERR();
  642. RETURN_FALSE;
  643. }
  644. }
  645. /* }}} */
  646. /* {{{ Returns true if the copy worked fine or false if error */
  647. PHP_METHOD(PDO_PGSql_Ext, pgsqlCopyToFile)
  648. {
  649. pdo_dbh_t *dbh;
  650. pdo_pgsql_db_handle *H;
  651. char *table_name, *pg_delim = NULL, *pg_null_as = NULL, *pg_fields = NULL, *filename = NULL;
  652. size_t table_name_len, pg_delim_len = 0, pg_null_as_len = 0, pg_fields_len, filename_len;
  653. char *query;
  654. PGresult *pgsql_result;
  655. ExecStatusType status;
  656. php_stream *stream;
  657. if (zend_parse_parameters(ZEND_NUM_ARGS(), "sp|sss!",
  658. &table_name, &table_name_len, &filename, &filename_len,
  659. &pg_delim, &pg_delim_len, &pg_null_as, &pg_null_as_len, &pg_fields, &pg_fields_len) == FAILURE) {
  660. RETURN_THROWS();
  661. }
  662. dbh = Z_PDO_DBH_P(ZEND_THIS);
  663. PDO_CONSTRUCT_CHECK;
  664. PDO_DBH_CLEAR_ERR();
  665. H = (pdo_pgsql_db_handle *)dbh->driver_data;
  666. stream = php_stream_open_wrapper_ex(filename, "wb", 0, NULL, FG(default_context));
  667. if (!stream) {
  668. pdo_pgsql_error_msg(dbh, PGRES_FATAL_ERROR, "Unable to open the file for writing");
  669. PDO_HANDLE_DBH_ERR();
  670. RETURN_FALSE;
  671. }
  672. while ((pgsql_result = PQgetResult(H->server))) {
  673. PQclear(pgsql_result);
  674. }
  675. /* using pre-9.0 syntax as PDO_pgsql is 7.4+ compatible */
  676. if (pg_fields) {
  677. spprintf(&query, 0, "COPY %s (%s) TO STDIN WITH DELIMITER E'%c' NULL AS E'%s'", table_name, pg_fields, (pg_delim_len ? *pg_delim : '\t'), (pg_null_as_len ? pg_null_as : "\\\\N"));
  678. } else {
  679. spprintf(&query, 0, "COPY %s TO STDIN WITH DELIMITER E'%c' NULL AS E'%s'", table_name, (pg_delim_len ? *pg_delim : '\t'), (pg_null_as_len ? pg_null_as : "\\\\N"));
  680. }
  681. pgsql_result = PQexec(H->server, query);
  682. efree(query);
  683. if (pgsql_result) {
  684. status = PQresultStatus(pgsql_result);
  685. } else {
  686. status = (ExecStatusType) PQstatus(H->server);
  687. }
  688. if (status == PGRES_COPY_OUT && pgsql_result) {
  689. PQclear(pgsql_result);
  690. while (1) {
  691. char *csv = NULL;
  692. int ret = PQgetCopyData(H->server, &csv, 0);
  693. if (ret == -1) {
  694. break; /* done */
  695. } else if (ret > 0) {
  696. if (php_stream_write(stream, csv, ret) != (size_t)ret) {
  697. pdo_pgsql_error_msg(dbh, PGRES_FATAL_ERROR, "Unable to write to file");
  698. PQfreemem(csv);
  699. php_stream_close(stream);
  700. PDO_HANDLE_DBH_ERR();
  701. RETURN_FALSE;
  702. } else {
  703. PQfreemem(csv);
  704. }
  705. } else {
  706. pdo_pgsql_error(dbh, PGRES_FATAL_ERROR, NULL);
  707. php_stream_close(stream);
  708. PDO_HANDLE_DBH_ERR();
  709. RETURN_FALSE;
  710. }
  711. }
  712. php_stream_close(stream);
  713. while ((pgsql_result = PQgetResult(H->server))) {
  714. PQclear(pgsql_result);
  715. }
  716. RETURN_TRUE;
  717. } else {
  718. php_stream_close(stream);
  719. pdo_pgsql_error(dbh, PGRES_FATAL_ERROR, pdo_pgsql_sqlstate(pgsql_result));
  720. PQclear(pgsql_result);
  721. PDO_HANDLE_DBH_ERR();
  722. RETURN_FALSE;
  723. }
  724. }
  725. /* }}} */
  726. /* {{{ Returns true if the copy worked fine or false if error */
  727. PHP_METHOD(PDO_PGSql_Ext, pgsqlCopyToArray)
  728. {
  729. pdo_dbh_t *dbh;
  730. pdo_pgsql_db_handle *H;
  731. char *table_name, *pg_delim = NULL, *pg_null_as = NULL, *pg_fields = NULL;
  732. size_t table_name_len, pg_delim_len = 0, pg_null_as_len = 0, pg_fields_len;
  733. char *query;
  734. PGresult *pgsql_result;
  735. ExecStatusType status;
  736. if (zend_parse_parameters(ZEND_NUM_ARGS(), "s|sss!",
  737. &table_name, &table_name_len,
  738. &pg_delim, &pg_delim_len, &pg_null_as, &pg_null_as_len, &pg_fields, &pg_fields_len) == FAILURE) {
  739. RETURN_THROWS();
  740. }
  741. dbh = Z_PDO_DBH_P(ZEND_THIS);
  742. PDO_CONSTRUCT_CHECK;
  743. PDO_DBH_CLEAR_ERR();
  744. H = (pdo_pgsql_db_handle *)dbh->driver_data;
  745. while ((pgsql_result = PQgetResult(H->server))) {
  746. PQclear(pgsql_result);
  747. }
  748. /* using pre-9.0 syntax as PDO_pgsql is 7.4+ compatible */
  749. if (pg_fields) {
  750. spprintf(&query, 0, "COPY %s (%s) TO STDIN WITH DELIMITER E'%c' NULL AS E'%s'", table_name, pg_fields, (pg_delim_len ? *pg_delim : '\t'), (pg_null_as_len ? pg_null_as : "\\\\N"));
  751. } else {
  752. spprintf(&query, 0, "COPY %s TO STDIN WITH DELIMITER E'%c' NULL AS E'%s'", table_name, (pg_delim_len ? *pg_delim : '\t'), (pg_null_as_len ? pg_null_as : "\\\\N"));
  753. }
  754. pgsql_result = PQexec(H->server, query);
  755. efree(query);
  756. if (pgsql_result) {
  757. status = PQresultStatus(pgsql_result);
  758. } else {
  759. status = (ExecStatusType) PQstatus(H->server);
  760. }
  761. if (status == PGRES_COPY_OUT && pgsql_result) {
  762. PQclear(pgsql_result);
  763. array_init(return_value);
  764. while (1) {
  765. char *csv = NULL;
  766. int ret = PQgetCopyData(H->server, &csv, 0);
  767. if (ret == -1) {
  768. break; /* copy done */
  769. } else if (ret > 0) {
  770. add_next_index_stringl(return_value, csv, ret);
  771. PQfreemem(csv);
  772. } else {
  773. pdo_pgsql_error(dbh, PGRES_FATAL_ERROR, NULL);
  774. PDO_HANDLE_DBH_ERR();
  775. RETURN_FALSE;
  776. }
  777. }
  778. while ((pgsql_result = PQgetResult(H->server))) {
  779. PQclear(pgsql_result);
  780. }
  781. } else {
  782. pdo_pgsql_error(dbh, PGRES_FATAL_ERROR, pdo_pgsql_sqlstate(pgsql_result));
  783. PQclear(pgsql_result);
  784. PDO_HANDLE_DBH_ERR();
  785. RETURN_FALSE;
  786. }
  787. }
  788. /* }}} */
  789. /* {{{ Creates a new large object, returning its identifier. Must be called inside a transaction. */
  790. PHP_METHOD(PDO_PGSql_Ext, pgsqlLOBCreate)
  791. {
  792. pdo_dbh_t *dbh;
  793. pdo_pgsql_db_handle *H;
  794. Oid lfd;
  795. ZEND_PARSE_PARAMETERS_NONE();
  796. dbh = Z_PDO_DBH_P(ZEND_THIS);
  797. PDO_CONSTRUCT_CHECK;
  798. PDO_DBH_CLEAR_ERR();
  799. H = (pdo_pgsql_db_handle *)dbh->driver_data;
  800. lfd = lo_creat(H->server, INV_READ|INV_WRITE);
  801. if (lfd != InvalidOid) {
  802. zend_string *buf = strpprintf(0, ZEND_ULONG_FMT, (zend_long) lfd);
  803. RETURN_STR(buf);
  804. }
  805. pdo_pgsql_error(dbh, PGRES_FATAL_ERROR, NULL);
  806. PDO_HANDLE_DBH_ERR();
  807. RETURN_FALSE;
  808. }
  809. /* }}} */
  810. /* {{{ Opens an existing large object stream. Must be called inside a transaction. */
  811. PHP_METHOD(PDO_PGSql_Ext, pgsqlLOBOpen)
  812. {
  813. pdo_dbh_t *dbh;
  814. pdo_pgsql_db_handle *H;
  815. Oid oid;
  816. int lfd;
  817. char *oidstr;
  818. size_t oidstrlen;
  819. char *modestr = "rb";
  820. size_t modestrlen;
  821. int mode = INV_READ;
  822. char *end_ptr;
  823. if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS(), "s|s",
  824. &oidstr, &oidstrlen, &modestr, &modestrlen)) {
  825. RETURN_THROWS();
  826. }
  827. oid = (Oid)strtoul(oidstr, &end_ptr, 10);
  828. if (oid == 0 && (errno == ERANGE || errno == EINVAL)) {
  829. RETURN_FALSE;
  830. }
  831. if (strpbrk(modestr, "+w")) {
  832. mode = INV_READ|INV_WRITE;
  833. }
  834. dbh = Z_PDO_DBH_P(ZEND_THIS);
  835. PDO_CONSTRUCT_CHECK;
  836. PDO_DBH_CLEAR_ERR();
  837. H = (pdo_pgsql_db_handle *)dbh->driver_data;
  838. lfd = lo_open(H->server, oid, mode);
  839. if (lfd >= 0) {
  840. php_stream *stream = pdo_pgsql_create_lob_stream(ZEND_THIS, lfd, oid);
  841. if (stream) {
  842. php_stream_to_zval(stream, return_value);
  843. return;
  844. }
  845. } else {
  846. pdo_pgsql_error(dbh, PGRES_FATAL_ERROR, NULL);
  847. }
  848. PDO_HANDLE_DBH_ERR();
  849. RETURN_FALSE;
  850. }
  851. /* }}} */
  852. /* {{{ Deletes the large object identified by oid. Must be called inside a transaction. */
  853. PHP_METHOD(PDO_PGSql_Ext, pgsqlLOBUnlink)
  854. {
  855. pdo_dbh_t *dbh;
  856. pdo_pgsql_db_handle *H;
  857. Oid oid;
  858. char *oidstr, *end_ptr;
  859. size_t oidlen;
  860. if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS(), "s",
  861. &oidstr, &oidlen)) {
  862. RETURN_THROWS();
  863. }
  864. oid = (Oid)strtoul(oidstr, &end_ptr, 10);
  865. if (oid == 0 && (errno == ERANGE || errno == EINVAL)) {
  866. RETURN_FALSE;
  867. }
  868. dbh = Z_PDO_DBH_P(ZEND_THIS);
  869. PDO_CONSTRUCT_CHECK;
  870. PDO_DBH_CLEAR_ERR();
  871. H = (pdo_pgsql_db_handle *)dbh->driver_data;
  872. if (1 == lo_unlink(H->server, oid)) {
  873. RETURN_TRUE;
  874. }
  875. pdo_pgsql_error(dbh, PGRES_FATAL_ERROR, NULL);
  876. PDO_HANDLE_DBH_ERR();
  877. RETURN_FALSE;
  878. }
  879. /* }}} */
  880. /* {{{ Get asynchronous notification */
  881. PHP_METHOD(PDO_PGSql_Ext, pgsqlGetNotify)
  882. {
  883. pdo_dbh_t *dbh;
  884. pdo_pgsql_db_handle *H;
  885. zend_long result_type = PDO_FETCH_USE_DEFAULT;
  886. zend_long ms_timeout = 0;
  887. PGnotify *pgsql_notify;
  888. if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS(), "|ll",
  889. &result_type, &ms_timeout)) {
  890. RETURN_THROWS();
  891. }
  892. dbh = Z_PDO_DBH_P(ZEND_THIS);
  893. PDO_CONSTRUCT_CHECK;
  894. if (result_type == PDO_FETCH_USE_DEFAULT) {
  895. result_type = dbh->default_fetch_type;
  896. }
  897. if (result_type != PDO_FETCH_BOTH && result_type != PDO_FETCH_ASSOC && result_type != PDO_FETCH_NUM) {
  898. zend_argument_value_error(1, "must be one of PDO::FETCH_BOTH, PDO::FETCH_ASSOC, or PDO::FETCH_NUM");
  899. RETURN_THROWS();
  900. }
  901. if (ms_timeout < 0) {
  902. zend_argument_value_error(2, "must be greater than or equal to 0");
  903. RETURN_THROWS();
  904. #ifdef ZEND_ENABLE_ZVAL_LONG64
  905. } else if (ms_timeout > INT_MAX) {
  906. php_error_docref(NULL, E_WARNING, "Timeout was shrunk to %d", INT_MAX);
  907. ms_timeout = INT_MAX;
  908. #endif
  909. }
  910. H = (pdo_pgsql_db_handle *)dbh->driver_data;
  911. if (!PQconsumeInput(H->server)) {
  912. pdo_pgsql_error(dbh, PGRES_FATAL_ERROR, NULL);
  913. PDO_HANDLE_DBH_ERR();
  914. RETURN_FALSE;
  915. }
  916. pgsql_notify = PQnotifies(H->server);
  917. if (ms_timeout && !pgsql_notify) {
  918. php_pollfd_for_ms(PQsocket(H->server), PHP_POLLREADABLE, (int)ms_timeout);
  919. if (!PQconsumeInput(H->server)) {
  920. pdo_pgsql_error(dbh, PGRES_FATAL_ERROR, NULL);
  921. PDO_HANDLE_DBH_ERR();
  922. RETURN_FALSE;
  923. }
  924. pgsql_notify = PQnotifies(H->server);
  925. }
  926. if (!pgsql_notify) {
  927. RETURN_FALSE;
  928. }
  929. array_init(return_value);
  930. if (result_type == PDO_FETCH_NUM || result_type == PDO_FETCH_BOTH) {
  931. add_index_string(return_value, 0, pgsql_notify->relname);
  932. add_index_long(return_value, 1, pgsql_notify->be_pid);
  933. if (pgsql_notify->extra && pgsql_notify->extra[0]) {
  934. add_index_string(return_value, 2, pgsql_notify->extra);
  935. }
  936. }
  937. if (result_type == PDO_FETCH_ASSOC || result_type == PDO_FETCH_BOTH) {
  938. add_assoc_string(return_value, "message", pgsql_notify->relname);
  939. add_assoc_long(return_value, "pid", pgsql_notify->be_pid);
  940. if (pgsql_notify->extra && pgsql_notify->extra[0]) {
  941. add_assoc_string(return_value, "payload", pgsql_notify->extra);
  942. }
  943. }
  944. PQfreemem(pgsql_notify);
  945. }
  946. /* }}} */
  947. /* {{{ Get backend(server) pid */
  948. PHP_METHOD(PDO_PGSql_Ext, pgsqlGetPid)
  949. {
  950. pdo_dbh_t *dbh;
  951. pdo_pgsql_db_handle *H;
  952. ZEND_PARSE_PARAMETERS_NONE();
  953. dbh = Z_PDO_DBH_P(ZEND_THIS);
  954. PDO_CONSTRUCT_CHECK;
  955. H = (pdo_pgsql_db_handle *)dbh->driver_data;
  956. RETURN_LONG(PQbackendPID(H->server));
  957. }
  958. /* }}} */
  959. static const zend_function_entry *pdo_pgsql_get_driver_methods(pdo_dbh_t *dbh, int kind)
  960. {
  961. switch (kind) {
  962. case PDO_DBH_DRIVER_METHOD_KIND_DBH:
  963. return class_PDO_PGSql_Ext_methods;
  964. default:
  965. return NULL;
  966. }
  967. }
  968. static bool pdo_pgsql_set_attr(pdo_dbh_t *dbh, zend_long attr, zval *val)
  969. {
  970. bool bval;
  971. pdo_pgsql_db_handle *H = (pdo_pgsql_db_handle *)dbh->driver_data;
  972. switch (attr) {
  973. case PDO_ATTR_EMULATE_PREPARES:
  974. if (!pdo_get_bool_param(&bval, val)) {
  975. return false;
  976. }
  977. H->emulate_prepares = bval;
  978. return true;
  979. case PDO_PGSQL_ATTR_DISABLE_PREPARES:
  980. if (!pdo_get_bool_param(&bval, val)) {
  981. return false;
  982. }
  983. H->disable_prepares = bval;
  984. return true;
  985. default:
  986. return false;
  987. }
  988. }
  989. static const struct pdo_dbh_methods pgsql_methods = {
  990. pgsql_handle_closer,
  991. pgsql_handle_preparer,
  992. pgsql_handle_doer,
  993. pgsql_handle_quoter,
  994. pgsql_handle_begin,
  995. pgsql_handle_commit,
  996. pgsql_handle_rollback,
  997. pdo_pgsql_set_attr,
  998. pdo_pgsql_last_insert_id,
  999. pdo_pgsql_fetch_error_func,
  1000. pdo_pgsql_get_attribute,
  1001. pdo_pgsql_check_liveness, /* check_liveness */
  1002. pdo_pgsql_get_driver_methods, /* get_driver_methods */
  1003. NULL,
  1004. pgsql_handle_in_transaction,
  1005. NULL /* get_gc */
  1006. };
  1007. static int pdo_pgsql_handle_factory(pdo_dbh_t *dbh, zval *driver_options) /* {{{ */
  1008. {
  1009. pdo_pgsql_db_handle *H;
  1010. int ret = 0;
  1011. char *conn_str, *p, *e;
  1012. zend_string *tmp_user, *tmp_pass;
  1013. zend_long connect_timeout = 30;
  1014. H = pecalloc(1, sizeof(pdo_pgsql_db_handle), dbh->is_persistent);
  1015. dbh->driver_data = H;
  1016. dbh->skip_param_evt =
  1017. 1 << PDO_PARAM_EVT_EXEC_POST |
  1018. 1 << PDO_PARAM_EVT_FETCH_PRE |
  1019. 1 << PDO_PARAM_EVT_FETCH_POST;
  1020. H->einfo.errcode = 0;
  1021. H->einfo.errmsg = NULL;
  1022. /* PostgreSQL wants params in the connect string to be separated by spaces,
  1023. * if the PDO standard semicolons are used, we convert them to spaces
  1024. */
  1025. e = (char *) dbh->data_source + strlen(dbh->data_source);
  1026. p = (char *) dbh->data_source;
  1027. while ((p = memchr(p, ';', (e - p)))) {
  1028. *p = ' ';
  1029. }
  1030. if (driver_options) {
  1031. connect_timeout = pdo_attr_lval(driver_options, PDO_ATTR_TIMEOUT, 30);
  1032. }
  1033. /* escape username and password, if provided */
  1034. tmp_user = _pdo_pgsql_escape_credentials(dbh->username);
  1035. tmp_pass = _pdo_pgsql_escape_credentials(dbh->password);
  1036. /* support both full connection string & connection string + login and/or password */
  1037. if (tmp_user && tmp_pass) {
  1038. spprintf(&conn_str, 0, "%s user='%s' password='%s' connect_timeout=" ZEND_LONG_FMT, (char *) dbh->data_source, ZSTR_VAL(tmp_user), ZSTR_VAL(tmp_pass), connect_timeout);
  1039. } else if (tmp_user) {
  1040. spprintf(&conn_str, 0, "%s user='%s' connect_timeout=" ZEND_LONG_FMT, (char *) dbh->data_source, ZSTR_VAL(tmp_user), connect_timeout);
  1041. } else if (tmp_pass) {
  1042. spprintf(&conn_str, 0, "%s password='%s' connect_timeout=" ZEND_LONG_FMT, (char *) dbh->data_source, ZSTR_VAL(tmp_pass), connect_timeout);
  1043. } else {
  1044. spprintf(&conn_str, 0, "%s connect_timeout=" ZEND_LONG_FMT, (char *) dbh->data_source, connect_timeout);
  1045. }
  1046. H->server = PQconnectdb(conn_str);
  1047. if (tmp_user) {
  1048. zend_string_release_ex(tmp_user, 0);
  1049. }
  1050. if (tmp_pass) {
  1051. zend_string_release_ex(tmp_pass, 0);
  1052. }
  1053. efree(conn_str);
  1054. if (PQstatus(H->server) != CONNECTION_OK) {
  1055. pdo_pgsql_error(dbh, PGRES_FATAL_ERROR, PHP_PDO_PGSQL_CONNECTION_FAILURE_SQLSTATE);
  1056. goto cleanup;
  1057. }
  1058. PQsetNoticeProcessor(H->server, (void(*)(void*,const char*))_pdo_pgsql_notice, (void *)&dbh);
  1059. H->attached = 1;
  1060. H->pgoid = -1;
  1061. dbh->methods = &pgsql_methods;
  1062. dbh->alloc_own_columns = 1;
  1063. dbh->max_escaped_char_length = 2;
  1064. ret = 1;
  1065. cleanup:
  1066. dbh->methods = &pgsql_methods;
  1067. if (!ret) {
  1068. pgsql_handle_closer(dbh);
  1069. }
  1070. return ret;
  1071. }
  1072. /* }}} */
  1073. const pdo_driver_t pdo_pgsql_driver = {
  1074. PDO_DRIVER_HEADER(pgsql),
  1075. pdo_pgsql_handle_factory
  1076. };