Skip to content

Commit

Permalink
Fixed insert sql queries allowing table alias
Browse files Browse the repository at this point in the history
  • Loading branch information
mario-deluna committed Aug 7, 2015
1 parent 8c52423 commit 808da50
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 40 deletions.
14 changes: 9 additions & 5 deletions src/Builder.php
Expand Up @@ -121,11 +121,7 @@ public function table($table)
{
$database = null;

if (strpos($table, '.') === false)
{
$table = $table;
}
else
if (is_string($table) && strpos($table, '.') !== false)
{
$selection = explode('.', $table);

Expand All @@ -137,6 +133,14 @@ public function table($table)
list($database, $table) = $selection;
}

// the table might include an alias we need to parse that one out
if (is_string($table) && strpos($table, ' as ') !== false)
{
$tableParts = explode(' as ', $table);
$table = array($tableParts[0] => $tableParts[1]);
unset($tableParts);
}

// create and return new query instance
return new $this->queryClass(array($this, 'executeQuery'), $table, $database);
}
Expand Down
76 changes: 41 additions & 35 deletions src/Translator/Mysql.php
Expand Up @@ -53,14 +53,14 @@ class Mysql implements TranslatorInterface
*/
public function translate(BaseQuery $query)
{
// retrive the query attributes
$this->attributes = $query->attributes();

// handle SQL SELECT queries
if ($query instanceof Select)
{
$queryString = $this->translateSelect();
}
// retrive the query attributes
$this->attributes = $query->attributes();

// handle SQL SELECT queries
if ($query instanceof Select)
{
$queryString = $this->translateSelect();
}
// handle SQL INSERT queries
elseif ($query instanceof Insert)
{
Expand All @@ -86,27 +86,27 @@ public function translate(BaseQuery $query)
{
$queryString = $this->translateTruncate();
}
// everything else is wrong
else
{
throw new Exception('Unknown query type. Cannot translate: '.get_class($query));
}
// everything else is wrong
else
{
throw new Exception('Unknown query type. Cannot translate: '.get_class($query));
}

// get the query parameters and reset
$queryParameters = $this->parameters; $this->clearParameters();
// get the query parameters and reset
$queryParameters = $this->parameters; $this->clearParameters();

return array($queryString, $queryParameters);
return array($queryString, $queryParameters);
}

/**
* Returns the an attribute value for the given key
*
* @param string $key
* @param string $key
* @return mixed
*/
protected function attr($key)
{
return $this->attributes[$key];
return $this->attributes[$key];
}

/**
Expand Down Expand Up @@ -280,25 +280,31 @@ protected function escapeList($array)
*
* @return string
*/
protected function escapeTable()
protected function escapeTable($allowAlias = true)
{
$table = $this->attr('table');
$database = $this->attr('database');

$buffer = '';

if (!is_null($database))
{
$buffer .= $this->escape($database) . '.';
$buffer .= $this->escape($database) . '.';
}

if (is_array($table))
{
reset($table); $table = key($table) . ' as ' . $table[key($table)];
reset($table);

if ($allowAlias)
{
$table = key($table) . ' as ' . $table[key($table)];
} else {
$table = key($table);
}
}

return $buffer . $this->escape($table);
}
}

/**
* Convert data to parameters and bind them to the query
Expand Down Expand Up @@ -329,7 +335,7 @@ protected function translateInsert()
{
$build = ($this->attr('ignore') ? 'insert ignore' : 'insert');

$build .= ' into ' . $this->escapeTable() . ' ';
$build .= ' into ' . $this->escapeTable(false) . ' ';

if (!$valueCollection = $this->attr('values'))
{
Expand Down Expand Up @@ -414,7 +420,7 @@ protected function translateDelete()
*/
protected function translateSelect()
{
// normal or distinct selection?
// normal or distinct selection?
$build = ($this->attr('distinct') ? 'select distinct' : 'select') . ' ';

// build the selected fields
Expand All @@ -424,7 +430,7 @@ protected function translateSelect()
{
foreach ($fields as $key => $field)
{
list($column, $alias) = $field;
list($column, $alias) = $field;

if (!is_null($alias))
{
Expand All @@ -451,31 +457,31 @@ protected function translateSelect()
// build the join statements
if ($this->attr('joins'))
{
$build .= $this->translateJoins();
$build .= $this->translateJoins();
}

// build the where statements
if ($wheres = $this->attr('wheres'))
{
$build .= $this->translateWhere($wheres);
$build .= $this->translateWhere($wheres);
}

// build the groups
if ($this->attr('groups'))
{
$build .= $this->translateGroupBy();
$build .= $this->translateGroupBy();
}

// build the order statement
if ($this->attr('orders'))
{
$build .= $this->translateOrderBy();
$build .= $this->translateOrderBy();
}

// build offset and limit
if ($this->attr('limit') || $this->attr('offset'))
{
$build .= $this->translateLimitWithOffset();
$build .= $this->translateLimitWithOffset();
}

return $build;
Expand All @@ -484,7 +490,7 @@ protected function translateSelect()
/**
* Translate the where statements into sql
*
* @param array $wheres
* @param array $wheres
* @return string
*/
protected function translateWhere($wheres)
Expand All @@ -497,7 +503,7 @@ protected function translateWhere($wheres)
// wich will create a new query where you can add your nested wheres
if (!isset($where[2]) && isset( $where[1] ) && $where[1] instanceof BaseQuery )
{
$subAttributes = $where[1]->attributes();
$subAttributes = $where[1]->attributes();

// The parameters get added by the call of compile where
$build .= ' ' . $where[0] . ' ( ' . substr($this->translateWhere($subAttributes['wheres']), 7) . ' )';
Expand Down Expand Up @@ -535,8 +541,8 @@ protected function translateJoins()

foreach ($this->attr('joins') as $join)
{
list($type, $table, $localKey, $operator, $referenceKey) = $join;
$build .= ' ' . $type . ' join ' . $this->escape($table) . ' on ' . $this->escape($localKey) . ' ' . $operator . ' ' . $this->escape($referenceKey);
list($type, $table, $localKey, $operator, $referenceKey) = $join;
$build .= ' ' . $type . ' join ' . $this->escape($table) . ' on ' . $this->escape($localKey) . ' ' . $operator . ' ' . $this->escape($referenceKey);
}

return $build;
Expand Down
18 changes: 18 additions & 0 deletions tests/Translator/Mysql.php
Expand Up @@ -427,6 +427,24 @@ public function testInsertSimple()
});
}

/**
* mysql grammar tests
*/
public function testInsertWithUnwantedAlias()
{
// simple
$this->assertQueryTranslation('insert into `test` (`foo`) values (?)', array('bar'), function($q)
{
return $q->table('test as foo')->insert()->values(array('foo' => 'bar'));
});

// some more complexity
$this->assertQueryTranslation('insert into `test` (`bar`, `foo`) values (?, ?)', array('foo','bar'), function($q)
{
return $q->table(array('test' => 'nope'))->insert()->values(array('foo' => 'bar', 'bar' => 'foo'));
});
}

/**
* mysql grammar tests
*/
Expand Down

0 comments on commit 808da50

Please sign in to comment.