Skip to content

Commit b49ace9

Browse files
KlimTodrikdjklim87
andauthored
Expose SQL escape trait to core (#146)
Co-authored-by: djklim87 <klim@manticoresearch.com>
1 parent f15eb00 commit b49ace9

3 files changed

Lines changed: 102 additions & 0 deletions

File tree

src/Lib/SqlEscapingTrait.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php declare(strict_types=1);
2+
3+
/*
4+
Copyright (c) 2026, Manticore Software LTD (https://manticoresearch.com)
5+
6+
This program is free software; you can redistribute it and/or modify
7+
it under the terms of the GNU General Public License version 2 or any later
8+
version. You should have received a copy of the GPL license along with this
9+
program; if you did not, you can find it at http://www.gnu.org/
10+
*/
11+
12+
namespace Manticoresearch\Buddy\Core\Lib;
13+
14+
trait SqlEscapingTrait {
15+
protected static function escapeSqlString(string $value): string {
16+
return strtr(
17+
$value,
18+
[
19+
'\\' => '\\\\',
20+
"\0" => '\\0',
21+
"\n" => '\\n',
22+
"\r" => '\\r',
23+
"'" => "\\'",
24+
'"' => '\\"',
25+
"\x1a" => '\\Z',
26+
]
27+
);
28+
}
29+
30+
protected static function quoteSqlString(string $value): string {
31+
return "'" . self::escapeSqlString($value) . "'";
32+
}
33+
34+
protected function sqlEscape(string $value): string {
35+
return self::escapeSqlString($value);
36+
}
37+
38+
protected function quote(string $value): string {
39+
return self::quoteSqlString($value);
40+
}
41+
42+
protected function escapeString(string $value): string {
43+
return self::escapeSqlString($value);
44+
}
45+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php declare(strict_types=1);
2+
3+
/*
4+
Copyright (c) 2026, Manticore Software LTD (https://manticoresearch.com)
5+
6+
This program is free software; you can redistribute it and/or modify
7+
it under the terms of the GNU General Public License version 2 or any later
8+
version. You should have received a copy of the GPL license along with this
9+
program; if you did not, you can find it at http://www.gnu.org/
10+
*/
11+
12+
use Manticoresearch\Buddy\CoreTest\Lib\SqlEscapingTraitTestClass;
13+
use PHPUnit\Framework\TestCase;
14+
15+
class SqlEscapingTraitTest extends TestCase {
16+
private SqlEscapingTraitTestClass $testClass;
17+
18+
public function testSqlEscapeSpecialCharacters(): void {
19+
$reflection = new ReflectionClass($this->testClass);
20+
$method = $reflection->getMethod('sqlEscape');
21+
$method->setAccessible(true);
22+
23+
$result = $method->invoke($this->testClass, "line1\nline2\r\"quoted\"\\slash\0\x1a'");
24+
$this->assertEquals('line1\\nline2\\r\\"quoted\\"\\\\slash\\0\\Z\\\'', $result);
25+
}
26+
27+
public function testQuoteWrapsEscapedString(): void {
28+
$reflection = new ReflectionClass($this->testClass);
29+
$method = $reflection->getMethod('quote');
30+
$method->setAccessible(true);
31+
32+
$result = $method->invoke($this->testClass, "O'Reilly");
33+
$this->assertEquals("'O\\'Reilly'", $result);
34+
}
35+
36+
protected function setUp(): void {
37+
$this->testClass = new SqlEscapingTraitTestClass();
38+
}
39+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php declare(strict_types=1);
2+
3+
/*
4+
Copyright (c) 2026, Manticore Software LTD (https://manticoresearch.com)
5+
6+
This program is free software; you can redistribute it and/or modify
7+
it under the terms of the GNU General Public License version 2 or any later
8+
version. You should have received a copy of the GPL license along with this
9+
program; if you did not, you can find it at http://www.gnu.org/
10+
*/
11+
12+
namespace Manticoresearch\Buddy\CoreTest\Lib;
13+
14+
use Manticoresearch\Buddy\Core\Lib\SqlEscapingTrait;
15+
16+
class SqlEscapingTraitTestClass {
17+
use SqlEscapingTrait;
18+
}

0 commit comments

Comments
 (0)