Skip to content

Commit

Permalink
refactor: escape string and quotes
Browse files Browse the repository at this point in the history
  • Loading branch information
darkterminal committed Jul 15, 2024
1 parent 6d47798 commit 4fce60a
Showing 1 changed file with 18 additions and 6 deletions.
24 changes: 18 additions & 6 deletions src/Database/LibSQLConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -298,19 +298,31 @@ protected function isUniqueConstraintError(Exception $exception): bool

public function escapeString($value)
{
// DISCUSSION: Open PR if you have best approach
$escaped_value = str_replace(
['\\', "\x00", "\n", "\r", "\x1a", "'", '"'],
['\\\\', '\\0', '\\n', '\\r', '\\Z', "\\'", '\\"'],
$value
// Handle NULL values
if ($value === null) {
return 'NULL';
}

// Use strtr for more efficient character replacement
$escaped_value = strtr(
$value,
[
'\\' => '\\\\',
"\x00" => '\\0',
"\n" => '\\n',
"\r" => '\\r',
"\x1a" => '\\Z',
"'" => "\\'",
'"' => '\\"',
]
);

return $escaped_value;
}

public function quote(string $value): string
{
return $this->escapeString($value);
return "'" . $this->escapeString($value) . "'";
}

private function isArrayAssoc(array $data)
Expand Down

0 comments on commit 4fce60a

Please sign in to comment.