Skip to content
Open
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
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ Using the package, you can perform common database tasks such as creating, readi
records in a database table, as well as executing raw SQL queries.

```php
$rows = (new Query($db))
->select(['id', 'email'])
$rows = $db->select(['id', 'email'])
->from('{{%user}}')
->where(['last_name' => 'Smith'])
->limit(10)
Expand Down
3 changes: 1 addition & 2 deletions docs/guide/en/query-builder.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ use Yiisoft\Db\Query\Query;

/** @var ConnectionInterface $db */

$rows = (new Query($db))
->select(['id', 'email'])
$rows = $db->select(['id', 'email'])
->from('{{%user}}')
->where(['last_name' => 'Smith'])
->limit(10)
Expand Down
4 changes: 2 additions & 2 deletions docs/guide/en/query/select.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ use Yiisoft\Db\Query\Query;

/** @var ConnectionInterface $db */

$subQuery = (new Query($db))->select('COUNT(*)')->from('{{%user}}');
$query = (new Query($db))->select(['id', 'count' => $subQuery])->from('{{%post}}');
$subQuery = $db->select('COUNT(*)')->from('{{%user}}');
$query = $db->select(['id', 'count' => $subQuery])->from('{{%post}}');
```

The equivalent SQL is:
Expand Down
4 changes: 2 additions & 2 deletions docs/guide/en/query/union.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ use Yiisoft\Db\Query\Query;

/** @var ConnectionInterface $db */

$query1 = (new Query($db))->select("id, category_id AS type, name")->from('{{%post}}')->limit(10);
$query2 = (new Query($db))->select('id, type, name')->from('{{%user}}')->limit(10);
$query1 = $db->select("id, category_id AS type, name")->from('{{%post}}')->limit(10);
$query2 = $db->select('id, type, name')->from('{{%user}}')->limit(10);
$query1->union($query2);
```

Expand Down
2 changes: 1 addition & 1 deletion docs/guide/en/query/where.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ use Yiisoft\Db\Query\Query;

/** @var ConnectionInterface $db */

$userQuery = (new Query($db))->select('id')->from('user');
$userQuery = $db->select('id')->from('user');
$query->where(['id' => $userQuery]);
```

Expand Down
9 changes: 3 additions & 6 deletions docs/guide/en/query/with-query.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,15 @@ use Yiisoft\Db\Query\Query;

/** @var ConnectionInterface $db */

$initialQuery = (new Query($db))
->select(['parent', 'child'])
$initialQuery = $db->select(['parent', 'child'])
->from(['aic' => '{{%auth_item_child}}'])
->where(['parent' => 'admin']);

$recursiveQuery = (new Query($db))
->select(['aic.parent', 'aic.child'])
$recursiveQuery = $db->select(['aic.parent', 'aic.child'])
->from(['aic' => '{{%auth_item_child}}'])
->innerJoin('t1', 't1.child = aic.parent');

$mainQuery = (new Query($db))
->select(['parent', 'child'])
$mainQuery = $db->select(['parent', 'child'])
->from('{{%t1}}')
->withQuery($initialQuery->union($recursiveQuery), 't1', true);
```
Expand Down
4 changes: 2 additions & 2 deletions docs/guide/en/schema/typecasting.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ the values when retrieving them from the database. Using these methods, values a
they are returned.

```php
$query = (new Query($db))->from('customer')->where(['id' => 1]);
$query = $db->createQuery()->from('customer')->where(['id' => 1]);

$row = $query->withTypecasting()->one();
$isActive = $row['is_active'];
Expand All @@ -126,7 +126,7 @@ Some databases support date and time types, such as `timestamp`, `datetime`, `da
These types are casted to `DateTimeImmutable` objects using `DateTimeColumn` class when type casting is enabled.

```php
$query = (new Query($db))->from('customer')->where(['id' => 1]);
$query = $db->createQuery()->from('customer')->where(['id' => 1]);

$row = $query->withTypecasting()->one();
$createdAt = $row['created_at']; // `DateTimeImmutable` object
Expand Down
3 changes: 1 addition & 2 deletions docs/guide/pt-BR/query-builder.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ use Yiisoft\Db\Query\Query;

/** @var ConnectionInterface $db */

