swoole / ext-postgresql

🐘 Coroutine-based client for PostgreSQL
64 stars 21 forks source link

New features and fixes #37

Closed codercms closed 3 years ago

codercms commented 3 years ago

Changes:

Testing error in prepare query

$connection = new \Swoole\Coroutine\PostgreSQL();
$connection->connect("host=127.0.0.1 port=5432 user=postgres dbname=test");

$result = $connection->prepare('123', 'SELECT FROM rere');
var_dump($result, $connection);

$result = $connection->prepare('123', 'SELECT 1');
var_dump($result, $connection);
Output is (click me): ``` bool(false) object(Swoole\Coroutine\PostgreSQL)#5 (4) { ["error"]=> string(86) "ERROR: relation "rere" does not exist LINE 1: SELECT FROM rere ^ " ["errCode"]=> int(0) ["resultStatus"]=> int(7) ["resultDiag"]=> array(17) { ["severity"]=> string(5) "ERROR" ["sqlstate"]=> string(5) "42P01" ["message_primary"]=> string(30) "relation "rere" does not exist" ["message_detail"]=> NULL ["message_hint"]=> NULL ["statement_position"]=> string(2) "13" ["internal_position"]=> NULL ["internal_query"]=> NULL ["content"]=> NULL ["schema_name"]=> NULL ["table_name"]=> NULL ["column_name"]=> NULL ["datatype_name"]=> NULL ["constraint_name"]=> NULL ["source_file"]=> string(16) "parse_relation.c" ["source_line"]=> string(4) "1191" ["source_function"]=> string(15) "parserOpenTable" } } bool(true) object(Swoole\Coroutine\PostgreSQL)#5 (4) { ["error"]=> NULL ["errCode"]=> int(0) ["resultStatus"]=> int(1) ["resultDiag"]=> NULL } ```

Testing error in query

$result = $connection->query('SELECT * FROM test WHERE undefined_colmun = 1');
var_dump($result, $connection);
$result = $connection->query('SELECT * FROM test LIMIT 1');
var_dump($result, $connection);
Output is (click me): ``` bool(false) object(Swoole\Coroutine\PostgreSQL)#5 (4) { ["error"]=> string(138) "ERROR: column "undefined_colmun" does not exist LINE 1: SELECT * FROM test WHERE undefined_colmun = 1 ^ " ["errCode"]=> int(0) ["resultStatus"]=> int(7) ["resultDiag"]=> array(17) { ["severity"]=> string(5) "ERROR" ["sqlstate"]=> string(5) "42703" ["message_primary"]=> string(40) "column "undefined_colmun" does not exist" ["message_detail"]=> NULL ["message_hint"]=> NULL ["statement_position"]=> string(2) "26" ["internal_position"]=> NULL ["internal_query"]=> NULL ["content"]=> NULL ["schema_name"]=> NULL ["table_name"]=> NULL ["column_name"]=> NULL ["datatype_name"]=> NULL ["constraint_name"]=> NULL ["source_file"]=> string(16) "parse_relation.c" ["source_line"]=> string(4) "3349" ["source_function"]=> string(18) "errorMissingColumn" } } resource(17) of type (pgsql result) object(Swoole\Coroutine\PostgreSQL)#5 (4) { ["error"]=> NULL ["errCode"]=> int(0) ["resultStatus"]=> int(2) ["resultDiag"]=> NULL } ```

Testing constraint violation

$connection->query("CREATE TABLE test (domain VARCHAR(63), tld VARCHAR(63), PRIMARY KEY (domain, tld))");

$result = $connection->query("INSERT INTO test (domain, tld)VALUES('google', 'com')");
var_dump($result, $connection);
$result = $connection->query("INSERT INTO test (domain, tld)VALUES('google', 'com')");
var_dump($result, $connection);
Output is (click me): ``` resource(17) of type (pgsql result) object(Swoole\Coroutine\PostgreSQL)#5 (4) { ["error"]=> NULL ["errCode"]=> int(0) ["resultStatus"]=> int(1) ["resultDiag"]=> NULL } bool(false) object(Swoole\Coroutine\PostgreSQL)#5 (4) { ["error"]=> string(124) "ERROR: duplicate key value violates unique constraint "test_pkey" DETAIL: Key (domain, tld)=(google, com) already exists. " ["errCode"]=> int(0) ["resultStatus"]=> int(7) ["resultDiag"]=> array(17) { ["severity"]=> string(5) "ERROR" ["sqlstate"]=> string(5) "23505" ["message_primary"]=> string(58) "duplicate key value violates unique constraint "test_pkey"" ["message_detail"]=> string(47) "Key (domain, tld)=(google, com) already exists." ["message_hint"]=> NULL ["statement_position"]=> NULL ["internal_position"]=> NULL ["internal_query"]=> NULL ["content"]=> NULL ["schema_name"]=> string(6) "public" ["table_name"]=> string(4) "test" ["column_name"]=> NULL ["datatype_name"]=> NULL ["constraint_name"]=> string(9) "test_pkey" ["source_file"]=> string(11) "nbtinsert.c" ["source_line"]=> string(3) "563" ["source_function"]=> string(16) "_bt_check_unique" } } ```
codercms commented 3 years ago

Actually all tests performed on the feature/new-features branch (which is based on latest stable 08c47e4eafe3b457e9ec92110297883f62cb49e6 commit and with this fix applied https://github.com/swoole/ext-postgresql/pull/36), because for this moment it's impossible to build this extension from the master branch due to breaking changes (Swoole 4.5.4 which is not released yet)