Skip to content

Support array value escape #8

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
46 changes: 28 additions & 18 deletions src/QueryBuilder/Helpers/Escape.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,34 +25,44 @@ protected function keyEscape(string $value): string
return substr($str, 0, -1);
}

/**
* @throws QueryBuilderException
*/
protected function escape(mixed $value): mixed
{
if (is_null($value))
return 'NULL';

if (gettype($value) == 'integer' || gettype($value) == 'double')
return $value;

if (is_bool($value))
return intval($value);

return sprintf("'%s'", $this->escapeString($value));
return match (gettype($value)) {
'NULL' => 'NULL',
'integer', 'double' => $value,
'boolean' => intval($value),
'string' => sprintf("'%s'", $this->escapeString($value)),
'array' => implode(',', array_map([$this, 'escape'], $value)),
default => throw new QueryBuilderException(sprintf('Unsupported (%s) value type.', gettype($value)))
};
}

/*
* Note that this mapping assumes an ASCII-compatible charset encoding such
* as UTF-8, ISO 8859 and others.
*
* Note that `'` will be escaped as `''` instead of `\'` to provide some
* limited support for the `NO_BACKSLASH_ESCAPES` SQL mode. This assumes all
* strings will always be enclosed in `'` instead of `"` which is guaranteed
* as long as this class is only used internally for the `query()` method.
*/
protected function escapeString(string $query): string
{
$replacementMap = [
"\0" => "\\0",
"\n" => "\\n",
"\r" => "\\r",
"\t" => "\\t",
chr(26) => "\\Z",
chr(8) => "\\b",
'"' => '\"',
"'" => "\'",
// "\n" => "\\n",
// "\r" => "\\r",
// "\t" => "\\t",
// chr(26) => "\\Z",
// chr(8) => "\\b",
// '"' => '\"',
"'" => "''",
// '_' => "\_",
// "%" => "\%",
'\\' => '\\\\'
"\\" => "\\\\"
];

return strtr($query, $replacementMap);
Expand Down
2 changes: 1 addition & 1 deletion tests/QueryBuilderTest/CapabilityTest/WhereTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ public function testWhereGroup()
self::assertCount(1, $whereStatements->getValue($w));
self::assertCount(2, $whereStatements->getValue($w)[0]);
self::assertEquals("`name` = 'John'", $whereStatements->getValue($w)[0][0]);
self::assertEquals("`family` = 'Doe\''", $whereStatements->getValue($w)[0][1]);
self::assertEquals("`family` = 'Doe'''", $whereStatements->getValue($w)[0][1]);
}

/**
Expand Down
25 changes: 22 additions & 3 deletions tests/QueryBuilderTest/HelpersTest/EscapeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,25 @@ public function escapeBooleans()
self::assertEquals("'false'", $reflection->invoke($e, "false"));
}

/**
* @test
* @throws \ReflectionException
*/
public function escapeArray()
{
$e = new class () {
use Escape;
};

$reflection = new \ReflectionMethod($e, "escape");
$reflection->setAccessible(true);

self::assertEquals(
"'docnotL',13,3.14,NULL,1,'who''s'",
$reflection->invoke($e, ['docnotL', 13, 3.14, null, true, "who's"])
);
}


/**
* @test
Expand All @@ -138,9 +157,9 @@ public function escapeString()
$reflection = new \ReflectionMethod($e, "escape");
$reflection->setAccessible(true);

self::assertEquals("'\\0'", $reflection->invoke($e, "\00"));
self::assertEquals("'\''", $reflection->invoke($e, "'"));
self::assertEquals("'\\\"'", $reflection->invoke($e, "\""));
// self::assertEquals("'\\0'", $reflection->invoke($e, "\0"));
self::assertEquals("''''", $reflection->invoke($e, "'"));
// self::assertEquals("'\\\"'", $reflection->invoke($e, "\""));
self::assertEquals("'\\\\'", $reflection->invoke($e, "\\"));
}
}