Skip to content

Commit

Permalink
feat: support Query\Builder::toRawSql() (#123)
Browse files Browse the repository at this point in the history
Co-authored-by: halnique <shunsuke4dev@gmail.com>
  • Loading branch information
taka-oyama and halnique committed Aug 14, 2023
1 parent 042af93 commit 6bd0168
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ Added
- Added `ManagesMutations::insertOrUpdateUsingMutation` and `UsesMutations::insertOrUpdateUsingMutation` to do upserts (#109)
- Added Support for `Schema\Builder::dropIfExists()` (#115)
- Added support for adding row deletion policy when modifying table (#124)
- Added Support for `Query\Builder::toRawSql()` (#123)

Changed
- `Connection::waitForOperation` and `Connection::isDoneOperation` has been removed. (#99)
Expand Down
20 changes: 19 additions & 1 deletion src/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ public function disconnect()
*/
protected function getDefaultQueryGrammar(): QueryGrammar
{
return new QueryGrammar();
return (new QueryGrammar())->setConnection($this);
}

/**
Expand Down Expand Up @@ -420,6 +420,24 @@ public function prepareBindings(array $bindings)
return $bindings;
}

/**
* @inheritDoc
*/
protected function escapeBool($value)
{
return $value ? 'true' : 'false';
}

/**
* @inheritDoc
*/
protected function escapeString($value)
{
return str_contains($value, "\n")
? 'r"""' . addcslashes($value, '"') . '"""'
: '"' . addcslashes($value, '"') . '"';
}

/**
* @inheritDoc
*/
Expand Down
23 changes: 23 additions & 0 deletions tests/Query/BuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -867,4 +867,27 @@ public function test_sharedLock(): void
$qb = $conn->table(self::TABLE_NAME_USER);
$qb->sharedLock()->get();
}

public function test_toRawSql(): void
{
$conn = $this->getDefaultConnection();

$sql = $conn->table(self::TABLE_NAME_USER)->where('b', true)->toRawSql();
$this->assertSame($sql, 'select * from `User` where `b` = true', 'true');

$sql = $conn->table(self::TABLE_NAME_USER)->where('b', false)->toRawSql();
$this->assertSame($sql, 'select * from `User` where `b` = false', 'false');

$sql = $conn->table(self::TABLE_NAME_USER)->where('t', 'test')->toRawSql();
$this->assertSame($sql, 'select * from `User` where `t` = "test"', 'text');

$sql = $conn->table(self::TABLE_NAME_USER)->where('t', '"tes\'s"')->toRawSql();
$this->assertSame($sql, 'select * from `User` where `t` = "\"tes\'s\""', 'escaped');

$sql = $conn->table(self::TABLE_NAME_USER)->where('t', "tes\nt")->toRawSql();
$this->assertSame($sql, "select * from `User` where `t` = r\"\"\"tes\nt\"\"\"", 'newline');

$sql = $conn->table(self::TABLE_NAME_USER)->where('t', "t\"e\"s\nt")->toRawSql();
$this->assertSame($sql, "select * from `User` where `t` = r\"\"\"t\\\"e\\\"s\nt\"\"\"", 'newline with escaped quote');
}
}

0 comments on commit 6bd0168

Please sign in to comment.