pgsql_driver.c 35 KB

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