Skip to content

Support boolean type #319

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Apr 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
- Enh #313: Refactor according changes in `db` package (@Tigrov)
- New #311: Add `caseSensitive` option to like condition (@vjik)
- Enh #315: Remove `getCacheKey()` and `getCacheTag()` methods from `Schema` class (@Tigrov)
- Enh #319: Support `boolean` type (@Tigrov)
- Enh #318, #320: Use `DbArrayHelper::arrange()` instead of `DbArrayHelper::index()` method (@Tigrov)

## 1.3.0 March 21, 2024
Expand Down
40 changes: 40 additions & 0 deletions src/Column/BooleanColumn.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Db\Oracle\Column;

use Yiisoft\Db\Constant\ColumnType;
use Yiisoft\Db\Constant\PhpType;
use Yiisoft\Db\Expression\ExpressionInterface;
use Yiisoft\Db\Schema\Column\AbstractColumn;

final class BooleanColumn extends AbstractColumn
{
protected const DEFAULT_TYPE = ColumnType::BOOLEAN;

public function dbTypecast(mixed $value): string|ExpressionInterface|null
{
return match ($value) {
true => '1',
false => '0',
null, '' => null,
default => $value instanceof ExpressionInterface ? $value : ($value ? '1' : '0'),
};
}

/** @psalm-mutation-free */
public function getPhpType(): string
{
return PhpType::BOOL;
}

public function phpTypecast(mixed $value): bool|null
{
if ($value === null) {
return null;
}

return $value && $value !== "\0";
}
}
5 changes: 5 additions & 0 deletions src/Column/ColumnBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ public static function binary(int|null $size = null): BinaryColumn
return new BinaryColumn(ColumnType::BINARY, size: $size);
}

public static function boolean(): BooleanColumn
{
return new BooleanColumn(ColumnType::BOOLEAN);
}

