Improved Database class
This commit is contained in:
@@ -23,6 +23,10 @@
|
||||
|
||||
namespace Thelia\Install;
|
||||
|
||||
use Propel\Runtime\Connection\ConnectionWrapper;
|
||||
use Propel\Runtime\Propel;
|
||||
use Propel\Runtime\ServiceContainer\ServiceContainerInterface;
|
||||
|
||||
/**
|
||||
* Class Database
|
||||
* @package Thelia\Install
|
||||
@@ -30,10 +34,33 @@ namespace Thelia\Install;
|
||||
*/
|
||||
class Database
|
||||
{
|
||||
public $connection;
|
||||
/**
|
||||
* @var \PDO
|
||||
*/
|
||||
protected $connection;
|
||||
|
||||
public function __construct(\PDO $connection)
|
||||
/**
|
||||
* Create a new instance, using the provided connection information, either none for
|
||||
* automatically a connection, a ConnectionWrapper instance or a PDo connection.
|
||||
*
|
||||
* @param ConnectionWrapper|\PDO|null $connection the connection object
|
||||
* @throws \InvalidArgumentException if $connection is not of the suitable type.
|
||||
*/
|
||||
public function __construct($connection = null)
|
||||
{
|
||||
// Get a connection from Propel if we don't have one
|
||||
if (null == $connection) {
|
||||
$connection = Propel::getConnection(ServiceContainerInterface::CONNECTION_WRITE);
|
||||
}
|
||||
|
||||
// Get the PDO connection from an
|
||||
if ($connection instanceof ConnectionWrapper)
|
||||
$connection = $connection->getWrappedConnection();
|
||||
|
||||
if (! $connection instanceof \PDO) {
|
||||
throw new \InvalidArgumentException("A PDO connextion shoud be provided");
|
||||
}
|
||||
|
||||
$this->connection = $connection;
|
||||
}
|
||||
|
||||
@@ -69,11 +96,21 @@ class Database
|
||||
$size = count($sql);
|
||||
for ($i = 0; $i < $size; $i ++) {
|
||||
if (!empty($sql[$i])) {
|
||||
$this->connection->query($sql[$i]);
|
||||
$this->execute($sql[$i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A simple wrapper around PDO::exec
|
||||
*
|
||||
* @param string $sql SQL query
|
||||
* @param array $args SQL request parameters (PDO style)
|
||||
*/
|
||||
public function execute($sql, $args = array()) {
|
||||
$this->connection->exec($sql, $args);
|
||||
}
|
||||
|
||||
/**
|
||||
* Separate each sql instruction in an array
|
||||
*
|
||||
@@ -104,7 +141,7 @@ class Database
|
||||
*/
|
||||
public function createDatabase($dbName)
|
||||
{
|
||||
$this->connection->exec(
|
||||
$this->execute(
|
||||
sprintf(
|
||||
"CREATE DATABASE IF NOT EXISTS %s CHARACTER SET utf8",
|
||||
$dbName
|
||||
|
||||
Reference in New Issue
Block a user