Skip to content

Commit

Permalink
Refactored to use the SQL command class instead of just a string.
Browse files Browse the repository at this point in the history
  • Loading branch information
ziminji committed May 1, 2014
1 parent c33f355 commit b412203
Show file tree
Hide file tree
Showing 92 changed files with 473 additions and 396 deletions.
24 changes: 12 additions & 12 deletions src/classes/Leap/Core/DB/Connection/Driver.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
* @access public
* @class
* @package Leap\Core\DB\Connection
* @version 2014-01-28
* @version 2014-04-30
*/
abstract class Driver extends \Leap\Core\Object {

Expand Down Expand Up @@ -66,7 +66,7 @@ abstract class Driver extends \Leap\Core\Object {
* This variable stores the last SQL statement executed.
*
* @access protected
* @var string
* @var \Leap\Core\DB\SQL\Command
*/
protected $sql;

Expand All @@ -81,7 +81,7 @@ public function __construct(\Leap\Core\DB\DataSource $data_source) {
$this->data_source = $data_source;
$this->lock = \Leap\Core\DB\SQL\Lock\Builder::factory($this);
$this->resource = NULL;
$this->sql = '';
$this->sql = new \Leap\Core\DB\SQL\Command();
}

/**
Expand Down Expand Up @@ -129,12 +129,12 @@ public abstract function begin_transaction();
* This method manages query caching.
*
* @access protected
* @param string $sql the SQL statement being queried
* @param \Leap\Core\DB\SQL\Command $sql the SQL statement being queried
* @param string $type the return type that is being used
* @param \Leap\Core\DB\ResultSet $results the result set
* @return \Leap\Core\DB\ResultSet the result set for the specified
*/
protected function cache($sql, $type, $results = NULL) {
protected function cache(\Leap\Core\DB\SQL\Command $sql, $type, $results = NULL) {
if ($this->data_source->cache->enabled) {
if ($results !== NULL) {
if ($this->data_source->cache->lifetime > 0) {
Expand All @@ -143,7 +143,7 @@ protected function cache($sql, $type, $results = NULL) {
return $results;
}
else if ($this->data_source->cache->lifetime !== NULL) {
$this->cache_key = 'Leap\\Plugin\\DB\\Connection\\Driver::query("' . $this->data_source->id . '", "' . $type . '", "' . $sql . '")';
$this->cache_key = 'Leap\\Plugin\\DB\\Connection\\Driver::query("' . $this->data_source->id . '", "' . $type . '", "' . $sql->text . '")';
$results = \Kohana::cache($this->cache_key, NULL, $this->data_source->cache->lifetime);
if (($results !== NULL) AND ! $this->data_source->cache->force) {
return $results;
Expand Down Expand Up @@ -177,11 +177,11 @@ public abstract function commit();
*
* @access public
* @abstract
* @param string $sql the SQL statement
* @param \Leap\Core\DB\SQL\Command $sql the SQL statement
* @throws \Leap\Core\Throwable\SQL\Exception indicates that the executed
* statement failed
*/
public abstract function execute($sql);
public abstract function execute(\Leap\Core\DB\SQL\Command $sql);

/**
* This method returns the last insert id.
Expand Down Expand Up @@ -232,12 +232,12 @@ public abstract function open();
* This method processes an SQL statement that will return data.
*
* @access public
* @param string $sql the SQL statement
* @param \Leap\Core\DB\SQL\Command $sql the SQL statement
* @param string $type the return type to be used
* @return \Leap\Core\DB\ResultSet the result set
* @throws \Leap\Core\Throwable\SQL\Exception indicates that the query failed
*/
public function query($sql, $type = 'array') {
public function query(\Leap\Core\DB\SQL\Command $sql, $type = 'array') {
if ( ! $this->is_connected()) {
throw new \Leap\Core\Throwable\SQL\Exception('Message: Failed to query SQL statement. Reason: Unable to find connection.');
}
Expand Down Expand Up @@ -295,11 +295,11 @@ public function quote($string, $escape = NULL) {
* This method creates a data reader for query the specified SQL statement.
*
* @access public
* @param string $sql the SQL statement
* @param \Leap\Core\DB\SQL\Command $sql the SQL statement
* @return \Leap\Core\DB\SQL\DataReader the SQL data reader
* @throws \Leap\Core\Throwable\SQL\Exception indicates that the query failed
*/
public function reader($sql) {
public function reader(\Leap\Core\DB\SQL\Command $sql) {
if ( ! $this->is_connected()) {
throw new \Leap\Core\Throwable\SQL\Exception('Message: Failed to create SQL data reader. Reason: Unable to find connection.');
}
Expand Down
6 changes: 3 additions & 3 deletions src/classes/Leap/Core/DB/ORM/Delete/Proxy.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
* @access public
* @class
* @package Leap\Core\DB\ORM\Delete
* @version 2014-04-24
* @version 2014-04-30
*/
class Proxy extends \Leap\Core\Object implements \Leap\Core\DB\SQL\Statement {

Expand Down Expand Up @@ -106,7 +106,7 @@ public function __construct($model) {
* @return string the raw SQL statement
*/
public function __toString() {
return $this->builder->statement(TRUE);
return $this->builder->statement(TRUE)->__toString();
}

/**
Expand Down Expand Up @@ -177,7 +177,7 @@ public function reset() {
* @override
* @param boolean $terminated whether to add a semi-colon to the end
* of the statement
* @return string the SQL statement
* @return \Leap\Core\DB\SQL\Command the SQL statement
*/
public function statement($terminated = TRUE) {
return $this->builder->statement($terminated);
Expand Down
6 changes: 3 additions & 3 deletions src/classes/Leap/Core/DB/ORM/Insert/Proxy.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
* @access public
* @class
* @package Leap\Core\DB\ORM\Insert
* @version 2014-01-28
* @version 2014-04-30
*/
class Proxy extends \Leap\Core\Object implements \Leap\Core\DB\SQL\Statement {

Expand Down Expand Up @@ -127,7 +127,7 @@ public function column($column, $value) {
* @override
* @param boolean $terminated whether to add a semi-colon to the end
* of the statement
* @return string the SQL statement
* @return \Leap\Core\DB\SQL\Command the SQL statement
*/
public function statement($terminated = TRUE) {
return $this->builder->statement($terminated);
Expand All @@ -141,7 +141,7 @@ public function statement($terminated = TRUE) {
* @return string the raw SQL statement
*/
public function __toString() {
return $this->builder->statement(TRUE);
return $this->builder->statement(TRUE)->__toString();
}

/**
Expand Down
6 changes: 3 additions & 3 deletions src/classes/Leap/Core/DB/ORM/Select/Proxy.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
* @access public
* @class
* @package Leap\Core\DB\ORM\Select
* @version 2014-01-28
* @version 2014-04-30
*/
class Proxy extends \Leap\Core\Object implements \Leap\Core\DB\SQL\Statement {

Expand Down Expand Up @@ -127,7 +127,7 @@ public function __construct($model, Array $columns = array()) {
* @return string the raw SQL statement
*/
public function __toString() {
return $this->builder->statement(TRUE);
return $this->builder->statement(TRUE)->__toString();
}

/**
Expand Down Expand Up @@ -339,7 +339,7 @@ public function reset() {
* @override
* @param boolean $terminated whether to add a semi-colon to the end
* of the statement
* @return string the SQL statement
* @return \Leap\Core\DB\SQL\Command the SQL statement
*/
public function statement($terminated = TRUE) {
return $this->builder->statement($terminated);
Expand Down
6 changes: 3 additions & 3 deletions src/classes/Leap/Core/DB/ORM/Update/Proxy.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
* @access public
* @class
* @package Leap\Core\DB\ORM\Update
* @version 2014-01-28
* @version 2014-04-30
*/
class Proxy extends \Leap\Core\Object implements \Leap\Core\DB\SQL\Statement {

Expand Down Expand Up @@ -106,7 +106,7 @@ public function __construct($model) {
* @return string the raw SQL statement
*/
public function __toString() {
return $this->builder->statement(TRUE);
return $this->builder->statement(TRUE)->__toString();
}

/**
Expand Down Expand Up @@ -190,7 +190,7 @@ public function set($column, $value) {
* @override
* @param boolean $terminated whether to add a semi-colon to the end
* of the statement
* @return string the SQL statement
* @return \Leap\Core\DB\SQL\Command the SQL statement
*/
public function statement($terminated = TRUE) {
return $this->builder->statement($terminated);
Expand Down
4 changes: 2 additions & 2 deletions src/classes/Leap/Core/DB/SQL/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
* @access public
* @class
* @package Leap\Core\DB\SQL
* @version 2014-01-26
* @version 2014-04-30
*/
abstract class Builder extends \Leap\Core\Object implements \Leap\Core\DB\SQL\Statement {

Expand Down Expand Up @@ -78,7 +78,7 @@ abstract class Builder extends \Leap\Core\Object implements \Leap\Core\DB\SQL\St
* @return string the raw SQL statement
*/
public function __toString() {
return $this->statement(TRUE);
return $this->statement(TRUE)->__toString();
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Expand Down
63 changes: 57 additions & 6 deletions src/classes/Leap/Core/DB/SQL/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,26 +25,66 @@
* @access public
* @class
* @package Leap\Core\DB\SQL
* @version 2014-04-26
* @version 2014-04-30
*/
class Command extends \Leap\Core\Object {

/**
* This variable stores the text.
* This variable stores the data associated with the command.
*
* @access protected
* @var string
*/
protected $text;
protected $data;

/**
* This constructor initializes the class with the specified text.
*
* @access public
* @param string $text the text of the command
*/
public function __construct($text) {
$this->text = $text;
public function __construct($text = '') {
$this->data = array();
$this->data['text'] = $text;
}

/**
* This method returns the value associated with the specified property.
*
* @access public
* @override
* @param string $name the name of the property
* @return mixed the value of the property
* @throws \Leap\Core\Throwable\InvalidProperty\Exception indicates that the specified property is
* either inaccessible or undefined
*/
public function __get($name) {
switch ($name) {
case 'text':
return $this->data[$name];
default:
throw new \Leap\Core\Throwable\InvalidProperty\Exception('Message: Unable to get the specified property. Reason: Property :name is either inaccessible or undefined.', array(':name' => $name));
}
}

/**
* This method sets the value for the specified key.
*
* @access public
* @override
* @param string $name the name of the property
* @param mixed $value the value of the property
* @throws \Leap\Core\Throwable\InvalidProperty\Exception indicates that the specified property is
* either inaccessible or undefined
*/
public function __set($name, $value) {
switch ($name) {
case 'text':
$this->data[$name] = (string) $value;
break;
default:
throw new \Leap\Core\Throwable\InvalidProperty\Exception('Message: Unable to set the specified property. Reason: Property :name is either inaccessible or undefined.', array(':name' => $name));
}
}

/**
Expand All @@ -54,7 +94,18 @@ public function __construct($text) {
* @return string a string that represents the object
*/
public function __toString() {
return $this->text;
return $this->data['text'];
}

/**
* This method trims the semicolon off an SQL statement.
*
* @access protected
* @param string $text the SQL statement
* @return string the SQL statement after being trimmed
*/
protected function trim($text) {
return trim($text, "; \t\n\r\0\x0B");
}

}
Expand Down
16 changes: 8 additions & 8 deletions src/classes/Leap/Core/DB/SQL/Connection/PDO.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
* @access public
* @class
* @package Leap\Core\DB\SQL\Connection
* @version 2014-01-26
* @version 2014-04-30
*
* @see http://www.php.net/manual/en/book.pdo.php
* @see http://www.electrictoolbox.com/php-pdo-dsn-connection-string/
Expand Down Expand Up @@ -58,7 +58,7 @@ public function __destruct() {
public function begin_transaction() {
try {
$this->resource->beginTransaction();
$this->sql = 'BEGIN TRANSACTION;';
$this->sql = new \Leap\Core\DB\SQL\Command('BEGIN TRANSACTION;');
}
catch (\Exception $ex) {
throw new \Leap\Core\Throwable\SQL\Exception('Message: Failed to begin SQL transaction. Reason: :reason', array(':reason' => $ex->getMessage()));
Expand Down Expand Up @@ -92,7 +92,7 @@ public function close() {
public function commit() {
try {
$this->resource->commit();
$this->sql = 'COMMIT;';
$this->sql = new \Leap\Core\DB\SQL\Command('COMMIT;');
}
catch (\Exception $ex) {
throw new \Leap\Core\Throwable\SQL\Exception('Message: Failed to commit SQL transaction. Reason: :reason', array(':reason' => $ex->getMessage()));
Expand All @@ -104,15 +104,15 @@ public function commit() {
*
* @access public
* @override
* @param string $sql the SQL statement
* @param \Leap\Core\DB\SQL\Command $sql the SQL statement
* @throws \Leap\Core\Throwable\SQL\Exception indicates that the executed
* statement failed
*/
public function execute($sql) {
public function execute(\Leap\Core\DB\SQL\Command $sql) {
if ( ! $this->is_connected()) {
throw new \Leap\Core\Throwable\SQL\Exception('Message: Failed to execute SQL statement. Reason: Unable to find connection.');
}
$command = @$this->resource->exec($sql);
$command = @$this->resource->exec($sql->text);
if ($command === FALSE) {
throw new \Leap\Core\Throwable\SQL\Exception('Message: Failed to execute SQL statement. Reason: :reason', array(':reason' => $this->resource->errorInfo()));
}
Expand Down Expand Up @@ -142,7 +142,7 @@ public function get_last_insert_id($table = NULL, $column = 'id') {
$table = $precompiler->prepare_identifier($table);
$column = $precompiler->prepare_identifier($column);
$alias = $precompiler->prepare_alias('id');
$id = (int) $this->query("SELECT MAX({$column}) AS {$alias} FROM {$table};")->get('id', 0);
$id = (int) $this->query(new \Leap\Core\DB\SQL\Command("SELECT MAX({$column}) AS {$alias} FROM {$table};"))->get('id', 0);
$this->sql = $sql;
return $id;
}
Expand Down Expand Up @@ -206,7 +206,7 @@ public function quote($string, $escape = NULL) {
public function rollback() {
try {
$this->resource->rollBack();
$this->sql = 'ROLLBACK;';
$this->sql = new \Leap\Core\DB\SQL\Command('ROLLBACK;');
}
catch (\Exception $ex) {
throw new \Leap\Core\Throwable\SQL\Exception('Message: Failed to rollback SQL transaction. Reason: :reason', array(':reason' => $ex->getMessage()));
Expand Down
Loading

0 comments on commit b412203

Please sign in to comment.