Skip to content
Permalink
Browse files

Refactoring expression transformation in SQL dialect

  • Loading branch information...
lorenzo committed Mar 9, 2013
1 parent eee3353 commit 53717fc5567747fbbc9bba90d1cf8b3d48516611
@@ -22,13 +22,11 @@
trait SqliteDialectTrait {
protected function _selectQueryTranslator($query) {
return parent::_selectQueryTranslator($query)
->traverseExpressions(function($expression) {
if ($expression instanceof FunctionExpression) {
$this->_transformFunctionExpression($expression);
}
});
protected function _expressionTranslators() {
$namespace = 'Cake\Model\Datasource\Database\Expression';
return [
$namespace . '\FunctionExpression' => '_transformFunctionExpression'
];
}
protected function _transformFunctionExpression(FunctionExpression $expression) {
@@ -76,12 +76,45 @@ public function quoteIdentifier($identifier) {
return $identifier;
}
/**
* Returns a callable function that will be used to transform a passed Query object.
* This function, in turn, will return an instance of a Query object that has been
* transformed to accommodate any specificities of the SQL dialect in use.
*
* @param string $type the type of query to be transformed
* (select, insert, update, delete)
* @return callable
*/
public function queryTranslator($type) {
return function($query) use ($type) {
return $this->{'_' . $type . 'QueryTranslator'}($query);
$query = $this->{'_' . $type . 'QueryTranslator'}($query);
if (!$this->_expressionTranslators()) {
return $query;
}
$query->traverseExpressions(function($expression) {
foreach ($this->_expressionTranslators() as $class => $method) {
if ($expression instanceof $class) {
$this->{$method}($expression);
}
}
});
return $query;
};
}
/**
* Returns an associative array of methods that will transform Expression
* objects to conform with the specific SQL dialect. Keys are class names
* and values a method in this class.
*
* @return void
*/
protected function _expressionTranslators() {
return [];
}
/**
* Apply translation steps to select queries.
*

0 comments on commit 53717fc

Please sign in to comment.
You can’t perform that action at this time.