Skip to content

Commit

Permalink
Merge pull request #1640 from codeigniter4/dbcompiled
Browse files Browse the repository at this point in the history
Update getCompiledX methods in BaseBuilder to return fully compiled q…
  • Loading branch information
lonnieezell authored Dec 31, 2018
2 parents a540312 + d560ab2 commit 9ea1f48
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 35 deletions.
29 changes: 25 additions & 4 deletions system/Database/BaseBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -1399,11 +1399,32 @@ public function getCompiledSelect($reset = true)
$this->resetSelect();
}

return $select;
return $this->compileFinalQuery($select);
}

//--------------------------------------------------------------------

/**
* Returns a finalized, compiled query string with the bindings
* inserted and prefixes swapped out.
*
* @param string $sql
*
* @return mixed|string
*/
protected function compileFinalQuery(string $sql): string
{
$query = new Query($this->db);
$query->setQuery($sql, $this->binds);

if (! empty($this->db->swapPre) && ! empty($this->db->DBPrefix))
{
$query->swapPrefix($this->db->DBPrefix, $this->db->swapPre);
}

return $query->getQuery();
}

/**
* Get
*
Expand Down Expand Up @@ -1741,7 +1762,7 @@ public function getCompiledInsert($reset = true)
$this->resetWrite();
}

return $sql;
return $this->compileFinalQuery($sql);
}

//--------------------------------------------------------------------
Expand Down Expand Up @@ -1931,7 +1952,7 @@ public function getCompiledUpdate($reset = true)
$this->resetWrite();
}

return $sql;
return $this->compileFinalQuery($sql);
}

//--------------------------------------------------------------------
Expand Down Expand Up @@ -2312,7 +2333,7 @@ public function getCompiledDelete($reset = true)
$sql = $this->delete($table, '', null, $reset);
$this->returnDeleteSQL = false;

return $sql;
return $this->compileFinalQuery($sql);
}

//--------------------------------------------------------------------
Expand Down
10 changes: 5 additions & 5 deletions tests/system/Database/Builder/GroupTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public function testOrHavingBy()
->having('id >', 3)
->orHaving('SUM(id) > 2');

$expectedSQL = 'SELECT "name" FROM "user" GROUP BY "name" HAVING "id" > :id: OR SUM(id) > 2';
$expectedSQL = 'SELECT "name" FROM "user" GROUP BY "name" HAVING "id" > 3 OR SUM(id) > 2';

$this->assertEquals($expectedSQL, str_replace("\n", ' ', $builder->getCompiledSelect()));
}
Expand All @@ -73,7 +73,7 @@ public function testAndGroups()
->groupEnd()
->where('name', 'Darth');

$expectedSQL = 'SELECT * FROM "user" WHERE ( "id" > :id: AND "name" != :name: ) AND "name" = :name0:';
$expectedSQL = 'SELECT * FROM "user" WHERE ( "id" > 3 AND "name" != \'Luke\' ) AND "name" = \'Darth\'';

$this->assertEquals($expectedSQL, str_replace("\n", ' ', $builder->getCompiledSelect()));
}
Expand All @@ -90,7 +90,7 @@ public function testOrGroups()
->where('name !=', 'Luke')
->groupEnd();

$expectedSQL = 'SELECT * FROM "user" WHERE "name" = :name: OR ( "id" > :id: AND "name" != :name0: )';
$expectedSQL = 'SELECT * FROM "user" WHERE "name" = \'Darth\' OR ( "id" > 3 AND "name" != \'Luke\' )';

$this->assertEquals($expectedSQL, str_replace("\n", ' ', $builder->getCompiledSelect()));
}
Expand All @@ -107,7 +107,7 @@ public function testNotGroups()
->where('name !=', 'Luke')
->groupEnd();

$expectedSQL = 'SELECT * FROM "user" WHERE "name" = :name: AND NOT ( "id" > :id: AND "name" != :name0: )';
$expectedSQL = 'SELECT * FROM "user" WHERE "name" = \'Darth\' AND NOT ( "id" > 3 AND "name" != \'Luke\' )';

$this->assertEquals($expectedSQL, str_replace("\n", ' ', $builder->getCompiledSelect()));
}
Expand All @@ -124,7 +124,7 @@ public function testOrNotGroups()
->where('name !=', 'Luke')
->groupEnd();

$expectedSQL = 'SELECT * FROM "user" WHERE "name" = :name: OR NOT ( "id" > :id: AND "name" != :name0: )';
$expectedSQL = 'SELECT * FROM "user" WHERE "name" = \'Darth\' OR NOT ( "id" > 3 AND "name" != \'Luke\' )';

$this->assertEquals($expectedSQL, str_replace("\n", ' ', $builder->getCompiledSelect()));
}
Expand Down
2 changes: 1 addition & 1 deletion tests/system/Database/Builder/InsertTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function testSimpleInsert()
];
$builder->insert($insertData, true, true);

$expectedSQL = 'INSERT INTO "jobs" ("id", "name") VALUES (:id:, :name:)';
$expectedSQL = 'INSERT INTO "jobs" ("id", "name") VALUES (1, \'Grocery Sales\')';
$expectedBinds = $insertData;

$this->assertEquals($expectedSQL, str_replace("\n", ' ', $builder->getCompiledInsert()));
Expand Down
16 changes: 8 additions & 8 deletions tests/system/Database/Builder/LikeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public function testSimpleLike()

$builder->like('name', 'veloper');

$expectedSQL = "SELECT * FROM \"job\" WHERE \"name\" LIKE :name: ESCAPE '!'";
$expectedSQL = "SELECT * FROM \"job\" WHERE \"name\" LIKE '%veloper%' ESCAPE '!'";
$expectedBinds = ['name' => '%veloper%'];

$this->assertEquals($expectedSQL, str_replace("\n", ' ', $builder->getCompiledSelect()));
Expand All @@ -39,7 +39,7 @@ public function testLikeNoSide()

$builder->like('name', 'veloper', 'none');

$expectedSQL = "SELECT * FROM \"job\" WHERE \"name\" LIKE :name: ESCAPE '!'";
$expectedSQL = "SELECT * FROM \"job\" WHERE \"name\" LIKE 'veloper' ESCAPE '!'";
$expectedBinds = ['name' => 'veloper'];

$this->assertEquals($expectedSQL, str_replace("\n", ' ', $builder->getCompiledSelect()));
Expand All @@ -54,7 +54,7 @@ public function testLikeBeforeOnly()

$builder->like('name', 'veloper', 'before');

$expectedSQL = "SELECT * FROM \"job\" WHERE \"name\" LIKE :name: ESCAPE '!'";
$expectedSQL = "SELECT * FROM \"job\" WHERE \"name\" LIKE '%veloper' ESCAPE '!'";
$expectedBinds = ['name' => '%veloper'];

$this->assertEquals($expectedSQL, str_replace("\n", ' ', $builder->getCompiledSelect()));
Expand All @@ -69,7 +69,7 @@ public function testLikeAfterOnly()

$builder->like('name', 'veloper', 'after');

$expectedSQL = "SELECT * FROM \"job\" WHERE \"name\" LIKE :name: ESCAPE '!'";
$expectedSQL = "SELECT * FROM \"job\" WHERE \"name\" LIKE 'veloper%' ESCAPE '!'";
$expectedBinds = ['name' => 'veloper%'];

$this->assertEquals($expectedSQL, str_replace("\n", ' ', $builder->getCompiledSelect()));
Expand All @@ -84,7 +84,7 @@ public function testOrLike()

$builder->like('name', 'veloper')->orLike('name', 'ian');

$expectedSQL = "SELECT * FROM \"job\" WHERE \"name\" LIKE :name: ESCAPE '!' OR \"name\" LIKE :name0: ESCAPE '!'";
$expectedSQL = "SELECT * FROM \"job\" WHERE \"name\" LIKE '%veloper%' ESCAPE '!' OR \"name\" LIKE '%ian%' ESCAPE '!'";
$expectedBinds = [
'name' => '%veloper%',
'name0' => '%ian%',
Expand All @@ -102,7 +102,7 @@ public function testNotLike()

$builder->notLike('name', 'veloper');

$expectedSQL = "SELECT * FROM \"job\" WHERE \"name\" NOT LIKE :name: ESCAPE '!'";
$expectedSQL = "SELECT * FROM \"job\" WHERE \"name\" NOT LIKE '%veloper%' ESCAPE '!'";
$expectedBinds = ['name' => '%veloper%'];

$this->assertEquals($expectedSQL, str_replace("\n", ' ', $builder->getCompiledSelect()));
Expand All @@ -117,7 +117,7 @@ public function testOrNotLike()

$builder->like('name', 'veloper')->orNotLike('name', 'ian');

$expectedSQL = "SELECT * FROM \"job\" WHERE \"name\" LIKE :name: ESCAPE '!' OR \"name\" NOT LIKE :name0: ESCAPE '!'";
$expectedSQL = "SELECT * FROM \"job\" WHERE \"name\" LIKE '%veloper%' ESCAPE '!' OR \"name\" NOT LIKE '%ian%' ESCAPE '!'";
$expectedBinds = [
'name' => '%veloper%',
'name0' => '%ian%',
Expand All @@ -138,7 +138,7 @@ public function testCaseInsensitiveLike()

$builder->like('name', 'VELOPER', 'both', null, true);

$expectedSQL = "SELECT * FROM \"job\" WHERE LOWER(name) LIKE :name: ESCAPE '!'";
$expectedSQL = "SELECT * FROM \"job\" WHERE LOWER(name) LIKE '%veloper%' ESCAPE '!'";
$expectedBinds = ['name' => '%veloper%'];

$this->assertEquals($expectedSQL, str_replace("\n", ' ', $builder->getCompiledSelect()));
Expand Down
14 changes: 7 additions & 7 deletions tests/system/Database/Builder/UpdateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public function testUpdate()

$builder->where('id', 1)->update(['name' => 'Programmer'], null, null, true);

$expectedSQL = 'UPDATE "jobs" SET "name" = :name: WHERE "id" = :id:';
$expectedSQL = 'UPDATE "jobs" SET "name" = \'Programmer\' WHERE "id" = 1';
$expectedBinds = [
'id' => 1,
'name' => 'Programmer',
Expand All @@ -43,7 +43,7 @@ public function testUpdateInternalWhereAndLimit()

$builder->update(['name' => 'Programmer'], ['id' => 1], 5, true);

$expectedSQL = 'UPDATE "jobs" SET "name" = :name: WHERE "id" = :id: LIMIT 5';
$expectedSQL = 'UPDATE "jobs" SET "name" = \'Programmer\' WHERE "id" = 1 LIMIT 5';
$expectedBinds = [
'id' => 1,
'name' => 'Programmer',
Expand All @@ -61,7 +61,7 @@ public function testUpdateWithSet()

$builder->set('name', 'Programmer')->where('id', 1)->update(null, null, null, true);

$expectedSQL = 'UPDATE "jobs" SET "name" = :name: WHERE "id" = :id:';
$expectedSQL = 'UPDATE "jobs" SET "name" = \'Programmer\' WHERE "id" = 1';
$expectedBinds = [
'id' => 1,
'name' => 'Programmer',
Expand Down Expand Up @@ -177,7 +177,7 @@ public function testUpdateWithWhereSameColumn()

$builder->update(['name' => 'foobar'], ['name' => 'Programmer'], null, true);

$expectedSQL = 'UPDATE "jobs" SET "name" = :name: WHERE "name" = :name0:';
$expectedSQL = 'UPDATE "jobs" SET "name" = \'foobar\' WHERE "name" = \'Programmer\'';
$expectedBinds = [
'name' => 'foobar',
'name0' => 'Programmer',
Expand All @@ -198,7 +198,7 @@ public function testUpdateWithWhereSameColumn2()
->where('name', 'Programmer')
->update(null, null, null, true);

$expectedSQL = 'UPDATE "jobs" SET "name" = :name: WHERE "name" = :name0:';
$expectedSQL = 'UPDATE "jobs" SET "name" = \'foobar\' WHERE "name" = \'Programmer\'';
$expectedBinds = [
'name' => 'foobar',
'name0' => 'Programmer',
Expand All @@ -218,7 +218,7 @@ public function testUpdateWithWhereSameColumn3()
$builder->where('name', 'Programmer')
->update(['name' => 'foobar'], null, null, true);

$expectedSQL = 'UPDATE "jobs" SET "name" = :name0: WHERE "name" = :name:';
$expectedSQL = 'UPDATE "jobs" SET "name" = \'foobar\' WHERE "name" = \'Programmer\'';
$expectedBinds = [
'name' => 'Programmer',
'name0' => 'foobar',
Expand All @@ -239,7 +239,7 @@ public function testSetWithoutEscape()
->where('id', 2)
->update(null, null, null, true);

$expectedSQL = 'UPDATE "mytable" SET field = field+1 WHERE "id" = :id:';
$expectedSQL = 'UPDATE "mytable" SET field = field+1 WHERE "id" = 2';
$expectedBinds = ['id' => 2];

$this->assertEquals($expectedSQL, str_replace("\n", ' ', $builder->getCompiledUpdate()));
Expand Down
20 changes: 10 additions & 10 deletions tests/system/Database/Builder/WhereTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public function testSimpleWhere()
{
$builder = $this->db->table('users');

$expectedSQL = 'SELECT * FROM "users" WHERE "id" = :id:';
$expectedSQL = 'SELECT * FROM "users" WHERE "id" = 3';
$expectedBinds = ['id' => 3];

$builder->where('id', 3);
Expand All @@ -35,7 +35,7 @@ public function testWhereNoEscape()
{
$builder = $this->db->table('users');

$expectedSQL = 'SELECT * FROM "users" WHERE id = :id:';
$expectedSQL = 'SELECT * FROM "users" WHERE id = 3';
$expectedBinds = ['id' => 3];

$builder->where('id', 3, false);
Expand All @@ -49,7 +49,7 @@ public function testWhereCustomKeyOperator()
{
$builder = $this->db->table('users');

$expectedSQL = 'SELECT * FROM "users" WHERE "id" != :id:';
$expectedSQL = 'SELECT * FROM "users" WHERE "id" != 3';
$expectedBinds = ['id' => 3];

$builder->where('id !=', 3);
Expand All @@ -68,7 +68,7 @@ public function testWhereAssociateArray()
'name !=' => 'Accountant',
];

$expectedSQL = 'SELECT * FROM "jobs" WHERE "id" = :id: AND "name" != :name:';
$expectedSQL = 'SELECT * FROM "jobs" WHERE "id" = 2 AND "name" != \'Accountant\'';
$expectedBinds = [
'id' => 2,
'name' => 'Accountant',
Expand Down Expand Up @@ -104,7 +104,7 @@ public function testOrWhere()
$builder->where('name !=', 'Accountant')
->orWhere('id >', 3);

$expectedSQL = 'SELECT * FROM "jobs" WHERE "name" != :name: OR "id" > :id:';
$expectedSQL = 'SELECT * FROM "jobs" WHERE "name" != \'Accountant\' OR "id" > 3';
$expectedBinds = [
'name' => 'Accountant',
'id' => 3,
Expand All @@ -123,7 +123,7 @@ public function testOrWhereSameColumn()
$builder->where('name', 'Accountant')
->orWhere('name', 'foobar');

$expectedSQL = 'SELECT * FROM "jobs" WHERE "name" = :name: OR "name" = :name0:';
$expectedSQL = 'SELECT * FROM "jobs" WHERE "name" = \'Accountant\' OR "name" = \'foobar\'';
$expectedBinds = [
'name' => 'Accountant',
'name0' => 'foobar',
Expand All @@ -141,7 +141,7 @@ public function testWhereIn()

$builder->whereIn('name', ['Politician', 'Accountant']);

$expectedSQL = 'SELECT * FROM "jobs" WHERE "name" IN :name:';
$expectedSQL = 'SELECT * FROM "jobs" WHERE "name" IN (\'Politician\',\'Accountant\')';
$expectedBinds = [
'name' => [
'Politician',
Expand All @@ -161,7 +161,7 @@ public function testWhereNotIn()

$builder->whereNotIn('name', ['Politician', 'Accountant']);

$expectedSQL = 'SELECT * FROM "jobs" WHERE "name" NOT IN :name:';
$expectedSQL = 'SELECT * FROM "jobs" WHERE "name" NOT IN (\'Politician\',\'Accountant\')';
$expectedBinds = [
'name' => [
'Politician',
Expand All @@ -181,7 +181,7 @@ public function testOrWhereIn()

$builder->where('id', 2)->orWhereIn('name', ['Politician', 'Accountant']);

$expectedSQL = 'SELECT * FROM "jobs" WHERE "id" = :id: OR "name" IN :name:';
$expectedSQL = 'SELECT * FROM "jobs" WHERE "id" = 2 OR "name" IN (\'Politician\',\'Accountant\')';
$expectedBinds = [
'id' => 2,
'name' => [
Expand All @@ -202,7 +202,7 @@ public function testOrWhereNotIn()

$builder->where('id', 2)->orWhereNotIn('name', ['Politician', 'Accountant']);

$expectedSQL = 'SELECT * FROM "jobs" WHERE "id" = :id: OR "name" NOT IN :name:';
$expectedSQL = 'SELECT * FROM "jobs" WHERE "id" = 2 OR "name" NOT IN (\'Politician\',\'Accountant\')';
$expectedBinds = [
'id' => 2,
'name' => [
Expand Down

0 comments on commit 9ea1f48

Please sign in to comment.