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

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

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

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

A quick fix will be
$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:
// 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:
// 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
$this->select('mytable.tablename AS table');
// or
$this->select('mytable.tablename table');

Theme © iAndrew 2016 - Forum software by © MyBB