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

New features and fixes #37

Merged
merged 7 commits into from
Sep 16, 2020
Merged

New features and fixes #37

merged 7 commits into from
Sep 16, 2020

Conversation

codercms
Copy link
Collaborator

@codercms codercms commented Sep 15, 2020

Changes:

  • Fixed "prepare" method that always returns true (even when an error has occurred)
  • Added "resultStatus" property so you can figure out how to interpret the result
  • Added "resultDiag" property that can be very useful for error message building and for errors troubleshooting.
  • Added "fieldCount" method that returns number of columns in the result set

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"
  }
}

* Fixed "prepare" method that always returns true (even when an error has occurred)
* Added "resultStatus" property so you can figure out how to interpret the result
* Added "resultDiag" property that can be very useful for error message building and for errors troubleshooting.
…s-master

# Conflicts:
#	swoole_postgresql_coro.cc
* Fixed "prepare" method that always returns true (even when an error has occurred)
* Added "resultStatus" property so you can figure out how to interpret the result
* Added "resultDiag" property that can be very useful for error message building and for errors troubleshooting.
* Fixed "prepare" method that always returns true (even when an error has occurred)
* Added "resultStatus" property so you can figure out how to interpret the result
* Added "resultDiag" property that can be very useful for error message building and for errors troubleshooting.
* Fixed "prepare" method that always returns true (even when an error has occurred)
* Added "resultStatus" property so you can figure out how to interpret the result
* Added "resultDiag" property that can be very useful for error message building and for errors troubleshooting.
@codercms
Copy link
Collaborator Author

codercms commented Sep 15, 2020

Actually all tests performed on the feature/new-features branch (which is based on latest stable 08c47e4 commit and with this fix applied #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)

* "fieldCount" method added
Fix formatting
@matyhtf matyhtf merged commit 28d7af5 into master Sep 16, 2020
@sy-records sy-records deleted the feature/new-features-master branch January 11, 2021 07:51
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants