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

Commit 1518395

Browse files
committed
Merge branch 'master' of github.com:swoole/ext-postgresql
2 parents dbab4b1 + 73dbcbc commit 1518395

File tree

2 files changed

+31
-8
lines changed

2 files changed

+31
-8
lines changed

swoole_postgresql_coro.cc

+9-2
Original file line numberDiff line numberDiff line change
@@ -1055,8 +1055,8 @@ static inline void php_pgsql_get_field_value(
10551055
*/
10561056
int swoole_pgsql_result2array(PGresult *pg_result, zval *ret_array, long result_type) {
10571057
zval row;
1058-
char *field_name;
1059-
size_t num_fields;
1058+
const char *field_name;
1059+
size_t num_fields, unknown_columns;
10601060
int pg_numrows, pg_row;
10611061
uint32_t i;
10621062
assert(Z_TYPE_P(ret_array) == IS_ARRAY);
@@ -1066,11 +1066,18 @@ int swoole_pgsql_result2array(PGresult *pg_result, zval *ret_array, long result_
10661066
}
10671067
for (pg_row = 0; pg_row < pg_numrows; pg_row++) {
10681068
array_init(&row);
1069+
unknown_columns = 0;
10691070
for (i = 0, num_fields = PQnfields(pg_result); i < num_fields; i++) {
10701071
if (result_type & PGSQL_ASSOC) {
10711072
zval value;
10721073
php_pgsql_get_field_value(&value, pg_result, result_type, pg_row, i);
10731074
field_name = PQfname(pg_result, i);
1075+
if (0 == strcmp("?column?", field_name)) {
1076+
if (unknown_columns > 0) {
1077+
field_name = (std::string(field_name) + std::to_string(unknown_columns)).c_str();
1078+
}
1079+
++unknown_columns;
1080+
}
10741081
add_assoc_zval(&row, field_name, &value);
10751082
}
10761083
if (result_type & PGSQL_NUM) {

tests/unit/PostgreSQLTest.php

+22-6
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
});
@@ -52,11 +54,25 @@ public function testQuery()
5254
run(function () {
5355
$pg = $this->getConn();
5456
$result = $pg->query('SELECT * FROM weather;');
55-
$this->assertNotFalse($result, (string)$pg->error);
57+
$this->assertNotFalse($result, (string) $pg->error);
5658

5759
$arr = $pg->fetchAll($result);
5860
$this->assertIsArray($arr);
5961
$this->assertEquals($arr[0]['city'], 'San Francisco');
6062
});
6163
}
64+
65+
public function testNoFieldName()
66+
{
67+
run(function () {
68+
$pg = $this->getConn();
69+
$result = $pg->query('SELECT 11, 22');
70+
$this->assertNotFalse($result, (string) $pg->error);
71+
72+
$arr = $pg->fetchAll($result);
73+
$this->assertIsArray($arr);
74+
$this->assertEquals($arr[0]['?column?'], 11);
75+
$this->assertEquals($arr[0]['?column?1'], 22);
76+
});
77+
}
6278
}

0 commit comments

Comments
 (0)