Skip to content
This repository was archived by the owner on May 11, 2021. It is now read-only.

Commit

Permalink
Fix PostgreSQL boolean handling for PHP 8.0.5 (#299)
Browse files Browse the repository at this point in the history
  • Loading branch information
mvorisek authored May 1, 2021
1 parent 30d9f16 commit 61b17b1
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 35 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
"require-release": {
"php": ">=7.3.0",
"ext-pdo": "*",
"atk4/core": "~2.5.0",
"atk4/core": "~3.0.0",
"doctrine/dbal": "^2.10 || ^3.0"
},
"require-dev": {
Expand Down
11 changes: 8 additions & 3 deletions src/Expression.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Doctrine\DBAL\Connection as DbalConnection;
use Doctrine\DBAL\Exception as DbalException;
use Doctrine\DBAL\Platforms\PostgreSQL94Platform;
use Doctrine\DBAL\Result as DbalResult;

class Expression implements \ArrayAccess
Expand Down Expand Up @@ -528,9 +529,13 @@ public function execute(object $connection = null): object
if (is_int($val)) {
$type = \PDO::PARAM_INT;
} elseif (is_bool($val)) {
// SQL does not like booleans at all, so convert them INT
$type = \PDO::PARAM_INT;
$val = (int) $val;
if ($this->connection->getDatabasePlatform() instanceof PostgreSQL94Platform) {
$type = \PDO::PARAM_STR;
$val = $val ? '1' : '0';
} else {
$type = \PDO::PARAM_INT;
$val = $val ? 1 : 0;
}
} elseif ($val === null) {
$type = \PDO::PARAM_NULL;
} elseif (is_string($val) || is_float($val)) {
Expand Down
6 changes: 0 additions & 6 deletions src/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -870,12 +870,6 @@ protected function _render_group()
*/
public function set($field, $value = null)
{
if ($value === false) {
throw (new Exception('Value "false" is not supported by SQL'))
->addMoreInfo('field', $field)
->addMoreInfo('value', $value);
}

if (is_array($value)) {
throw (new Exception('Array values are not supported by SQL'))
->addMoreInfo('field', $field)
Expand Down
4 changes: 2 additions & 2 deletions tests/QueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1468,14 +1468,14 @@ public function testSetReturnValue()
}

/**
* Value [false] is not supported by SQL.
* Value of type array is not supported by SQL.
*
* @covers ::set
*/
public function testSetException1()
{
$this->expectException(Exception::class);
$this->q()->set('name', false);
$this->q()->set('name', []);
}

/**
Expand Down
14 changes: 7 additions & 7 deletions tests/WithDb/SelectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public function testBasicQueries()

$this->assertSame(
['surname' => 'Williams'],
$this->q('employee')->field('surname')->where('retired', '1')->getRow()
$this->q('employee')->field('surname')->where('retired', true)->getRow()
);

$this->assertSame(
Expand Down Expand Up @@ -201,10 +201,10 @@ public function testOtherQueries()

// insert
$this->q('employee')
->set(['id' => 1, 'name' => 'John', 'surname' => 'Doe', 'retired' => 1])
->set(['id' => 1, 'name' => 'John', 'surname' => 'Doe', 'retired' => true])
->insert();
$this->q('employee')
->set(['id' => 2, 'name' => 'Jane', 'surname' => 'Doe', 'retired' => 0])
->set(['id' => 2, 'name' => 'Jane', 'surname' => 'Doe', 'retired' => false])
->insert();
$this->assertSame(
[['id' => '1', 'name' => 'John'], ['id' => '2', 'name' => 'Jane']],
Expand All @@ -224,12 +224,12 @@ public function testOtherQueries()
// replace
if ($this->c->getDatabasePlatform() instanceof PostgreSQL94Platform || $this->c->getDatabasePlatform() instanceof SQLServer2012Platform || $this->c->getDatabasePlatform() instanceof OraclePlatform) {
$this->q('employee')
->set(['name' => 'Peter', 'surname' => 'Doe', 'retired' => 1])
->set(['name' => 'Peter', 'surname' => 'Doe', 'retired' => true])
->where('id', 1)
->update();
} else {
$this->q('employee')
->set(['id' => 1, 'name' => 'Peter', 'surname' => 'Doe', 'retired' => 1])
->set(['id' => 1, 'name' => 'Peter', 'surname' => 'Doe', 'retired' => true])
->replace();
}

Expand All @@ -251,7 +251,7 @@ public function testOtherQueries()

// delete
$this->q('employee')
->where('retired', 1)
->where('retired', true)
->delete();
$this->assertSame(
[['id' => '2', 'name' => 'Jane']],
Expand All @@ -271,7 +271,7 @@ public function testWhereExpression()
{
$this->assertSame(
[['id' => '2', 'name' => 'Jack', 'surname' => 'Williams', 'retired' => '1']],
$this->q('employee')->where('retired', 1)->where($this->q()->expr('{}=[] or {}=[]', ['surname', 'Williams', 'surname', 'Smith']))->getRows()
$this->q('employee')->where('retired', true)->where($this->q()->expr('{}=[] or {}=[]', ['surname', 'Williams', 'surname', 'Smith']))->getRows()
);
}

Expand Down
32 changes: 16 additions & 16 deletions tests/WithDb/TransactionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -149,10 +149,10 @@ public function testTransactions()
// without transaction, ignoring exceptions
try {
$this->q('employee')
->set(['id' => 1, 'name' => 'John', 'surname' => 'Doe', 'retired' => 1])
->set(['id' => 1, 'name' => 'John', 'surname' => 'Doe', 'retired' => true])
->insert();
$this->q('employee')
->set(['id' => 2, 'FOO' => 'bar', 'name' => 'Jane', 'surname' => 'Doe', 'retired' => 0])
->set(['id' => 2, 'FOO' => 'bar', 'name' => 'Jane', 'surname' => 'Doe', 'retired' => false])
->insert();
} catch (\Exception $e) {
// ignore
Expand All @@ -166,7 +166,7 @@ public function testTransactions()
// 1-level transaction: begin, insert, 2, rollback, 1
$this->c->beginTransaction();
$this->q('employee')
->set(['id' => 3, 'name' => 'John', 'surname' => 'Doe', 'retired' => 1])
->set(['id' => 3, 'name' => 'John', 'surname' => 'Doe', 'retired' => true])
->insert();
$this->assertSame(
'2',
Expand All @@ -183,10 +183,10 @@ public function testTransactions()
try {
$this->c->atomic(function () {
$this->q('employee')
->set(['id' => 3, 'name' => 'John', 'surname' => 'Doe', 'retired' => 1])
->set(['id' => 3, 'name' => 'John', 'surname' => 'Doe', 'retired' => true])
->insert();
$this->q('employee')
->set(['id' => 4, 'FOO' => 'bar', 'name' => 'Jane', 'surname' => 'Doe', 'retired' => 0])
->set(['id' => 4, 'FOO' => 'bar', 'name' => 'Jane', 'surname' => 'Doe', 'retired' => false])
->insert();
});
} catch (\Exception $e) {
Expand All @@ -202,21 +202,21 @@ public function testTransactions()
try {
$this->c->atomic(function () {
$this->q('employee')
->set(['id' => 3, 'name' => 'John', 'surname' => 'Doe', 'retired' => 1])
->set(['id' => 3, 'name' => 'John', 'surname' => 'Doe', 'retired' => true])
->insert();

// success, in, fail, out, fail
$this->c->atomic(function () {
$this->q('employee')
->set(['id' => 4, 'name' => 'John', 'surname' => 'Doe', 'retired' => 1])
->set(['id' => 4, 'name' => 'John', 'surname' => 'Doe', 'retired' => true])
->insert();
$this->q('employee')
->set(['id' => 5, 'FOO' => 'bar', 'name' => 'Jane', 'surname' => 'Doe', 'retired' => 0])
->set(['id' => 5, 'FOO' => 'bar', 'name' => 'Jane', 'surname' => 'Doe', 'retired' => false])
->insert();
});

$this->q('employee')
->set(['id' => 6, 'FOO' => 'bar', 'name' => 'Jane', 'surname' => 'Doe', 'retired' => 0])
->set(['id' => 6, 'FOO' => 'bar', 'name' => 'Jane', 'surname' => 'Doe', 'retired' => false])
->insert();
});
} catch (\Exception $e) {
Expand All @@ -232,18 +232,18 @@ public function testTransactions()
try {
$this->c->atomic(function () {
$this->q('employee')
->set(['id' => 3, 'name' => 'John', 'surname' => 'Doe', 'retired' => 1])
->set(['id' => 3, 'name' => 'John', 'surname' => 'Doe', 'retired' => true])
->insert();

// success, in, success, out, fail
$this->c->atomic(function () {
$this->q('employee')
->set(['id' => 4, 'name' => 'John', 'surname' => 'Doe', 'retired' => 1])
->set(['id' => 4, 'name' => 'John', 'surname' => 'Doe', 'retired' => true])
->insert();
});

$this->q('employee')
->set(['id' => 5, 'FOO' => 'bar', 'name' => 'Jane', 'surname' => 'Doe', 'retired' => 0])
->set(['id' => 5, 'FOO' => 'bar', 'name' => 'Jane', 'surname' => 'Doe', 'retired' => false])
->insert();
});
} catch (\Exception $e) {
Expand All @@ -259,18 +259,18 @@ public function testTransactions()
try {
$this->c->atomic(function () {
$this->q('employee')
->set(['id' => 3, 'name' => 'John', 'surname' => 'Doe', 'retired' => 1])
->set(['id' => 3, 'name' => 'John', 'surname' => 'Doe', 'retired' => true])
->insert();

// success, in, fail, out, catch exception
$this->c->atomic(function () {
$this->q('employee')
->set(['id' => 4, 'FOO' => 'bar', 'name' => 'John', 'surname' => 'Doe', 'retired' => 1])
->set(['id' => 4, 'FOO' => 'bar', 'name' => 'John', 'surname' => 'Doe', 'retired' => true])
->insert();
});

$this->q('employee')
->set(['id' => 5, 'name' => 'Jane', 'surname' => 'Doe', 'retired' => 0])
->set(['id' => 5, 'name' => 'Jane', 'surname' => 'Doe', 'retired' => false])
->insert();
});
} catch (\Exception $e) {
Expand All @@ -286,7 +286,7 @@ public function testTransactions()
try {
$this->c->atomic(function () {
$this->q('employee')
->set(['id' => 3, 'name' => 'John', 'surname' => 'Doe', 'retired' => 1])
->set(['id' => 3, 'name' => 'John', 'surname' => 'Doe', 'retired' => true])
->insert();
});
} catch (\Exception $e) {
Expand Down

0 comments on commit 61b17b1

Please sign in to comment.