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.

527 lines
12 KiB

21 years ago
21 years ago
21 years ago
  1. /*
  2. +----------------------------------------------------------------------+
  3. | PHP Version 5 |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1997-2005 The PHP Group |
  6. +----------------------------------------------------------------------+
  7. | This source file is subject to version 3.0 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_0.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. | Author: Edin Kadribasic <edink@emini.dk> |
  16. +----------------------------------------------------------------------+
  17. */
  18. /* $Id$ */
  19. #ifdef HAVE_CONFIG_H
  20. #include "config.h"
  21. #endif
  22. #include "php.h"
  23. #include "php_ini.h"
  24. #include "ext/standard/info.h"
  25. #include "pdo/php_pdo.h"
  26. #include "pdo/php_pdo_driver.h"
  27. #include "php_pdo_pgsql.h"
  28. #include "php_pdo_pgsql_int.h"
  29. /* from postgresql/src/include/catalog/pg_type.h */
  30. #define BOOLOID 16
  31. #define BYTEAOID 17
  32. #define INT8OID 20
  33. #define INT2OID 21
  34. #define INT4OID 23
  35. #define OIDOID 26
  36. static int pgsql_stmt_dtor(pdo_stmt_t *stmt TSRMLS_DC)
  37. {
  38. pdo_pgsql_stmt *S = (pdo_pgsql_stmt*)stmt->driver_data;
  39. if (S->result) {
  40. /* free the resource */
  41. PQclear(S->result);
  42. S->result = NULL;
  43. }
  44. #if HAVE_PQPREPARE
  45. if (S->stmt_name) {
  46. pdo_pgsql_db_handle *H = S->H;
  47. char *q = NULL;
  48. PGresult *res;
  49. spprintf(&q, 0, "DEALLOCATE %s", S->stmt_name);
  50. res = PQexec(H->server, q);
  51. efree(q);
  52. if (res) PQclear(res);
  53. efree(S->stmt_name);
  54. S->stmt_name = NULL;
  55. }
  56. if (S->param_lengths) {
  57. efree(S->param_lengths);
  58. S->param_lengths = NULL;
  59. }
  60. if (S->param_values) {
  61. efree(S->param_values);
  62. S->param_values = NULL;
  63. }
  64. if (S->param_formats) {
  65. efree(S->param_formats);
  66. S->param_formats = NULL;
  67. }
  68. #endif
  69. if (S->cursor_name) {
  70. pdo_pgsql_db_handle *H = S->H;
  71. char *q = NULL;
  72. PGresult *res;
  73. spprintf(&q, 0, "CLOSE %s", S->cursor_name);
  74. res = PQexec(H->server, q);
  75. efree(q);
  76. if (res) PQclear(res);
  77. efree(S->cursor_name);
  78. S->cursor_name = NULL;
  79. }
  80. if(S->cols) {
  81. efree(S->cols);
  82. S->cols = NULL;
  83. }
  84. efree(S);
  85. stmt->driver_data = NULL;
  86. return 1;
  87. }
  88. static int pgsql_stmt_execute(pdo_stmt_t *stmt TSRMLS_DC)
  89. {
  90. pdo_pgsql_stmt *S = (pdo_pgsql_stmt*)stmt->driver_data;
  91. pdo_pgsql_db_handle *H = S->H;
  92. ExecStatusType status;
  93. if (stmt->executed) {
  94. /* ensure that we free any previous unfetched results */
  95. if(S->result) {
  96. PQclear(S->result);
  97. S->result = NULL;
  98. }
  99. }
  100. S->current_row = 0;
  101. #if HAVE_PQPREPARE
  102. if (S->stmt_name) {
  103. /* using a prepared statement */
  104. S->result = PQexecPrepared(H->server, S->stmt_name,
  105. stmt->bound_params ?
  106. zend_hash_num_elements(stmt->bound_params) :
  107. 0,
  108. (const char**)S->param_values,
  109. S->param_lengths,
  110. NULL,
  111. 0);
  112. } else
  113. #endif
  114. if (S->cursor_name) {
  115. char *q = NULL;
  116. spprintf(&q, 0, "DECLARE %s CURSOR FOR %s", S->cursor_name, stmt->active_query_string);
  117. S->result = PQexec(H->server, q);
  118. efree(q);
  119. } else {
  120. S->result = PQexec(H->server, stmt->active_query_string);
  121. }
  122. status = PQresultStatus(S->result);
  123. if (status != PGRES_COMMAND_OK && status != PGRES_TUPLES_OK) {
  124. pdo_pgsql_error_stmt(stmt, status, pdo_pgsql_sqlstate(S->result));
  125. return 0;
  126. }
  127. if(!stmt->executed) {
  128. stmt->column_count = (int) PQnfields(S->result);
  129. S->cols = ecalloc(stmt->column_count, sizeof(pdo_pgsql_column));
  130. }
  131. if (status == PGRES_COMMAND_OK) {
  132. stmt->row_count = (long)atoi(PQcmdTuples(S->result));
  133. H->pgoid = PQoidValue(S->result);
  134. } else {
  135. stmt->row_count = (long)PQntuples(S->result);
  136. }
  137. return 1;
  138. }
  139. static int pgsql_stmt_param_hook(pdo_stmt_t *stmt, struct pdo_bound_param_data *param,
  140. enum pdo_param_event event_type TSRMLS_DC)
  141. {
  142. pdo_pgsql_stmt *S = (pdo_pgsql_stmt*)stmt->driver_data;
  143. #if HAVE_PQPREPARE
  144. if (S->stmt_name && param->is_param) {
  145. switch (event_type) {
  146. case PDO_PARAM_EVT_ALLOC:
  147. /* decode name from $1, $2 into 0, 1 etc. */
  148. if (param->name) {
  149. if (param->name[0] == '$') {
  150. param->paramno = atoi(param->name + 1);
  151. } else {
  152. /* resolve parameter name to rewritten name */
  153. char *nameptr;
  154. if (SUCCESS == zend_hash_find(stmt->bound_param_map,
  155. param->name, param->namelen + 1, (void**)&nameptr)) {
  156. param->paramno = atoi(nameptr + 1) - 1;
  157. } else {
  158. pdo_pgsql_error_stmt(stmt, PGRES_FATAL_ERROR, "HY093");
  159. return 0;
  160. }
  161. }
  162. }
  163. break;
  164. case PDO_PARAM_EVT_EXEC_PRE:
  165. if (!S->param_values) {
  166. S->param_values = ecalloc(
  167. zend_hash_num_elements(stmt->bound_params),
  168. sizeof(char*));
  169. S->param_lengths = ecalloc(
  170. zend_hash_num_elements(stmt->bound_params),
  171. sizeof(int));
  172. S->param_formats = ecalloc(
  173. zend_hash_num_elements(stmt->bound_params),
  174. sizeof(int));
  175. }
  176. if (PDO_PARAM_TYPE(param->param_type) == PDO_PARAM_NULL ||
  177. Z_TYPE_P(param->parameter) == IS_NULL) {
  178. S->param_values[param->paramno] = NULL;
  179. S->param_lengths[param->paramno] = 0;
  180. } else {
  181. convert_to_string(param->parameter);
  182. S->param_values[param->paramno] = Z_STRVAL_P(param->parameter);
  183. S->param_lengths[param->paramno] = Z_STRLEN_P(param->parameter);
  184. S->param_formats[param->paramno] = 1;
  185. }
  186. break;
  187. }
  188. }
  189. #endif
  190. return 1;
  191. }
  192. static int pgsql_stmt_fetch(pdo_stmt_t *stmt,
  193. enum pdo_fetch_orientation ori, long offset TSRMLS_DC)
  194. {
  195. pdo_pgsql_stmt *S = (pdo_pgsql_stmt*)stmt->driver_data;
  196. if (S->cursor_name) {
  197. char *ori_str;
  198. char *q = NULL;
  199. ExecStatusType status;
  200. switch (ori) {
  201. case PDO_FETCH_ORI_NEXT: ori_str = "FORWARD"; break;
  202. case PDO_FETCH_ORI_PRIOR: ori_str = "BACKWARD"; break;
  203. case PDO_FETCH_ORI_REL: ori_str = "RELATIVE"; break;
  204. default:
  205. return 0;
  206. }
  207. spprintf(&q, 0, "FETCH %s %ld FROM %s", ori_str, offset, S->cursor_name);
  208. S->result = PQexec(S->H->server, q);
  209. status = PQresultStatus(S->result);
  210. if (status != PGRES_COMMAND_OK && status != PGRES_TUPLES_OK) {
  211. pdo_pgsql_error_stmt(stmt, status, pdo_pgsql_sqlstate(S->result));
  212. return 0;
  213. }
  214. S->current_row = 1;
  215. return 1;
  216. } else {
  217. if (S->current_row < stmt->row_count) {
  218. S->current_row++;
  219. return 1;
  220. } else {
  221. return 0;
  222. }
  223. }
  224. }
  225. static int pgsql_stmt_describe(pdo_stmt_t *stmt, int colno TSRMLS_DC)
  226. {
  227. pdo_pgsql_stmt *S = (pdo_pgsql_stmt*)stmt->driver_data;
  228. struct pdo_column_data *cols = stmt->columns;
  229. if (!S->result) {
  230. return 0;
  231. }
  232. cols[colno].name = estrdup(PQfname(S->result, colno));
  233. cols[colno].namelen = strlen(cols[colno].name);
  234. cols[colno].maxlen = PQfsize(S->result, colno);
  235. cols[colno].precision = PQfmod(S->result, colno);
  236. S->cols[colno].pgsql_type = PQftype(S->result, colno);
  237. switch(S->cols[colno].pgsql_type) {
  238. case BOOLOID:
  239. cols[colno].param_type = PDO_PARAM_BOOL;
  240. break;
  241. case INT2OID:
  242. case INT4OID:
  243. case OIDOID:
  244. cols[colno].param_type = PDO_PARAM_INT;
  245. break;
  246. case INT8OID:
  247. if (sizeof(long)>=8) {
  248. cols[colno].param_type = PDO_PARAM_INT;
  249. } else {
  250. cols[colno].param_type = PDO_PARAM_STR;
  251. }
  252. break;
  253. case BYTEAOID:
  254. cols[colno].param_type = PDO_PARAM_LOB;
  255. break;
  256. default:
  257. cols[colno].param_type = PDO_PARAM_STR;
  258. }
  259. return 1;
  260. }
  261. /* PQunescapeBytea() from PostgreSQL 7.3 to provide bytea unescape feature to 7.2 users.
  262. Renamed to php_pdo_pgsql_unescape_bytea() */
  263. /*
  264. * PQunescapeBytea - converts the null terminated string representation
  265. * of a bytea, strtext, into binary, filling a buffer. It returns a
  266. * pointer to the buffer which is NULL on error, and the size of the
  267. * buffer in retbuflen. The pointer may subsequently be used as an
  268. * argument to the function free(3). It is the reverse of PQescapeBytea.
  269. *
  270. * The following transformations are reversed:
  271. * '\0' == ASCII 0 == \000
  272. * '\'' == ASCII 39 == \'
  273. * '\\' == ASCII 92 == \\
  274. *
  275. * States:
  276. * 0 normal 0->1->2->3->4
  277. * 1 \ 1->5
  278. * 2 \0 1->6
  279. * 3 \00
  280. * 4 \000
  281. * 5 \'
  282. * 6 \\
  283. */
  284. static unsigned char *php_pdo_pgsql_unescape_bytea(unsigned char *strtext, size_t *retbuflen)
  285. {
  286. size_t buflen;
  287. unsigned char *buffer,
  288. *sp,
  289. *bp;
  290. unsigned int state = 0;
  291. if (strtext == NULL)
  292. return NULL;
  293. buflen = strlen(strtext); /* will shrink, also we discover if
  294. * strtext */
  295. buffer = (unsigned char *) emalloc(buflen); /* isn't NULL terminated */
  296. for (bp = buffer, sp = strtext; *sp != '\0'; bp++, sp++)
  297. {
  298. switch (state)
  299. {
  300. case 0:
  301. if (*sp == '\\')
  302. state = 1;
  303. *bp = *sp;
  304. break;
  305. case 1:
  306. if (*sp == '\'') /* state=5 */
  307. { /* replace \' with 39 */
  308. bp--;
  309. *bp = '\'';
  310. buflen--;
  311. state = 0;
  312. }
  313. else if (*sp == '\\') /* state=6 */
  314. { /* replace \\ with 92 */
  315. bp--;
  316. *bp = '\\';
  317. buflen--;
  318. state = 0;
  319. }
  320. else
  321. {
  322. if (isdigit(*sp))
  323. state = 2;
  324. else
  325. state = 0;
  326. *bp = *sp;
  327. }
  328. break;
  329. case 2:
  330. if (isdigit(*sp))
  331. state = 3;
  332. else
  333. state = 0;
  334. *bp = *sp;
  335. break;
  336. case 3:
  337. if (isdigit(*sp)) /* state=4 */
  338. {
  339. unsigned char *start, *end, buf[4]; /* 000 + '\0' */
  340. bp -= 3;
  341. memcpy(buf, sp-2, 3);
  342. buf[3] = '\0';
  343. start = buf;
  344. *bp = (unsigned char)strtoul(start, (char **)&end, 8);
  345. buflen -= 3;
  346. state = 0;
  347. }
  348. else
  349. {
  350. *bp = *sp;
  351. state = 0;
  352. }
  353. break;
  354. }
  355. }
  356. buffer = erealloc(buffer, buflen+1);
  357. buffer[buflen] = '\0';
  358. *retbuflen = buflen;
  359. return buffer;
  360. }
  361. static int pgsql_stmt_get_col(pdo_stmt_t *stmt, int colno, char **ptr, unsigned long *len, int *caller_frees TSRMLS_DC)
  362. {
  363. pdo_pgsql_stmt *S = (pdo_pgsql_stmt*)stmt->driver_data;
  364. struct pdo_column_data *cols = stmt->columns;
  365. size_t tmp_len;
  366. if (!S->result) {
  367. return 0;
  368. }
  369. /* We have already increased count by 1 in pgsql_stmt_fetch() */
  370. if (PQgetisnull(S->result, S->current_row - 1, colno)) { /* Check if we got NULL */
  371. *ptr = NULL;
  372. *len = 0;
  373. } else {
  374. *ptr = PQgetvalue(S->result, S->current_row - 1, colno);
  375. *len = PQgetlength(S->result, S->current_row - 1, colno);
  376. switch(cols[colno].param_type) {
  377. case PDO_PARAM_INT:
  378. S->cols[colno].intval = atol(*ptr);
  379. *ptr = (char *) &(S->cols[colno].intval);
  380. *len = sizeof(long);
  381. break;
  382. case PDO_PARAM_BOOL:
  383. S->cols[colno].boolval = **ptr == 't' ? 1: 0;
  384. *ptr = (char *) &(S->cols[colno].boolval);
  385. *len = sizeof(zend_bool);
  386. break;
  387. case PDO_PARAM_LOB:
  388. *ptr = php_pdo_pgsql_unescape_bytea(*ptr, &tmp_len);
  389. *len = tmp_len;
  390. *caller_frees = 1;
  391. break;
  392. case PDO_PARAM_NULL:
  393. case PDO_PARAM_STR:
  394. case PDO_PARAM_STMT:
  395. case PDO_PARAM_INPUT_OUTPUT:
  396. break;
  397. }
  398. }
  399. return 1;
  400. }
  401. static int pgsql_stmt_get_column_meta(pdo_stmt_t *stmt, long colno, zval *return_value TSRMLS_DC)
  402. {
  403. pdo_pgsql_stmt *S = (pdo_pgsql_stmt*)stmt->driver_data;
  404. PGresult *res;
  405. char *q=NULL;
  406. ExecStatusType status;
  407. if (!S->result) {
  408. return FAILURE;
  409. }
  410. if (colno >= stmt->column_count) {
  411. return FAILURE;
  412. }
  413. array_init(return_value);
  414. add_assoc_long(return_value, "pgsql:oid", S->cols[colno].pgsql_type);
  415. /* Fetch metadata from Postgres system catalogue */
  416. spprintf(&q, 0, "SELECT TYPNAME FROM PG_TYPE WHERE OID=%d", S->cols[colno].pgsql_type);
  417. res = PQexec(S->H->server, q);
  418. efree(q);
  419. status = PQresultStatus(res);
  420. if (status != PGRES_TUPLES_OK) {
  421. /* Failed to get system catalogue, but return success
  422. * with the data we have collected so far
  423. */
  424. PQclear(res);
  425. return 1;
  426. }
  427. /* We want exactly one row returned */
  428. if (1 != PQntuples(res)) {
  429. PQclear(res);
  430. return 1;
  431. }
  432. add_assoc_string(return_value, "native_type", PQgetvalue(res, 0, 0), 1);
  433. PQclear(res);
  434. return 1;
  435. }
  436. static int pdo_pgsql_stmt_cursor_closer(pdo_stmt_t *stmt TSRMLS_DC)
  437. {
  438. return 1;
  439. }
  440. struct pdo_stmt_methods pgsql_stmt_methods = {
  441. pgsql_stmt_dtor,
  442. pgsql_stmt_execute,
  443. pgsql_stmt_fetch,
  444. pgsql_stmt_describe,
  445. pgsql_stmt_get_col,
  446. pgsql_stmt_param_hook,
  447. NULL, /* set_attr */
  448. NULL, /* get_attr */
  449. pgsql_stmt_get_column_meta,
  450. NULL, /* next_rowset */
  451. pdo_pgsql_stmt_cursor_closer
  452. };
  453. /*
  454. * Local variables:
  455. * tab-width: 4
  456. * c-basic-offset: 4
  457. * End:
  458. * vim600: noet sw=4 ts=4 fdm=marker
  459. * vim<600: noet sw=4 ts=4
  460. */