pgsql_statement.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738
  1. /*
  2. +----------------------------------------------------------------------+
  3. | PHP Version 7 |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1997-2018 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. #ifdef HAVE_CONFIG_H
  21. #include "config.h"
  22. #endif
  23. #include "php.h"
  24. #include "php_ini.h"
  25. #include "ext/standard/info.h"
  26. #include "pdo/php_pdo.h"
  27. #include "pdo/php_pdo_driver.h"
  28. #include "php_pdo_pgsql.h"
  29. #include "php_pdo_pgsql_int.h"
  30. #if HAVE_NETINET_IN_H
  31. #include <netinet/in.h>
  32. #endif
  33. /* from postgresql/src/include/catalog/pg_type.h */
  34. #define BOOLLABEL "bool"
  35. #define BOOLOID 16
  36. #define BYTEALABEL "bytea"
  37. #define BYTEAOID 17
  38. #define DATELABEL "date"
  39. #define DATEOID 1082
  40. #define INT2LABEL "int2"
  41. #define INT2OID 21
  42. #define INT4LABEL "int4"
  43. #define INT4OID 23
  44. #define INT8LABEL "int8"
  45. #define INT8OID 20
  46. #define OIDOID 26
  47. #define TEXTLABEL "text"
  48. #define TEXTOID 25
  49. #define TIMESTAMPLABEL "timestamp"
  50. #define TIMESTAMPOID 1114
  51. #define VARCHARLABEL "varchar"
  52. #define VARCHAROID 1043
  53. static int pgsql_stmt_dtor(pdo_stmt_t *stmt)
  54. {
  55. pdo_pgsql_stmt *S = (pdo_pgsql_stmt*)stmt->driver_data;
  56. zend_bool server_obj_usable = !Z_ISUNDEF(stmt->database_object_handle)
  57. && IS_OBJ_VALID(EG(objects_store).object_buckets[Z_OBJ_HANDLE(stmt->database_object_handle)])
  58. && !(OBJ_FLAGS(Z_OBJ(stmt->database_object_handle)) & IS_OBJ_FREE_CALLED);
  59. if (S->result) {
  60. /* free the resource */
  61. PQclear(S->result);
  62. S->result = NULL;
  63. }
  64. if (S->stmt_name) {
  65. if (S->is_prepared && server_obj_usable) {
  66. pdo_pgsql_db_handle *H = S->H;
  67. char *q = NULL;
  68. PGresult *res;
  69. spprintf(&q, 0, "DEALLOCATE %s", S->stmt_name);
  70. res = PQexec(H->server, q);
  71. efree(q);
  72. if (res) {
  73. PQclear(res);
  74. }
  75. }
  76. efree(S->stmt_name);
  77. S->stmt_name = NULL;
  78. }
  79. if (S->param_lengths) {
  80. efree(S->param_lengths);
  81. S->param_lengths = NULL;
  82. }
  83. if (S->param_values) {
  84. efree(S->param_values);
  85. S->param_values = NULL;
  86. }
  87. if (S->param_formats) {
  88. efree(S->param_formats);
  89. S->param_formats = NULL;
  90. }
  91. if (S->param_types) {
  92. efree(S->param_types);
  93. S->param_types = NULL;
  94. }
  95. if (S->query) {
  96. efree(S->query);
  97. S->query = NULL;
  98. }
  99. if (S->cursor_name) {
  100. if (server_obj_usable) {
  101. pdo_pgsql_db_handle *H = S->H;
  102. char *q = NULL;
  103. PGresult *res;
  104. spprintf(&q, 0, "CLOSE %s", S->cursor_name);
  105. res = PQexec(H->server, q);
  106. efree(q);
  107. if (res) PQclear(res);
  108. }
  109. efree(S->cursor_name);
  110. S->cursor_name = NULL;
  111. }
  112. if(S->cols) {
  113. efree(S->cols);
  114. S->cols = NULL;
  115. }
  116. efree(S);
  117. stmt->driver_data = NULL;
  118. return 1;
  119. }
  120. static int pgsql_stmt_execute(pdo_stmt_t *stmt)
  121. {
  122. pdo_pgsql_stmt *S = (pdo_pgsql_stmt*)stmt->driver_data;
  123. pdo_pgsql_db_handle *H = S->H;
  124. ExecStatusType status;
  125. /* ensure that we free any previous unfetched results */
  126. if(S->result) {
  127. PQclear(S->result);
  128. S->result = NULL;
  129. }
  130. S->current_row = 0;
  131. if (S->cursor_name) {
  132. char *q = NULL;
  133. if (S->is_prepared) {
  134. spprintf(&q, 0, "CLOSE %s", S->cursor_name);
  135. S->result = PQexec(H->server, q);
  136. efree(q);
  137. }
  138. spprintf(&q, 0, "DECLARE %s SCROLL CURSOR WITH HOLD FOR %s", S->cursor_name, stmt->active_query_string);
  139. S->result = PQexec(H->server, q);
  140. efree(q);
  141. /* check if declare failed */
  142. status = PQresultStatus(S->result);
  143. if (status != PGRES_COMMAND_OK && status != PGRES_TUPLES_OK) {
  144. pdo_pgsql_error_stmt(stmt, status, pdo_pgsql_sqlstate(S->result));
  145. return 0;
  146. }
  147. PQclear(S->result);
  148. /* the cursor was declared correctly */
  149. S->is_prepared = 1;
  150. /* fetch to be able to get the number of tuples later, but don't advance the cursor pointer */
  151. spprintf(&q, 0, "FETCH FORWARD 0 FROM %s", S->cursor_name);
  152. S->result = PQexec(H->server, q);
  153. efree(q);
  154. } else if (S->stmt_name) {
  155. /* using a prepared statement */
  156. if (!S->is_prepared) {
  157. stmt_retry:
  158. /* we deferred the prepare until now, because we didn't
  159. * know anything about the parameter types; now we do */
  160. S->result = PQprepare(H->server, S->stmt_name, S->query,
  161. stmt->bound_params ? zend_hash_num_elements(stmt->bound_params) : 0,
  162. S->param_types);
  163. status = PQresultStatus(S->result);
  164. switch (status) {
  165. case PGRES_COMMAND_OK:
  166. case PGRES_TUPLES_OK:
  167. /* it worked */
  168. S->is_prepared = 1;
  169. PQclear(S->result);
  170. break;
  171. default: {
  172. char *sqlstate = pdo_pgsql_sqlstate(S->result);
  173. /* 42P05 means that the prepared statement already existed. this can happen if you use
  174. * a connection pooling software line pgpool which doesn't close the db-connection once
  175. * php disconnects. if php dies (no chance to run RSHUTDOWN) during execution it has no
  176. * chance to DEALLOCATE the prepared statements it has created. so, if we hit a 42P05 we
  177. * deallocate it and retry ONCE (thies 2005.12.15)
  178. */
  179. if (sqlstate && !strcmp(sqlstate, "42P05")) {
  180. char buf[100]; /* stmt_name == "pdo_crsr_%08x" */
  181. PGresult *res;
  182. snprintf(buf, sizeof(buf), "DEALLOCATE %s", S->stmt_name);
  183. res = PQexec(H->server, buf);
  184. if (res) {
  185. PQclear(res);
  186. }
  187. goto stmt_retry;
  188. } else {
  189. pdo_pgsql_error_stmt(stmt, status, sqlstate);
  190. return 0;
  191. }
  192. }
  193. }
  194. }
  195. S->result = PQexecPrepared(H->server, S->stmt_name,
  196. stmt->bound_params ?
  197. zend_hash_num_elements(stmt->bound_params) :
  198. 0,
  199. (const char**)S->param_values,
  200. S->param_lengths,
  201. S->param_formats,
  202. 0);
  203. } else if (stmt->supports_placeholders == PDO_PLACEHOLDER_NAMED) {
  204. /* execute query with parameters */
  205. S->result = PQexecParams(H->server, S->query,
  206. stmt->bound_params ? zend_hash_num_elements(stmt->bound_params) : 0,
  207. S->param_types,
  208. (const char**)S->param_values,
  209. S->param_lengths,
  210. S->param_formats,
  211. 0);
  212. } else {
  213. /* execute plain query (with embedded parameters) */
  214. S->result = PQexec(H->server, stmt->active_query_string);
  215. }
  216. status = PQresultStatus(S->result);
  217. if (status != PGRES_COMMAND_OK && status != PGRES_TUPLES_OK) {
  218. pdo_pgsql_error_stmt(stmt, status, pdo_pgsql_sqlstate(S->result));
  219. return 0;
  220. }
  221. if (!stmt->executed && (!stmt->column_count || S->cols == NULL)) {
  222. stmt->column_count = (int) PQnfields(S->result);
  223. S->cols = ecalloc(stmt->column_count, sizeof(pdo_pgsql_column));
  224. }
  225. if (status == PGRES_COMMAND_OK) {
  226. ZEND_ATOL(stmt->row_count, PQcmdTuples(S->result));
  227. H->pgoid = PQoidValue(S->result);
  228. } else {
  229. stmt->row_count = (zend_long)PQntuples(S->result);
  230. }
  231. return 1;
  232. }
  233. static int pgsql_stmt_param_hook(pdo_stmt_t *stmt, struct pdo_bound_param_data *param,
  234. enum pdo_param_event event_type)
  235. {
  236. pdo_pgsql_stmt *S = (pdo_pgsql_stmt*)stmt->driver_data;
  237. if (stmt->supports_placeholders == PDO_PLACEHOLDER_NAMED && param->is_param) {
  238. switch (event_type) {
  239. case PDO_PARAM_EVT_FREE:
  240. if (param->driver_data) {
  241. efree(param->driver_data);
  242. }
  243. break;
  244. case PDO_PARAM_EVT_NORMALIZE:
  245. /* decode name from $1, $2 into 0, 1 etc. */
  246. if (param->name) {
  247. if (ZSTR_VAL(param->name)[0] == '$') {
  248. ZEND_ATOL(param->paramno, ZSTR_VAL(param->name) + 1);
  249. } else {
  250. /* resolve parameter name to rewritten name */
  251. char *namevar;
  252. if (stmt->bound_param_map && (namevar = zend_hash_find_ptr(stmt->bound_param_map,
  253. param->name)) != NULL) {
  254. ZEND_ATOL(param->paramno, namevar + 1);
  255. param->paramno--;
  256. } else {
  257. pdo_raise_impl_error(stmt->dbh, stmt, "HY093", ZSTR_VAL(param->name));
  258. return 0;
  259. }
  260. }
  261. }
  262. break;
  263. case PDO_PARAM_EVT_ALLOC:
  264. if (!stmt->bound_param_map) {
  265. return 1;
  266. }
  267. if (!zend_hash_index_exists(stmt->bound_param_map, param->paramno)) {
  268. pdo_raise_impl_error(stmt->dbh, stmt, "HY093", "parameter was not defined");
  269. return 0;
  270. }
  271. case PDO_PARAM_EVT_EXEC_POST:
  272. case PDO_PARAM_EVT_FETCH_PRE:
  273. case PDO_PARAM_EVT_FETCH_POST:
  274. /* work is handled by EVT_NORMALIZE */
  275. return 1;
  276. case PDO_PARAM_EVT_EXEC_PRE:
  277. if (!stmt->bound_param_map) {
  278. return 1;
  279. }
  280. if (!S->param_values) {
  281. S->param_values = ecalloc(
  282. zend_hash_num_elements(stmt->bound_param_map),
  283. sizeof(char*));
  284. S->param_lengths = ecalloc(
  285. zend_hash_num_elements(stmt->bound_param_map),
  286. sizeof(int));
  287. S->param_formats = ecalloc(
  288. zend_hash_num_elements(stmt->bound_param_map),
  289. sizeof(int));
  290. S->param_types = ecalloc(
  291. zend_hash_num_elements(stmt->bound_param_map),
  292. sizeof(Oid));
  293. }
  294. if (param->paramno >= 0) {
  295. zval *parameter;
  296. /*
  297. if (param->paramno >= zend_hash_num_elements(stmt->bound_params)) {
  298. pdo_raise_impl_error(stmt->dbh, stmt, "HY093", "parameter was not defined");
  299. return 0;
  300. }
  301. */
  302. if (Z_ISREF(param->parameter)) {
  303. parameter = Z_REFVAL(param->parameter);
  304. } else {
  305. parameter = &param->parameter;
  306. }
  307. if (PDO_PARAM_TYPE(param->param_type) == PDO_PARAM_LOB &&
  308. Z_TYPE_P(parameter) == IS_RESOURCE) {
  309. php_stream *stm;
  310. php_stream_from_zval_no_verify(stm, parameter);
  311. if (stm) {
  312. if (php_stream_is(stm, &pdo_pgsql_lob_stream_ops)) {
  313. struct pdo_pgsql_lob_self *self = (struct pdo_pgsql_lob_self*)stm->abstract;
  314. pdo_pgsql_bound_param *P = param->driver_data;
  315. if (P == NULL) {
  316. P = ecalloc(1, sizeof(*P));
  317. param->driver_data = P;
  318. }
  319. P->oid = htonl(self->oid);
  320. S->param_values[param->paramno] = (char*)&P->oid;
  321. S->param_lengths[param->paramno] = sizeof(P->oid);
  322. S->param_formats[param->paramno] = 1;
  323. S->param_types[param->paramno] = OIDOID;
  324. return 1;
  325. } else {
  326. zend_string *str = php_stream_copy_to_mem(stm, PHP_STREAM_COPY_ALL, 0);
  327. if (str != NULL) {
  328. //??SEPARATE_ZVAL_IF_NOT_REF(&param->parameter);
  329. ZVAL_STR(parameter, str);
  330. } else {
  331. ZVAL_EMPTY_STRING(parameter);
  332. }
  333. }
  334. } else {
  335. /* expected a stream resource */
  336. pdo_pgsql_error_stmt(stmt, PGRES_FATAL_ERROR, "HY105");
  337. return 0;
  338. }
  339. }
  340. if (PDO_PARAM_TYPE(param->param_type) == PDO_PARAM_NULL ||
  341. Z_TYPE_P(parameter) == IS_NULL) {
  342. S->param_values[param->paramno] = NULL;
  343. S->param_lengths[param->paramno] = 0;
  344. } else if (Z_TYPE_P(parameter) == IS_FALSE || Z_TYPE_P(parameter) == IS_TRUE) {
  345. S->param_values[param->paramno] = Z_TYPE_P(parameter) == IS_TRUE ? "t" : "f";
  346. S->param_lengths[param->paramno] = 1;
  347. S->param_formats[param->paramno] = 0;
  348. } else {
  349. //SEPARATE_ZVAL_IF_NOT_REF(&param->parameter);
  350. convert_to_string_ex(parameter);
  351. S->param_values[param->paramno] = Z_STRVAL_P(parameter);
  352. S->param_lengths[param->paramno] = Z_STRLEN_P(parameter);
  353. S->param_formats[param->paramno] = 0;
  354. }
  355. if (PDO_PARAM_TYPE(param->param_type) == PDO_PARAM_LOB) {
  356. S->param_types[param->paramno] = 0;
  357. S->param_formats[param->paramno] = 1;
  358. } else {
  359. S->param_types[param->paramno] = 0;
  360. }
  361. }
  362. break;
  363. }
  364. } else if (param->is_param && event_type == PDO_PARAM_EVT_NORMALIZE) {
  365. /* We need to manually convert to a pg native boolean value */
  366. if (PDO_PARAM_TYPE(param->param_type) == PDO_PARAM_BOOL &&
  367. ((param->param_type & PDO_PARAM_INPUT_OUTPUT) != PDO_PARAM_INPUT_OUTPUT)) {
  368. const char *s = zend_is_true(&param->parameter) ? "t" : "f";
  369. param->param_type = PDO_PARAM_STR;
  370. zval_ptr_dtor(&param->parameter);
  371. ZVAL_STRINGL(&param->parameter, s, 1);
  372. }
  373. }
  374. return 1;
  375. }
  376. static int pgsql_stmt_fetch(pdo_stmt_t *stmt,
  377. enum pdo_fetch_orientation ori, zend_long offset)
  378. {
  379. pdo_pgsql_stmt *S = (pdo_pgsql_stmt*)stmt->driver_data;
  380. if (S->cursor_name) {
  381. char *ori_str = NULL;
  382. char *q = NULL;
  383. ExecStatusType status;
  384. switch (ori) {
  385. case PDO_FETCH_ORI_NEXT: spprintf(&ori_str, 0, "NEXT"); break;
  386. case PDO_FETCH_ORI_PRIOR: spprintf(&ori_str, 0, "BACKWARD"); break;
  387. case PDO_FETCH_ORI_FIRST: spprintf(&ori_str, 0, "FIRST"); break;
  388. case PDO_FETCH_ORI_LAST: spprintf(&ori_str, 0, "LAST"); break;
  389. case PDO_FETCH_ORI_ABS: spprintf(&ori_str, 0, "ABSOLUTE " ZEND_LONG_FMT, offset); break;
  390. case PDO_FETCH_ORI_REL: spprintf(&ori_str, 0, "RELATIVE " ZEND_LONG_FMT, offset); break;
  391. default:
  392. return 0;
  393. }
  394. if(S->result) {
  395. PQclear(S->result);
  396. S->result = NULL;
  397. }
  398. spprintf(&q, 0, "FETCH %s FROM %s", ori_str, S->cursor_name);
  399. efree(ori_str);
  400. S->result = PQexec(S->H->server, q);
  401. efree(q);
  402. status = PQresultStatus(S->result);
  403. if (status != PGRES_COMMAND_OK && status != PGRES_TUPLES_OK) {
  404. pdo_pgsql_error_stmt(stmt, status, pdo_pgsql_sqlstate(S->result));
  405. return 0;
  406. }
  407. if (PQntuples(S->result)) {
  408. S->current_row = 1;
  409. return 1;
  410. } else {
  411. return 0;
  412. }
  413. } else {
  414. if (S->current_row < stmt->row_count) {
  415. S->current_row++;
  416. return 1;
  417. } else {
  418. return 0;
  419. }
  420. }
  421. }
  422. static int pgsql_stmt_describe(pdo_stmt_t *stmt, int colno)
  423. {
  424. pdo_pgsql_stmt *S = (pdo_pgsql_stmt*)stmt->driver_data;
  425. struct pdo_column_data *cols = stmt->columns;
  426. struct pdo_bound_param_data *param;
  427. char *str;
  428. if (!S->result) {
  429. return 0;
  430. }
  431. str = PQfname(S->result, colno);
  432. cols[colno].name = zend_string_init(str, strlen(str), 0);
  433. cols[colno].maxlen = PQfsize(S->result, colno);
  434. cols[colno].precision = PQfmod(S->result, colno);
  435. S->cols[colno].pgsql_type = PQftype(S->result, colno);
  436. switch (S->cols[colno].pgsql_type) {
  437. case BOOLOID:
  438. cols[colno].param_type = PDO_PARAM_BOOL;
  439. break;
  440. case OIDOID:
  441. /* did the user bind the column as a LOB ? */
  442. if (stmt->bound_columns && (
  443. (param = zend_hash_index_find_ptr(stmt->bound_columns, colno)) != NULL ||
  444. (param = zend_hash_find_ptr(stmt->bound_columns, cols[colno].name)) != NULL)) {
  445. if (PDO_PARAM_TYPE(param->param_type) == PDO_PARAM_LOB) {
  446. cols[colno].param_type = PDO_PARAM_LOB;
  447. break;
  448. }
  449. }
  450. cols[colno].param_type = PDO_PARAM_INT;
  451. break;
  452. case INT2OID:
  453. case INT4OID:
  454. cols[colno].param_type = PDO_PARAM_INT;
  455. break;
  456. case INT8OID:
  457. if (sizeof(zend_long)>=8) {
  458. cols[colno].param_type = PDO_PARAM_INT;
  459. } else {
  460. cols[colno].param_type = PDO_PARAM_STR;
  461. }
  462. break;
  463. case BYTEAOID:
  464. cols[colno].param_type = PDO_PARAM_LOB;
  465. break;
  466. default:
  467. cols[colno].param_type = PDO_PARAM_STR;
  468. }
  469. return 1;
  470. }
  471. static int pgsql_stmt_get_col(pdo_stmt_t *stmt, int colno, char **ptr, zend_ulong *len, int *caller_frees )
  472. {
  473. pdo_pgsql_stmt *S = (pdo_pgsql_stmt*)stmt->driver_data;
  474. struct pdo_column_data *cols = stmt->columns;
  475. size_t tmp_len;
  476. if (!S->result) {
  477. return 0;
  478. }
  479. /* We have already increased count by 1 in pgsql_stmt_fetch() */
  480. if (PQgetisnull(S->result, S->current_row - 1, colno)) { /* Check if we got NULL */
  481. *ptr = NULL;
  482. *len = 0;
  483. } else {
  484. *ptr = PQgetvalue(S->result, S->current_row - 1, colno);
  485. *len = PQgetlength(S->result, S->current_row - 1, colno);
  486. switch (cols[colno].param_type) {
  487. case PDO_PARAM_INT:
  488. ZEND_ATOL(S->cols[colno].intval, *ptr);
  489. *ptr = (char *) &(S->cols[colno].intval);
  490. *len = sizeof(zend_long);
  491. break;
  492. case PDO_PARAM_BOOL:
  493. S->cols[colno].boolval = **ptr == 't' ? 1: 0;
  494. *ptr = (char *) &(S->cols[colno].boolval);
  495. *len = sizeof(zend_bool);
  496. break;
  497. case PDO_PARAM_LOB:
  498. if (S->cols[colno].pgsql_type == OIDOID) {
  499. /* ooo, a real large object */
  500. char *end_ptr;
  501. Oid oid = (Oid)strtoul(*ptr, &end_ptr, 10);
  502. int loid = lo_open(S->H->server, oid, INV_READ);
  503. if (loid >= 0) {
  504. *ptr = (char*)pdo_pgsql_create_lob_stream(&stmt->database_object_handle, loid, oid);
  505. *len = 0;
  506. return *ptr ? 1 : 0;
  507. }
  508. *ptr = NULL;
  509. *len = 0;
  510. return 0;
  511. } else {
  512. char *tmp_ptr = (char *)PQunescapeBytea((unsigned char *)*ptr, &tmp_len);
  513. if (!tmp_ptr) {
  514. /* PQunescapeBytea returned an error */
  515. *len = 0;
  516. return 0;
  517. }
  518. if (!tmp_len) {
  519. /* Empty string, return as empty stream */
  520. *ptr = (char *)php_stream_memory_open(TEMP_STREAM_READONLY, "", 0);
  521. PQfreemem(tmp_ptr);
  522. *len = 0;
  523. } else {
  524. *ptr = estrndup(tmp_ptr, tmp_len);
  525. PQfreemem(tmp_ptr);
  526. *len = tmp_len;
  527. *caller_frees = 1;
  528. }
  529. }
  530. break;
  531. case PDO_PARAM_NULL:
  532. case PDO_PARAM_STR:
  533. case PDO_PARAM_STMT:
  534. case PDO_PARAM_INPUT_OUTPUT:
  535. case PDO_PARAM_ZVAL:
  536. default:
  537. break;
  538. }
  539. }
  540. return 1;
  541. }
  542. static zend_always_inline char * pdo_pgsql_translate_oid_to_table(Oid oid, PGconn *conn)
  543. {
  544. char *table_name = NULL;
  545. PGresult *tmp_res;
  546. char *querystr = NULL;
  547. spprintf(&querystr, 0, "SELECT RELNAME FROM PG_CLASS WHERE OID=%d", oid);
  548. if ((tmp_res = PQexec(conn, querystr)) == NULL || PQresultStatus(tmp_res) != PGRES_TUPLES_OK) {
  549. if (tmp_res) {
  550. PQclear(tmp_res);
  551. }
  552. efree(querystr);
  553. return 0;
  554. }
  555. efree(querystr);
  556. if (1 == PQgetisnull(tmp_res, 0, 0) || (table_name = PQgetvalue(tmp_res, 0, 0)) == NULL) {
  557. PQclear(tmp_res);
  558. return 0;
  559. }
  560. table_name = estrdup(table_name);
  561. PQclear(tmp_res);
  562. return table_name;
  563. }
  564. static int pgsql_stmt_get_column_meta(pdo_stmt_t *stmt, zend_long colno, zval *return_value)
  565. {
  566. pdo_pgsql_stmt *S = (pdo_pgsql_stmt*)stmt->driver_data;
  567. PGresult *res;
  568. char *q=NULL;
  569. ExecStatusType status;
  570. Oid table_oid;
  571. char *table_name=NULL;
  572. if (!S->result) {
  573. return FAILURE;
  574. }
  575. if (colno >= stmt->column_count) {
  576. return FAILURE;
  577. }
  578. array_init(return_value);
  579. add_assoc_long(return_value, "pgsql:oid", S->cols[colno].pgsql_type);
  580. table_oid = PQftable(S->result, colno);
  581. add_assoc_long(return_value, "pgsql:table_oid", table_oid);
  582. table_name = pdo_pgsql_translate_oid_to_table(table_oid, S->H->server);
  583. if (table_name) {
  584. add_assoc_string(return_value, "table", table_name);
  585. efree(table_name);
  586. }
  587. switch (S->cols[colno].pgsql_type) {
  588. case BOOLOID:
  589. add_assoc_string(return_value, "native_type", BOOLLABEL);
  590. break;
  591. case BYTEAOID:
  592. add_assoc_string(return_value, "native_type", BYTEALABEL);
  593. break;
  594. case INT8OID:
  595. add_assoc_string(return_value, "native_type", INT8LABEL);
  596. break;
  597. case INT2OID:
  598. add_assoc_string(return_value, "native_type", INT2LABEL);
  599. break;
  600. case INT4OID:
  601. add_assoc_string(return_value, "native_type", INT4LABEL);
  602. break;
  603. case TEXTOID:
  604. add_assoc_string(return_value, "native_type", TEXTLABEL);
  605. break;
  606. case VARCHAROID:
  607. add_assoc_string(return_value, "native_type", VARCHARLABEL);
  608. break;
  609. case DATEOID:
  610. add_assoc_string(return_value, "native_type", DATELABEL);
  611. break;
  612. case TIMESTAMPOID:
  613. add_assoc_string(return_value, "native_type", TIMESTAMPLABEL);
  614. break;
  615. default:
  616. /* Fetch metadata from Postgres system catalogue */
  617. spprintf(&q, 0, "SELECT TYPNAME FROM PG_TYPE WHERE OID=%u", S->cols[colno].pgsql_type);
  618. res = PQexec(S->H->server, q);
  619. efree(q);
  620. status = PQresultStatus(res);
  621. if (status == PGRES_TUPLES_OK && 1 == PQntuples(res)) {
  622. add_assoc_string(return_value, "native_type", PQgetvalue(res, 0, 0));
  623. }
  624. PQclear(res);
  625. }
  626. return 1;
  627. }
  628. static int pdo_pgsql_stmt_cursor_closer(pdo_stmt_t *stmt)
  629. {
  630. pdo_pgsql_stmt *S = (pdo_pgsql_stmt*)stmt->driver_data;
  631. if (S->cols != NULL){
  632. efree(S->cols);
  633. S->cols = NULL;
  634. }
  635. return 1;
  636. }
  637. const struct pdo_stmt_methods pgsql_stmt_methods = {
  638. pgsql_stmt_dtor,
  639. pgsql_stmt_execute,
  640. pgsql_stmt_fetch,
  641. pgsql_stmt_describe,
  642. pgsql_stmt_get_col,
  643. pgsql_stmt_param_hook,
  644. NULL, /* set_attr */
  645. NULL, /* get_attr */
  646. pgsql_stmt_get_column_meta,
  647. NULL, /* next_rowset */
  648. pdo_pgsql_stmt_cursor_closer
  649. };
  650. /*
  651. * Local variables:
  652. * tab-width: 4
  653. * c-basic-offset: 4
  654. * End:
  655. * vim600: noet sw=4 ts=4 fdm=marker
  656. * vim<600: noet sw=4 ts=4
  657. */