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.

660 lines
17 KiB

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-2006 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. #include "php_pdo_pgsql.h"
  30. #include "php_pdo_pgsql_int.h"
  31. #if HAVE_NETINET_IN_H
  32. #include <netinet/in.h>
  33. #endif
  34. /* from postgresql/src/include/catalog/pg_type.h */
  35. #define BOOLOID 16
  36. #define BYTEAOID 17
  37. #define INT8OID 20
  38. #define INT2OID 21
  39. #define INT4OID 23
  40. #define TEXTOID 25
  41. #define OIDOID 26
  42. static int pgsql_stmt_dtor(pdo_stmt_t *stmt TSRMLS_DC)
  43. {
  44. pdo_pgsql_stmt *S = (pdo_pgsql_stmt*)stmt->driver_data;
  45. if (S->result) {
  46. /* free the resource */
  47. PQclear(S->result);
  48. S->result = NULL;
  49. }
  50. #if HAVE_PQPREPARE
  51. if (S->stmt_name) {
  52. pdo_pgsql_db_handle *H = S->H;
  53. char *q = NULL;
  54. PGresult *res;
  55. spprintf(&q, 0, "DEALLOCATE %s", S->stmt_name);
  56. res = PQexec(H->server, q);
  57. efree(q);
  58. if (res) PQclear(res);
  59. efree(S->stmt_name);
  60. S->stmt_name = NULL;
  61. }
  62. if (S->param_lengths) {
  63. efree(S->param_lengths);
  64. S->param_lengths = NULL;
  65. }
  66. if (S->param_values) {
  67. efree(S->param_values);
  68. S->param_values = NULL;
  69. }
  70. if (S->param_formats) {
  71. efree(S->param_formats);
  72. S->param_formats = NULL;
  73. }
  74. if (S->param_types) {
  75. efree(S->param_types);
  76. S->param_types = NULL;
  77. }
  78. if (S->query) {
  79. efree(S->query);
  80. S->query = NULL;
  81. }
  82. #endif
  83. if (S->cursor_name) {
  84. pdo_pgsql_db_handle *H = S->H;
  85. char *q = NULL;
  86. PGresult *res;
  87. spprintf(&q, 0, "CLOSE %s", S->cursor_name);
  88. res = PQexec(H->server, q);
  89. efree(q);
  90. if (res) PQclear(res);
  91. efree(S->cursor_name);
  92. S->cursor_name = NULL;
  93. }
  94. if(S->cols) {
  95. efree(S->cols);
  96. S->cols = NULL;
  97. }
  98. efree(S);
  99. stmt->driver_data = NULL;
  100. return 1;
  101. }
  102. static int pgsql_stmt_execute(pdo_stmt_t *stmt TSRMLS_DC)
  103. {
  104. pdo_pgsql_stmt *S = (pdo_pgsql_stmt*)stmt->driver_data;
  105. pdo_pgsql_db_handle *H = S->H;
  106. ExecStatusType status;
  107. /* ensure that we free any previous unfetched results */
  108. if(S->result) {
  109. PQclear(S->result);
  110. S->result = NULL;
  111. }
  112. S->current_row = 0;
  113. #if HAVE_PQPREPARE
  114. if (S->stmt_name) {
  115. /* using a prepared statement */
  116. if (!S->is_prepared) {
  117. stmt_retry:
  118. /* we deferred the prepare until now, because we didn't
  119. * know anything about the parameter types; now we do */
  120. S->result = PQprepare(H->server, S->stmt_name, S->query,
  121. stmt->bound_params ? zend_hash_num_elements(stmt->bound_params) : 0,
  122. S->param_types);
  123. status = PQresultStatus(S->result);
  124. switch (status) {
  125. case PGRES_COMMAND_OK:
  126. case PGRES_TUPLES_OK:
  127. /* it worked */
  128. S->is_prepared = 1;
  129. PQclear(S->result);
  130. break;
  131. default: {
  132. char *sqlstate = pdo_pgsql_sqlstate(S->result);
  133. /* 42P05 means that the prepared statement already existed. this can happen if you use
  134. * a connection pooling software line pgpool which doesn't close the db-connection once
  135. * php disconnects. if php dies (no chanche to run RSHUTDOWN) during execution it has no
  136. * chance to DEALLOCATE the prepared statements it has created. so, if we hit a 42P05 we
  137. * deallocate it and retry ONCE (thies 2005.12.15)
  138. */
  139. if (!strcmp(sqlstate, "42P05")) {
  140. char buf[100]; /* stmt_name == "pdo_pgsql_cursor_%08x" */
  141. PGresult *res;
  142. snprintf(buf, sizeof(buf), "DEALLOCATE %s", S->stmt_name);
  143. res = PQexec(H->server, buf);
  144. if (res) {
  145. PQclear(res);
  146. }
  147. goto stmt_retry;
  148. } else {
  149. pdo_pgsql_error_stmt(stmt, status, sqlstate);
  150. return 0;
  151. }
  152. }
  153. }
  154. }
  155. S->result = PQexecPrepared(H->server, S->stmt_name,
  156. stmt->bound_params ?
  157. zend_hash_num_elements(stmt->bound_params) :
  158. 0,
  159. (const char**)S->param_values,
  160. S->param_lengths,
  161. S->param_formats,
  162. 0);
  163. } else
  164. #endif
  165. if (S->cursor_name) {
  166. char *q = NULL;
  167. spprintf(&q, 0, "DECLARE %s CURSOR FOR %s", S->cursor_name, stmt->active_query_string);
  168. S->result = PQexec(H->server, q);
  169. efree(q);
  170. } else {
  171. S->result = PQexec(H->server, stmt->active_query_string);
  172. }
  173. status = PQresultStatus(S->result);
  174. if (status != PGRES_COMMAND_OK && status != PGRES_TUPLES_OK) {
  175. pdo_pgsql_error_stmt(stmt, status, pdo_pgsql_sqlstate(S->result));
  176. return 0;
  177. }
  178. if(!stmt->executed) {
  179. stmt->column_count = (int) PQnfields(S->result);
  180. S->cols = ecalloc(stmt->column_count, sizeof(pdo_pgsql_column));
  181. }
  182. if (status == PGRES_COMMAND_OK) {
  183. stmt->row_count = (long)atoi(PQcmdTuples(S->result));
  184. H->pgoid = PQoidValue(S->result);
  185. } else {
  186. stmt->row_count = (long)PQntuples(S->result);
  187. }
  188. return 1;
  189. }
  190. static int pgsql_stmt_param_hook(pdo_stmt_t *stmt, struct pdo_bound_param_data *param,
  191. enum pdo_param_event event_type TSRMLS_DC)
  192. {
  193. pdo_pgsql_stmt *S = (pdo_pgsql_stmt*)stmt->driver_data;
  194. #if HAVE_PQPREPARE
  195. if (S->stmt_name && param->is_param) {
  196. switch (event_type) {
  197. case PDO_PARAM_EVT_FREE:
  198. if (param->driver_data) {
  199. efree(param->driver_data);
  200. }
  201. break;
  202. case PDO_PARAM_EVT_ALLOC:
  203. /* decode name from $1, $2 into 0, 1 etc. */
  204. if (param->name) {
  205. if (param->name[0] == '$') {
  206. param->paramno = atoi(param->name + 1);
  207. } else {
  208. /* resolve parameter name to rewritten name */
  209. char *nameptr;
  210. if (SUCCESS == zend_hash_find(stmt->bound_param_map,
  211. param->name, param->namelen + 1, (void**)&nameptr)) {
  212. param->paramno = atoi(nameptr + 1) - 1;
  213. } else {
  214. pdo_pgsql_error_stmt(stmt, PGRES_FATAL_ERROR, "HY093");
  215. return 0;
  216. }
  217. }
  218. }
  219. break;
  220. case PDO_PARAM_EVT_EXEC_PRE:
  221. if (!S->param_values) {
  222. S->param_values = ecalloc(
  223. zend_hash_num_elements(stmt->bound_params),
  224. sizeof(char*));
  225. S->param_lengths = ecalloc(
  226. zend_hash_num_elements(stmt->bound_params),
  227. sizeof(int));
  228. S->param_formats = ecalloc(
  229. zend_hash_num_elements(stmt->bound_params),
  230. sizeof(int));
  231. S->param_types = ecalloc(
  232. zend_hash_num_elements(stmt->bound_params),
  233. sizeof(Oid));
  234. }
  235. if (param->paramno >= 0) {
  236. if (PDO_PARAM_TYPE(param->param_type) == PDO_PARAM_LOB &&
  237. Z_TYPE_P(param->parameter) == IS_RESOURCE) {
  238. php_stream *stm;
  239. php_stream_from_zval_no_verify(stm, &param->parameter);
  240. if (stm) {
  241. if (php_stream_is(stm, &pdo_pgsql_lob_stream_ops)) {
  242. struct pdo_pgsql_lob_self *self = (struct pdo_pgsql_lob_self*)stm->abstract;
  243. pdo_pgsql_bound_param *P = param->driver_data;
  244. if (P == NULL) {
  245. P = ecalloc(1, sizeof(*P));
  246. param->driver_data = P;
  247. }
  248. P->oid = htonl(self->oid);
  249. S->param_values[param->paramno] = (char*)&P->oid;
  250. S->param_lengths[param->paramno] = sizeof(P->oid);
  251. S->param_formats[param->paramno] = 1;
  252. S->param_types[param->paramno] = OIDOID;
  253. return 1;
  254. } else {
  255. SEPARATE_ZVAL_IF_NOT_REF(&param->parameter);
  256. Z_TYPE_P(param->parameter) = IS_STRING;
  257. Z_STRLEN_P(param->parameter) = php_stream_copy_to_mem(stm,
  258. &Z_STRVAL_P(param->parameter), PHP_STREAM_COPY_ALL, 0);
  259. }
  260. } else {
  261. /* expected a stream resource */
  262. pdo_pgsql_error_stmt(stmt, PGRES_FATAL_ERROR, "HY105");
  263. return 0;
  264. }
  265. }
  266. if (PDO_PARAM_TYPE(param->param_type) == PDO_PARAM_NULL ||
  267. Z_TYPE_P(param->parameter) == IS_NULL) {
  268. S->param_values[param->paramno] = NULL;
  269. S->param_lengths[param->paramno] = 0;
  270. } else if (Z_TYPE_P(param->parameter) == IS_BOOL) {
  271. S->param_values[param->paramno] = Z_BVAL_P(param->parameter) ? "t" : "f";
  272. S->param_lengths[param->paramno] = 1;
  273. S->param_formats[param->paramno] = 0;
  274. } else {
  275. convert_to_string(param->parameter);
  276. S->param_values[param->paramno] = Z_STRVAL_P(param->parameter);
  277. S->param_lengths[param->paramno] = Z_STRLEN_P(param->parameter);
  278. S->param_formats[param->paramno] = 0;
  279. }
  280. if (PDO_PARAM_TYPE(param->param_type) == PDO_PARAM_LOB) {
  281. S->param_types[param->paramno] = 0;
  282. S->param_formats[param->paramno] = 1;
  283. } else {
  284. S->param_types[param->paramno] = 0;
  285. }
  286. }
  287. break;
  288. }
  289. }
  290. #endif
  291. return 1;
  292. }
  293. static int pgsql_stmt_fetch(pdo_stmt_t *stmt,
  294. enum pdo_fetch_orientation ori, long offset TSRMLS_DC)
  295. {
  296. pdo_pgsql_stmt *S = (pdo_pgsql_stmt*)stmt->driver_data;
  297. if (S->cursor_name) {
  298. char *ori_str;
  299. char *q = NULL;
  300. ExecStatusType status;
  301. switch (ori) {
  302. case PDO_FETCH_ORI_NEXT: ori_str = "FORWARD"; break;
  303. case PDO_FETCH_ORI_PRIOR: ori_str = "BACKWARD"; break;
  304. case PDO_FETCH_ORI_REL: ori_str = "RELATIVE"; break;
  305. default:
  306. return 0;
  307. }
  308. spprintf(&q, 0, "FETCH %s %ld FROM %s", ori_str, offset, S->cursor_name);
  309. S->result = PQexec(S->H->server, q);
  310. status = PQresultStatus(S->result);
  311. if (status != PGRES_COMMAND_OK && status != PGRES_TUPLES_OK) {
  312. pdo_pgsql_error_stmt(stmt, status, pdo_pgsql_sqlstate(S->result));
  313. return 0;
  314. }
  315. S->current_row = 1;
  316. return 1;
  317. } else {
  318. if (S->current_row < stmt->row_count) {
  319. S->current_row++;
  320. return 1;
  321. } else {
  322. return 0;
  323. }
  324. }
  325. }
  326. static int pgsql_stmt_describe(pdo_stmt_t *stmt, int colno TSRMLS_DC)
  327. {
  328. pdo_pgsql_stmt *S = (pdo_pgsql_stmt*)stmt->driver_data;
  329. struct pdo_column_data *cols = stmt->columns;
  330. struct pdo_bound_param_data *param;
  331. if (!S->result) {
  332. return 0;
  333. }
  334. cols[colno].name = estrdup(PQfname(S->result, colno));
  335. cols[colno].namelen = strlen(cols[colno].name);
  336. cols[colno].maxlen = PQfsize(S->result, colno);
  337. cols[colno].precision = PQfmod(S->result, colno);
  338. S->cols[colno].pgsql_type = PQftype(S->result, colno);
  339. switch(S->cols[colno].pgsql_type) {
  340. case BOOLOID:
  341. cols[colno].param_type = PDO_PARAM_BOOL;
  342. break;
  343. case OIDOID:
  344. /* did the user bind the column as a LOB ? */
  345. if (stmt->bound_columns && (
  346. SUCCESS == zend_hash_index_find(stmt->bound_columns,
  347. colno, (void**)&param) ||
  348. SUCCESS == zend_hash_find(stmt->bound_columns,
  349. cols[colno].name, cols[colno].namelen,
  350. (void**)&param))) {
  351. if (PDO_PARAM_TYPE(param->param_type) == PDO_PARAM_LOB) {
  352. cols[colno].param_type = PDO_PARAM_LOB;
  353. break;
  354. }
  355. }
  356. cols[colno].param_type = PDO_PARAM_INT;
  357. break;
  358. case INT2OID:
  359. case INT4OID:
  360. cols[colno].param_type = PDO_PARAM_INT;
  361. break;
  362. case INT8OID:
  363. if (sizeof(long)>=8) {
  364. cols[colno].param_type = PDO_PARAM_INT;
  365. } else {
  366. cols[colno].param_type = PDO_PARAM_STR;
  367. }
  368. break;
  369. case BYTEAOID:
  370. cols[colno].param_type = PDO_PARAM_LOB;
  371. break;
  372. default:
  373. cols[colno].param_type = PDO_PARAM_STR;
  374. }
  375. return 1;
  376. }
  377. /* PQunescapeBytea() from PostgreSQL 7.3 to provide bytea unescape feature to 7.2 users.
  378. Renamed to php_pdo_pgsql_unescape_bytea() */
  379. /*
  380. * PQunescapeBytea - converts the null terminated string representation
  381. * of a bytea, strtext, into binary, filling a buffer. It returns a
  382. * pointer to the buffer which is NULL on error, and the size of the
  383. * buffer in retbuflen. The pointer may subsequently be used as an
  384. * argument to the function free(3). It is the reverse of PQescapeBytea.
  385. *
  386. * The following transformations are reversed:
  387. * '\0' == ASCII 0 == \000
  388. * '\'' == ASCII 39 == \'
  389. * '\\' == ASCII 92 == \\
  390. *
  391. * States:
  392. * 0 normal 0->1->2->3->4
  393. * 1 \ 1->5
  394. * 2 \0 1->6
  395. * 3 \00
  396. * 4 \000
  397. * 5 \'
  398. * 6 \\
  399. */
  400. static unsigned char *php_pdo_pgsql_unescape_bytea(unsigned char *strtext, size_t *retbuflen)
  401. {
  402. size_t buflen;
  403. unsigned char *buffer,
  404. *sp,
  405. *bp;
  406. unsigned int state = 0;
  407. if (strtext == NULL)
  408. return NULL;
  409. buflen = strlen(strtext); /* will shrink, also we discover if
  410. * strtext */
  411. buffer = (unsigned char *) emalloc(buflen); /* isn't NULL terminated */
  412. for (bp = buffer, sp = strtext; *sp != '\0'; bp++, sp++)
  413. {
  414. switch (state)
  415. {
  416. case 0:
  417. if (*sp == '\\')
  418. state = 1;
  419. *bp = *sp;
  420. break;
  421. case 1:
  422. if (*sp == '\'') /* state=5 */
  423. { /* replace \' with 39 */
  424. bp--;
  425. *bp = '\'';
  426. buflen--;
  427. state = 0;
  428. }
  429. else if (*sp == '\\') /* state=6 */
  430. { /* replace \\ with 92 */
  431. bp--;
  432. *bp = '\\';
  433. buflen--;
  434. state = 0;
  435. }
  436. else
  437. {
  438. if (isdigit(*sp))
  439. state = 2;
  440. else
  441. state = 0;
  442. *bp = *sp;
  443. }
  444. break;
  445. case 2:
  446. if (isdigit(*sp))
  447. state = 3;
  448. else
  449. state = 0;
  450. *bp = *sp;
  451. break;
  452. case 3:
  453. if (isdigit(*sp)) /* state=4 */
  454. {
  455. unsigned char *start, *end, buf[4]; /* 000 + '\0' */
  456. bp -= 3;
  457. memcpy(buf, sp-2, 3);
  458. buf[3] = '\0';
  459. start = buf;
  460. *bp = (unsigned char)strtoul(start, (char **)&end, 8);
  461. buflen -= 3;
  462. state = 0;
  463. }
  464. else
  465. {
  466. *bp = *sp;
  467. state = 0;
  468. }
  469. break;
  470. }
  471. }
  472. buffer = erealloc(buffer, buflen+1);
  473. buffer[buflen] = '\0';
  474. *retbuflen = buflen;
  475. return buffer;
  476. }
  477. static int pgsql_stmt_get_col(pdo_stmt_t *stmt, int colno, char **ptr, unsigned long *len, int *caller_frees TSRMLS_DC)
  478. {
  479. pdo_pgsql_stmt *S = (pdo_pgsql_stmt*)stmt->driver_data;
  480. struct pdo_column_data *cols = stmt->columns;
  481. size_t tmp_len;
  482. if (!S->result) {
  483. return 0;
  484. }
  485. /* We have already increased count by 1 in pgsql_stmt_fetch() */
  486. if (PQgetisnull(S->result, S->current_row - 1, colno)) { /* Check if we got NULL */
  487. *ptr = NULL;
  488. *len = 0;
  489. } else {
  490. *ptr = PQgetvalue(S->result, S->current_row - 1, colno);
  491. *len = PQgetlength(S->result, S->current_row - 1, colno);
  492. switch(cols[colno].param_type) {
  493. case PDO_PARAM_INT:
  494. S->cols[colno].intval = atol(*ptr);
  495. *ptr = (char *) &(S->cols[colno].intval);
  496. *len = sizeof(long);
  497. break;
  498. case PDO_PARAM_BOOL:
  499. S->cols[colno].boolval = **ptr == 't' ? 1: 0;
  500. *ptr = (char *) &(S->cols[colno].boolval);
  501. *len = sizeof(zend_bool);
  502. break;
  503. case PDO_PARAM_LOB:
  504. if (S->cols[colno].pgsql_type == OIDOID) {
  505. /* ooo, a real large object */
  506. char *end_ptr;
  507. Oid oid = (Oid)strtoul(*ptr, &end_ptr, 10);
  508. int loid = lo_open(S->H->server, oid, INV_READ);
  509. if (loid >= 0) {
  510. *ptr = (char*)pdo_pgsql_create_lob_stream(stmt->dbh, loid, oid TSRMLS_CC);
  511. *len = 0;
  512. return *ptr ? 1 : 0;
  513. }
  514. *ptr = NULL;
  515. *len = 0;
  516. return 0;
  517. } else {
  518. *ptr = php_pdo_pgsql_unescape_bytea(*ptr, &tmp_len);
  519. *len = tmp_len;
  520. *caller_frees = 1;
  521. }
  522. break;
  523. case PDO_PARAM_NULL:
  524. case PDO_PARAM_STR:
  525. case PDO_PARAM_STMT:
  526. case PDO_PARAM_INPUT_OUTPUT:
  527. break;
  528. }
  529. }
  530. return 1;
  531. }
  532. static int pgsql_stmt_get_column_meta(pdo_stmt_t *stmt, long colno, zval *return_value TSRMLS_DC)
  533. {
  534. pdo_pgsql_stmt *S = (pdo_pgsql_stmt*)stmt->driver_data;
  535. PGresult *res;
  536. char *q=NULL;
  537. ExecStatusType status;
  538. if (!S->result) {
  539. return FAILURE;
  540. }
  541. if (colno >= stmt->column_count) {
  542. return FAILURE;
  543. }
  544. array_init(return_value);
  545. add_assoc_long(return_value, "pgsql:oid", S->cols[colno].pgsql_type);
  546. /* Fetch metadata from Postgres system catalogue */
  547. spprintf(&q, 0, "SELECT TYPNAME FROM PG_TYPE WHERE OID=%d", S->cols[colno].pgsql_type);
  548. res = PQexec(S->H->server, q);
  549. efree(q);
  550. status = PQresultStatus(res);
  551. if (status != PGRES_TUPLES_OK) {
  552. /* Failed to get system catalogue, but return success
  553. * with the data we have collected so far
  554. */
  555. PQclear(res);
  556. return 1;
  557. }
  558. /* We want exactly one row returned */
  559. if (1 != PQntuples(res)) {
  560. PQclear(res);
  561. return 1;
  562. }
  563. add_assoc_string(return_value, "native_type", PQgetvalue(res, 0, 0), 1);
  564. PQclear(res);
  565. return 1;
  566. }
  567. static int pdo_pgsql_stmt_cursor_closer(pdo_stmt_t *stmt TSRMLS_DC)
  568. {
  569. return 1;
  570. }
  571. struct pdo_stmt_methods pgsql_stmt_methods = {
  572. pgsql_stmt_dtor,
  573. pgsql_stmt_execute,
  574. pgsql_stmt_fetch,
  575. pgsql_stmt_describe,
  576. pgsql_stmt_get_col,
  577. pgsql_stmt_param_hook,
  578. NULL, /* set_attr */
  579. NULL, /* get_attr */
  580. pgsql_stmt_get_column_meta,
  581. NULL, /* next_rowset */
  582. pdo_pgsql_stmt_cursor_closer
  583. };
  584. /*
  585. * Local variables:
  586. * tab-width: 4
  587. * c-basic-offset: 4
  588. * End:
  589. * vim600: noet sw=4 ts=4 fdm=marker
  590. * vim<600: noet sw=4 ts=4
  591. */