You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

726 lines
19 KiB

21 years ago
21 years ago
20 years ago
20 years ago
21 years ago
21 years ago
21 years ago
21 years ago
21 years ago
21 years ago
21 years ago
21 years ago
21 years ago
21 years ago
21 years ago
21 years ago
  1. /*
  2. +----------------------------------------------------------------------+
  3. | PHP Version 5 |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1997-2009 The PHP Group |
  6. +----------------------------------------------------------------------+
  7. | This source file is subject to version 3.01 of the PHP license, |
  8. | that is bundled with this package in the file LICENSE, and is |
  9. | available through the world-wide-web at the following url: |
  10. | http://www.php.net/license/3_01.txt |
  11. | If you did not receive a copy of the PHP license and are unable to |
  12. | obtain it through the world-wide-web, please send a note to |
  13. | license@php.net so we can mail you a copy immediately. |
  14. +----------------------------------------------------------------------+
  15. | Authors: Edin Kadribasic <edink@emini.dk> |
  16. | Ilia Alshanestsky <ilia@prohost.org> |
  17. | Wez Furlong <wez@php.net> |
  18. +----------------------------------------------------------------------+
  19. */
  20. /* $Id$ */
  21. #ifdef HAVE_CONFIG_H
  22. #include "config.h"
  23. #endif
  24. #include "php.h"
  25. #include "php_ini.h"
  26. #include "ext/standard/info.h"
  27. #include "pdo/php_pdo.h"
  28. #include "pdo/php_pdo_driver.h"
  29. #undef PACKAGE_BUGREPORT
  30. #undef PACKAGE_NAME
  31. #undef PACKAGE_STRING
  32. #undef PACKAGE_TARNAME
  33. #undef PACKAGE_VERSION
  34. #include "pg_config.h" /* needed for PG_VERSION */
  35. #include "php_pdo_pgsql.h"
  36. #include "php_pdo_pgsql_int.h"
  37. #include "zend_exceptions.h"
  38. static char * _pdo_pgsql_trim_message(const char *message, int persistent)
  39. {
  40. register int i = strlen(message)-1;
  41. char *tmp;
  42. if (i>1 && (message[i-1] == '\r' || message[i-1] == '\n') && message[i] == '.') {
  43. --i;
  44. }
  45. while (i>0 && (message[i] == '\r' || message[i] == '\n')) {
  46. --i;
  47. }
  48. ++i;
  49. tmp = pemalloc(i + 1, persistent);
  50. memcpy(tmp, message, i);
  51. tmp[i] = '\0';
  52. return tmp;
  53. }
  54. int _pdo_pgsql_error(pdo_dbh_t *dbh, pdo_stmt_t *stmt, int errcode, const char *sqlstate, const char *file, int line TSRMLS_DC) /* {{{ */
  55. {
  56. pdo_pgsql_db_handle *H = (pdo_pgsql_db_handle *)dbh->driver_data;
  57. pdo_error_type *pdo_err = stmt ? &stmt->error_code : &dbh->error_code;
  58. pdo_pgsql_error_info *einfo = &H->einfo;
  59. char *errmsg = PQerrorMessage(H->server);
  60. einfo->errcode = errcode;
  61. einfo->file = file;
  62. einfo->line = line;
  63. if (einfo->errmsg) {
  64. pefree(einfo->errmsg, dbh->is_persistent);
  65. einfo->errmsg = NULL;
  66. }
  67. if (sqlstate == NULL) {
  68. strcpy(*pdo_err, "HY000");
  69. }
  70. else {
  71. strcpy(*pdo_err, sqlstate);
  72. }
  73. if (errmsg) {
  74. einfo->errmsg = _pdo_pgsql_trim_message(errmsg, dbh->is_persistent);
  75. }
  76. if (!dbh->methods) {
  77. zend_throw_exception_ex(php_pdo_get_exception(), 0 TSRMLS_CC, "SQLSTATE[%s] [%d] %s",
  78. *pdo_err, einfo->errcode, einfo->errmsg);
  79. }
  80. return errcode;
  81. }
  82. /* }}} */
  83. static void _pdo_pgsql_notice(pdo_dbh_t *dbh, const char *message) /* {{{ */
  84. {
  85. /* pdo_pgsql_db_handle *H = (pdo_pgsql_db_handle *)dbh->driver_data; */
  86. }
  87. /* }}} */
  88. static int pdo_pgsql_fetch_error_func(pdo_dbh_t *dbh, pdo_stmt_t *stmt, zval *info TSRMLS_DC) /* {{{ */
  89. {
  90. pdo_pgsql_db_handle *H = (pdo_pgsql_db_handle *)dbh->driver_data;
  91. pdo_pgsql_error_info *einfo = &H->einfo;
  92. if (einfo->errcode) {
  93. add_next_index_long(info, einfo->errcode);
  94. add_next_index_string(info, einfo->errmsg, 1);
  95. }
  96. return 1;
  97. }
  98. /* }}} */
  99. /* {{{ pdo_pgsql_create_lob_stream */
  100. static size_t pgsql_lob_write(php_stream *stream, const char *buf, size_t count TSRMLS_DC)
  101. {
  102. struct pdo_pgsql_lob_self *self = (struct pdo_pgsql_lob_self*)stream->abstract;
  103. return lo_write(self->conn, self->lfd, (char*)buf, count);
  104. }
  105. static size_t pgsql_lob_read(php_stream *stream, char *buf, size_t count TSRMLS_DC)
  106. {
  107. struct pdo_pgsql_lob_self *self = (struct pdo_pgsql_lob_self*)stream->abstract;
  108. return lo_read(self->conn, self->lfd, buf, count);
  109. }
  110. static int pgsql_lob_close(php_stream *stream, int close_handle TSRMLS_DC)
  111. {
  112. struct pdo_pgsql_lob_self *self = (struct pdo_pgsql_lob_self*)stream->abstract;
  113. pdo_dbh_t *dbh = self->dbh;
  114. if (close_handle) {
  115. lo_close(self->conn, self->lfd);
  116. }
  117. efree(self);
  118. php_pdo_dbh_delref(dbh TSRMLS_CC);
  119. return 0;
  120. }
  121. static int pgsql_lob_flush(php_stream *stream TSRMLS_DC)
  122. {
  123. return 0;
  124. }
  125. static int pgsql_lob_seek(php_stream *stream, off_t offset, int whence,
  126. off_t *newoffset TSRMLS_DC)
  127. {
  128. struct pdo_pgsql_lob_self *self = (struct pdo_pgsql_lob_self*)stream->abstract;
  129. int pos = lo_lseek(self->conn, self->lfd, offset, whence);
  130. *newoffset = pos;
  131. return pos >= 0 ? 0 : -1;
  132. }
  133. php_stream_ops pdo_pgsql_lob_stream_ops = {
  134. pgsql_lob_write,
  135. pgsql_lob_read,
  136. pgsql_lob_close,
  137. pgsql_lob_flush,
  138. "pdo_pgsql lob stream",
  139. pgsql_lob_seek,
  140. NULL,
  141. NULL,
  142. NULL
  143. };
  144. php_stream *pdo_pgsql_create_lob_stream(pdo_dbh_t *dbh, int lfd, Oid oid TSRMLS_DC)
  145. {
  146. php_stream *stm;
  147. struct pdo_pgsql_lob_self *self = ecalloc(1, sizeof(*self));
  148. pdo_pgsql_db_handle *H = (pdo_pgsql_db_handle *)dbh->driver_data;
  149. self->dbh = dbh;
  150. self->lfd = lfd;
  151. self->oid = oid;
  152. self->conn = H->server;
  153. stm = php_stream_alloc(&pdo_pgsql_lob_stream_ops, self, 0, "r+b");
  154. if (stm) {
  155. php_pdo_dbh_addref(dbh TSRMLS_CC);
  156. return stm;
  157. }
  158. efree(self);
  159. return NULL;
  160. }
  161. /* }}} */
  162. static int pgsql_handle_closer(pdo_dbh_t *dbh TSRMLS_DC) /* {{{ */
  163. {
  164. pdo_pgsql_db_handle *H = (pdo_pgsql_db_handle *)dbh->driver_data;
  165. if (H) {
  166. if (H->server) {
  167. PQfinish(H->server);
  168. H->server = NULL;
  169. }
  170. if (H->einfo.errmsg) {
  171. pefree(H->einfo.errmsg, dbh->is_persistent);
  172. H->einfo.errmsg = NULL;
  173. }
  174. pefree(H, dbh->is_persistent);
  175. dbh->driver_data = NULL;
  176. }
  177. return 0;
  178. }
  179. /* }}} */
  180. static int pgsql_handle_preparer(pdo_dbh_t *dbh, const char *sql, long sql_len, pdo_stmt_t *stmt, zval *driver_options TSRMLS_DC)
  181. {
  182. pdo_pgsql_db_handle *H = (pdo_pgsql_db_handle *)dbh->driver_data;
  183. pdo_pgsql_stmt *S = ecalloc(1, sizeof(pdo_pgsql_stmt));
  184. int scrollable;
  185. #if HAVE_PQPREPARE
  186. int ret;
  187. char *nsql = NULL;
  188. int nsql_len = 0;
  189. int emulate = 0;
  190. #endif
  191. S->H = H;
  192. stmt->driver_data = S;
  193. stmt->methods = &pgsql_stmt_methods;
  194. scrollable = pdo_attr_lval(driver_options, PDO_ATTR_CURSOR,
  195. PDO_CURSOR_FWDONLY TSRMLS_CC) == PDO_CURSOR_SCROLL;
  196. if (scrollable) {
  197. if (S->cursor_name) {
  198. efree(S->cursor_name);
  199. }
  200. spprintf(&S->cursor_name, 0, "pdo_crsr_%016lx", (unsigned long) stmt);
  201. #if HAVE_PQPREPARE
  202. emulate = 1;
  203. #endif
  204. }
  205. #if HAVE_PQPREPARE
  206. else if (driver_options) {
  207. if (pdo_attr_lval(driver_options,
  208. PDO_PGSQL_ATTR_DISABLE_NATIVE_PREPARED_STATEMENT, 0 TSRMLS_CC) == 1) {
  209. emulate = 1;
  210. } else if (pdo_attr_lval(driver_options, PDO_ATTR_EMULATE_PREPARES,
  211. 0 TSRMLS_CC) == 1) {
  212. emulate = 1;
  213. }
  214. }
  215. if (!emulate && PQprotocolVersion(H->server) > 2) {
  216. stmt->supports_placeholders = PDO_PLACEHOLDER_NAMED;
  217. stmt->named_rewrite_template = "$%d";
  218. ret = pdo_parse_params(stmt, (char*)sql, sql_len, &nsql, &nsql_len TSRMLS_CC);
  219. if (ret == 1) {
  220. /* query was re-written */
  221. sql = nsql;
  222. } else if (ret == -1) {
  223. /* couldn't grok it */
  224. strcpy(dbh->error_code, stmt->error_code);
  225. return 0;
  226. }
  227. spprintf(&S->stmt_name, 0, "pdo_stmt_%016lx", (unsigned long)stmt);
  228. /* that's all for now; we'll defer the actual prepare until the first execute call */
  229. if (nsql) {
  230. S->query = nsql;
  231. } else {
  232. S->query = estrdup(sql);
  233. }
  234. return 1;
  235. }
  236. #endif
  237. stmt->supports_placeholders = PDO_PLACEHOLDER_NONE;
  238. return 1;
  239. }
  240. static long pgsql_handle_doer(pdo_dbh_t *dbh, const char *sql, long sql_len TSRMLS_DC)
  241. {
  242. pdo_pgsql_db_handle *H = (pdo_pgsql_db_handle *)dbh->driver_data;
  243. PGresult *res;
  244. long ret = 1;
  245. ExecStatusType qs;
  246. if (!(res = PQexec(H->server, sql))) {
  247. /* fatal error */
  248. pdo_pgsql_error(dbh, PGRES_FATAL_ERROR, NULL);
  249. return -1;
  250. }
  251. qs = PQresultStatus(res);
  252. if (qs != PGRES_COMMAND_OK && qs != PGRES_TUPLES_OK) {
  253. pdo_pgsql_error(dbh, qs, pdo_pgsql_sqlstate(res));
  254. PQclear(res);
  255. return -1;
  256. }
  257. H->pgoid = PQoidValue(res);
  258. ret = atol(PQcmdTuples(res));
  259. PQclear(res);
  260. return ret;
  261. }
  262. static int pgsql_handle_quoter(pdo_dbh_t *dbh, const char *unquoted, int unquotedlen, char **quoted, int *quotedlen, enum pdo_param_type paramtype TSRMLS_DC)
  263. {
  264. unsigned char *escaped;
  265. pdo_pgsql_db_handle *H = (pdo_pgsql_db_handle *)dbh->driver_data;
  266. size_t tmp_len;
  267. switch (paramtype) {
  268. case PDO_PARAM_LOB:
  269. /* escapedlen returned by PQescapeBytea() accounts for trailing 0 */
  270. #ifdef HAVE_PQESCAPE_BYTEA_CONN
  271. escaped = PQescapeByteaConn(H->server, unquoted, unquotedlen, &tmp_len);
  272. #else
  273. escaped = PQescapeBytea(unquoted, unquotedlen, &tmp_len);
  274. #endif
  275. *quotedlen = (int)tmp_len + 1;
  276. *quoted = emalloc(*quotedlen + 1);
  277. memcpy((*quoted)+1, escaped, *quotedlen-2);
  278. (*quoted)[0] = '\'';
  279. (*quoted)[*quotedlen-1] = '\'';
  280. (*quoted)[*quotedlen] = '\0';
  281. PQfreemem(escaped);
  282. break;
  283. default:
  284. *quoted = safe_emalloc(2, unquotedlen, 3);
  285. (*quoted)[0] = '\'';
  286. #ifndef HAVE_PQESCAPE_CONN
  287. *quotedlen = PQescapeString(*quoted + 1, unquoted, unquotedlen);
  288. #else
  289. *quotedlen = PQescapeStringConn(H->server, *quoted + 1, unquoted, unquotedlen, NULL);
  290. #endif
  291. (*quoted)[*quotedlen + 1] = '\'';
  292. (*quoted)[*quotedlen + 2] = '\0';
  293. *quotedlen += 2;
  294. }
  295. return 1;
  296. }
  297. static char *pdo_pgsql_last_insert_id(pdo_dbh_t *dbh, const char *name, unsigned int *len TSRMLS_DC)
  298. {
  299. pdo_pgsql_db_handle *H = (pdo_pgsql_db_handle *)dbh->driver_data;
  300. char *id = NULL;
  301. if (name == NULL) {
  302. if (H->pgoid == InvalidOid) {
  303. return NULL;
  304. }
  305. *len = spprintf(&id, 0, "%ld", (long) H->pgoid);
  306. } else {
  307. PGresult *res;
  308. ExecStatusType status;
  309. const char *q[1];
  310. q[0] = name;
  311. res = PQexecParams(H->server, "SELECT CURRVAL($1)", 1, NULL, q, NULL, NULL, 0);
  312. status = PQresultStatus(res);
  313. if (res && (status == PGRES_TUPLES_OK)) {
  314. id = estrdup((char *)PQgetvalue(res, 0, 0));
  315. *len = PQgetlength(res, 0, 0);
  316. } else {
  317. pdo_pgsql_error(dbh, status, pdo_pgsql_sqlstate(res));
  318. }
  319. if (res) {
  320. PQclear(res);
  321. }
  322. }
  323. return id;
  324. }
  325. static int pdo_pgsql_get_attribute(pdo_dbh_t *dbh, long attr, zval *return_value TSRMLS_DC)
  326. {
  327. pdo_pgsql_db_handle *H = (pdo_pgsql_db_handle *)dbh->driver_data;
  328. switch (attr) {
  329. case PDO_ATTR_CLIENT_VERSION:
  330. ZVAL_STRING(return_value, PG_VERSION, 1);
  331. break;
  332. case PDO_ATTR_SERVER_VERSION:
  333. if (PQprotocolVersion(H->server) >= 3) { /* PostgreSQL 7.4 or later */
  334. ZVAL_STRING(return_value, (char*)PQparameterStatus(H->server, "server_version"), 1);
  335. } else /* emulate above via a query */
  336. {
  337. PGresult *res = PQexec(H->server, "SELECT VERSION()");
  338. if (res && PQresultStatus(res) == PGRES_TUPLES_OK) {
  339. ZVAL_STRING(return_value, (char *)PQgetvalue(res, 0, 0), 1);
  340. }
  341. if (res) {
  342. PQclear(res);
  343. }
  344. }
  345. break;
  346. case PDO_ATTR_CONNECTION_STATUS:
  347. switch (PQstatus(H->server)) {
  348. case CONNECTION_STARTED:
  349. ZVAL_STRINGL(return_value, "Waiting for connection to be made.", sizeof("Waiting for connection to be made.")-1, 1);
  350. break;
  351. case CONNECTION_MADE:
  352. case CONNECTION_OK:
  353. ZVAL_STRINGL(return_value, "Connection OK; waiting to send.", sizeof("Connection OK; waiting to send.")-1, 1);
  354. break;
  355. case CONNECTION_AWAITING_RESPONSE:
  356. ZVAL_STRINGL(return_value, "Waiting for a response from the server.", sizeof("Waiting for a response from the server.")-1, 1);
  357. break;
  358. case CONNECTION_AUTH_OK:
  359. ZVAL_STRINGL(return_value, "Received authentication; waiting for backend start-up to finish.", sizeof("Received authentication; waiting for backend start-up to finish.")-1, 1);
  360. break;
  361. #ifdef CONNECTION_SSL_STARTUP
  362. case CONNECTION_SSL_STARTUP:
  363. ZVAL_STRINGL(return_value, "Negotiating SSL encryption.", sizeof("Negotiating SSL encryption.")-1, 1);
  364. break;
  365. #endif
  366. case CONNECTION_SETENV:
  367. ZVAL_STRINGL(return_value, "Negotiating environment-driven parameter settings.", sizeof("Negotiating environment-driven parameter settings.")-1, 1);
  368. break;
  369. case CONNECTION_BAD:
  370. default:
  371. ZVAL_STRINGL(return_value, "Bad connection.", sizeof("Bad connection.")-1, 1);
  372. break;
  373. }
  374. break;
  375. case PDO_ATTR_SERVER_INFO: {
  376. int spid = PQbackendPID(H->server);
  377. char *tmp;
  378. spprintf(&tmp, 0,
  379. "PID: %d; Client Encoding: %s; Is Superuser: %s; Session Authorization: %s; Date Style: %s",
  380. spid,
  381. (char*)PQparameterStatus(H->server, "client_encoding"),
  382. (char*)PQparameterStatus(H->server, "is_superuser"),
  383. (char*)PQparameterStatus(H->server, "session_authorization"),
  384. (char*)PQparameterStatus(H->server, "DateStyle"));
  385. ZVAL_STRING(return_value, tmp, 0);
  386. }
  387. break;
  388. default:
  389. return 0;
  390. }
  391. return 1;
  392. }
  393. /* {{{ */
  394. static int pdo_pgsql_check_liveness(pdo_dbh_t *dbh TSRMLS_DC)
  395. {
  396. pdo_pgsql_db_handle *H = (pdo_pgsql_db_handle *)dbh->driver_data;
  397. if (PQstatus(H->server) == CONNECTION_BAD) {
  398. PQreset(H->server);
  399. }
  400. return (PQstatus(H->server) == CONNECTION_OK) ? SUCCESS : FAILURE;
  401. }
  402. /* }}} */
  403. static int pdo_pgsql_transaction_cmd(const char *cmd, pdo_dbh_t *dbh TSRMLS_DC)
  404. {
  405. pdo_pgsql_db_handle *H = (pdo_pgsql_db_handle *)dbh->driver_data;
  406. PGresult *res;
  407. int ret = 1;
  408. res = PQexec(H->server, cmd);
  409. if (PQresultStatus(res) != PGRES_COMMAND_OK) {
  410. pdo_pgsql_error(dbh, PQresultStatus(res), pdo_pgsql_sqlstate(res));
  411. ret = 0;
  412. }
  413. PQclear(res);
  414. return ret;
  415. }
  416. static int pgsql_handle_begin(pdo_dbh_t *dbh TSRMLS_DC)
  417. {
  418. return pdo_pgsql_transaction_cmd("BEGIN", dbh TSRMLS_CC);
  419. }
  420. static int pgsql_handle_commit(pdo_dbh_t *dbh TSRMLS_DC)
  421. {
  422. return pdo_pgsql_transaction_cmd("COMMIT", dbh TSRMLS_CC);
  423. }
  424. static int pgsql_handle_rollback(pdo_dbh_t *dbh TSRMLS_DC)
  425. {
  426. return pdo_pgsql_transaction_cmd("ROLLBACK", dbh TSRMLS_CC);
  427. }
  428. /* {{{ proto string PDO::pgsqlLOBCreate()
  429. Creates a new large object, returning its identifier. Must be called inside a transaction. */
  430. static PHP_METHOD(PDO, pgsqlLOBCreate)
  431. {
  432. pdo_dbh_t *dbh;
  433. pdo_pgsql_db_handle *H;
  434. Oid lfd;
  435. dbh = zend_object_store_get_object(getThis() TSRMLS_CC);
  436. PDO_CONSTRUCT_CHECK;
  437. H = (pdo_pgsql_db_handle *)dbh->driver_data;
  438. lfd = lo_creat(H->server, INV_READ|INV_WRITE);
  439. if (lfd != InvalidOid) {
  440. char *buf;
  441. spprintf(&buf, 0, "%lu", (long) lfd);
  442. RETURN_STRING(buf, 0);
  443. }
  444. pdo_pgsql_error(dbh, PGRES_FATAL_ERROR, "HY000");
  445. RETURN_FALSE;
  446. }
  447. /* }}} */
  448. /* {{{ proto resource PDO::pgsqlLOBOpen(string oid [, string mode = 'rb'])
  449. Opens an existing large object stream. Must be called inside a transaction. */
  450. static PHP_METHOD(PDO, pgsqlLOBOpen)
  451. {
  452. pdo_dbh_t *dbh;
  453. pdo_pgsql_db_handle *H;
  454. Oid oid;
  455. int lfd;
  456. char *oidstr;
  457. int oidstrlen;
  458. char *modestr = "rb";
  459. int modestrlen;
  460. int mode = INV_READ;
  461. char *end_ptr;
  462. if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|s",
  463. &oidstr, &oidstrlen, &modestr, &modestrlen)) {
  464. RETURN_FALSE;
  465. }
  466. oid = (Oid)strtoul(oidstr, &end_ptr, 10);
  467. if (oid == 0 && (errno == ERANGE || errno == EINVAL)) {
  468. RETURN_FALSE;
  469. }
  470. if (strpbrk(modestr, "+w")) {
  471. mode = INV_READ|INV_WRITE;
  472. }
  473. dbh = zend_object_store_get_object(getThis() TSRMLS_CC);
  474. PDO_CONSTRUCT_CHECK;
  475. H = (pdo_pgsql_db_handle *)dbh->driver_data;
  476. lfd = lo_open(H->server, oid, mode);
  477. if (lfd >= 0) {
  478. php_stream *stream = pdo_pgsql_create_lob_stream(dbh, lfd, oid TSRMLS_CC);
  479. if (stream) {
  480. php_stream_to_zval(stream, return_value);
  481. return;
  482. }
  483. } else {
  484. pdo_pgsql_error(dbh, PGRES_FATAL_ERROR, "HY000");
  485. }
  486. RETURN_FALSE;
  487. }
  488. /* }}} */
  489. /* {{{ proto bool PDO::pgsqlLOBUnlink(string oid)
  490. Deletes the large object identified by oid. Must be called inside a transaction. */
  491. static PHP_METHOD(PDO, pgsqlLOBUnlink)
  492. {
  493. pdo_dbh_t *dbh;
  494. pdo_pgsql_db_handle *H;
  495. Oid oid;
  496. char *oidstr, *end_ptr;
  497. int oidlen;
  498. if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s",
  499. &oidstr, &oidlen)) {
  500. RETURN_FALSE;
  501. }
  502. oid = (Oid)strtoul(oidstr, &end_ptr, 10);
  503. if (oid == 0 && (errno == ERANGE || errno == EINVAL)) {
  504. RETURN_FALSE;
  505. }
  506. dbh = zend_object_store_get_object(getThis() TSRMLS_CC);
  507. PDO_CONSTRUCT_CHECK;
  508. H = (pdo_pgsql_db_handle *)dbh->driver_data;
  509. if (1 == lo_unlink(H->server, oid)) {
  510. RETURN_TRUE;
  511. }
  512. pdo_pgsql_error(dbh, PGRES_FATAL_ERROR, "HY000");
  513. RETURN_FALSE;
  514. }
  515. /* }}} */
  516. static const zend_function_entry dbh_methods[] = {
  517. PHP_ME(PDO, pgsqlLOBCreate, NULL, ZEND_ACC_PUBLIC)
  518. PHP_ME(PDO, pgsqlLOBOpen, NULL, ZEND_ACC_PUBLIC)
  519. PHP_ME(PDO, pgsqlLOBUnlink, NULL, ZEND_ACC_PUBLIC)
  520. {NULL, NULL, NULL}
  521. };
  522. static const zend_function_entry *pdo_pgsql_get_driver_methods(pdo_dbh_t *dbh, int kind TSRMLS_DC)
  523. {
  524. switch (kind) {
  525. case PDO_DBH_DRIVER_METHOD_KIND_DBH:
  526. return dbh_methods;
  527. default:
  528. return NULL;
  529. }
  530. }
  531. static int pdo_pgsql_set_attr(pdo_dbh_t *dbh, long attr, zval *val TSRMLS_DC)
  532. {
  533. return 0;
  534. }
  535. static struct pdo_dbh_methods pgsql_methods = {
  536. pgsql_handle_closer,
  537. pgsql_handle_preparer,
  538. pgsql_handle_doer,
  539. pgsql_handle_quoter,
  540. pgsql_handle_begin,
  541. pgsql_handle_commit,
  542. pgsql_handle_rollback,
  543. pdo_pgsql_set_attr,
  544. pdo_pgsql_last_insert_id,
  545. pdo_pgsql_fetch_error_func,
  546. pdo_pgsql_get_attribute,
  547. pdo_pgsql_check_liveness, /* check_liveness */
  548. pdo_pgsql_get_driver_methods /* get_driver_methods */
  549. };
  550. static int pdo_pgsql_handle_factory(pdo_dbh_t *dbh, zval *driver_options TSRMLS_DC) /* {{{ */
  551. {
  552. pdo_pgsql_db_handle *H;
  553. int ret = 0;
  554. char *conn_str, *p, *e;
  555. long connect_timeout = 30;
  556. H = pecalloc(1, sizeof(pdo_pgsql_db_handle), dbh->is_persistent);
  557. dbh->driver_data = H;
  558. H->einfo.errcode = 0;
  559. H->einfo.errmsg = NULL;
  560. /* PostgreSQL wants params in the connect string to be separated by spaces,
  561. * if the PDO standard semicolons are used, we convert them to spaces
  562. */
  563. e = (char *) dbh->data_source + strlen(dbh->data_source);
  564. p = (char *) dbh->data_source;
  565. while ((p = memchr(p, ';', (e - p)))) {
  566. *p = ' ';
  567. }
  568. if (driver_options) {
  569. connect_timeout = pdo_attr_lval(driver_options, PDO_ATTR_TIMEOUT, 30 TSRMLS_CC);
  570. }
  571. /* support both full connection string & connection string + login and/or password */
  572. if (dbh->username && dbh->password) {
  573. spprintf(&conn_str, 0, "%s user=%s password=%s connect_timeout=%ld", dbh->data_source, dbh->username, dbh->password, connect_timeout);
  574. } else if (dbh->username) {
  575. spprintf(&conn_str, 0, "%s user=%s connect_timeout=%ld", dbh->data_source, dbh->username, connect_timeout);
  576. } else if (dbh->password) {
  577. spprintf(&conn_str, 0, "%s password=%s connect_timeout=%ld", dbh->data_source, dbh->password, connect_timeout);
  578. } else {
  579. spprintf(&conn_str, 0, "%s connect_timeout=%ld", (char *) dbh->data_source, connect_timeout);
  580. }
  581. H->server = PQconnectdb(conn_str);
  582. efree(conn_str);
  583. if (PQstatus(H->server) != CONNECTION_OK) {
  584. pdo_pgsql_error(dbh, PGRES_FATAL_ERROR, PHP_PDO_PGSQL_CONNECTION_FAILURE_SQLSTATE);
  585. goto cleanup;
  586. }
  587. PQsetNoticeProcessor(H->server, (void(*)(void*,const char*))_pdo_pgsql_notice, (void *)&dbh);
  588. H->attached = 1;
  589. H->pgoid = -1;
  590. dbh->methods = &pgsql_methods;
  591. dbh->alloc_own_columns = 1;
  592. dbh->max_escaped_char_length = 2;
  593. ret = 1;
  594. cleanup:
  595. dbh->methods = &pgsql_methods;
  596. if (!ret) {
  597. pgsql_handle_closer(dbh TSRMLS_CC);
  598. }
  599. return ret;
  600. }
  601. /* }}} */
  602. pdo_driver_t pdo_pgsql_driver = {
  603. PDO_DRIVER_HEADER(pgsql),
  604. pdo_pgsql_handle_factory
  605. };
  606. /*
  607. * Local variables:
  608. * tab-width: 4
  609. * c-basic-offset: 4
  610. * End:
  611. * vim600: noet sw=4 ts=4 fdm=marker
  612. * vim<600: noet sw=4 ts=4
  613. */