Skip to content

Commit

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

* phpunit10 fix

* phpunit10 fix2
  • Loading branch information
darkdef committed Feb 4, 2023
1 parent 0fd9abe commit d48d3c4
Show file tree
Hide file tree
Showing 10 changed files with 51 additions and 25 deletions.
3 changes: 0 additions & 3 deletions src/Expression/JsonExpression.php
Expand Up @@ -19,9 +19,6 @@
*/
class JsonExpression implements ExpressionInterface, JsonSerializable
{
public const TYPE_JSON = 'json';
public const TYPE_JSONB = 'jsonb';

public function __construct(protected mixed $value, private string|null $type = null)
{
if ($value instanceof self) {
Expand Down
5 changes: 0 additions & 5 deletions src/QueryBuilder/QueryBuilderInterface.php
Expand Up @@ -12,11 +12,6 @@

interface QueryBuilderInterface extends DDLQueryBuilderInterface, DMLQueryBuilderInterface, DQLQueryBuilderInterface
{
/**
* Defines a UNIQUE index type for {@see createIndex()}.
*/
public const INDEX_UNIQUE = 'UNIQUE';

/**
* Helper method to add $value to $params array using {@see PARAM_PREFIX}.
*
Expand Down
2 changes: 1 addition & 1 deletion src/Schema/AbstractColumnSchemaBuilder.php
Expand Up @@ -20,7 +20,7 @@
* For example, the following code creates a column schema for an integer column:
*
* ```php
* $column = (new ColumnSchemaBuilder(Schema::TYPE_INTEGER))->notNull()->defaultValue(0);
* $column = (new ColumnSchemaBuilder(SchemaInterface::TYPE_INTEGER))->notNull()->defaultValue(0);
* ```
*
* The AbstractColumnSchemaBuilder class provides a fluent interface, which means that the methods can be chained
Expand Down
28 changes: 28 additions & 0 deletions src/Schema/SchemaInterface.php
Expand Up @@ -24,6 +24,33 @@ interface SchemaInterface extends ConstraintSchemaInterface
public const FOREIGN_KEYS = 'foreignKeys';
public const DEFAULT_VALUES = 'defaultValues';
public const UNIQUES = 'uniques';
public const DEFAULTS = 'defaults';

/**
* Types of supported indexes {@see QueryBuilderInterface::createIndex()}.
* MySQL, MSSQL, Oracle, PostgreSQL, SQLite
*/
public const INDEX_UNIQUE = 'UNIQUE';

/* MySQL, PostgreSQL */
public const INDEX_BTREE = 'BTREE';
public const INDEX_HASH = 'HASH';

/* MySQL */
public const INDEX_FULLTEXT = 'FULLTEXT';
public const INDEX_SPATIAL = 'SPATIAL';

/* PostgreSQL */
public const INDEX_GIST = 'GIST';
public const INDEX_GIN = 'GIN';
public const INDEX_BRIN = 'BRIN';

/* MS SQL */
public const INDEX_CLUSTERED = 'CLUSTERED';
public const INDEX_NONCLUSTERED = 'NONCLUSTERED';

/* Oracle */
public const INDEX_BITMAP = 'BITMAP';

public const TYPE_PK = 'pk';
public const TYPE_UPK = 'upk';
Expand All @@ -47,6 +74,7 @@ interface SchemaInterface extends ConstraintSchemaInterface
public const TYPE_BOOLEAN = 'boolean';
public const TYPE_MONEY = 'money';
public const TYPE_JSON = 'json';
public const TYPE_JSONB = 'jsonb';

public const PHP_TYPE_INTEGER = 'integer';
public const PHP_TYPE_STRING = 'string';
Expand Down
8 changes: 4 additions & 4 deletions tests/AbstractQueryBuilderTest.php
Expand Up @@ -51,13 +51,13 @@ public function testAddColumn(): void

$qb = $db->getQueryBuilder();
$schema = $db->getSchema();
$sql = $qb->addColumn('table', 'column', $schema::TYPE_STRING);
$sql = $qb->addColumn('table', 'column', SchemaInterface::TYPE_STRING);

$this->assertSame(
DbHelper::replaceQuotes(
<<<SQL
ALTER TABLE [[table]] ADD [[column]]
SQL . ' ' . $qb->getColumnType($schema::TYPE_STRING),
SQL . ' ' . $qb->getColumnType(SchemaInterface::TYPE_STRING),
$db->getName(),
),
$sql,
Expand Down Expand Up @@ -171,13 +171,13 @@ public function testAlterColumn(): void

$qb = $db->getQueryBuilder();
$schema = $db->getSchema();
$sql = $qb->alterColumn('customer', 'email', $schema::TYPE_STRING);
$sql = $qb->alterColumn('customer', 'email', SchemaInterface::TYPE_STRING);

$this->assertSame(
DbHelper::replaceQuotes(
<<<SQL
ALTER TABLE [[customer]] CHANGE [[email]] [[email]]
SQL . ' ' . $qb->getColumnType($schema::TYPE_STRING),
SQL . ' ' . $qb->getColumnType(SchemaInterface::TYPE_STRING),
$db->getName(),
),
$sql,
Expand Down
9 changes: 8 additions & 1 deletion tests/AbstractQueryTest.php
Expand Up @@ -820,8 +820,15 @@ public function testPopulateWithIncorrectIndexByWithObject(Closure|string|null $

$rows = json_decode(json_encode($rows));

$this->expectWarning();
set_error_handler(static function (int $errno, string $errstr) {
throw new \Exception('E_WARNING: ' . $errstr, $errno);
}, E_WARNING);

$this->expectExceptionMessageMatches('/^E_WARNING: /');

$query->populate($rows);

restore_error_handler();
}

public function populateProviderWithIndexBy(): array
Expand Down
7 changes: 3 additions & 4 deletions tests/Common/CommonSchemaTest.php
Expand Up @@ -17,7 +17,6 @@
use Yiisoft\Db\Exception\InvalidCallException;
use Yiisoft\Db\Exception\InvalidConfigException;
use Yiisoft\Db\Exception\NotSupportedException;
use Yiisoft\Db\QueryBuilder\QueryBuilderInterface;
use Yiisoft\Db\Schema\SchemaInterface;
use Yiisoft\Db\Schema\TableSchemaInterface;
use Yiisoft\Db\Tests\AbstractSchemaTest;
Expand Down Expand Up @@ -140,7 +139,7 @@ public function testFindUniquesIndexes(): void
'somecolUnique',
'uniqueIndex',
'somecol',
QueryBuilderInterface::INDEX_UNIQUE,
SchemaInterface::INDEX_UNIQUE,
)->execute();
$tableSchema = $schema->getTableSchema('uniqueIndex', true);

Expand All @@ -159,7 +158,7 @@ public function testFindUniquesIndexes(): void
'someCol2Unique',
'uniqueIndex',
'someCol2',
QueryBuilderInterface::INDEX_UNIQUE,
SchemaInterface::INDEX_UNIQUE,
)->execute();
$tableSchema = $schema->getTableSchema('uniqueIndex', true);

Expand All @@ -174,7 +173,7 @@ public function testFindUniquesIndexes(): void
'another unique index',
'uniqueIndex',
'someCol3',
QueryBuilderInterface::INDEX_UNIQUE,
SchemaInterface::INDEX_UNIQUE,
)->execute();
$tableSchema = $schema->getTableSchema('uniqueIndex', true);

Expand Down
6 changes: 3 additions & 3 deletions tests/Provider/AbstractCommandProvider.php
Expand Up @@ -6,7 +6,7 @@

use Yiisoft\Db\Expression\Expression;
use Yiisoft\Db\Query\Query;
use Yiisoft\Db\QueryBuilder\QueryBuilderInterface;
use Yiisoft\Db\Schema\SchemaInterface;
use Yiisoft\Db\Tests\Support\DbHelper;
use Yiisoft\Db\Tests\Support\TestTrait;

Expand Down Expand Up @@ -350,7 +350,7 @@ public function createIndex(): array
{
return [
['{{test_idx_constraint_1}}', '{{test_idx}}', 'int1', null, null],
['{{test_idx_constraint_2}}', '{{test_idx}}', ['int1'], QueryBuilderInterface::INDEX_UNIQUE, null],
['{{test_idx_constraint_2}}', '{{test_idx}}', ['int1'], SchemaInterface::INDEX_UNIQUE, null],
['{{test_idx_constraint_3}}', '{{test_idx}}', ['int1', 'int2'], null, null],
];
}
Expand Down Expand Up @@ -388,7 +388,7 @@ public function createIndexSql(): array
'{{name}}',
'{{table}}',
['column1', 'column2'],
QueryBuilderInterface::INDEX_UNIQUE,
SchemaInterface::INDEX_UNIQUE,
'',
DbHelper::replaceQuotes(
<<<SQL
Expand Down
5 changes: 3 additions & 2 deletions tests/Provider/AbstractQueryBuilderProvider.php
Expand Up @@ -10,6 +10,7 @@
use Yiisoft\Db\QueryBuilder\Condition\InCondition;
use Yiisoft\Db\QueryBuilder\Condition\LikeCondition;
use Yiisoft\Db\QueryBuilder\QueryBuilderInterface;
use Yiisoft\Db\Schema\SchemaInterface;
use Yiisoft\Db\Tests\Support\DbHelper;
use Yiisoft\Db\Tests\Support\TestTrait;
use Yiisoft\Db\Tests\Support\TraversableObject;
Expand Down Expand Up @@ -867,7 +868,7 @@ public function createIndex(): array
$name1,
$tableName,
'C_index_1',
QueryBuilderInterface::INDEX_UNIQUE,
SchemaInterface::INDEX_UNIQUE,
),
],
'create unique (2 columns)' => [
Expand All @@ -878,7 +879,7 @@ public function createIndex(): array
$name2,
$tableName,
'C_index_2_1, C_index_2_2',
QueryBuilderInterface::INDEX_UNIQUE,
SchemaInterface::INDEX_UNIQUE,
),
],
];
Expand Down
3 changes: 1 addition & 2 deletions tests/Provider/AbstractSchemaProvider.php
Expand Up @@ -9,7 +9,6 @@
use Yiisoft\Db\Constraint\Constraint;
use Yiisoft\Db\Constraint\ForeignKeyConstraint;
use Yiisoft\Db\Constraint\IndexConstraint;
use Yiisoft\Db\QueryBuilder\QueryBuilderInterface;
use Yiisoft\Db\Schema\SchemaInterface;
use Yiisoft\Db\Tests\Support\AnyValue;

Expand Down Expand Up @@ -200,7 +199,7 @@ public function withIndexDataProvider(): array
{
return [
[
'indexType' => QueryBuilderInterface::INDEX_UNIQUE,
'indexType' => SchemaInterface::INDEX_UNIQUE,
'indexMethod' => null,
'columnType' => null,
'isPrimary' => false,
Expand Down

0 comments on commit d48d3c4

Please sign in to comment.