Skip to content

Commit

Permalink
Moving SQL generation to a separate object inside Connection
Browse files Browse the repository at this point in the history
This cleans up Query a bit and opens the possibility of having
a compiler per driver, which will be used for fixing some Sqlserver
failing tests
  • Loading branch information
lorenzo committed Apr 12, 2014
1 parent 284dd00 commit 293e903
Show file tree
Hide file tree
Showing 7 changed files with 367 additions and 285 deletions.
50 changes: 50 additions & 0 deletions src/Database/Connection.php
Expand Up @@ -21,6 +21,8 @@
use Cake\Database\Log\LoggingStatement;
use Cake\Database\Log\QueryLogger;
use Cake\Database\Query;
use Cake\Database\Querycompiler;
use Cake\Database\ValueBinder;

/**
* Represents a connection with a database server.
Expand Down Expand Up @@ -226,6 +228,54 @@ public function execute($query, array $params = [], array $types = []) {
return $statement;
}

/**
* Copiles a Query object into a SQL string according to the dialect for this
* connection's driver
*
* @param Cake\Database\$query The query to be compiled
* @param ValueBinder $generator The placeholder generator to use
* @return string
*/
public function compileQuery(Query $query, ValueBinder $generator) {
$processor = new Querycompiler;
$query = $this->_transformQuery($query);
return $processor->compile($query, $generator);
}

/**
* Executes the provided query after compiling it for the specific dirver
* dialect and returns the executed Statement object.
*
* @param Cake\Database\$query The query to be executed
* @return \Cake\Database\StatementInterface executed statement
*/
public function run(Query $query) {
$binder = $query->valueBinder();
$binder->resetCount();
$query = $this->_transformQuery($query);

$processor = new Querycompiler;
$sql = $processor->compile($query, $binder);

$statement = $this->prepare($sql);
$processor->bindStatement($binder, $statement);
$statement->execute();

return $statement;
}

/**
* Returns a query that has been translated to the specific SQL dialect for the
* driver
*
* @param \Cake\Database\Query $query The query to transform
* @return \Cake\Database\Query
*/
protected function _transformQuery($query) {
$translator = $this->driver()->queryTranslator($query->type());
return $translator($query);
}

/**
* Executes a SQL statement and returns the Statement object as result.
*
Expand Down
4 changes: 2 additions & 2 deletions src/Database/Dialect/SqliteDialectTrait.php
Expand Up @@ -14,7 +14,7 @@
*/
namespace Cake\Database\Dialect;

use Cake\Database\Dialect\TumpleComparisonTranslatorTrait;
use Cake\Database\Dialect\TupleComparisonTranslatorTrait;
use Cake\Database\ExpressionInterface;
use Cake\Database\Expression\FunctionExpression;
use Cake\Database\SqlDialectTrait;
Expand All @@ -25,7 +25,7 @@
trait SqliteDialectTrait {

use SqlDialectTrait;
use TumpleComparisonTranslatorTrait;
use TupleComparisonTranslatorTrait;

/**
* String used to start a database identifier quoting to make it safe
Expand Down

0 comments on commit 293e903

Please sign in to comment.