Commit 00215994 by Qiang Xue

w

parent 5e1e3fd1
<?php
/**
* CDbException class file.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @link http://www.yiiframework.com/
* @copyright Copyright &copy; 2008-2011 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
/**
* CDbException represents an exception that is caused by some DB-related operations.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @version $Id: CDbException.php 2799 2011-01-01 19:31:13Z qiang.xue $
* @package system.db
* @since 1.0
*/
class CDbException extends CException
{
/**
* @var mixed the error info provided by a PDO exception. This is the same as returned
* by {@link http://www.php.net/manual/en/pdo.errorinfo.php PDO::errorInfo}.
* @since 1.1.4
*/
public $errorInfo;
/**
* Constructor.
* @param string $message PDO error message
* @param integer $code PDO error code
* @param mixed $errorInfo PDO error info
*/
public function __construct($message, $code = 0, $errorInfo = null)
{
$this->errorInfo = $errorInfo;
parent::__construct($message, $code);
}
}
\ No newline at end of file
<?php
/**
* CActiveRecordBehavior class file.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @link http://www.yiiframework.com/
* @copyright Copyright &copy; 2008-2011 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
/**
* CActiveRecordBehavior is the base class for behaviors that can be attached to {@link CActiveRecord}.
* Compared with {@link CModelBehavior}, CActiveRecordBehavior attaches to more events
* that are only defined by {@link CActiveRecord}.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @version $Id: CActiveRecordBehavior.php 2799 2011-01-01 19:31:13Z qiang.xue $
* @package system.db.ar
* @since 1.0.2
*/
class CActiveRecordBehavior extends CModelBehavior
{
/**
* Declares events and the corresponding event handler methods.
* If you override this method, make sure you merge the parent result to the return value.
* @return array events (array keys) and the corresponding event handler methods (array values).
* @see CBehavior::events
*/
public function events()
{
return array_merge(parent::events(), array(
'onBeforeSave' => 'beforeSave',
'onAfterSave' => 'afterSave',
'onBeforeDelete' => 'beforeDelete',
'onAfterDelete' => 'afterDelete',
'onBeforeFind' => 'beforeFind',
'onAfterFind' => 'afterFind',
));
}
/**
* Responds to {@link CActiveRecord::onBeforeSave} event.
* Overrides this method if you want to handle the corresponding event of the {@link CBehavior::owner owner}.
* You may set {@link CModelEvent::isValid} to be false to quit the saving process.
* @param CModelEvent $event event parameter
*/
public function beforeSave($event)
{
}
/**
* Responds to {@link CActiveRecord::onAfterSave} event.
* Overrides this method if you want to handle the corresponding event of the {@link CBehavior::owner owner}.
* @param CModelEvent $event event parameter
*/
public function afterSave($event)
{
}
/**
* Responds to {@link CActiveRecord::onBeforeDelete} event.
* Overrides this method if you want to handle the corresponding event of the {@link CBehavior::owner owner}.
* You may set {@link CModelEvent::isValid} to be false to quit the deletion process.
* @param CEvent $event event parameter
*/
public function beforeDelete($event)
{
}
/**
* Responds to {@link CActiveRecord::onAfterDelete} event.
* Overrides this method if you want to handle the corresponding event of the {@link CBehavior::owner owner}.
* @param CEvent $event event parameter
*/
public function afterDelete($event)
{
}
/**
* Responds to {@link CActiveRecord::onBeforeFind} event.
* Overrides this method if you want to handle the corresponding event of the {@link CBehavior::owner owner}.
* @param CEvent $event event parameter
* @since 1.0.9
*/
public function beforeFind($event)
{
}
/**
* Responds to {@link CActiveRecord::onAfterFind} event.
* Overrides this method if you want to handle the corresponding event of the {@link CBehavior::owner owner}.
* @param CEvent $event event parameter
*/
public function afterFind($event)
{
}
}
<?php
/**
* CDbDataReader class file
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @link http://www.yiiframework.com/
* @copyright Copyright &copy; 2008-2011 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
/**
* CDbDataReader represents a forward-only stream of rows from a query result set.
*
* To read the current row of data, call {@link read}. The method {@link readAll}
* returns all the rows in a single array.
*
* One can also retrieve the rows of data in CDbDataReader by using foreach:
* <pre>
* foreach($reader as $row)
* // $row represents a row of data
* </pre>
* Since CDbDataReader is a forward-only stream, you can only traverse it once.
*
* It is possible to use a specific mode of data fetching by setting
* {@link setFetchMode FetchMode}. See {@link http://www.php.net/manual/en/function.PDOStatement-setFetchMode.php}
* for more details.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @version $Id: CDbDataReader.php 3204 2011-05-05 21:36:32Z alexander.makarow $
* @package system.db
* @since 1.0
*/
class CDbDataReader extends CComponent implements Iterator, Countable
{
private $_statement;
private $_closed = false;
private $_row;
private $_index = -1;
/**
* Constructor.
* @param CDbCommand $command the command generating the query result
*/
public function __construct(CDbCommand $command)
{
$this->_statement = $command->getPdoStatement();
$this->_statement->setFetchMode(PDO::FETCH_ASSOC);
}
/**
* Binds a column to a PHP variable.
* When rows of data are being fetched, the corresponding column value
* will be set in the variable. Note, the fetch mode must include PDO::FETCH_BOUND.
* @param mixed $column Number of the column (1-indexed) or name of the column
* in the result set. If using the column name, be aware that the name
* should match the case of the column, as returned by the driver.
* @param mixed $value Name of the PHP variable to which the column will be bound.
* @param integer $dataType Data type of the parameter
* @see http://www.php.net/manual/en/function.PDOStatement-bindColumn.php
*/
public function bindColumn($column, &$value, $dataType = null)
{
if ($dataType === null)
$this->_statement->bindColumn($column, $value);
else
$this->_statement->bindColumn($column, $value, $dataType);
}
/**
* Set the default fetch mode for this statement
* @param mixed $mode fetch mode
* @see http://www.php.net/manual/en/function.PDOStatement-setFetchMode.php
*/
public function setFetchMode($mode)
{
$params = func_get_args();
call_user_func_array(array($this->_statement, 'setFetchMode'), $params);
}
/**
* Advances the reader to the next row in a result set.
* @return array|false the current row, false if no more row available
*/
public function read()
{
return $this->_statement->fetch();
}
/**
* Returns a single column from the next row of a result set.
* @param integer $columnIndex zero-based column index
* @return mixed|false the column of the current row, false if no more row available
*/
public function readColumn($columnIndex)
{
return $this->_statement->fetchColumn($columnIndex);
}
/**
* Returns an object populated with the next row of data.
* @param string $className class name of the object to be created and populated
* @param array $fields Elements of this array are passed to the constructor
* @return mixed|false the populated object, false if no more row of data available
*/
public function readObject($className, $fields)
{
return $this->_statement->fetchObject($className, $fields);
}
/**
* Reads the whole result set into an array.
* @return array the result set (each array element represents a row of data).
* An empty array will be returned if the result contains no row.
*/
public function readAll()
{
return $this->_statement->fetchAll();
}
/**
* Advances the reader to the next result when reading the results of a batch of statements.
* This method is only useful when there are multiple result sets
* returned by the query. Not all DBMS support this feature.
* @return boolean Returns true on success or false on failure.
*/
public function nextResult()
{
if (($result = $this->_statement->nextRowset()) !== false)
$this->_index = -1;
return $result;
}
/**
* Closes the reader.
* This frees up the resources allocated for executing this SQL statement.
* Read attemps after this method call are unpredictable.
*/
public function close()
{
$this->_statement->closeCursor();
$this->_closed = true;
}
/**
* whether the reader is closed or not.
* @return boolean whether the reader is closed or not.
*/
public function getIsClosed()
{
return $this->_closed;
}
/**
* Returns the number of rows in the result set.
* Note, most DBMS may not give a meaningful count.
* In this case, use "SELECT COUNT(*) FROM tableName" to obtain the number of rows.
* @return integer number of rows contained in the result.
*/
public function getRowCount()
{
return $this->_statement->rowCount();
}
/**
* Returns the number of rows in the result set.
* This method is required by the Countable interface.
* Note, most DBMS may not give a meaningful count.
* In this case, use "SELECT COUNT(*) FROM tableName" to obtain the number of rows.
* @return integer number of rows contained in the result.
*/
public function count()
{
return $this->getRowCount();
}
/**
* Returns the number of columns in the result set.
* Note, even there's no row in the reader, this still gives correct column number.
* @return integer the number of columns in the result set.
*/
public function getColumnCount()
{
return $this->_statement->columnCount();
}
/**
* Resets the iterator to the initial state.
* This method is required by the interface Iterator.
* @throws CException if this method is invoked twice
*/
public function rewind()
{
if ($this->_index < 0)
{
$this->_row = $this->_statement->fetch();
$this->_index = 0;
}
else
throw new CDbException(Yii::t('yii', 'CDbDataReader cannot rewind. It is a forward-only reader.'));
}
/**
* Returns the index of the current row.
* This method is required by the interface Iterator.
* @return integer the index of the current row.
*/
public function key()
{
return $this->_index;
}
/**
* Returns the current row.
* This method is required by the interface Iterator.
* @return mixed the current row.
*/
public function current()
{
return $this->_row;
}
/**
* Moves the internal pointer to the next row.
* This method is required by the interface Iterator.
*/
public function next()
{
$this->_row = $this->_statement->fetch();
$this->_index++;
}
/**
* Returns whether there is a row of data at current position.
* This method is required by the interface Iterator.
* @return boolean whether there is a row of data at current position.
*/
public function valid()
{
return $this->_row !== false;
}
}
<?php
/**
* CDbTransaction class file
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @link http://www.yiiframework.com/
* @copyright Copyright &copy; 2008-2011 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
/**
* CDbTransaction represents a DB transaction.
*
* It is usually created by calling {@link CDbConnection::beginTransaction}.
*
* The following code is a common scenario of using transactions:
* <pre>
* $transaction=$connection->beginTransaction();
* try
* {
* $connection->createCommand($sql1)->execute();
* $connection->createCommand($sql2)->execute();
* //.... other SQL executions
* $transaction->commit();
* }
* catch(Exception $e)
* {
* $transaction->rollBack();
* }
* </pre>
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @version $Id: CDbTransaction.php 3069 2011-03-14 00:28:38Z qiang.xue $
* @package system.db
* @since 1.0
*/
class CDbTransaction extends CComponent
{
private $_connection = null;
private $_active;
/**
* Constructor.
* @param CDbConnection $connection the connection associated with this transaction
* @see CDbConnection::beginTransaction
*/
public function __construct(CDbConnection $connection)
{
$this->_connection = $connection;
$this->_active = true;
}
/**
* Commits a transaction.
* @throws CException if the transaction or the DB connection is not active.
*/
public function commit()
{
if ($this->_active && $this->_connection->getActive())
{
Yii::trace('Committing transaction', 'system.db.CDbTransaction');
$this->_connection->getPdoInstance()->commit();
$this->_active = false;
}
else
throw new CDbException(Yii::t('yii', 'CDbTransaction is inactive and cannot perform commit or roll back operations.'));
}
/**
* Rolls back a transaction.
* @throws CException if the transaction or the DB connection is not active.
*/
public function rollback()
{
if ($this->_active && $this->_connection->getActive())
{
Yii::trace('Rolling back transaction', 'system.db.CDbTransaction');
$this->_connection->getPdoInstance()->rollBack();
$this->_active = false;
}
else
throw new CDbException(Yii::t('yii', 'CDbTransaction is inactive and cannot perform commit or roll back operations.'));
}
/**
* @return CDbConnection the DB connection for this transaction
*/
public function getConnection()
{
return $this->_connection;
}
/**
* @return boolean whether this transaction is active
*/
public function getActive()
{
return $this->_active;
}
/**
* @param boolean $value whether this transaction is active
*/
protected function setActive($value)
{
$this->_active = $value;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment