Skip to content

Commit

Permalink
Refactor duplicated methods
Browse files Browse the repository at this point in the history
Signed-off-by: Hugues Peccatte <hugues.peccatte@gmail.com>
  • Loading branch information
Tithugues committed Nov 1, 2019
1 parent f0d4122 commit 69a4061
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 29 deletions.
29 changes: 6 additions & 23 deletions libraries/classes/Util.php
Original file line number Diff line number Diff line change
Expand Up @@ -872,26 +872,7 @@ public static function getTableList(
*/
public static function backquote($a_name, $do_it = true)
{
if (is_array($a_name)) {
foreach ($a_name as &$data) {
$data = self::backquote($data, $do_it);
}
return $a_name;
}

if (! $do_it) {
if (! (Context::isKeyword($a_name) & Token::FLAG_KEYWORD_RESERVED)
) {
return $a_name;
}
}

// '0' is also empty for php :-(
if (strlen((string) $a_name) > 0 && $a_name !== '*') {
return '`' . str_replace('`', '``', $a_name) . '`';
}

return $a_name;
return static::backquoteCompat($a_name, 'NONE', $do_it);
} // end of the 'backquote()' function

/**
Expand Down Expand Up @@ -928,7 +909,7 @@ public static function backquoteCompat(
}

if (! $do_it) {
if (! Context::isKeyword($a_name)) {
if (! (Context::isKeyword($a_name) & Token::FLAG_KEYWORD_RESERVED)) {
return $a_name;
}
}
Expand All @@ -937,15 +918,17 @@ public static function backquoteCompat(
switch ($compatibility) {
case 'MSSQL':
$quote = '"';
$escapeChar = '\\';
break;
default:
$quote = "`";
$quote = '`';
$escapeChar = '`';
break;
}

// '0' is also empty for php :-(
if (strlen((string) $a_name) > 0 && $a_name !== '*') {
return $quote . $a_name . $quote;
return $quote . str_replace($quote, $escapeChar . $quote, $a_name) . $quote;
}

return $a_name;
Expand Down
25 changes: 19 additions & 6 deletions test/classes/UtilTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2308,25 +2308,29 @@ public function providerBackquote()
/**
* backquoteCompat test with different param $compatibility (NONE, MSSQL)
*
* @param string $a String
* @param string $b Expected output
* @param string $entry String
* @param string $expectedNoneOutput Expected none output
* @param string $expectedMssqlOutput Expected MSSQL output
*
* @return void
*
* @covers \PhpMyAdmin\Util::backquoteCompat
* @dataProvider providerBackquoteCompat
*/
public function testBackquoteCompat($a, $b): void
public function testBackquoteCompat($entry, $expectedNoneOutput, $expectedMssqlOutput): void
{
// Test bypass quoting (used by dump functions)
$this->assertEquals($a, Util::backquoteCompat($a, 'NONE', false));
$this->assertEquals($entry, Util::backquoteCompat($entry, 'NONE', false));

// Run tests in MSSQL compatibility mode
// Test bypass quoting (used by dump functions)
$this->assertEquals($a, Util::backquoteCompat($a, 'MSSQL', false));
$this->assertEquals($entry, Util::backquoteCompat($entry, 'MSSQL', false));

// Test backquote
$this->assertEquals($expectedNoneOutput, Util::backquoteCompat($entry, 'NONE'));

// Test backquote
$this->assertEquals($b, Util::backquoteCompat($a, 'MSSQL'));
$this->assertEquals($expectedMssqlOutput, Util::backquoteCompat($entry, 'MSSQL'));
}

/**
Expand All @@ -2339,14 +2343,17 @@ public function providerBackquoteCompat()
return [
[
'0',
'`0`',
'"0"',
],
[
'test',
'`test`',
'"test"',
],
[
'te`st',
'`te``st`',
'"te`st"',
],
[
Expand All @@ -2356,6 +2363,12 @@ public function providerBackquoteCompat()
'',
'*',
],
[
'`test`',
'`te``st`',
'',
'*',
],
[
'"test"',
'"te`st"',
Expand Down

0 comments on commit 69a4061

Please sign in to comment.