$rows = (new Query($db))
->select(['id', 'email'])
$rows = $db->select(['id', 'email'])
->from('{{%user}}')
->where(['last_name' => 'Smith'])
->limit(10)
Expand Down
4 changes: 2 additions & 2 deletions docs/guide/pt-BR/query/select.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ use Yiisoft\Db\Query\Query;

/** @var ConnectionInterface $db */

$subQuery = (new Query($db))->select('COUNT(*)')->from('{{%user}}');
$query = (new Query($db))->select(['id', 'count' => $subQuery])->from('{{%post}}');
$subQuery = $db->select('COUNT(*)')->from('{{%user}}');
$query = $db->select(['id', 'count' => $subQuery])->from('{{%post}}');
```

O SQL equivalente é:
Expand Down
4 changes: 2 additions & 2 deletions docs/guide/pt-BR/query/union.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ use Yiisoft\Db\Query\Query;

/** @var ConnectionInterface $db */

$query1 = (new Query($db))->select("id, category_id AS type, name")->from('{{%post}}')->limit(10);
$query2 = (new Query($db))->select('id, type, name')->from('{{%user}}')->limit(10);
$query1 = $db->select("id, category_id AS type, name")->from('{{%post}}')->limit(10);
$query2 = $db->select('id, type, name')->from('{{%user}}')->limit(10);
$query1->union($query2);
```

Expand Down
2 changes: 1 addition & 1 deletion docs/guide/pt-BR/query/where.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ use Yiisoft\Db\Query\Query;

/** @var ConnectionInterface $db */

