diff options
author | Christian Schneider | 2021-01-25 16:15:29 +0100 |
---|---|---|
committer | Christian Schneider | 2021-01-25 16:15:29 +0100 |
commit | edfc5b21d8df6a4209172b9288405d4c5aff4059 (patch) | |
tree | 7b1297bc3386f0f85cc5427ebed8a9b126f2e949 | |
parent | a39d9275bf2fb694560eef64d0afebecf141bc28 (diff) | |
download | itools-edfc5b21d8df6a4209172b9288405d4c5aff4059.tar.gz itools-edfc5b21d8df6a4209172b9288405d4c5aff4059.tar.bz2 itools-edfc5b21d8df6a4209172b9288405d4c5aff4059.zip |
Fix generating errors on SQL syntax errors, add tests for it
-rw-r--r-- | it_dbi.class | 6 | ||||
-rw-r--r-- | it_dbi_postgres.class | 7 | ||||
-rwxr-xr-x | test/it_dbi.t | 15 |
3 files changed, 20 insertions, 8 deletions
diff --git a/it_dbi.class b/it_dbi.class index 2b6f475..d8dfa1b 100644 --- a/it_dbi.class +++ b/it_dbi.class @@ -579,8 +579,8 @@ function query($query, $p = array()) if (!($result = $this->_query($query, $p))) { - if ($result === false) - return $result; + if ($result === null) + return false; $this->_fatal("query(\"$query\") failed"); } else if (it::match('^(CREATE|ALTER|DROP) ', $query, array('utf8' => false))) @@ -1106,7 +1106,7 @@ function _query($query, $p) { $errno = mysqli_errno($this->_link); if (($p['safety'] < 2) && ($errno == 1062)) # Duplicate entry - return false; + return null; if ($errno == 2006) # mysql server has gone away: retry { diff --git a/it_dbi_postgres.class b/it_dbi_postgres.class index 1161632..bce0fd9 100644 --- a/it_dbi_postgres.class +++ b/it_dbi_postgres.class @@ -110,11 +110,10 @@ function _query($query, $p) if (!($result = pg_query($this->_link, $query)) && $p['safety']) { - /* TODO - $errno = mysqli_errno($this->_link); - if (($p['safety'] < 2) && ($errno == 1062)) # Duplicate entry - return false; + if (($p['safety'] < 2) && it::match('duplicate key value', pg_last_error($this->_link))) # Duplicate entry + return null; + /* TODO if ($errno == 2006) # mysql server has gone away: retry { it::log('sqllog', "it_dbi(): reconnecting mysqli_connect {$p['server']}, {$p['db']}"); diff --git a/test/it_dbi.t b/test/it_dbi.t index 42fae74..7b1c9d7 100755 --- a/test/it_dbi.t +++ b/test/it_dbi.t @@ -33,6 +33,13 @@ $dbi->query("create temporary table it_dbi_test ( $opts['subclass']::createclass(['table' => "it_dbi_test", 'forcecreate' => true]); $record = new it_dbi_test; +$GLOBALS['it_defaultconfig']['fatal_throws_exception'] = true; + +try { + is(@$record->select("SYNTAX ERROR"), "Exception", "Syntax triggers exception for fatal_throws_exception mode"); +} catch (Exception $e) { + is(it::match('syntax', $e->getMessage()), 'syntax', "Syntax error triggers error"); +} $record->insert(['x' => 42, 'foo' => null]); $record->insert(['foo' => "bar"]); @@ -44,6 +51,12 @@ is( "auto_increment" ); +try { + @$record->insert(['ID' => 3]); +} catch (Exception $e) { + is($e->getMessage(), false, "Silently ignore duplicate inserts with default safety"); +} + $record->read(1); is( [$record->_key, $record->x, $record->foo], @@ -58,7 +71,7 @@ is( ); is( - $record->select(['foo <>' => ""], "LIMIT 1"), + @$record->select(['foo <>' => ""], "LIMIT 1"), 1, "select with multiple parameters (LIMIT part)" ); |