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
75 changes: 34 additions & 41 deletions system/database/DB_driver.php
Original file line number Diff line number Diff line change
Expand Up @@ -1354,24 +1354,23 @@ public function escape_identifiers($item, $split = TRUE)
return $item;
}

static $preg_ec;

if (empty($preg_ec))
if (is_array($this->_escape_char))
{
if (is_array($this->_escape_char))
{
$preg_ec = array(
preg_quote($this->_escape_char[0]),
preg_quote($this->_escape_char[1]),
$this->_escape_char[0],
$this->_escape_char[1]
);
}
else
{
$preg_ec[0] = $preg_ec[1] = preg_quote($this->_escape_char);
$preg_ec[2] = $preg_ec[3] = $this->_escape_char;
}
$preg_ec = array(
preg_quote($this->_escape_char[0]),
preg_quote($this->_escape_char[1]),
$this->_escape_char[0],
$this->_escape_char[1]
);
}
else
{
$preg_ec = array(
preg_quote($this->_escape_char),
preg_quote($this->_escape_char),
$this->_escape_char,
$this->_escape_char
);
}

foreach ($this->_reserved_identifiers as $id)
Expand Down Expand Up @@ -1503,30 +1502,24 @@ protected function _has_operator($str)
*/
protected function _get_operator($str)
{
static $_operators;

if (empty($_operators))
{
$_les = ($this->_like_escape_str !== '')
? '\s+'.preg_quote(trim(sprintf($this->_like_escape_str, $this->_like_escape_chr)), '/')
: '';
$_operators = array(
'\s*(?:<|>|!)?=\s*', // =, <=, >=, !=
'\s*<>?\s*', // <, <>
'\s*>\s*', // >
'\s+IS NULL', // IS NULL
'\s+IS NOT NULL', // IS NOT NULL
'\s+EXISTS\s*\(.*\)', // EXISTS(sql)
'\s+NOT EXISTS\s*\(.*\)', // NOT EXISTS(sql)
'\s+BETWEEN\s+', // BETWEEN value AND value
'\s+NOT BETWEEN\s+', // NOT BETWEEN value AND value
'\s+IN\s*\(.*\)', // IN(list)
'\s+NOT IN\s*\(.*\)', // NOT IN (list)
'\s+LIKE\s+\S.*('.$_les.')?', // LIKE 'expr'[ ESCAPE '%s']
'\s+NOT LIKE\s+\S.*('.$_les.')?' // NOT LIKE 'expr'[ ESCAPE '%s']
);

}
$_les = ($this->_like_escape_str !== '')
? '\s+'.preg_quote(trim(sprintf($this->_like_escape_str, $this->_like_escape_chr)), '/')
: '';
$_operators = array(
'\s*(?:<|>|!)?=\s*', // =, <=, >=, !=
'\s*<>?\s*', // <, <>
'\s*>\s*', // >
'\s+IS NULL', // IS NULL
'\s+IS NOT NULL', // IS NOT NULL
'\s+EXISTS\s*\(.*\)', // EXISTS(sql)
'\s+NOT EXISTS\s*\(.*\)', // NOT EXISTS(sql)
'\s+BETWEEN\s+', // BETWEEN value AND value
'\s+NOT BETWEEN\s+', // NOT BETWEEN value AND value
'\s+IN\s*\(.*\)', // IN(list)
'\s+NOT IN\s*\(.*\)', // NOT IN (list)
'\s+LIKE\s+\S.*('.$_les.')?', // LIKE 'expr'[ ESCAPE '%s']
'\s+NOT LIKE\s+\S.*('.$_les.')?' // NOT LIKE 'expr'[ ESCAPE '%s']
);

return preg_match('/'.implode('|', $_operators).'/i', $str, $match)
? $match[0] : FALSE;
Expand Down
9 changes: 2 additions & 7 deletions system/database/DB_query_builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -2803,13 +2803,8 @@ protected function _is_literal($str)
return TRUE;
}

static $_str;

if (empty($_str))
{
$_str = ($this->_escape_char !== '"')
? array('"', "'") : array("'");
}
$_str = ($this->_escape_char !== '"')
? array('"', "'") : array("'");

return in_array($str[0], $_str, TRUE);
}
Expand Down
37 changes: 37 additions & 0 deletions tests/codeigniter/database/Driver_escape_test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

class Driver_escape_test extends CI_TestCase {

public function set_up()
{
class_exists('Mock_Database_DB_Driver', TRUE);

$this->mysql = new CI_DB_mysqli_driver(array());
$this->postgre = new CI_DB_postgre_driver(array());
}

// --------------------------------------------------------------------

public function test_escape_identifiers_per_driver()
{
$this->assertSame('`table`.`col`', $this->mysql->escape_identifiers('table.col'));
$this->assertSame('"table"."col"', $this->postgre->escape_identifiers('table.col'));
$this->assertSame('`table`.`col`', $this->mysql->escape_identifiers('table.col'));
}

// --------------------------------------------------------------------

public function test_is_literal_per_driver()
{
$is_literal = new ReflectionMethod('CI_DB_query_builder', '_is_literal');
if (PHP_VERSION_ID < 80100)
{
$is_literal->setAccessible(TRUE);
}

$this->assertTrue($is_literal->invoke($this->mysql, '"foo"'));
$this->assertFalse($is_literal->invoke($this->postgre, '"foo"'));
$this->assertTrue($is_literal->invoke($this->mysql, '"foo"'));
}

}