$userQuery = (new Query($db))->select('id')->from('user');
$userQuery = $db->select('id')->from('user');
$query->where(['id' => $userQuery]);
```

Expand Down
9 changes: 3 additions & 6 deletions docs/guide/pt-BR/query/with-query.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,15 @@ use Yiisoft\Db\Query\Query;

/** @var ConnectionInterface $db */

$initialQuery = (new Query($db))
->select(['parent', 'child'])
$initialQuery = $db->select(['parent', 'child'])
->from(['aic' => '{{%auth_item_child}}'])
->where(['parent' => 'admin']);

$recursiveQuery = (new Query($db))
->select(['aic.parent', 'aic.child'])
$recursiveQuery = $db->select(['aic.parent', 'aic.child'])
->from(['aic' => '{{%auth_item_child}}'])
->innerJoin('t1', 't1.child = aic.parent');

$mainQuery = (new Query($db))
->select(['parent', 'child'])
$mainQuery = $db->select(['parent', 'child'])
->from('{{%t1}}')
->withQuery($initialQuery->union($recursiveQuery), 't1', true);
```
Expand Down
2 changes: 1 addition & 1 deletion src/Query/QueryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ public function prepare(QueryBuilderInterface $builder): self;
* For example:
*
* ```php
* $users = (new Query($db))
* $users = $db->createQuery()
* ->from('user')
* ->resultCallback(function (array $rows): array {
* foreach ($rows as &$row) {
Expand Down
3 changes: 1 addition & 2 deletions tests/AbstractConnectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
use Yiisoft\Db\Profiler\ContextInterface;
use Yiisoft\Db\Profiler\ProfilerInterface;
use Yiisoft\Db\Query\BatchQueryResult;
use Yiisoft\Db\Query\Query;
use Yiisoft\Db\Schema\Column\ColumnBuilder;
use Yiisoft\Db\Tests\Support\Assert;
use Yiisoft\Db\Tests\Support\DbHelper;
Expand All @@ -32,7 +31,7 @@ public function testCreateBatchQueryResult(): void
{
$db = $this->getConnection();

$query = (new Query($db))->from('customer');
$query = $db->createQuery()->from('customer');

$this->assertInstanceOf(BatchQueryResult::class, $db->createBatchQueryResult($query));
}
Expand Down
32 changes: 13 additions & 19 deletions tests/Common/CommonCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ public function testBatchInsert(
$command->prepare(false);
$command->execute();

$this->assertEquals($insertedRow, (new Query($db))->from($table)->count());
$this->assertEquals($insertedRow, $db->createQuery()->from($table)->count());

$db->close();
}
Expand Down Expand Up @@ -364,8 +364,7 @@ public function testBatchInsertWithDuplicates(): void

$this->assertSame(1, $command->execute());

$result = (new Query($db))
->select(['email', 'name', 'address'])
$result = $db->select(['email', 'name', 'address'])
->from('{{customer}}')
->where(['=', '{{email}}', '[email protected]'])
->one();
Expand All @@ -392,7 +391,7 @@ public function testBatchInsertWithManyData(): void

$this->assertSame($attemptsInsertRows, $command->execute());

$insertedRowsCount = (new Query($db))->from('{{customer}}')->count();
$insertedRowsCount = $db->createQuery()->from('{{customer}}')->count();

$this->assertGreaterThanOrEqual($attemptsInsertRows, $insertedRowsCount);

Expand Down Expand Up @@ -491,7 +490,7 @@ public function testCreateView(): void

$command = $db->createCommand();
$schema = $db->getSchema();
$subQuery = (new Query($db))->select('{{bar}}')->from('{{testCreateViewTable}}')->where(['>', 'bar', '5']);
$subQuery = $db->select('{{bar}}')->from('{{testCreateViewTable}}')->where(['>', 'bar', '5']);

if ($schema->getTableSchema('{{testCreateView}}') !== null) {
$command->dropView('{{testCreateView}}')->execute();
Expand Down Expand Up @@ -1107,7 +1106,7 @@ public function testsInsertQueryAsColumnValue(): void
$orderId = $db->getLastInsertId();
}

$columnValueQuery = (new Query($db))->select('{{created_at}}')->from('{{order}}')->where(['id' => $orderId]);
$columnValueQuery = $db->select('{{created_at}}')->from('{{order}}')->where(['id' => $orderId]);
$command->insert(
'{{order_with_null_fk}}',
['customer_id' => $orderId, 'created_at' => $columnValueQuery, 'total' => 42],
Expand Down Expand Up @@ -1150,8 +1149,7 @@ public function testInsertSelect(): void
'{{customer}}',
['email' => '[email protected]', 'name' => 'test', 'address' => 'test address']
)->execute();
$query = (new Query($db))
->select(['{{customer}}.{{email}} as name', '{{name}} as email', '{{address}}'])
$query = $db->select(['{{customer}}.{{email}} as name', '{{name}} as email', '{{address}}'])
->from('{{customer}}')
->where(['and', ['<>', 'name', 'foo'], ['status' => [0, 1, 2, 3]]]);
$command->insert('{{customer}}', $query)->execute();
Expand Down Expand Up @@ -1196,8 +1194,7 @@ public function testInsertSelectAlias(): void
'address' => 'test address',
]
)->execute();
$query = (new Query($db))
->select(['email' => '{{customer}}.{{email}}', 'address' => 'name', 'name' => 'address'])
$query = $db->select(['email' => '{{customer}}.{{email}}', 'address' => 'name', 'name' => 'address'])
->from('{{customer}}')
->where(['and', ['<>', 'name', 'foo'], ['status' => [0, 1, 2, 3]]]);
$command->insert('{{customer}}', $query)->execute();
Expand Down Expand Up @@ -1701,7 +1698,7 @@ public function testUpdate(

$this->assertSame($expectedCount, $count);

$values = (new Query($db))
$values = $db->createQuery()
->from($table)
->where($conditions, $params)
->limit(1)
Expand Down Expand Up @@ -1822,8 +1819,7 @@ protected function performAndCompareUpsertResult(PdoConnectionInterface $db, arr

$command->execute();

$actual = (new Query($db))
->select(['email', 'address' => new Expression($this->upsertTestCharCast), 'status'])
$actual = $db->select(['email', 'address' => new Expression($this->upsertTestCharCast), 'status'])
->from('{{T_upsert}}')
->one();
$this->assertEquals($expected, $actual, $this->upsertTestCharCast);
Expand Down Expand Up @@ -1869,7 +1865,7 @@ public function testInsertReturningPksWithQuery(): void
{
$db = $this->getConnection(true);

$query = (new Query($db))->select([
$query = $db->select([
'name' => new Expression("'test_1'"),
'email' => new Expression("'[email protected]'"),
]);
Expand Down Expand Up @@ -1916,8 +1912,7 @@ public function testUpsertReturning(
$this->assertEquals($expectedValues, $returnedValues);

if (!empty($returnColumns)) {
$selectedValues = (new Query($db))
->select(array_keys($expectedValues))
$selectedValues = $db->select(array_keys($expectedValues))
->from($table)
->where($selectCondition)
->one();
Expand Down Expand Up @@ -2077,8 +2072,7 @@ public function testUuid(): void
'uuid_pk' => $uuidValue,
])->execute();

$uuid = (new Query($db))
->select(['[[uuid_pk]]'])
$uuid = $db->select(['[[uuid_pk]]'])
->from($tableName)
->where(['int_col' => 1])
->scalar();
Expand Down Expand Up @@ -2125,7 +2119,7 @@ public function testJsonTable(): void
$this->assertSame('json_col', $tableSchema->getColumn('json_col')->getName());
$this->assertSame(ColumnType::JSON, $tableSchema->getColumn('json_col')->getType());

$value = (new Query($db))->select('json_col')->from('json_table')->where(['id' => 1])->scalar();
$value = $db->select('json_col')->from('json_table')->where(['id' => 1])->scalar();
$this->assertSame('{"a":1,"b":2}', str_replace(' ', '', $value));
}
}
3 changes: 1 addition & 2 deletions tests/Db/Expression/Value/Builder/ArrayValueBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
use Yiisoft\Db\Constant\DataType;
use Yiisoft\Db\Expression\Value\ArrayValue;
use Yiisoft\Db\Expression\Value\Builder\ArrayValueBuilder;
use Yiisoft\Db\Query\Query;
use Yiisoft\Db\Schema\Data\LazyArray;
use Yiisoft\Db\Schema\Data\LazyArrayInterface;
use Yiisoft\Db\Schema\Data\JsonLazyArray;
Expand Down Expand Up @@ -71,7 +70,7 @@ public function testBuildQueryExpression(): void

$params = [];
$builder = new ArrayValueBuilder($qb);
$expression = new ArrayValue((new Query($db))->select('json_field')->from('json_table'));
$expression = new ArrayValue($db->select('json_field')->from('json_table'));

$this->assertSame('(SELECT [json_field] FROM [json_table])', $builder->build($expression, $params));
$this->assertSame([], $params);
Expand Down
3 changes: 1 addition & 2 deletions tests/Db/Expression/Value/Builder/JsonValueBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
use Yiisoft\Db\Constant\DataType;
use Yiisoft\Db\Expression\Value\JsonValue;
use Yiisoft\Db\Expression\Value\Builder\JsonValueBuilder;
use Yiisoft\Db\Query\Query;
use Yiisoft\Db\Schema\Data\LazyArray;
use Yiisoft\Db\Schema\Data\JsonLazyArray;
use Yiisoft\Db\Schema\Data\StructuredLazyArray;
Expand Down Expand Up @@ -82,7 +81,7 @@ public function testBuildQueryExpression(): void

$params = [];
$builder = new JsonValueBuilder($qb);
$expression = new JsonValue((new Query($db))->select('json_field')->from('json_table'));
$expression = new JsonValue($db->select('json_field')->from('json_table'));

$this->assertSame('(SELECT [json_field] FROM [json_table])', $builder->build($expression, $params));
$this->assertSame([], $params);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
use Yiisoft\Db\Constant\DataType;
use Yiisoft\Db\Expression\Value\StructuredValue;
use Yiisoft\Db\Expression\Value\Builder\StructuredValueBuilder;
use Yiisoft\Db\Query\Query;
use Yiisoft\Db\Schema\Column\AbstractStructuredColumn;
use Yiisoft\Db\Schema\Column\ColumnBuilder;
use Yiisoft\Db\Schema\Data\JsonLazyArray;
Expand Down Expand Up @@ -81,7 +80,7 @@ public function testBuildQueryExpression(): void

$params = [];
$builder = new StructuredValueBuilder($qb);
$expression = new StructuredValue((new Query($db))->select('json_field')->from('json_table'));
$expression = new StructuredValue($db->select('json_field')->from('json_table'));

$this->assertSame('(SELECT [json_field] FROM [json_table])', $builder->build($expression, $params));
$this->assertSame([], $params);
Expand Down
Loading