pgsql_statement.c 19 KB

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