Skip to content
This repository was archived by the owner on Sep 28, 2022. It is now read-only.

Commit 73dbcbc

Browse files
authored
Fix query results without field names (#61)
1 parent 8aa7157 commit 73dbcbc

File tree

2 files changed

+31
-8
lines changed

2 files changed

+31
-8
lines changed

swoole_postgresql_coro.cc

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1044,8 +1044,8 @@ static inline void php_pgsql_get_field_value(
10441044
*/
10451045
int swoole_pgsql_result2array(PGresult *pg_result, zval *ret_array, long result_type) {
10461046
zval row;
1047-
char *field_name;
1048-
size_t num_fields;
1047+
const char *field_name;
1048+
size_t num_fields, unknown_columns;
10491049
int pg_numrows, pg_row;
10501050
uint32_t i;
10511051
assert(Z_TYPE_P(ret_array) == IS_ARRAY);
@@ -1055,11 +1055,18 @@ int swoole_pgsql_result2array(PGresult *pg_result, zval *ret_array, long result_
10551055
}
10561056
for (pg_row = 0; pg_row < pg_numrows; pg_row++) {
10571057
array_init(&row);
1058+
unknown_columns = 0;
10581059
for (i = 0, num_fields = PQnfields(pg_result); i < num_fields; i++) {
10591060
if (result_type & PGSQL_ASSOC) {
10601061
zval value;
10611062
php_pgsql_get_field_value(&value, pg_result, result_type, pg_row, i);
10621063
field_name = PQfname(pg_result, i);
1064+
if (0 == strcmp("?column?", field_name)) {
1065+
if (unknown_columns > 0) {
1066+
field_name = (std::string(field_name) + std::to_string(unknown_columns)).c_str();
1067+
}
1068+
++unknown_columns;
1069+
}
10631070
add_assoc_zval(&row, field_name, &value);
10641071
}
10651072
if (result_type & PGSQL_NUM) {

tests/unit/PostgreSQLTest.php

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
<?php
22

3+
use PHPUnit\Framework\TestCase;
34
use Swoole\Coroutine\PostgreSQL;
45
use function Swoole\Coroutine\run;
5-
use PHPUnit\Framework\TestCase;
66

77
class PostgreSQLTest extends TestCase
88
{
9-
protected function getConn() {
9+
protected function getConn()
10+
{
1011
$pg = new Swoole\Coroutine\PostgreSQL();
1112
$conn = $pg->connect(TEST_DB_URI);
12-
$this->assertNotFalse($conn, (string)$pg->error);
13+
$this->assertNotFalse($conn, (string) $pg->error);
14+
1315
return $pg;
1416
}
1517

@@ -18,7 +20,7 @@ public function testEscape()
1820
run(function () {
1921
$pg = $this->getConn();
2022
$result = $pg->escape("' or 1=1 & 2");
21-
$this->assertNotFalse($result, (string)$pg->error);
23+
$this->assertNotFalse($result, (string) $pg->error);
2224
$this->assertEquals("'' or 1=1 & 2", $result);
2325
});
2426
}
@@ -29,7 +31,7 @@ public function testInsert()
2931
$pg = $this->getConn();
3032
$result = $pg->query("INSERT INTO weather(city, temp_lo, temp_hi, prcp, date)
3133
VALUES ('Shanghai', 88, 10, 0.75,'1993-11-27') RETURNING id");
32-
$this->assertNotFalse($result, (string)$pg->error);
34+
$this->assertNotFalse($result, (string) $pg->error);
3335
$this->assertEquals(1, $pg->numRows($result));
3436
$this->assertGreaterThan(1, $pg->fetchAssoc($result)['id']);
3537
});
@@ -40,11 +42,25 @@ public function testQuery()
4042
run(function () {
4143
$pg = $this->getConn();
4244
$result = $pg->query('SELECT * FROM weather;');
43-
$this->assertNotFalse($result, (string)$pg->error);
45+
$this->assertNotFalse($result, (string) $pg->error);
4446

4547
$arr = $pg->fetchAll($result);
4648
$this->assertIsArray($arr);
4749
$this->assertEquals($arr[0]['city'], 'San Francisco');
4850
});
4951
}
52+
53+
public function testNoFieldName()
54+
{
55+
run(function () {
56+
$pg = $this->getConn();
57+
$result = $pg->query('SELECT 11, 22');
58+
$this->assertNotFalse($result, (string) $pg->error);
59+
60+
$arr = $pg->fetchAll($result);
61+
$this->assertIsArray($arr);
62+
$this->assertEquals($arr[0]['?column?'], 11);
63+
$this->assertEquals($arr[0]['?column?1'], 22);
64+
});
65+
}
5066
}

0 commit comments

Comments
 (0)