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.

747 lines
20 KiB

22 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
21 years ago
21 years ago
22 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. /* TODO: check how scrollable cursors related to prepared statements */
  201. spprintf(&S->cursor_name, 0, "pdo_pgsql_cursor_%08x", (unsigned int) stmt);
  202. }
  203. #if HAVE_PQPREPARE
  204. if (driver_options) {
  205. if (pdo_attr_lval(driver_options,
  206. PDO_PGSQL_ATTR_DISABLE_NATIVE_PREPARED_STATEMENT, 0 TSRMLS_CC) == 1) {
  207. emulate = 1;
  208. } else if (pdo_attr_lval(driver_options, PDO_ATTR_EMULATE_PREPARES,
  209. 0 TSRMLS_CC) == 1) {
  210. emulate = 1;
  211. }
  212. }
  213. if (!emulate && PQprotocolVersion(H->server) > 2) {
  214. stmt->supports_placeholders = PDO_PLACEHOLDER_NAMED;
  215. stmt->named_rewrite_template = "$%d";
  216. ret = pdo_parse_params(stmt, (char*)sql, sql_len, &nsql, &nsql_len TSRMLS_CC);
  217. if (ret == 1) {
  218. /* query was re-written */
  219. sql = nsql;
  220. } else if (ret == -1) {
  221. /* couldn't grok it */
  222. strcpy(dbh->error_code, stmt->error_code);
  223. return 0;
  224. }
  225. spprintf(&S->stmt_name, 0, "pdo_pgsql_stmt_%08x", (unsigned int)stmt);
  226. /* that's all for now; we'll defer the actual prepare until the first execute call */
  227. if (nsql) {
  228. S->query = nsql;
  229. } else {
  230. S->query = estrdup(sql);
  231. }
  232. return 1;
  233. }
  234. #endif
  235. stmt->supports_placeholders = PDO_PLACEHOLDER_NONE;
  236. return 1;
  237. }
  238. static long pgsql_handle_doer(pdo_dbh_t *dbh, const char *sql, long sql_len TSRMLS_DC)
  239. {
  240. pdo_pgsql_db_handle *H = (pdo_pgsql_db_handle *)dbh->driver_data;
  241. PGresult *res;
  242. long ret = 1;
  243. ExecStatusType qs;
  244. if (!(res = PQexec(H->server, sql))) {
  245. /* fatal error */
  246. pdo_pgsql_error(dbh, PGRES_FATAL_ERROR, NULL);
  247. return -1;
  248. }
  249. qs = PQresultStatus(res);
  250. if (qs != PGRES_COMMAND_OK && qs != PGRES_TUPLES_OK) {
  251. pdo_pgsql_error(dbh, qs, pdo_pgsql_sqlstate(res));
  252. PQclear(res);
  253. return -1;
  254. }
  255. H->pgoid = PQoidValue(res);
  256. #if HAVE_PQCMDTUPLES
  257. ret = atol(PQcmdTuples(res));
  258. #endif
  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. switch (paramtype) {
  267. case PDO_PARAM_LOB:
  268. /* escapedlen returned by PQescapeBytea() accounts for trailing 0 */
  269. #ifdef HAVE_PQESCAPE_BYTEA_CONN
  270. escaped = PQescapeByteaConn(H->server, unquoted, unquotedlen, quotedlen);
  271. #else
  272. escaped = PQescapeBytea(unquoted, unquotedlen, quotedlen);
  273. #endif
  274. *quotedlen += 1;
  275. *quoted = emalloc(*quotedlen + 1);
  276. memcpy((*quoted)+1, escaped, *quotedlen-2);
  277. (*quoted)[0] = '\'';
  278. (*quoted)[*quotedlen-1] = '\'';
  279. (*quoted)[*quotedlen] = '\0';
  280. PQfreemem(escaped);
  281. break;
  282. default:
  283. *quoted = safe_emalloc(2, unquotedlen, 3);
  284. (*quoted)[0] = '\'';
  285. #ifndef HAVE_PQESCAPE_CONN
  286. *quotedlen = PQescapeString(*quoted + 1, unquoted, unquotedlen);
  287. #else
  288. *quotedlen = PQescapeStringConn(H->server, *quoted + 1, unquoted, unquotedlen, NULL);
  289. #endif
  290. (*quoted)[*quotedlen + 1] = '\'';
  291. (*quoted)[*quotedlen + 2] = '\0';
  292. *quotedlen += 2;
  293. }
  294. return 1;
  295. }
  296. static char *pdo_pgsql_last_insert_id(pdo_dbh_t *dbh, const char *name, unsigned int *len TSRMLS_DC)
  297. {
  298. pdo_pgsql_db_handle *H = (pdo_pgsql_db_handle *)dbh->driver_data;
  299. char *id = NULL;
  300. if (name == NULL) {
  301. if (H->pgoid == InvalidOid) {
  302. return NULL;
  303. }
  304. *len = spprintf(&id, 0, "%ld", (long) H->pgoid);
  305. } else {
  306. PGresult *res;
  307. ExecStatusType status;
  308. #ifdef HAVE_PQEXECPARAMS
  309. const char *q[1];
  310. q[0] = name;
  311. res = PQexecParams(H->server, "SELECT CURRVAL($1)", 1, NULL, q, NULL, NULL, 0);
  312. #else
  313. char *name_escaped, *q;
  314. size_t l = strlen(name);
  315. name_escaped = safe_emalloc(l, 2, 1);
  316. #ifndef HAVE_PQESCAPE_CONN
  317. PQescapeString(name_escaped, name, l);
  318. #else
  319. PQescapeStringConn(H->server, name_escaped, name, l, NULL);
  320. #endif
  321. spprintf(&q, 0, "SELECT CURRVAL('%s')", name_escaped);
  322. res = PQexec(H->server, q);
  323. efree(name_escaped);
  324. efree(q);
  325. #endif
  326. status = PQresultStatus(res);
  327. if (res && (status == PGRES_TUPLES_OK)) {
  328. id = estrdup((char *)PQgetvalue(res, 0, 0));
  329. *len = PQgetlength(res, 0, 0);
  330. } else {
  331. pdo_pgsql_error(dbh, status, pdo_pgsql_sqlstate(res));
  332. }
  333. if (res) {
  334. PQclear(res);
  335. }
  336. }
  337. return id;
  338. }
  339. static int pdo_pgsql_get_attribute(pdo_dbh_t *dbh, long attr, zval *return_value TSRMLS_DC)
  340. {
  341. pdo_pgsql_db_handle *H = (pdo_pgsql_db_handle *)dbh->driver_data;
  342. switch (attr) {
  343. case PDO_ATTR_CLIENT_VERSION:
  344. ZVAL_STRING(return_value, PG_VERSION, 1);
  345. break;
  346. case PDO_ATTR_SERVER_VERSION:
  347. #ifdef HAVE_PQPROTOCOLVERSION
  348. if (PQprotocolVersion(H->server) >= 3) { /* PostgreSQL 7.4 or later */
  349. ZVAL_STRING(return_value, (char*)PQparameterStatus(H->server, "server_version"), 1);
  350. } else /* emulate above via a query */
  351. #endif
  352. {
  353. PGresult *res = PQexec(H->server, "SELECT VERSION()");
  354. if (res && PQresultStatus(res) == PGRES_TUPLES_OK) {
  355. ZVAL_STRING(return_value, (char *)PQgetvalue(res, 0, 0), 1);
  356. }
  357. if (res) {
  358. PQclear(res);
  359. }
  360. }
  361. break;
  362. case PDO_ATTR_CONNECTION_STATUS:
  363. switch (PQstatus(H->server)) {
  364. case CONNECTION_STARTED:
  365. ZVAL_STRINGL(return_value, "Waiting for connection to be made.", sizeof("Waiting for connection to be made.")-1, 1);
  366. break;
  367. case CONNECTION_MADE:
  368. case CONNECTION_OK:
  369. ZVAL_STRINGL(return_value, "Connection OK; waiting to send.", sizeof("Connection OK; waiting to send.")-1, 1);
  370. break;
  371. case CONNECTION_AWAITING_RESPONSE:
  372. ZVAL_STRINGL(return_value, "Waiting for a response from the server.", sizeof("Waiting for a response from the server.")-1, 1);
  373. break;
  374. case CONNECTION_AUTH_OK:
  375. 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);
  376. break;
  377. #ifdef CONNECTION_SSL_STARTUP
  378. case CONNECTION_SSL_STARTUP:
  379. ZVAL_STRINGL(return_value, "Negotiating SSL encryption.", sizeof("Negotiating SSL encryption.")-1, 1);
  380. break;
  381. #endif
  382. case CONNECTION_SETENV:
  383. ZVAL_STRINGL(return_value, "Negotiating environment-driven parameter settings.", sizeof("Negotiating environment-driven parameter settings.")-1, 1);
  384. break;
  385. case CONNECTION_BAD:
  386. default:
  387. ZVAL_STRINGL(return_value, "Bad connection.", sizeof("Bad connection.")-1, 1);
  388. break;
  389. }
  390. break;
  391. case PDO_ATTR_SERVER_INFO: {
  392. int spid = PQbackendPID(H->server);
  393. char *tmp;
  394. #ifdef HAVE_PQPROTOCOLVERSION
  395. spprintf(&tmp, 0,
  396. "PID: %d; Client Encoding: %s; Is Superuser: %s; Session Authorization: %s; Date Style: %s",
  397. spid,
  398. (char*)PQparameterStatus(H->server, "client_encoding"),
  399. (char*)PQparameterStatus(H->server, "is_superuser"),
  400. (char*)PQparameterStatus(H->server, "session_authorization"),
  401. (char*)PQparameterStatus(H->server, "DateStyle"));
  402. #else
  403. spprintf(&tmp, 0, "PID: %d", spid);
  404. #endif
  405. ZVAL_STRING(return_value, tmp, 0);
  406. }
  407. break;
  408. default:
  409. return 0;
  410. }
  411. return 1;
  412. }
  413. /* {{{ */
  414. static int pdo_pgsql_check_liveness(pdo_dbh_t *dbh TSRMLS_DC)
  415. {
  416. pdo_pgsql_db_handle *H = (pdo_pgsql_db_handle *)dbh->driver_data;
  417. if (PQstatus(H->server) == CONNECTION_BAD) {
  418. PQreset(H->server);
  419. }
  420. return (PQstatus(H->server) == CONNECTION_OK) ? SUCCESS : FAILURE;
  421. }
  422. /* }}} */
  423. static int pdo_pgsql_transaction_cmd(const char *cmd, pdo_dbh_t *dbh TSRMLS_DC)
  424. {
  425. pdo_pgsql_db_handle *H = (pdo_pgsql_db_handle *)dbh->driver_data;
  426. PGresult *res;
  427. int ret = 1;
  428. res = PQexec(H->server, cmd);
  429. if (PQresultStatus(res) != PGRES_COMMAND_OK) {
  430. pdo_pgsql_error(dbh, PQresultStatus(res), pdo_pgsql_sqlstate(res));
  431. ret = 0;
  432. }
  433. PQclear(res);
  434. return ret;
  435. }
  436. static int pgsql_handle_begin(pdo_dbh_t *dbh TSRMLS_DC)
  437. {
  438. return pdo_pgsql_transaction_cmd("BEGIN", dbh TSRMLS_CC);
  439. }
  440. static int pgsql_handle_commit(pdo_dbh_t *dbh TSRMLS_DC)
  441. {
  442. return pdo_pgsql_transaction_cmd("COMMIT", dbh TSRMLS_CC);
  443. }
  444. static int pgsql_handle_rollback(pdo_dbh_t *dbh TSRMLS_DC)
  445. {
  446. return pdo_pgsql_transaction_cmd("ROLLBACK", dbh TSRMLS_CC);
  447. }
  448. /* {{{ proto string PDO::pgsqlLOBCreate()
  449. Creates a new large object, returning its identifier. Must be called inside a transaction. */
  450. static PHP_METHOD(PDO, pgsqlLOBCreate)
  451. {
  452. pdo_dbh_t *dbh;
  453. pdo_pgsql_db_handle *H;
  454. Oid lfd;
  455. dbh = zend_object_store_get_object(getThis() TSRMLS_CC);
  456. PDO_CONSTRUCT_CHECK;
  457. H = (pdo_pgsql_db_handle *)dbh->driver_data;
  458. lfd = lo_creat(H->server, INV_READ|INV_WRITE);
  459. if (lfd != InvalidOid) {
  460. char *buf;
  461. spprintf(&buf, 0, "%lu", (long) lfd);
  462. RETURN_STRING(buf, 0);
  463. }
  464. pdo_pgsql_error(dbh, PGRES_FATAL_ERROR, "HY000");
  465. RETURN_FALSE;
  466. }
  467. /* }}} */
  468. /* {{{ proto resource PDO::pgsqlLOBOpen(string oid [, string mode = 'rb'])
  469. Opens an existing large object stream. Must be called inside a transaction. */
  470. static PHP_METHOD(PDO, pgsqlLOBOpen)
  471. {
  472. pdo_dbh_t *dbh;
  473. pdo_pgsql_db_handle *H;
  474. Oid oid;
  475. int lfd;
  476. char *oidstr;
  477. int oidstrlen;
  478. char *modestr = "rb";
  479. int modestrlen;
  480. int mode = INV_READ;
  481. char *end_ptr;
  482. if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|s",
  483. &oidstr, &oidstrlen, &modestr, &modestrlen)) {
  484. RETURN_FALSE;
  485. }
  486. oid = (Oid)strtoul(oidstr, &end_ptr, 10);
  487. if (oid == 0 && (errno == ERANGE || errno == EINVAL)) {
  488. RETURN_FALSE;
  489. }
  490. if (strpbrk(modestr, "+w")) {
  491. mode = INV_READ|INV_WRITE;
  492. }
  493. dbh = zend_object_store_get_object(getThis() TSRMLS_CC);
  494. PDO_CONSTRUCT_CHECK;
  495. H = (pdo_pgsql_db_handle *)dbh->driver_data;
  496. lfd = lo_open(H->server, oid, mode);
  497. if (lfd >= 0) {
  498. php_stream *stream = pdo_pgsql_create_lob_stream(dbh, lfd, oid TSRMLS_CC);
  499. if (stream) {
  500. php_stream_to_zval(stream, return_value);
  501. return;
  502. }
  503. } else {
  504. pdo_pgsql_error(dbh, PGRES_FATAL_ERROR, "HY000");
  505. }
  506. RETURN_FALSE;
  507. }
  508. /* }}} */
  509. /* {{{ proto bool PDO::pgsqlLOBUnlink(string oid)
  510. Deletes the large object identified by oid. Must be called inside a transaction. */
  511. static PHP_METHOD(PDO, pgsqlLOBUnlink)
  512. {
  513. pdo_dbh_t *dbh;
  514. pdo_pgsql_db_handle *H;
  515. Oid oid;
  516. char *oidstr, *end_ptr;
  517. int oidlen;
  518. if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s",
  519. &oidstr, &oidlen)) {
  520. RETURN_FALSE;
  521. }
  522. oid = (Oid)strtoul(oidstr, &end_ptr, 10);
  523. if (oid == 0 && (errno == ERANGE || errno == EINVAL)) {
  524. RETURN_FALSE;
  525. }
  526. dbh = zend_object_store_get_object(getThis() TSRMLS_CC);
  527. PDO_CONSTRUCT_CHECK;
  528. H = (pdo_pgsql_db_handle *)dbh->driver_data;
  529. if (1 == lo_unlink(H->server, oid)) {
  530. RETURN_TRUE;
  531. }
  532. pdo_pgsql_error(dbh, PGRES_FATAL_ERROR, "HY000");
  533. RETURN_FALSE;
  534. }
  535. /* }}} */
  536. static zend_function_entry dbh_methods[] = {
  537. PHP_ME(PDO, pgsqlLOBCreate, NULL, ZEND_ACC_PUBLIC)
  538. PHP_ME(PDO, pgsqlLOBOpen, NULL, ZEND_ACC_PUBLIC)
  539. PHP_ME(PDO, pgsqlLOBUnlink, NULL, ZEND_ACC_PUBLIC)
  540. {NULL, NULL, NULL}
  541. };
  542. static zend_function_entry *pdo_pgsql_get_driver_methods(pdo_dbh_t *dbh, int kind TSRMLS_DC)
  543. {
  544. switch (kind) {
  545. case PDO_DBH_DRIVER_METHOD_KIND_DBH:
  546. return dbh_methods;
  547. default:
  548. return NULL;
  549. }
  550. }
  551. static int pdo_pgsql_set_attr(pdo_dbh_t *dbh, long attr, zval *val TSRMLS_DC)
  552. {
  553. return 0;
  554. }
  555. static struct pdo_dbh_methods pgsql_methods = {
  556. pgsql_handle_closer,
  557. pgsql_handle_preparer,
  558. pgsql_handle_doer,
  559. pgsql_handle_quoter,
  560. pgsql_handle_begin,
  561. pgsql_handle_commit,
  562. pgsql_handle_rollback,
  563. pdo_pgsql_set_attr,
  564. pdo_pgsql_last_insert_id,
  565. pdo_pgsql_fetch_error_func,
  566. pdo_pgsql_get_attribute,
  567. pdo_pgsql_check_liveness, /* check_liveness */
  568. pdo_pgsql_get_driver_methods /* get_driver_methods */
  569. };
  570. static int pdo_pgsql_handle_factory(pdo_dbh_t *dbh, zval *driver_options TSRMLS_DC) /* {{{ */
  571. {
  572. pdo_pgsql_db_handle *H;
  573. int ret = 0;
  574. char *conn_str, *p, *e;
  575. long connect_timeout = 30;
  576. H = pecalloc(1, sizeof(pdo_pgsql_db_handle), dbh->is_persistent);
  577. dbh->driver_data = H;
  578. H->einfo.errcode = 0;
  579. H->einfo.errmsg = NULL;
  580. /* PostgreSQL wants params in the connect string to be separated by spaces,
  581. * if the PDO standard semicolons are used, we convert them to spaces
  582. */
  583. e = (char *) dbh->data_source + strlen(dbh->data_source);
  584. p = (char *) dbh->data_source;
  585. while ((p = memchr(p, ';', (e - p)))) {
  586. *p = ' ';
  587. }
  588. if (driver_options) {
  589. connect_timeout = pdo_attr_lval(driver_options, PDO_ATTR_TIMEOUT, 30 TSRMLS_CC);
  590. }
  591. /* support both full connection string & connection string + login and/or password */
  592. if (dbh->username && dbh->password) {
  593. spprintf(&conn_str, 0, "%s user=%s password=%s connect_timeout=%ld", dbh->data_source, dbh->username, dbh->password, connect_timeout);
  594. } else if (dbh->username) {
  595. spprintf(&conn_str, 0, "%s user=%s connect_timeout=%ld", dbh->data_source, dbh->username, connect_timeout);
  596. } else if (dbh->password) {
  597. spprintf(&conn_str, 0, "%s password=%s connect_timeout=%ld", dbh->data_source, dbh->password, connect_timeout);
  598. } else {
  599. spprintf(&conn_str, 0, "%s connect_timeout=%ld", (char *) dbh->data_source, connect_timeout);
  600. }
  601. H->server = PQconnectdb(conn_str);
  602. efree(conn_str);
  603. if (PQstatus(H->server) != CONNECTION_OK) {
  604. pdo_pgsql_error(dbh, PGRES_FATAL_ERROR, PHP_PDO_PGSQL_CONNECTION_FAILURE_SQLSTATE);
  605. goto cleanup;
  606. }
  607. PQsetNoticeProcessor(H->server, (void(*)(void*,const char*))_pdo_pgsql_notice, (void *)&dbh);
  608. H->attached = 1;
  609. H->pgoid = -1;
  610. dbh->methods = &pgsql_methods;
  611. dbh->alloc_own_columns = 1;
  612. dbh->max_escaped_char_length = 2;
  613. ret = 1;
  614. cleanup:
  615. dbh->methods = &pgsql_methods;
  616. if (!ret) {
  617. pgsql_handle_closer(dbh TSRMLS_CC);
  618. }
  619. return ret;
  620. }
  621. /* }}} */
  622. pdo_driver_t pdo_pgsql_driver = {
  623. PDO_DRIVER_HEADER(pgsql),
  624. pdo_pgsql_handle_factory
  625. };
  626. /*
  627. * Local variables:
  628. * tab-width: 4
  629. * c-basic-offset: 4
  630. * End:
  631. * vim600: noet sw=4 ts=4 fdm=marker
  632. * vim<600: noet sw=4 ts=4
  633. */