Skip to content

Commit

Permalink
Const optimization (#226)
Browse files Browse the repository at this point in the history
* Const optimization

* styleci
  • Loading branch information
darkdef committed Feb 4, 2023
1 parent 626ac91 commit e95071b
Show file tree
Hide file tree
Showing 7 changed files with 13 additions and 39 deletions.
3 changes: 1 addition & 2 deletions src/Builder/ArrayExpressionBuilder.php
Expand Up @@ -13,7 +13,6 @@
use Yiisoft\Db\Expression\ExpressionBuilderInterface;
use Yiisoft\Db\Expression\ExpressionInterface;
use Yiisoft\Db\Expression\JsonExpression;
use Yiisoft\Db\Pgsql\Schema;
use Yiisoft\Db\Query\QueryInterface;
use Yiisoft\Db\QueryBuilder\QueryBuilderInterface;
use Yiisoft\Db\Schema\SchemaInterface;
Expand Down Expand Up @@ -166,7 +165,7 @@ protected function typecastValue(
return $value;
}

if (in_array($expression->getType(), [SchemaInterface::TYPE_JSON, Schema::TYPE_JSONB], true)) {
if (in_array($expression->getType(), [SchemaInterface::TYPE_JSON, SchemaInterface::TYPE_JSONB], true)) {
return new JsonExpression($value);
}

Expand Down
4 changes: 2 additions & 2 deletions src/ColumnSchema.php
Expand Up @@ -59,11 +59,11 @@ public function dbTypecast(mixed $value): mixed
return new ArrayExpression($value, $this->getDbType(), $this->dimension);
}

if (in_array($this->getDbType(), [SchemaInterface::TYPE_JSON, Schema::TYPE_JSONB], true)) {
if (in_array($this->getDbType(), [SchemaInterface::TYPE_JSON, SchemaInterface::TYPE_JSONB], true)) {
return new JsonExpression($value, $this->getDbType());
}

if ($this->getType() === Schema::TYPE_BINARY && is_string($value)) {
if ($this->getType() === SchemaInterface::TYPE_BINARY && is_string($value)) {
/** explicitly setup PDO param type for binary column */
return new Param($value, PDO::PARAM_LOB);
}
Expand Down
25 changes: 0 additions & 25 deletions src/QueryBuilder.php
Expand Up @@ -13,31 +13,6 @@
*/
final class QueryBuilder extends AbstractQueryBuilder
{
/**
* Defines a B-tree index method for {@see createIndex()}.
*/
public const INDEX_B_TREE = 'btree';

/**
* Defines a hash index method for {@see createIndex()}.
*/
public const INDEX_HASH = 'hash';

/**
* Defines a GiST index method for {@see createIndex()}.
*/
public const INDEX_GIST = 'gist';

/**
* Defines a GIN index method for {@see createIndex()}.
*/
public const INDEX_GIN = 'gin';

/**
* Defines a BRIN index method for {@see createIndex()}.
*/
public const INDEX_BRIN = 'brin';

/**
* @var array mapping from abstract column types (keys) to physical column types (values).
*
Expand Down
2 changes: 0 additions & 2 deletions src/Schema.php
Expand Up @@ -82,8 +82,6 @@
*/
final class Schema extends AbstractSchema
{
public const TYPE_JSONB = 'jsonb';

/**
* @var array The mapping from physical column types (keys) to abstract column types (values).
*
Expand Down
3 changes: 2 additions & 1 deletion tests/ColumnSchemaTest.php
Expand Up @@ -14,6 +14,7 @@
use Yiisoft\Db\Pgsql\ColumnSchema;
use Yiisoft\Db\Pgsql\Tests\Support\TestTrait;
use Yiisoft\Db\Query\Query;
use Yiisoft\Db\Schema\SchemaInterface;

/**
* @group pgsql
Expand Down Expand Up @@ -50,7 +51,7 @@ public function testPhpTypeCast(): void
'textarray2_col' => new ArrayExpression(null),
'json_col' => [['a' => 1, 'b' => null, 'c' => [1, 3, 5]]],
'jsonb_col' => new JsonExpression(new ArrayExpression([1, 2, 3])),
'jsonarray_col' => [new ArrayExpression([[',', 'null', true, 'false', 'f']], 'json')],
'jsonarray_col' => [new ArrayExpression([[',', 'null', true, 'false', 'f']], SchemaInterface::TYPE_JSON)],
]
);
$command->execute();
Expand Down
3 changes: 2 additions & 1 deletion tests/Provider/QueryBuilderProvider.php
Expand Up @@ -11,6 +11,7 @@
use Yiisoft\Db\Expression\JsonExpression;
use Yiisoft\Db\Pgsql\Tests\Support\TestTrait;
use Yiisoft\Db\Query\Query;
use Yiisoft\Db\Schema\SchemaInterface;
use Yiisoft\Db\Tests\Provider\AbstractQueryBuilderProvider;
use Yiisoft\Db\Tests\Support\TraversableObject;

Expand Down Expand Up @@ -201,7 +202,7 @@ public function buildCondition(): array
[':qp0' => '{"a":null,"b":123,"c":[4,5]}', ':qp1' => '[true]'],
],
'Items in ArrayExpression of type json should be casted to Json' => [
['=', 'colname', new ArrayExpression([['a' => null, 'b' => 123, 'c' => [4, 5]], [true]], 'json')],
['=', 'colname', new ArrayExpression([['a' => null, 'b' => 123, 'c' => [4, 5]], [true]], SchemaInterface::TYPE_JSON)],
'"colname" = ARRAY[:qp0, :qp1]::json[]',
[':qp0' => '{"a":null,"b":123,"c":[4,5]}', ':qp1' => '[true]'],
],
Expand Down
12 changes: 6 additions & 6 deletions tests/SchemaTest.php
Expand Up @@ -12,9 +12,9 @@
use Yiisoft\Db\Exception\InvalidConfigException;
use Yiisoft\Db\Exception\NotSupportedException;
use Yiisoft\Db\Expression\Expression;
use Yiisoft\Db\Pgsql\QueryBuilder;
use Yiisoft\Db\Pgsql\Schema;
use Yiisoft\Db\Pgsql\Tests\Support\TestTrait;
use Yiisoft\Db\Schema\SchemaInterface;
use Yiisoft\Db\Schema\TableSchemaInterface;
use Yiisoft\Db\Tests\Common\CommonSchemaTest;
use Yiisoft\Db\Tests\Support\DbHelper;
Expand Down Expand Up @@ -479,27 +479,27 @@ public function withIndexDataProvider(): array
return array_merge(parent::withIndexDataProvider(), [
[
'indexType' => null,
'indexMethod' => QueryBuilder::INDEX_B_TREE,
'indexMethod' => SchemaInterface::INDEX_BTREE,
'columnType' => 'varchar(16)',
],
[
'indexType' => null,
'indexMethod' => QueryBuilder::INDEX_HASH,
'indexMethod' => SchemaInterface::INDEX_HASH,
'columnType' => 'varchar(16)',
],
[
'indexType' => null,
'indexMethod' => QueryBuilder::INDEX_BRIN,
'indexMethod' => SchemaInterface::INDEX_BRIN,
'columnType' => 'varchar(16)',
],
[
'indexType' => null,
'indexMethod' => QueryBuilder::INDEX_GIN,
'indexMethod' => SchemaInterface::INDEX_GIN,
'columnType' => 'jsonb',
],
[
'indexType' => null,
'indexMethod' => QueryBuilder::INDEX_GIST,
'indexMethod' => SchemaInterface::INDEX_GIST,
'columnType' => 'tsvector',
],
]);
Expand Down

0 comments on commit e95071b

Please sign in to comment.