Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
bert-w committed Feb 27, 2024
1 parent 1db17cb commit 6ac84d9
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 171 deletions.
33 changes: 0 additions & 33 deletions src/Illuminate/Database/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,13 +117,6 @@ class Connection implements ConnectionInterface
*/
protected $fetchMode = PDO::FETCH_OBJ;

/**
* The fetch mode override for $statement->fetchAll() calls.
*
* @var array
*/
protected $fetchAllArgs = [];

/**
* The number of active transactions.
*
Expand Down Expand Up @@ -495,32 +488,6 @@ public function cursor($query, $bindings = [], $useReadPdo = true)
}
}

/**
* Set the arguments for calling $statement->fetchAll().
*
* @param int $mode
* @param mixed ...$args
* @return $this
*/
public function setFetchAllArgs($mode, ...$args)
{
$this->fetchAllArgs = [$mode, ...$args];

return $this;
}

/**
* Reset the arguments for calling $statement->fetchAll().
*
* @return $this
*/
public function resetFetchAllArgs()
{
$this->fetchAllArgs = [];

return $this;
}

/**
* Configure the PDO prepared statement.
*
Expand Down
2 changes: 1 addition & 1 deletion src/Illuminate/Database/ConnectionInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public function scalar($query, $bindings = [], $useReadPdo = true);
* @param bool $useReadPdo
* @return array
*/
public function select($query, $bindings = [], $useReadPdo = true);
public function select($query, $bindings = [], $useReadPdo = true, FetchMode $fetchMode = null);

