From 607b2844be8f2c89fbbb043bf21b2bad7c40b986 Mon Sep 17 00:00:00 2001 From: "godzilla80@gmx.net" <> Date: Mon, 8 Feb 2010 07:19:50 +0000 Subject: EHN: Add property TransactionClass (defaults to System.Data.TDbTransaction) to TDbConnection and modify beginTransaction() --- framework/Data/TDbConnection.php | 55 +++++++++++++++++++++++++++++++--------- 1 file changed, 43 insertions(+), 12 deletions(-) (limited to 'framework/Data') diff --git a/framework/Data/TDbConnection.php b/framework/Data/TDbConnection.php index 6b84e1b3..4726083c 100644 --- a/framework/Data/TDbConnection.php +++ b/framework/Data/TDbConnection.php @@ -27,10 +27,10 @@ Prado::using('System.Data.TDbCommand'); * specifying {@link setConnectionString ConnectionString}, {@link setUsername Username} * and {@link setPassword Password}. * - * Since 3.1.2, the connection charset can be set (for MySQL and PostgreSQL databases only) + * Since 3.1.2, the connection charset can be set (for MySQL and PostgreSQL databases only) * using the {@link setCharset Charset} property. The value of this property is database dependant. - * e.g. for mysql, you can use 'latin1' for cp1252 West European, 'utf8' for unicode, ... - * + * e.g. for mysql, you can use 'latin1' for cp1252 West European, 'utf8' for unicode, ... + * * The following example shows how to create a TDbConnection instance and establish * the actual connection: * @@ -83,6 +83,12 @@ Prado::using('System.Data.TDbCommand'); */ class TDbConnection extends TComponent { + /** + * + * @since 3.1.7 + */ + const DEFAULT_TRANSACTION_CLASS = 'System.Data.TDbTransaction'; + private $_dsn=''; private $_username=''; private $_password=''; @@ -92,13 +98,19 @@ class TDbConnection extends TComponent private $_pdo=null; private $_transaction; + /** + * @var string + * @since 3.1.7 + */ + private $_transactionClass=self::DEFAULT_TRANSACTION_CLASS; + /** * Constructor. * Note, the DB connection is not established when this connection * instance is created. Set {@link setActive Active} property to true * to establish the connection. * Since 3.1.2, you can set the charset for MySql connection - * + * * @param string The Data Source Name, or DSN, contains the information required to connect to the database. * @param string The user name for the DSN string. * @param string The password for the DSN string. @@ -167,9 +179,9 @@ class TDbConnection extends TComponent try { $this->_pdo=new PDO($this->getConnectionString(),$this->getUsername(), - $this->getPassword(),$this->_attributes); - // This attribute is only useful for PDO::MySql driver. - // Ignore the warning if a driver doesn't understand this. + $this->getPassword(),$this->_attributes); + // This attribute is only useful for PDO::MySql driver. + // Ignore the warning if a driver doesn't understand this. @$this->_pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, true); $this->_pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $this->_active=true; @@ -194,7 +206,7 @@ class TDbConnection extends TComponent /* * Set the database connection charset. - * Only MySql databases are supported for now. + * Only MySql databases are supported for now. * @since 3.1.2 */ protected function setConnectionCharset() @@ -212,7 +224,7 @@ class TDbConnection extends TComponent } $stmt->execute(array($this->_charset)); } - + /** * @return string The Data Source Name, or DSN, contains the information required to connect to the database. */ @@ -269,7 +281,7 @@ class TDbConnection extends TComponent { return $this->_charset; } - + /** * @param string the charset used for database connection */ @@ -278,7 +290,7 @@ class TDbConnection extends TComponent $this->_charset=$value; $this->setConnectionCharset(); } - + /** * @return PDO the PDO instance, null if the connection is not established yet */ @@ -324,12 +336,31 @@ class TDbConnection extends TComponent if($this->getActive()) { $this->_pdo->beginTransaction(); - return $this->_transaction=new TDbTransaction($this); + return $this->_transaction=Prado::createComponent($this->getTransactionClass(), $this); } else throw new TDbException('dbconnection_connection_inactive'); } + /** + * @return string Transaction class name to be created by calling {@link TDbConnection::beginTransaction}. Defaults to 'System.Data.TDbTransaction'. + * @since 3.1.7 + */ + public function getTransactionClass() + { + return $this->_transactionClass; + } + + + /** + * @param string Transaction class name to be created by calling {@link TDbConnection::beginTransaction}. + * @since 3.1.7 + */ + public function setTransactionClass($value) + { + $this->_transactionClass = (string)$value; + } + /** * Returns the ID of the last inserted row or sequence value. * @param string name of the sequence object (required by some DBMS) -- cgit v1.2.3