Make mysql/postgres drivers compatible with Quercus

This commit is contained in:
Timothy Warren 2012-07-05 14:19:49 -04:00
parent 7649f75cac
commit 1e71b225c5
10 changed files with 54 additions and 22 deletions

View File

@ -158,7 +158,13 @@ class Query_Builder {
// Convert array to object // Convert array to object
if (is_array($params)) if (is_array($params))
{ {
$p = new ArrayObject($params, ArrayObject::ARRAY_AS_PROPS); $p = new stdClass();
foreach($params as $k => $v)
{
$p->$k = $v;
}
$params = $p; $params = $p;
} }
@ -171,12 +177,20 @@ class Query_Builder {
$params->type = strtolower($params->type); $params->type = strtolower($params->type);
$dbtype = ($params->type !== 'postgresql') ? $params->type : 'pgsql'; $dbtype = ($params->type !== 'postgresql') ? $params->type : 'pgsql';
$dsn = '';
// Add the driver type to the dsn
if ($dbtype !== 'firebird' && $dbtype !== 'sqlite')
{
$dsn = strtolower($dbtype).':'.$dsn;
}
// Create the dsn for the database to connect to // Create the dsn for the database to connect to
switch($dbtype) switch($dbtype)
{ {
default: default:
$dsn = "dbname={$params->conn_db}"; $dsn .= "dbname={$params->conn_db}";
if ( ! empty($params->host)) if ( ! empty($params->host))
{ {
@ -191,14 +205,14 @@ class Query_Builder {
break; break;
case "sqlite": case "sqlite":
$dsn = $params->file; $dsn .= $params->file;
break; break;
case "firebird": case "firebird":
$dsn = "{$params->host}:{$params->file}"; $dsn = "{$params->host}:{$params->file}";
break; break;
} }
// Create the database connection // Create the database connection
if ( ! empty($params->user)) if ( ! empty($params->user))
{ {
@ -1209,7 +1223,7 @@ class Query_Builder {
// Only unset class variables that // Only unset class variables that
// are not callable. Otherwise, we'll // are not callable. Otherwise, we'll
// delete class methods! // delete class methods!
foreach($this as $name => &$var) foreach($this as $name => $var)
{ {
// Skip properties that are needed for every query // Skip properties that are needed for every query
if (in_array($name, array( if (in_array($name, array(

View File

@ -38,11 +38,19 @@ class MySQL extends DB_PDO {
*/ */
public function __construct($dsn, $username=null, $password=null, $options=array()) public function __construct($dsn, $username=null, $password=null, $options=array())
{ {
$options = array_merge($options, array( if (defined('PDO::MYSQL_ATTR_INIT_COMMAND'))
PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES UTF-8 COLLATE 'UTF-8'", {
)); $options = array_merge($options, array(
PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES UTF-8 COLLATE 'UTF-8'",
parent::__construct("mysql:{$dsn}", $username, $password, $options); ));
}
if (strpos($dsn, 'mysql') === FALSE)
{
$dsn = 'mysql:'.$dsn;
}
parent::__construct($dsn, $username, $password, $options);
} }
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------

View File

@ -171,7 +171,7 @@ class MySQL_Util extends DB_Util {
$output_sql = ''; $output_sql = '';
// Select the rows from each Table // Select the rows from each Table
foreach($tables as &$t) foreach($tables as $t)
{ {
$sql = "SELECT * FROM `{$t}`"; $sql = "SELECT * FROM `{$t}`";
$res = $this->query($sql); $res = $this->query($sql);
@ -188,10 +188,15 @@ class MySQL_Util extends DB_Util {
$insert_rows = array(); $insert_rows = array();
// Create the insert statements // Create the insert statements
foreach($rows as &$row) foreach($rows as $row)
{ {
$row = array_values($row); $row = array_values($row);
$row = array_map(array(&$this, 'quote'), $row);
// Workaround for Quercus
foreach($row as &$r)
{
$r = $this->quote($r);
}
$row = array_map('trim', $row); $row = array_map('trim', $row);
$row_string = 'INSERT INTO `'.trim($t).'` (`'.implode('`,`', $columns).'`) VALUES ('.implode(',', $row).');'; $row_string = 'INSERT INTO `'.trim($t).'` (`'.implode('`,`', $columns).'`) VALUES ('.implode(',', $row).');';

View File

@ -31,7 +31,12 @@ class PgSQL extends DB_PDO {
*/ */
public function __construct($dsn, $username=null, $password=null, $options=array()) public function __construct($dsn, $username=null, $password=null, $options=array())
{ {
parent::__construct("pgsql:{$dsn}", $username, $password, $options); if (strpos($dsn, 'pgsql') === FALSE)
{
$dsn = 'pgsql:'.$dsn;
}
parent::__construct($dsn, $username, $password, $options);
} }
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------

View File

@ -36,7 +36,7 @@ class SQLite extends DB_PDO {
* @param string $pass * @param string $pass
*/ */
public function __construct($dsn, $user=NULL, $pass=NULL) public function __construct($dsn, $user=NULL, $pass=NULL)
{ {
// DSN is simply `sqlite:/path/to/db` // DSN is simply `sqlite:/path/to/db`
parent::__construct("sqlite:{$dsn}", $user, $pass); parent::__construct("sqlite:{$dsn}", $user, $pass);
} }

View File

@ -24,7 +24,7 @@ class MySQLQBTest extends QBTest {
{ {
$params = json_decode(file_get_contents(QBASE_DIR . "test_config.json")); $params = json_decode(file_get_contents(QBASE_DIR . "test_config.json"));
$params = $params->mysql; $params = $params->mysql;
$params->type = "mysql"; $params->type = "MySQL";
$this->db = new Query_Builder($params); $this->db = new Query_Builder($params);

View File

@ -25,10 +25,10 @@ class MySQLTest extends DBTest {
// Attempt to connect, if there is a test config file // Attempt to connect, if there is a test config file
if (is_file(QBASE_DIR . "test_config.json")) if (is_file(QBASE_DIR . "test_config.json"))
{ {
$params = json_decode(file_get_contents(QBASE_DIR . "test_config.json"), TRUE); $params = json_decode(file_get_contents(QBASE_DIR . "test_config.json"));
$params = $params['mysql']; $params = $params->mysql;
$this->db = new MySQL("host={$params['host']};port={$params['port']};dbname={$params['database']}", $params['user'], $params['pass']); $this->db = new MySQL("mysql:host={$params->host};dbname={$params->database}", $params->user, $params->pass);
} }
elseif (($var = getenv('CI'))) elseif (($var = getenv('CI')))
{ {

View File

@ -49,6 +49,6 @@ class PgSQLQBTest extends QBTest {
public function TestExists() public function TestExists()
{ {
$this->assertTrue(in_array('pgsql', pdo_drivers())); $this->assertTrue(in_array('pgsql', PDO::getAvailableDrivers()));
} }
} }

View File

@ -34,7 +34,7 @@ class PgTest extends DBTest {
$params = json_decode(file_get_contents(QBASE_DIR . "test_config.json")); $params = json_decode(file_get_contents(QBASE_DIR . "test_config.json"));
$params = $params->pgsql; $params = $params->pgsql;
$this->db = new PgSQL("host={$params->host};port={$params->port};dbname={$params->database}", $params->user, $params->pass); $this->db = new PgSQL("pgsql:host={$params->host};dbname={$params->database}", $params->user, $params->pass);
} }
elseif (($var = getenv('CI'))) elseif (($var = getenv('CI')))
{ {
@ -46,7 +46,7 @@ class PgTest extends DBTest {
public function TestExists() public function TestExists()
{ {
$this->assertTrue(in_array('pgsql', pdo_drivers())); $this->assertTrue(in_array('pgsql', PDO::getAvailableDrivers()));
} }
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------

Binary file not shown.