Refactoring database transaction handling, fixing Model test for SQLite, all tests now pass

git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@6931 3807eeeb-6ff5-0310-8944-8be069107fe0
This commit is contained in:
nate 2008-05-18 20:19:08 +00:00
parent fe3f899f46
commit b7fbf8f080
7 changed files with 132 additions and 132 deletions

View file

@ -85,6 +85,17 @@ class DboSource extends DataSource {
* @var array
*/
var $__sqlOps = array('like', 'ilike', 'or', 'not', 'in', 'between', 'regexp', 'similar to');
/**
* Index of basic SQL commands
*
* @var array
* @access protected
*/
var $_commands = array(
'begin' => 'BEGIN',
'commit' => 'COMMIT',
'rollback' => 'ROLLBACK'
);
/**
* Constructor
*/
@ -156,10 +167,10 @@ class DboSource extends DataSource {
function execute($sql) {
$t = getMicrotime();
$this->_result = $this->_execute($sql);
$this->affected = $this->lastAffected();
$this->took = round((getMicrotime() - $t) * 1000, 0);
$this->affected = $this->lastAffected();
$this->error = $this->lastError();
$this->numRows = $this->lastNumRows($this->_result);
$this->numRows = $this->lastNumRows();
if (Configure::read() > 1) {
$this->logQuery($sql);
@ -276,7 +287,7 @@ class DboSource extends DataSource {
}
}
/**
* Returns a row from current resultset as an array .
* Returns a row from current resultset as an array
*
* @return array The fetched row as an array
*/
@ -470,7 +481,7 @@ class DboSource extends DataSource {
$sql = substr($sql, 0, 200) . '[...]';
}
if (($error) && Configure::read() > 1) {
if (($error) || Configure::read() > 1) {
e("<p style = \"text-align:left\"><b>Query:</b> {$sql} ");
if ($error) {
trigger_error("<span style = \"color:Red;text-align:left\"><b>SQL Error:</b> {$this->error}</span>", E_USER_WARNING);
@ -1450,6 +1461,21 @@ class DboSource extends DataSource {
function truncate($table) {
return $this->execute('TRUNCATE TABLE ' . $this->fullTableName($table));
}
/**
* Begin a transaction
*
* @param model $model
* @return boolean True on success, false on fail
* (i.e. if the database/model does not support transactions,
* or a transaction has not started).
*/
function begin(&$model) {
if (parent::begin($model) && $this->execute($this->_commands['begin'])) {
$this->_transactionStarted = true;
return true;
}
return false;
}
/**
* Commit a transaction
*
@ -1459,7 +1485,7 @@ class DboSource extends DataSource {
* or a transaction has not started).
*/
function commit(&$model) {
if (parent::commit($model) && $this->execute('COMMIT')) {
if (parent::commit($model) && $this->execute($this->_commands['commit'])) {
$this->_transactionStarted = false;
return true;
}
@ -1474,7 +1500,7 @@ class DboSource extends DataSource {
* or a transaction has not started).
*/
function rollback(&$model) {
if (parent::rollback($model) && $this->execute('ROLLBACK')) {
if (parent::rollback($model) && $this->execute($this->_commands['rollback'])) {
$this->_transactionStarted = false;
return true;
}