/**
* Run a select statement against the database and returns a generator.
Expand Down
111 changes: 0 additions & 111 deletions tests/Database/DatabaseQueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2648,117 +2648,6 @@ public function testLeftJoinLateralSqlServer()
$this->assertSame('select * from [users] outer apply (select * from [contacts] where [contracts].[user_id] = [users].[id]) as [sub]', $builder->toSql());
}

public function testJoinLateral()
{
$builder = $this->getMySqlBuilder();
$builder->getConnection()->shouldReceive('getDatabaseName');
$builder->from('users')->joinLateral('select * from `contacts` where `contracts`.`user_id` = `users`.`id`', 'sub');
$this->assertSame('select * from `users` inner join lateral (select * from `contacts` where `contracts`.`user_id` = `users`.`id`) as `sub` on true', $builder->toSql());

$builder = $this->getMySqlBuilder();
$builder->getConnection()->shouldReceive('getDatabaseName');
$builder->from('users')->joinLateral(function ($q) {
$q->from('contacts')->whereColumn('contracts.user_id', 'users.id');
}, 'sub');
$this->assertSame('select * from `users` inner join lateral (select * from `contacts` where `contracts`.`user_id` = `users`.`id`) as `sub` on true', $builder->toSql());

$builder = $this->getMySqlBuilder();
$builder->getConnection()->shouldReceive('getDatabaseName');
$sub = $this->getMySqlBuilder();
$sub->getConnection()->shouldReceive('getDatabaseName');
$eloquentBuilder = new EloquentBuilder($sub->from('contacts')->whereColumn('contracts.user_id', 'users.id'));
$builder->from('users')->joinLateral($eloquentBuilder, 'sub');
$this->assertSame('select * from `users` inner join lateral (select * from `contacts` where `contracts`.`user_id` = `users`.`id`) as `sub` on true', $builder->toSql());

$sub1 = $this->getMySqlBuilder();
$sub1->getConnection()->shouldReceive('getDatabaseName');
$sub1 = $sub1->from('contacts')->whereColumn('contracts.user_id', 'users.id')->where('name', 'foo');

$sub2 = $this->getMySqlBuilder();
$sub2->getConnection()->shouldReceive('getDatabaseName');
$sub2 = $sub2->from('contacts')->whereColumn('contracts.user_id', 'users.id')->where('name', 'bar');

$builder = $this->getMySqlBuilder();
$builder->getConnection()->shouldReceive('getDatabaseName');
$builder->from('users')->joinLateral($sub1, 'sub1')->joinLateral($sub2, 'sub2');

$expected = 'select * from `users` ';
$expected .= 'inner join lateral (select * from `contacts` where `contracts`.`user_id` = `users`.`id` and `name` = ?) as `sub1` on true ';
$expected .= 'inner join lateral (select * from `contacts` where `contracts`.`user_id` = `users`.`id` and `name` = ?) as `sub2` on true';

$this->assertEquals($expected, $builder->toSql());
$this->assertEquals(['foo', 'bar'], $builder->getRawBindings()['join']);

$this->expectException(InvalidArgumentException::class);
$builder = $this->getMySqlBuilder();
$builder->from('users')->joinLateral(['foo'], 'sub');
}

public function testJoinLateralSQLite()
{
$this->expectException(RuntimeException::class);
$builder = $this->getSQLiteBuilder();
$builder->getConnection()->shouldReceive('getDatabaseName');
$builder->from('users')->joinLateral(function ($q) {
$q->from('contacts')->whereColumn('contracts.user_id', 'users.id');
}, 'sub')->toSql();
}

public function testJoinLateralPostgres()
{
$builder = $this->getPostgresBuilder();
$builder->getConnection()->shouldReceive('getDatabaseName');
$builder->from('users')->joinLateral(function ($q) {
$q->from('contacts')->whereColumn('contracts.user_id', 'users.id');
}, 'sub');
$this->assertSame('select * from "users" inner join lateral (select * from "contacts" where "contracts"."user_id" = "users"."id") as "sub" on true', $builder->toSql());
}

public function testJoinLateralSqlServer()
{
$builder = $this->getSqlServerBuilder();
$builder->getConnection()->shouldReceive('getDatabaseName');
$builder->from('users')->joinLateral(function ($q) {
$q->from('contacts')->whereColumn('contracts.user_id', 'users.id');
}, 'sub');
$this->assertSame('select * from [users] cross apply (select * from [contacts] where [contracts].[user_id] = [users].[id]) as [sub]', $builder->toSql());
}

public function testJoinLateralWithPrefix()
{
$builder = $this->getMySqlBuilder();
$builder->getConnection()->shouldReceive('getDatabaseName');
$builder->getGrammar()->setTablePrefix('prefix_');
$builder->from('users')->joinLateral('select * from `contacts` where `contracts`.`user_id` = `users`.`id`', 'sub');
$this->assertSame('select * from `prefix_users` inner join lateral (select * from `contacts` where `contracts`.`user_id` = `users`.`id`) as `prefix_sub` on true', $builder->toSql());
}

public function testLeftJoinLateral()
{
$builder = $this->getMySqlBuilder();
$builder->getConnection()->shouldReceive('getDatabaseName');

$sub = $this->getMySqlBuilder();
$sub->getConnection()->shouldReceive('getDatabaseName');

$builder->from('users')->leftJoinLateral($sub->from('contacts')->whereColumn('contracts.user_id', 'users.id'), 'sub');
$this->assertSame('select * from `users` left join lateral (select * from `contacts` where `contracts`.`user_id` = `users`.`id`) as `sub` on true', $builder->toSql());

$this->expectException(InvalidArgumentException::class);
$builder = $this->getBuilder();
$builder->from('users')->leftJoinLateral(['foo'], 'sub');
}

public function testLeftJoinLateralSqlServer()
{
$builder = $this->getSqlServerBuilder();
$builder->getConnection()->shouldReceive('getDatabaseName');
$builder->from('users')->leftJoinLateral(function ($q) {
$q->from('contacts')->whereColumn('contracts.user_id', 'users.id');
}, 'sub');
$this->assertSame('select * from [users] outer apply (select * from [contacts] where [contracts].[user_id] = [users].[id]) as [sub]', $builder->toSql());
}

public function testRawExpressionsInSelect()
{
$builder = $this->getBuilder();
Expand Down
36 changes: 10 additions & 26 deletions tests/Integration/Database/QueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -416,30 +416,27 @@ public function testChunkMap()
$this->assertSame('Bar Post', $results[1]);
$this->assertCount(3, DB::getQueryLog());
}


#[DataProvider('pluckProvider')]
public function testPluck(string $pluckFn): void
public function testPluck(): void
{
// Test SELECT override, since pluck will take the first column.
$this->assertSame([
'Foo Post',
'Bar Post',
], DB::table('posts')->select(['content', 'id', 'title'])->$pluckFn('title')->toArray());
], DB::table('posts')->select(['content', 'id', 'title'])->pluck('title')->toArray());

// Test without SELECT override.
$this->assertSame([
'Foo Post',
'Bar Post',
], DB::table('posts')->$pluckFn('title')->toArray());
], DB::table('posts')->pluck('title')->toArray());

// Test specific key.
$this->assertSame([
1 => 'Foo Post',
2 => 'Bar Post',
], DB::table('posts')->$pluckFn('title', 'id')->toArray());
], DB::table('posts')->pluck('title', 'id')->toArray());

$results = DB::table('posts')->$pluckFn('title', 'created_at');
$results = DB::table('posts')->pluck('title', 'created_at');

// Test timestamps (truncates RDBMS differences).
$this->assertSame([
Expand All @@ -454,24 +451,22 @@ public function testPluck(string $pluckFn): void
// Test duplicate keys (a match will override a previous match).
$this->assertSame([
'Lorem Ipsum.' => 'Bar Post',
], DB::table('posts')->$pluckFn('title', 'content')->toArray());
], DB::table('posts')->pluck('title', 'content')->toArray());

// Test null and empty string as key.
$this->assertSame([
'science' => 'Lorem Ipsum b.',
'entertainment' => 'Lorem Ipsum c.',
null => 'Lorem Ipsum d.',
'' => 'Lorem Ipsum e.',
], DB::table('comments')->$pluckFn('content', 'tag')->toArray());
], DB::table('comments')->pluck('content', 'tag')->toArray());

// Test null and numeric as key.
$this->assertSame([
1 => 'Lorem Ipsum a.',
0 => 'Lorem Ipsum b.',
null => 'Lorem Ipsum e.',
], DB::table('comments')->$pluckFn('content', 'votes')->toArray());


], DB::table('comments')->pluck('content', 'votes')->toArray());

if ($this->driver !== 'sqlsrv') {
// Skip test for MS SQL Server since integers are returned as strings unless
Expand All @@ -484,25 +479,14 @@ public function testPluck(string $pluckFn): void
'Lorem Ipsum c.' => null,
'Lorem Ipsum d.' => null,
'Lorem Ipsum e.' => null,
], DB::table('comments')->$pluckFn('votes', 'content')->toArray());
], DB::table('comments')->pluck('votes', 'content')->toArray());
}
}

public static function pluckProvider(): array
{
return [
['pluck'],
['pluckPDO'],
];
}

public function testPluckPDORawExpressions(): void
{
// Test custom query calculations.
$this->assertSame([
2 => 'FOO POST',
4 => 'BAR POST',
], DB::table('posts')->pluckPDO(
], DB::table('posts')->pluck(
DB::raw('UPPER(title)'),
DB::raw('2 * id')
)->toArray());
Expand Down

0 comments on commit 6ac84d9

Please sign in to comment.