public static function json(): JsonColumn
{
return new JsonColumn(ColumnType::JSON);
Expand Down
10 changes: 7 additions & 3 deletions src/Column/ColumnDefinitionBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,17 @@
if (empty($check)) {
$name = $column->getName();

if (empty($name) || version_compare($this->queryBuilder->getServerInfo()->getVersion(), '21', '>=')) {
if (empty($name)) {
return '';
}

return match ($column->getType()) {
ColumnType::ARRAY, ColumnType::STRUCTURED, ColumnType::JSON =>
' CHECK (' . $this->queryBuilder->getQuoter()->quoteSimpleColumnName($name) . ' IS JSON)',
version_compare($this->queryBuilder->getServerInfo()->getVersion(), '21', '<')
? ' CHECK (' . $this->queryBuilder->getQuoter()->quoteSimpleColumnName($name) . ' IS JSON)'

Check warning on line 66 in src/Column/ColumnDefinitionBuilder.php

View check run for this annotation

Codecov / codecov/patch

src/Column/ColumnDefinitionBuilder.php#L66

Added line #L66 was not covered by tests
: '',
ColumnType::BOOLEAN =>
' CHECK (' . $this->queryBuilder->getQuoter()->quoteSimpleColumnName($name) . ' IN (0,1))',
default => '',
};
}
Expand Down Expand Up @@ -94,7 +98,7 @@
return match ($dbType) {
default => $dbType,
null => match ($column->getType()) {
ColumnType::BOOLEAN => 'number(1)',
ColumnType::BOOLEAN => 'char(1)',
ColumnType::BIT => match (true) {
$size === null => 'number(38)',
$size <= 126 => 'number(' . ceil(log10(2 ** $size)) . ')',
Expand Down
15 changes: 13 additions & 2 deletions src/Column/ColumnFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,18 @@ protected function getType(string $dbType, array $info = []): string
};
}

if (isset($info['check'], $info['name']) && strcasecmp($info['check'], '"' . $info['name'] . '" is json') === 0) {
return ColumnType::JSON;
if (isset($info['check'], $info['name'])) {
if (strcasecmp($info['check'], '"' . $info['name'] . '" is json') === 0) {
return ColumnType::JSON;
}

if (isset($info['size'])
&& $dbType === 'char'
&& $info['size'] === 1
&& strcasecmp($info['check'], '"' . $info['name'] . '" in (0,1)') === 0
) {
return ColumnType::BOOLEAN;
}
}

if ($dbType === 'interval day to second' && isset($info['scale']) && $info['scale'] === 0) {
Expand All @@ -80,6 +90,7 @@ protected function getColumnClass(string $type, array $info = []): string
{
return match ($type) {
ColumnType::BINARY => BinaryColumn::class,
ColumnType::BOOLEAN => BooleanColumn::class,
ColumnType::JSON => JsonColumn::class,
default => parent::getColumnClass($type, $info),
};
Expand Down
4 changes: 4 additions & 0 deletions src/QueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
*/
final class QueryBuilder extends AbstractQueryBuilder
{
protected const FALSE_VALUE = "'0'";

protected const TRUE_VALUE = "'1'";

public function __construct(ConnectionInterface $db)
{
$quoter = $db->getQuoter();
Expand Down
10 changes: 9 additions & 1 deletion tests/ColumnTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@
namespace Yiisoft\Db\Oracle\Tests;

use PDO;
use PHPUnit\Framework\Attributes\DataProviderExternal;
use Yiisoft\Db\Command\Param;
use Yiisoft\Db\Expression\Expression;
use Yiisoft\Db\Oracle\Column\BinaryColumn;
use Yiisoft\Db\Oracle\Column\JsonColumn;
use Yiisoft\Db\Oracle\Tests\Provider\ColumnProvider;
use Yiisoft\Db\Oracle\Tests\Support\TestTrait;
use Yiisoft\Db\Query\Query;
use Yiisoft\Db\Schema\Column\ColumnInterface;
Expand Down Expand Up @@ -106,12 +108,18 @@ public function testPredefinedType(string $className, string $type, string $phpT
parent::testPredefinedType($className, $type, $phpType);
}

/** @dataProvider \Yiisoft\Db\Oracle\Tests\Provider\ColumnProvider::dbTypecastColumns */
#[DataProviderExternal(ColumnProvider::class, 'dbTypecastColumns')]
public function testDbTypecastColumns(ColumnInterface $column, array $values): void
{
parent::testDbTypecastColumns($column, $values);
}

#[DataProviderExternal(ColumnProvider::class, 'phpTypecastColumns')]
public function testPhpTypecastColumns(ColumnInterface $column, array $values): void
{
parent::testPhpTypecastColumns($column, $values);
}

public function testBinaryColumn(): void
{
$binaryCol = new BinaryColumn();
Expand Down
2 changes: 2 additions & 0 deletions tests/Provider/ColumnBuilderProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Yiisoft\Db\Oracle\Tests\Provider;

use Yiisoft\Db\Oracle\Column\BinaryColumn;
use Yiisoft\Db\Oracle\Column\BooleanColumn;
use Yiisoft\Db\Oracle\Column\JsonColumn;

class ColumnBuilderProvider extends \Yiisoft\Db\Tests\Provider\ColumnBuilderProvider
Expand All @@ -15,6 +16,7 @@ public static function buildingMethods(): array

$values['binary()'][2] = BinaryColumn::class;
$values['binary(8)'][2] = BinaryColumn::class;
$values['boolean()'][2] = BooleanColumn::class;
$values['json()'][2] = JsonColumn::class;

return $values;
Expand Down
3 changes: 3 additions & 0 deletions tests/Provider/ColumnFactoryProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Yiisoft\Db\Constant\ColumnType;
use Yiisoft\Db\Expression\Expression;
use Yiisoft\Db\Oracle\Column\BinaryColumn;
use Yiisoft\Db\Oracle\Column\BooleanColumn;
use Yiisoft\Db\Oracle\Column\JsonColumn;
use Yiisoft\Db\Schema\Column\ArrayColumn;
use Yiisoft\Db\Schema\Column\BigIntColumn;
Expand Down Expand Up @@ -85,6 +86,8 @@ public static function types(): array
{
$types = parent::types();

$types['binary'][2] = BinaryColumn::class;
$types['boolean'][2] = BooleanColumn::class;
$types['json'][2] = JsonColumn::class;

return $types;
Expand Down
32 changes: 32 additions & 0 deletions tests/Provider/ColumnProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,19 @@

namespace Yiisoft\Db\Oracle\Tests\Provider;

use Yiisoft\Db\Expression\Expression;
use Yiisoft\Db\Oracle\Column\BinaryColumn;
use Yiisoft\Db\Oracle\Column\BooleanColumn;
use Yiisoft\Db\Oracle\Column\JsonColumn;

class ColumnProvider extends \Yiisoft\Db\Tests\Provider\ColumnProvider
{
public static function predefinedTypes(): array
{
$values = parent::predefinedTypes();
$values['binary'][0] = BinaryColumn::class;
$values['boolean'][0] = BooleanColumn::class;
$values['json'][0] = JsonColumn::class;

return $values;
}
Expand All @@ -20,6 +25,33 @@ public static function dbTypecastColumns(): array
{
$values = parent::dbTypecastColumns();
$values['binary'][0] = new BinaryColumn();
$values['boolean'] = [
new BooleanColumn(),
[
[null, null],
[null, ''],
['1', true],
['1', 1],
['1', 1.0],
['1', '1'],
['0', false],
['0', 0],
['0', 0.0],
['0', '0'],
[$expression = new Expression('expression'), $expression],
],
];
$values['json'][0] = new JsonColumn();

return $values;
}

public static function phpTypecastColumns(): array
{
$values = parent::phpTypecastColumns();
$values['binary'][0] = new BinaryColumn();
$values['boolean'][0] = new BooleanColumn();
$values['json'][0] = new JsonColumn();

return $values;
}
Expand Down
14 changes: 14 additions & 0 deletions tests/Provider/CommandProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,20 @@ public static function insertVarbinary(): array
];
}

public static function rawSql(): array
{
$rawSql = parent::rawSql();

foreach ($rawSql as &$values) {
$values[2] = strtr($values[2], [
'FALSE' => "'0'",
'TRUE' => "'1'",
]);
}

return $rawSql;
}

public static function createIndex(): array
{
return [
Expand Down
8 changes: 6 additions & 2 deletions tests/Provider/QueryBuilderProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -271,8 +271,8 @@ public static function buildColumnDefinition(): array
$values['bigPrimaryKey(false)'][0] = 'number(20) PRIMARY KEY';
$values['uuidPrimaryKey()'][0] = 'raw(16) DEFAULT sys_guid() PRIMARY KEY';
$values['uuidPrimaryKey(false)'][0] = 'raw(16) PRIMARY KEY';
$values['boolean()'][0] = 'number(1)';
$values['boolean(100)'][0] = 'number(1)';
$values['boolean()'] = ['char(1) CHECK ("boolean_col" IN (0,1))', $values['boolean()'][1]->withName('boolean_col')];
$values['boolean(100)'] = ['char(1) CHECK ("boolean_100" IN (0,1))', $values['boolean(100)'][1]->withName('boolean_100')];
$values['bit()'][0] = 'number(38)';
$values['bit(1)'][0] = 'number(1)';
$values['bit(8)'][0] = 'number(3)';
Expand Down Expand Up @@ -384,6 +384,8 @@ public static function prepareParam(): array
{
$values = parent::prepareParam();

$values['true'][0] = "'1'";
$values['false'][0] = "'0'";
$values['binary'][0] = "HEXTORAW('737472696e67')";

return $values;
Expand All @@ -393,6 +395,8 @@ public static function prepareValue(): array
{
$values = parent::prepareValue();

$values['true'][0] = "'1'";
$values['false'][0] = "'0'";
$values['binary'][0] = "HEXTORAW('737472696e67')";
$values['paramBinary'][0] = "HEXTORAW('737472696e67')";

Expand Down
9 changes: 4 additions & 5 deletions tests/Provider/SchemaProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Yiisoft\Db\Constraint\CheckConstraint;
use Yiisoft\Db\Expression\Expression;
use Yiisoft\Db\Oracle\Column\BinaryColumn;
use Yiisoft\Db\Oracle\Column\BooleanColumn;
use Yiisoft\Db\Oracle\Column\JsonColumn;
use Yiisoft\Db\Schema\Column\DoubleColumn;
use Yiisoft\Db\Schema\Column\IntegerColumn;
Expand Down Expand Up @@ -102,19 +103,17 @@ public static function columns(): array
scale: 1,
defaultValue: new Expression("INTERVAL '2 04:56:12' DAY(1) TO SECOND(0)"),
),
'bool_col' => new StringColumn(
ColumnType::CHAR,
'bool_col' => new BooleanColumn(
dbType: 'char',
check: '"bool_col" in (0,1)',
notNull: true,
size: 1,
),
'bool_col2' => new StringColumn(
ColumnType::CHAR,
'bool_col2' => new BooleanColumn(
dbType: 'char',
check: '"bool_col2" in (0,1)',
size: 1,
defaultValue: '1',
defaultValue: true,
),
'ts_default' => new StringColumn(
ColumnType::TIMESTAMP,
Expand Down
Loading