Welcome Guest, Not a member yet? Register   Sign In
Escape char when using SELECT and AS
#1

[eluser]Clooner[/eluser]
I noticed that when using
Code:
$this->select('mytable.tablename AS table');
it will generate an invalid query since table is a reserverd word.

AR will generate
Code:
SELECT `mytable`.`tablename` AS table

but should generate
Code:
SELECT `mytable`.`tablename` AS `table`

A quick fix will be
Code:
$this->select('mytable.tablename AS `table`');

however a better solution will be to update the function _protect_identifiers in the db_driver.php file.

Add this code:
Code:
// makes sure the alias get correct escape identifiers!
// works with a space or with the AS statement
if (strlen($alias)>0 && $protect_identifiers === TRUE)
{
  $alias_parts = explode(' ', $alias);
  $alias_parts[count($alias_parts)-1] = $this->_escape_identifiers(end($alias_parts));
  $alias = implode(' ', $alias_parts);
}

directly after this:
Code:
// If the item has an alias declaration we remove it and set it aside.
// Basically we remove everything to the right of the first space
$alias = '';
if (strpos($item, ' ') !== FALSE)
{
  $alias = strstr($item, " ");
  $item = substr($item, 0, - strlen($alias));
}

this way you will be able to write the code like this
Code:
$this->select('mytable.tablename AS table');
// or
$this->select('mytable.tablename table');


Messages In This Thread
Escape char when using SELECT and AS - by El Forum - 06-25-2010, 05:56 AM



Theme © iAndrew 2016 - Forum software by © MyBB