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.

665 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_NORMALIZE:
  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 (stmt->bound_param_map && 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_ALLOC:
  221. /* work is handled by EVT_NORMALIZE */
  222. return 1;
  223. case PDO_PARAM_EVT_EXEC_PRE:
  224. if (!S->param_values) {
  225. S->param_values = ecalloc(
  226. zend_hash_num_elements(stmt->bound_params),
  227. sizeof(char*));
  228. S->param_lengths = ecalloc(
  229. zend_hash_num_elements(stmt->bound_params),
  230. sizeof(int));
  231. S->param_formats = ecalloc(
  232. zend_hash_num_elements(stmt->bound_params),
  233. sizeof(int));
  234. S->param_types = ecalloc(
  235. zend_hash_num_elements(stmt->bound_params),
  236. sizeof(Oid));
  237. }
  238. if (param->paramno >= 0) {
  239. if (PDO_PARAM_TYPE(param->param_type) == PDO_PARAM_LOB &&
  240. Z_TYPE_P(param->parameter) == IS_RESOURCE) {
  241. php_stream *stm;
  242. php_stream_from_zval_no_verify(stm, &param->parameter);
  243. if (stm) {
  244. if (php_stream_is(stm, &pdo_pgsql_lob_stream_ops)) {
  245. struct pdo_pgsql_lob_self *self = (struct pdo_pgsql_lob_self*)stm->abstract;
  246. pdo_pgsql_bound_param *P = param->driver_data;
  247. if (P == NULL) {
  248. P = ecalloc(1, sizeof(*P));
  249. param->driver_data = P;
  250. }
  251. P->oid = htonl(self->oid);
  252. S->param_values[param->paramno] = (char*)&P->oid;
  253. S->param_lengths[param->paramno] = sizeof(P->oid);
  254. S->param_formats[param->paramno] = 1;
  255. S->param_types[param->paramno] = OIDOID;
  256. return 1;
  257. } else {
  258. SEPARATE_ZVAL_IF_NOT_REF(&param->parameter);
  259. Z_TYPE_P(param->parameter) = IS_STRING;
  260. Z_STRLEN_P(param->parameter) = php_stream_copy_to_mem(stm,
  261. &Z_STRVAL_P(param->parameter), PHP_STREAM_COPY_ALL, 0);
  262. }
  263. } else {
  264. /* expected a stream resource */
  265. pdo_pgsql_error_stmt(stmt, PGRES_FATAL_ERROR, "HY105");
  266. return 0;
  267. }
  268. }
  269. if (PDO_PARAM_TYPE(param->param_type) == PDO_PARAM_NULL ||
  270. Z_TYPE_P(param->parameter) == IS_NULL) {
  271. S->param_values[param->paramno] = NULL;
  272. S->param_lengths[param->paramno] = 0;
  273. } else if (Z_TYPE_P(param->parameter) == IS_BOOL) {
  274. S->param_values[param->paramno] = Z_BVAL_P(param->parameter) ? "t" : "f";
  275. S->param_lengths[param->paramno] = 1;
  276. S->param_formats[param->paramno] = 0;
  277. } else {
  278. convert_to_string(param->parameter);
  279. S->param_values[param->paramno] = Z_STRVAL_P(param->parameter);
  280. S->param_lengths[param->paramno] = Z_STRLEN_P(param->parameter);
  281. S->param_formats[param->paramno] = 0;
  282. }
  283. if (PDO_PARAM_TYPE(param->param_type) == PDO_PARAM_LOB) {
  284. S->param_types[param->paramno] = 0;
  285. S->param_formats[param->paramno] = 1;
  286. } else {
  287. S->param_types[param->paramno] = 0;
  288. }
  289. }
  290. break;
  291. }
  292. }
  293. #endif
  294. return 1;
  295. }
  296. static int pgsql_stmt_fetch(pdo_stmt_t *stmt,
  297. enum pdo_fetch_orientation ori, long offset TSRMLS_DC)
  298. {
  299. pdo_pgsql_stmt *S = (pdo_pgsql_stmt*)stmt->driver_data;
  300. if (S->cursor_name) {
  301. char *ori_str;
  302. char *q = NULL;
  303. ExecStatusType status;
  304. switch (ori) {
  305. case PDO_FETCH_ORI_NEXT: ori_str = "FORWARD"; break;
  306. case PDO_FETCH_ORI_PRIOR: ori_str = "BACKWARD"; break;
  307. case PDO_FETCH_ORI_REL: ori_str = "RELATIVE"; break;
  308. default:
  309. return 0;
  310. }
  311. spprintf(&q, 0, "FETCH %s %ld FROM %s", ori_str, offset, S->cursor_name);
  312. S->result = PQexec(S->H->server, q);
  313. efree(q);
  314. status = PQresultStatus(S->result);
  315. if (status != PGRES_COMMAND_OK && status != PGRES_TUPLES_OK) {
  316. pdo_pgsql_error_stmt(stmt, status, pdo_pgsql_sqlstate(S->result));
  317. return 0;
  318. }
  319. S->current_row = 1;
  320. return 1;
  321. } else {
  322. if (S->current_row < stmt->row_count) {
  323. S->current_row++;
  324. return 1;
  325. } else {
  326. return 0;
  327. }
  328. }
  329. }
  330. static int pgsql_stmt_describe(pdo_stmt_t *stmt, int colno TSRMLS_DC)
  331. {
  332. pdo_pgsql_stmt *S = (pdo_pgsql_stmt*)stmt->driver_data;
  333. struct pdo_column_data *cols = stmt->columns;
  334. struct pdo_bound_param_data *param;
  335. if (!S->result) {
  336. return 0;
  337. }
  338. cols[colno].name = estrdup(PQfname(S->result, colno));
  339. cols[colno].namelen = strlen(cols[colno].name);
  340. cols[colno].maxlen = PQfsize(S->result, colno);
  341. cols[colno].precision = PQfmod(S->result, colno);
  342. S->cols[colno].pgsql_type = PQftype(S->result, colno);
  343. switch(S->cols[colno].pgsql_type) {
  344. case BOOLOID:
  345. cols[colno].param_type = PDO_PARAM_BOOL;
  346. break;
  347. case OIDOID:
  348. /* did the user bind the column as a LOB ? */
  349. if (stmt->bound_columns && (
  350. SUCCESS == zend_hash_index_find(stmt->bound_columns,
  351. colno, (void**)&param) ||
  352. SUCCESS == zend_hash_find(stmt->bound_columns,
  353. cols[colno].name, cols[colno].namelen,
  354. (void**)&param))) {
  355. if (PDO_PARAM_TYPE(param->param_type) == PDO_PARAM_LOB) {
  356. cols[colno].param_type = PDO_PARAM_LOB;
  357. break;
  358. }
  359. }
  360. cols[colno].param_type = PDO_PARAM_INT;
  361. break;
  362. case INT2OID:
  363. case INT4OID:
  364. cols[colno].param_type = PDO_PARAM_INT;
  365. break;
  366. case INT8OID:
  367. if (sizeof(long)>=8) {
  368. cols[colno].param_type = PDO_PARAM_INT;
  369. } else {
  370. cols[colno].param_type = PDO_PARAM_STR;
  371. }
  372. break;
  373. case BYTEAOID:
  374. cols[colno].param_type = PDO_PARAM_LOB;
  375. break;
  376. default:
  377. cols[colno].param_type = PDO_PARAM_STR;
  378. }
  379. return 1;
  380. }
  381. /* PQunescapeBytea() from PostgreSQL 7.3 to provide bytea unescape feature to 7.2 users.
  382. Renamed to php_pdo_pgsql_unescape_bytea() */
  383. /*
  384. * PQunescapeBytea - converts the null terminated string representation
  385. * of a bytea, strtext, into binary, filling a buffer. It returns a
  386. * pointer to the buffer which is NULL on error, and the size of the
  387. * buffer in retbuflen. The pointer may subsequently be used as an
  388. * argument to the function free(3). It is the reverse of PQescapeBytea.
  389. *
  390. * The following transformations are reversed:
  391. * '\0' == ASCII 0 == \000
  392. * '\'' == ASCII 39 == \'
  393. * '\\' == ASCII 92 == \\
  394. *
  395. * States:
  396. * 0 normal 0->1->2->3->4
  397. * 1 \ 1->5
  398. * 2 \0 1->6
  399. * 3 \00
  400. * 4 \000
  401. * 5 \'
  402. * 6 \\
  403. */
  404. static unsigned char *php_pdo_pgsql_unescape_bytea(unsigned char *strtext, size_t *retbuflen)
  405. {
  406. size_t buflen;
  407. unsigned char *buffer,
  408. *sp,
  409. *bp;
  410. unsigned int state = 0;
  411. if (strtext == NULL)
  412. return NULL;
  413. buflen = strlen(strtext); /* will shrink, also we discover if
  414. * strtext */
  415. buffer = (unsigned char *) emalloc(buflen); /* isn't NULL terminated */
  416. for (bp = buffer, sp = strtext; *sp != '\0'; bp++, sp++)
  417. {
  418. switch (state)
  419. {
  420. case 0:
  421. if (*sp == '\\')
  422. state = 1;
  423. *bp = *sp;
  424. break;
  425. case 1:
  426. if (*sp == '\'') /* state=5 */
  427. { /* replace \' with 39 */
  428. bp--;
  429. *bp = '\'';
  430. buflen--;
  431. state = 0;
  432. }
  433. else if (*sp == '\\') /* state=6 */
  434. { /* replace \\ with 92 */
  435. bp--;
  436. *bp = '\\';
  437. buflen--;
  438. state = 0;
  439. }
  440. else
  441. {
  442. if (isdigit(*sp))
  443. state = 2;
  444. else
  445. state = 0;
  446. *bp = *sp;
  447. }
  448. break;
  449. case 2:
  450. if (isdigit(*sp))
  451. state = 3;
  452. else
  453. state = 0;
  454. *bp = *sp;
  455. break;
  456. case 3:
  457. if (isdigit(*sp)) /* state=4 */
  458. {
  459. unsigned char *start, *end, buf[4]; /* 000 + '\0' */
  460. bp -= 3;
  461. memcpy(buf, sp-2, 3);
  462. buf[3] = '\0';
  463. start = buf;
  464. *bp = (unsigned char)strtoul(start, (char **)&end, 8);
  465. buflen -= 3;
  466. state = 0;
  467. }
  468. else
  469. {
  470. *bp = *sp;
  471. state = 0;
  472. }
  473. break;
  474. }
  475. }
  476. buffer = erealloc(buffer, buflen+1);
  477. buffer[buflen] = '\0';
  478. *retbuflen = buflen;
  479. return buffer;
  480. }
  481. static int pgsql_stmt_get_col(pdo_stmt_t *stmt, int colno, char **ptr, unsigned long *len, int *caller_frees TSRMLS_DC)
  482. {
  483. pdo_pgsql_stmt *S = (pdo_pgsql_stmt*)stmt->driver_data;
  484. struct pdo_column_data *cols = stmt->columns;
  485. size_t tmp_len;
  486. if (!S->result) {
  487. return 0;
  488. }
  489. /* We have already increased count by 1 in pgsql_stmt_fetch() */
  490. if (PQgetisnull(S->result, S->current_row - 1, colno)) { /* Check if we got NULL */
  491. *ptr = NULL;
  492. *len = 0;
  493. } else {
  494. *ptr = PQgetvalue(S->result, S->current_row - 1, colno);
  495. *len = PQgetlength(S->result, S->current_row - 1, colno);
  496. switch(cols[colno].param_type) {
  497. case PDO_PARAM_INT:
  498. S->cols[colno].intval = atol(*ptr);
  499. *ptr = (char *) &(S->cols[colno].intval);
  500. *len = sizeof(long);
  501. break;
  502. case PDO_PARAM_BOOL:
  503. S->cols[colno].boolval = **ptr == 't' ? 1: 0;
  504. *ptr = (char *) &(S->cols[colno].boolval);
  505. *len = sizeof(zend_bool);
  506. break;
  507. case PDO_PARAM_LOB:
  508. if (S->cols[colno].pgsql_type == OIDOID) {
  509. /* ooo, a real large object */
  510. char *end_ptr;
  511. Oid oid = (Oid)strtoul(*ptr, &end_ptr, 10);
  512. int loid = lo_open(S->H->server, oid, INV_READ);
  513. if (loid >= 0) {
  514. *ptr = (char*)pdo_pgsql_create_lob_stream(stmt->dbh, loid, oid TSRMLS_CC);
  515. *len = 0;
  516. return *ptr ? 1 : 0;
  517. }
  518. *ptr = NULL;
  519. *len = 0;
  520. return 0;
  521. } else {
  522. *ptr = php_pdo_pgsql_unescape_bytea(*ptr, &tmp_len);
  523. *len = tmp_len;
  524. *caller_frees = 1;
  525. }
  526. break;
  527. case PDO_PARAM_NULL:
  528. case PDO_PARAM_STR:
  529. case PDO_PARAM_STMT:
  530. case PDO_PARAM_INPUT_OUTPUT:
  531. break;
  532. }
  533. }
  534. return 1;
  535. }
  536. static int pgsql_stmt_get_column_meta(pdo_stmt_t *stmt, long colno, zval *return_value TSRMLS_DC)
  537. {
  538. pdo_pgsql_stmt *S = (pdo_pgsql_stmt*)stmt->driver_data;
  539. PGresult *res;
  540. char *q=NULL;
  541. ExecStatusType status;
  542. if (!S->result) {
  543. return FAILURE;
  544. }
  545. if (colno >= stmt->column_count) {
  546. return FAILURE;
  547. }
  548. array_init(return_value);
  549. add_assoc_long(return_value, "pgsql:oid", S->cols[colno].pgsql_type);
  550. /* Fetch metadata from Postgres system catalogue */
  551. spprintf(&q, 0, "SELECT TYPNAME FROM PG_TYPE WHERE OID=%d", S->cols[colno].pgsql_type);
  552. res = PQexec(S->H->server, q);
  553. efree(q);
  554. status = PQresultStatus(res);
  555. if (status != PGRES_TUPLES_OK) {
  556. /* Failed to get system catalogue, but return success
  557. * with the data we have collected so far
  558. */
  559. PQclear(res);
  560. return 1;
  561. }
  562. /* We want exactly one row returned */
  563. if (1 != PQntuples(res)) {
  564. PQclear(res);
  565. return 1;
  566. }
  567. add_assoc_string(return_value, "native_type", PQgetvalue(res, 0, 0), 1);
  568. PQclear(res);
  569. return 1;
  570. }
  571. static int pdo_pgsql_stmt_cursor_closer(pdo_stmt_t *stmt TSRMLS_DC)
  572. {
  573. return 1;
  574. }
  575. struct pdo_stmt_methods pgsql_stmt_methods = {
  576. pgsql_stmt_dtor,
  577. pgsql_stmt_execute,
  578. pgsql_stmt_fetch,
  579. pgsql_stmt_describe,
  580. pgsql_stmt_get_col,
  581. pgsql_stmt_param_hook,
  582. NULL, /* set_attr */
  583. NULL, /* get_attr */
  584. pgsql_stmt_get_column_meta,
  585. NULL, /* next_rowset */
  586. pdo_pgsql_stmt_cursor_closer
  587. };
  588. /*
  589. * Local variables:
  590. * tab-width: 4
  591. * c-basic-offset: 4
  592. * End:
  593. * vim600: noet sw=4 ts=4 fdm=marker
  594. * vim<600: noet sw=4 ts=4
  595. */