Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support Query\Builder::toRawSql() #123

Merged
merged 4 commits into from
Aug 14, 2023
Merged
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Added
- Added deprecation warnings to `Connection::runDdl` and `Connection::runDdls` (#98)
- Added `ManagesMutations::insertOrUpdateUsingMutation` and `UsesMutations::insertOrUpdateUsingMutation` to do upserts (#109)
- Added Support for `Schema\Builder::dropIfExists()` (#115)
- 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, '"') . '"';
taka-oyama marked this conversation as resolved.
Show resolved Hide resolved
halnique marked this conversation as resolved.
Show resolved Hide resolved
}

/**
* @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');
taka-oyama marked this conversation as resolved.
Show resolved Hide resolved

$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');
}
}