Skip to content
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
45 changes: 45 additions & 0 deletions src/Lib/SqlEscapingTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php declare(strict_types=1);

/*
Copyright (c) 2026, Manticore Software LTD (https://manticoresearch.com)

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License version 2 or any later
version. You should have received a copy of the GPL license along with this
program; if you did not, you can find it at http://www.gnu.org/
*/

namespace Manticoresearch\Buddy\Core\Lib;

trait SqlEscapingTrait {
protected static function escapeSqlString(string $value): string {
return strtr(
$value,
[
'\\' => '\\\\',
"\0" => '\\0',
"\n" => '\\n',
"\r" => '\\r',
"'" => "\\'",
'"' => '\\"',
"\x1a" => '\\Z',
]
);
}

protected static function quoteSqlString(string $value): string {
return "'" . self::escapeSqlString($value) . "'";
}

protected function sqlEscape(string $value): string {
return self::escapeSqlString($value);
}

protected function quote(string $value): string {
return self::quoteSqlString($value);
}

protected function escapeString(string $value): string {
return self::escapeSqlString($value);
}
}
39 changes: 39 additions & 0 deletions test/BuddyCore/Lib/SqlEscapingTraitTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php declare(strict_types=1);

/*
Copyright (c) 2026, Manticore Software LTD (https://manticoresearch.com)

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License version 2 or any later
version. You should have received a copy of the GPL license along with this
program; if you did not, you can find it at http://www.gnu.org/
*/

use Manticoresearch\Buddy\CoreTest\Lib\SqlEscapingTraitTestClass;
use PHPUnit\Framework\TestCase;

class SqlEscapingTraitTest extends TestCase {
private SqlEscapingTraitTestClass $testClass;

public function testSqlEscapeSpecialCharacters(): void {
$reflection = new ReflectionClass($this->testClass);
$method = $reflection->getMethod('sqlEscape');
$method->setAccessible(true);

$result = $method->invoke($this->testClass, "line1\nline2\r\"quoted\"\\slash\0\x1a'");
$this->assertEquals('line1\\nline2\\r\\"quoted\\"\\\\slash\\0\\Z\\\'', $result);
}

public function testQuoteWrapsEscapedString(): void {
$reflection = new ReflectionClass($this->testClass);
$method = $reflection->getMethod('quote');
$method->setAccessible(true);

$result = $method->invoke($this->testClass, "O'Reilly");
$this->assertEquals("'O\\'Reilly'", $result);
}

protected function setUp(): void {
$this->testClass = new SqlEscapingTraitTestClass();
}
}
18 changes: 18 additions & 0 deletions test/src/Lib/SqlEscapingTraitTestClass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php declare(strict_types=1);

/*
Copyright (c) 2026, Manticore Software LTD (https://manticoresearch.com)

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License version 2 or any later
version. You should have received a copy of the GPL license along with this
program; if you did not, you can find it at http://www.gnu.org/
*/

namespace Manticoresearch\Buddy\CoreTest\Lib;

use Manticoresearch\Buddy\Core\Lib\SqlEscapingTrait;

class SqlEscapingTraitTestClass {
use SqlEscapingTrait;
}
Loading