PDO to ConnectionInterface #315

Merged
merged 1 commit into from Nov 20, 2012
Jump to file or symbol
Failed to load files and symbols.
+50 −46
Split
@@ -328,7 +328,7 @@ protected function addToVersion(&$script)
* Sets the properties of the current object to the value they had at a specific version
*
* @param integer \$versionNumber The version number to read
- * @param ConnectionInterface \$con the connection to use
+ * @param ConnectionInterface \$con The connection to use
*
* @return {$ARclassName} The current object (for fluent API support)
*/
@@ -612,10 +612,10 @@ protected function addCompareVersion(&$script)
* );
* </code>
*
- * @param integer \$versionNumber
- * @param string \$keys Main key used for the result diff (versions|columns)
- * @param PropelPDO \$con the connection to use
- * @param array \$ignoredColumns The columns to exclude from the diff.
+ * @param integer \$versionNumber
+ * @param string \$keys Main key used for the result diff (versions|columns)
+ * @param ConnectionInterfarce \$con the connection to use
+ * @param array \$ignoredColumns The columns to exclude from the diff.
*
* @return array A list of differences
*/
@@ -642,11 +642,11 @@ protected function addCompareVersions(&$script)
* );
* </code>
*
- * @param integer \$fromVersionNumber
- * @param integer \$toVersionNumber
- * @param string \$keys Main key used for the result diff (versions|columns)
- * @param PropelPDO \$con the connection to use
- * @param array \$ignoredColumns The columns to exclude from the diff.
+ * @param integer \$fromVersionNumber
+ * @param integer \$toVersionNumber
+ * @param string \$keys Main key used for the result diff (versions|columns)
+ * @param ConnectionInterface \$con the connection to use
+ * @param array \$ignoredColumns The columns to exclude from the diff.
*
* @return array A list of differences
*/
@@ -14,6 +14,8 @@
use Propel\Generator\Model\Table;
use Propel\Generator\Platform\PlatformInterface;
use Propel\Generator\Reverse\SchemaParserInterface;
+use Propel\Runtime\Adapter\AdapterFactory;
+use Propel\Runtime\Connection\ConnectionFactory;
use Propel\Runtime\Connection\ConnectionInterface;
/**
@@ -298,7 +300,7 @@ public function getBuildConnection($databaseName = null)
}
}
- public function getBuildPDO($database)
+ public function getConnection($database)
{
$buildConnection = $this->getBuildConnection($database);
$dsn = str_replace("@DB@", $database, $buildConnection['dsn']);
@@ -307,9 +309,8 @@ public function getBuildPDO($database)
$username = isset($buildConnection['user']) && $buildConnection['user'] ? $buildConnection['user'] : null;
$password = isset($buildConnection['password']) && $buildConnection['password'] ? $buildConnection['password'] : null;
- $pdo = new \PDO($dsn, $username, $password);
- $pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
+ $con = ConnectionFactory::create(array('dsn' => $dsn, 'user' => $username, 'password' => $password), AdapterFactory::create($buildConnection['adapter']));
- return $pdo;
+ return $con;
}
}
@@ -13,7 +13,8 @@
use Propel\Generator\Exception\BuildException;
use Propel\Generator\Model\IdMethod;
use Propel\Generator\Model\Database;
-use Propel\Runtime\Adapter\Pdo\PdoConnection;
+use Propel\Runtime\Adapter\AdapterFactory;
+use Propel\Runtime\Connection\ConnectionFactory;
/**
* @author William Durand <william.durand1@gmail.com>
@@ -190,7 +191,7 @@ protected function buildModel()
}
/**
- * @return PdoConnection
+ * @return ConnectionInterface
*/
protected function getConnection()
{
@@ -200,9 +201,8 @@ protected function getConnection()
$username = isset($buildConnection['user']) && $buildConnection['user'] ? $buildConnection['user'] : null;
$password = isset($buildConnection['password']) && $buildConnection['password'] ? $buildConnection['password'] : null;
- $pdo = new PdoConnection($buildConnection['dsn'], $username, $password);
- $pdo->setAttribute(PdoConnection::ATTR_ERRMODE, PdoConnection::ERRMODE_EXCEPTION);
+ $con = ConnectionFactory::create(array('dsn' => $dsn, 'user' => $username, 'password' => $password), AdapterFactory::create($buildConnection['adapter']));
- return $pdo;
+ return $con;
}
}
@@ -12,6 +12,8 @@
use Propel\Generator\Exception\InvalidArgumentException;
use Propel\Generator\Util\SqlParser;
+use Propel\Runtime\Adapter\AdapterFactory;
+use Propel\Runtime\Connection\ConnectionFactory;
/**
* Service class for managing SQL.
@@ -164,18 +166,18 @@ public function insertSql($datasource = null)
continue;
}
- $pdo = $this->getPdoConnection($database);
- $pdo->beginTransaction();
+ $con = $this->getConnectionInstance($database);
+ $con->beginTransaction();
try {
foreach ($sqls as $sql) {
- $stmt = $pdo->prepare($sql);
+ $stmt = $con->prepare($sql);
$stmt->execute();
}
- $pdo->commit();
+ $con->commit();
} catch (\PDOException $e) {
- $pdo->rollback();
+ $con->rollback();
throw $e;
}
}
@@ -184,22 +186,22 @@ public function insertSql($datasource = null)
}
/**
- * Returns a PDO connection for a given datasource.
+ * Returns a ConnectionInterface instance for a given datasource.
*
- * @return PDO
+ * @return ConnectionInterface
*/
- protected function getPdoConnection($datasource)
+ protected function getConnectionInstance($datasource)
{
$buildConnection = $this->getConnection($datasource);
+
$dsn = str_replace("@DB@", $datasource, $buildConnection['dsn']);
// Set user + password to null if they are empty strings or missing
$username = isset($buildConnection['user']) && $buildConnection['user'] ? $buildConnection['user'] : null;
$password = isset($buildConnection['password']) && $buildConnection['password'] ? $buildConnection['password'] : null;
- $pdo = new \PDO($dsn, $username, $password);
- $pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
+ $con = ConnectionFactory::create(array('dsn' => $dsn, 'user' => $username, 'password' => $password), AdapterFactory::create($buildConnection['adapter']));
- return $pdo;
+ return $con;
}
}
@@ -132,7 +132,7 @@ public function parse(Database $database)
protected function addColumns(Table $table)
{
$stmt = $this->dbh->query("SELECT COLUMN_NAME, DATA_TYPE, NULLABLE, DATA_LENGTH, DATA_PRECISION, DATA_SCALE, DATA_DEFAULT FROM USER_TAB_COLS WHERE TABLE_NAME = '" . $table->getName() . "'");
- /* @var stmt PDOStatement */
+ /* @var stmt StatementInterface */
while ($row = $stmt->fetch(\PDO::FETCH_ASSOC)) {
if (false !== strpos($row['COLUMN_NAME'], '$')) {
// this is an Oracle internal column - prune
@@ -228,11 +228,11 @@ protected function addForeignKeys(Table $table)
$foreignKeys = array();
$stmt = $this->dbh->query("SELECT CONSTRAINT_NAME, DELETE_RULE, R_CONSTRAINT_NAME FROM USER_CONSTRAINTS WHERE CONSTRAINT_TYPE = 'R' AND TABLE_NAME = '" . $table->getName(). "'");
- /* @var stmt PDOStatement */
+ /* @var stmt StatementInterface */
while ($row = $stmt->fetch(\PDO::FETCH_ASSOC)) {
// Local reference
$stmt2 = $this->dbh->query("SELECT COLUMN_NAME FROM USER_CONS_COLUMNS WHERE CONSTRAINT_NAME = '".$row['CONSTRAINT_NAME']."' AND TABLE_NAME = '" . $table->getName(). "'");
- /* @var stmt2 PDOStatement */
+ /* @var stmt2 StatementInterface */
$localReferenceInfo = $stmt2->fetch(\PDO::FETCH_ASSOC);
// Foreign reference
@@ -260,7 +260,7 @@ protected function addForeignKeys(Table $table)
protected function addPrimaryKey(Table $table)
{
$stmt = $this->dbh->query("SELECT COLS.COLUMN_NAME FROM USER_CONSTRAINTS CONS, USER_CONS_COLUMNS COLS WHERE CONS.CONSTRAINT_NAME = COLS.CONSTRAINT_NAME AND CONS.TABLE_NAME = '".$table->getName()."' AND CONS.CONSTRAINT_TYPE = 'P'");
- /* @var stmt PDOStatement */
+ /* @var stmt StatementInterface */
while ($row = $stmt->fetch(\PDO::FETCH_ASSOC)) {
// This fixes a strange behavior by PDO. Sometimes the
// row values are inside an index 0 of an array
@@ -10,6 +10,7 @@
namespace Propel\Generator\Util;
+use \Propel\Runtime\Connection\ConnectionInterface;
use \Propel\Runtime\Connection\StatementInterface;
/**
@@ -52,12 +53,12 @@ public function getSQL()
* Execute a list of DDL statements based on a string
* Does not use transactions since they are not supported in DDL statements
*
- * @param string $input The SQL statements
- * @param PDO $connection a connection object
+ * @param string $input The SQL statements
+ * @param ConnectionInterface $connection a connection object
*
* @return integer the number of executed statements
*/
- public static function executeString($input, $connection)
+ public static function executeString($input, ConnectionInterface $connection)
{
return self::executeStatements(self::parseString($input), $connection);
}
@@ -66,12 +67,12 @@ public static function executeString($input, $connection)
* Execute a list of DDL statements based on the path to the SQL file
* Does not use transactions since they are not supported in DDL statements
*
- * @param string $file the path to the SQL file
- * @param PDO $connection a connection object
+ * @param string $file the path to the SQL file
+ * @param ConnectionInterface $connection a connection object
*
* @return integer the number of executed statements
*/
- public static function executeFile($file, $connection)
+ public static function executeFile($file, ConnectionInterface $connection)
{
return self::executeStatements(self::parseFile($file), $connection);
}
@@ -80,18 +81,18 @@ public static function executeFile($file, $connection)
* Execute a list of DDL statements based on an array
* Does not use transactions since they are not supported in DDL statements
*
- * @param array $statements a list of SQL statements
- * @param PDO $connection a connection object
+ * @param array $statements a list of SQL statements
+ * @param ConnectionInterface $connection a connection object
*
* @return integer the number of executed statements
*/
- protected static function executeStatements($statements, $connection)
+ protected static function executeStatements($statements, ConnectionInterface $connection)
{
$executed = 0;
foreach ($statements as $statement) {
$stmt = $connection->prepare($statement);
- if (($stmt instanceof PDOStatement) || ($stmt instanceof StatementInterface)) {
+ if ($stmt instanceof StatementInterface) {
// only execute if has no error
$stmt->execute();
$executed++;
@@ -1335,7 +1335,7 @@ public function findPks($keys, $con = null)
*
* @param ConnectionInterface $con A connection object
*
- * @return \PDOStatement A PDO statement executed using the connection, ready to be fetched
+ * @return StatementInterface A statement executed using the connection, ready to be fetched
*/
protected function doSelect($con)
{
@@ -28,7 +28,7 @@ class OnDemandIterator implements \Iterator
protected $formatter;
/**
- * @var PDOStatement
+ * @var StatementInterface
*/
protected $stmt;