#endif
/* from postgresql/src/include/catalog/pg_type.h */
+#define BOOLLABEL "bool"
#define BOOLOID 16
+#define BYTEALABEL "bytea"
#define BYTEAOID 17
-#define INT8OID 20
+#define DATELABEL "date"
+#define DATEOID 1082
+#define INT2LABEL "int2"
#define INT2OID 21
+#define INT4LABEL "int4"
#define INT4OID 23
-#define TEXTOID 25
+#define INT8LABEL "int8"
+#define INT8OID 20
#define OIDOID 26
+#define TEXTLABEL "text"
+#define TEXTOID 25
+#define TIMESTAMPLABEL "timestamp"
+#define TIMESTAMPOID 1114
+#define VARCHARLABEL "varchar"
+#define VARCHAROID 1043
+
+
static int pgsql_stmt_dtor(pdo_stmt_t *stmt)
{
array_init(return_value);
add_assoc_long(return_value, "pgsql:oid", S->cols[colno].pgsql_type);
- /* Fetch metadata from Postgres system catalogue */
- spprintf(&q, 0, "SELECT TYPNAME FROM PG_TYPE WHERE OID=%u", S->cols[colno].pgsql_type);
- res = PQexec(S->H->server, q);
- efree(q);
-
- status = PQresultStatus(res);
-
- if (status != PGRES_TUPLES_OK) {
- /* Failed to get system catalogue, but return success
- * with the data we have collected so far
- */
- goto done;
- }
-
- /* We want exactly one row returned */
- if (1 != PQntuples(res)) {
- goto done;
- }
-
- add_assoc_string(return_value, "native_type", PQgetvalue(res, 0, 0));
-done:
- PQclear(res);
- return 1;
+ switch (S->cols[colno].pgsql_type) {
+ case BOOLOID:
+ add_assoc_string(return_value, "native_type", BOOLLABEL);
+ break;
+ case BYTEAOID:
+ add_assoc_string(return_value, "native_type", BYTEALABEL);
+ break;
+ case INT8OID:
+ add_assoc_string(return_value, "native_type", INT8LABEL);
+ break;
+ case INT2OID:
+ add_assoc_string(return_value, "native_type", INT2LABEL);
+ break;
+ case INT4OID:
+ add_assoc_string(return_value, "native_type", INT4LABEL);
+ break;
+ case TEXTOID:
+ add_assoc_string(return_value, "native_type", TEXTLABEL);
+ break;
+ case VARCHAROID:
+ add_assoc_string(return_value, "native_type", VARCHARLABEL);
+ break;
+ case DATEOID:
+ add_assoc_string(return_value, "native_type", DATELABEL);
+ break;
+ case TIMESTAMPOID:
+ add_assoc_string(return_value, "native_type", TIMESTAMPLABEL);
+ break;
+ default:
+ /* Fetch metadata from Postgres system catalogue */
+ spprintf(&q, 0, "SELECT TYPNAME FROM PG_TYPE WHERE OID=%u", S->cols[colno].pgsql_type);
+ res = PQexec(S->H->server, q);
+ efree(q);
+ status = PQresultStatus(res);
+ if (status == PGRES_TUPLES_OK && 1 == PQntuples(res)) {
+ add_assoc_string(return_value, "native_type", PQgetvalue(res, 0, 0));
+ }
+ PQclear(res);
+ }
+ return 1;
}
static int pdo_pgsql_stmt_cursor_closer(pdo_stmt_t *stmt)
--- /dev/null
+--TEST--
+PDO PgSQL Bug #62498 (pdo_pgsql inefficient when getColumnMeta() is used)
+--SKIPIF--
+<?php
+if (!extension_loaded('pdo') || !extension_loaded('pdo_pgsql')) die('skip not loaded');
+require dirname(__FILE__) . '/config.inc';
+require dirname(__FILE__) . '/../../../ext/pdo/tests/pdo_test.inc';
+PDOTest::skip();
+?>
+--FILE--
+<?php
+echo "Begin test...\n";
+
+require dirname(__FILE__) . '/../../../ext/pdo/tests/pdo_test.inc';
+$db = PDOTest::test_factory(dirname(__FILE__) . '/common.phpt');
+$db->setAttribute (\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
+
+// create the table
+$db->exec("CREATE TEMPORARY TABLE bugtest_62498 (intcol INTEGER, stringcol VARCHAR(255), boolcol BOOLEAN, datecol DATE)");
+
+// insert some data
+$statement = $db->prepare("INSERT INTO bugtest_62498 (intcol, stringcol, boolcol, datecol) VALUES (:intval, :stringval, :boolval, :dateval)");
+$statement->execute(array(
+ "intval" => "42",
+ "stringval" => "The Answer",
+ "boolval" => true,
+ "dateval" => '2015-12-14',
+));
+
+$select = $db->query('SELECT intcol, stringcol, boolcol, datecol FROM bugtest_62498');
+$meta = [];
+for ($i=0; $i < 4; $i++) {
+ $meta[] = $select->getColumnMeta(0);
+}
+var_dump($meta);
+
+?>
+Done
+--EXPECT--
+Begin test...
+array(4) {
+ [0]=>
+ array(6) {
+ ["pgsql:oid"]=>
+ int(23)
+ ["native_type"]=>
+ string(4) "int4"
+ ["name"]=>
+ string(6) "intcol"
+ ["len"]=>
+ int(4)
+ ["precision"]=>
+ int(-1)
+ ["pdo_type"]=>
+ int(1)
+ }
+ [1]=>
+ array(6) {
+ ["pgsql:oid"]=>
+ int(23)
+ ["native_type"]=>
+ string(4) "int4"
+ ["name"]=>
+ string(6) "intcol"
+ ["len"]=>
+ int(4)
+ ["precision"]=>
+ int(-1)
+ ["pdo_type"]=>
+ int(1)
+ }
+ [2]=>
+ array(6) {
+ ["pgsql:oid"]=>
+ int(23)
+ ["native_type"]=>
+ string(4) "int4"
+ ["name"]=>
+ string(6) "intcol"
+ ["len"]=>
+ int(4)
+ ["precision"]=>
+ int(-1)
+ ["pdo_type"]=>
+ int(1)
+ }
+ [3]=>
+ array(6) {
+ ["pgsql:oid"]=>
+ int(23)
+ ["native_type"]=>
+ string(4) "int4"
+ ["name"]=>
+ string(6) "intcol"
+ ["len"]=>
+ int(4)
+ ["precision"]=>
+ int(-1)
+ ["pdo_type"]=>
+ int(1)
+ }
+}
+Done