/** * gets an instance of this class * * @static * @access public * @return Object, System_Database_Connection */ public static function getInstance() { if (!self::$_instance) { self::$_instance = new self(); } return self::$_instance; }
/** * gets a connection to the database and sets it to the _db property * * @param string $connection_name, name of the connection to get * @access public * @return object, System_Database_Base */ public function getConnection($connection_name) { $this->_db = System_Database_Connection::getInstance()->getConnection($connection_name); return $this; }
/** * creates the commit query based on the object registrations * * @access public * @return boolean status of the transaction */ public function commit() { /** * gather all of the queries */ $this->buildNew()->buildDirty()->buildRemoved(); $committed = false; $count = count($this->_query_stack); $transaction_stack = $this->_query_stack; ksort($transaction_stack); /** * get the current db being used */ $db = System_Database_Connection::getInstance()->getConnection('default'); /** * check for errors, if so set the return to false and do not process any queries * check to see if the transaction_stack has any queries. if none set the return to false and process none * if no errors, loop through the transaction_stack and run each query * if the query fails, run the ROLLBACK query, set the return to false, and break the loop * if all of the queries execute fine, run a COMMIT query to commit the transaction * run the reset method no matter what happens */ if ($this->_errors || !$count) { $committed = false; } else { $db->runQuery('START TRANSACTION'); $iter = 0; foreach ($transaction_stack as $group) { $rollback = false; $query = $group['query']; $query_type = $group['type']; if (is_object($query)) { $query->{$query_type}()->run(); if ($query->error()) { $rollback = true; } } elseif (is_string($query)) { $db->runQuery($query); if ($db->error()) { $rollback = true; } } if ($rollback) { $db->runQuery('ROLLBACK'); $committed = false; break; } else { $iter++; } } if ($iter == $count) { $db->runQuery('COMMIT'); $committed = true; } } $this->reset(); return $committed; }