Commit 4602b575 by Gevik Babakhani

Save point: WIP implementing db schema functionality.

parent c30eae62
...@@ -352,6 +352,8 @@ class Connection extends Component ...@@ -352,6 +352,8 @@ class Connection extends Component
$driver = strtolower(substr($this->dsn, 0, $pos)); $driver = strtolower(substr($this->dsn, 0, $pos));
if ($driver === 'mssql' || $driver === 'dblib' || $driver === 'sqlsrv') { if ($driver === 'mssql' || $driver === 'dblib' || $driver === 'sqlsrv') {
$pdoClass = 'yii\db\mssql\PDO'; $pdoClass = 'yii\db\mssql\PDO';
} else if ($driver === 'pgsql') {
$pdoClass = 'yii\db\pgsql\PDO';
} }
} }
return new $pdoClass($this->dsn, $this->username, $this->password, $this->attributes); return new $pdoClass($this->dsn, $this->username, $this->password, $this->attributes);
......
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\db\pgsql;
/**
* This is an extension of the default PDO class for PostgreSQL drivers.
* It provides additional low level functionality for setting database
* configuration parameters.
*
* @author Gevik babakhani <gevikb@gmail.com>
* @since 2.0
*/
class PDO extends \PDO {
/**
* Here we override the default PDO constructor in order to
* find and set the default schema search path.
*/
public function __construct($dsn, $username, $passwd, $options) {
$searchPath = null;
if (is_array($options) && isset($options['search_path'])) {
$matches = null;
if (preg_match("/(\s?)+(\w)+((\s+)?,(\s+)?\w+)*/", $options['search_path'], $matches) === 1) {
$searchPath = $matches[0];
}
}
parent::__construct($dsn, $username, $passwd, $options);
if (!is_null($searchPath)) {
$this->setSchemaSearchPath($searchPath);
}
}
/**
* Sets the schema search path of the current users session.
* The syntax of the path is a comma separated string with
* your custom search path at the beginning and the "public"
* schema at the end.
*
* This method automatically adds the "public" schema at the
* end of the search path if it is not provied.
* @param string custom schema search path. defaults to public
*/
public function setSchemaSearchPath($searchPath = 'public') {
$schemas = explode(',', str_replace(' ', '', $searchPath));
if (end($schemas) !== 'public') {
$schemas[] = 'public';
}
foreach ($schemas as $k => $item) {
$schemas[$k] = '"' . str_replace(array('"', "'", ';'), '', $item) . '"';
}
$path = implode(', ', $schemas);
$this->exec('SET search_path TO ' . $path);
}
}
<?php <?php
/** /**
* @link http://www.yiiframework.com/ * @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC * @copyright Copyright (c) 2008 Yii Software LLC
...@@ -16,15 +17,39 @@ use yii\db\ColumnSchema; ...@@ -16,15 +17,39 @@ use yii\db\ColumnSchema;
* @author Gevik Babakhani <gevikb@gmail.com> * @author Gevik Babakhani <gevikb@gmail.com>
* @since 2.0 * @since 2.0
*/ */
class Schema extends \yii\db\Schema class Schema extends \yii\db\Schema {
{
/**
* Resolves the table name and schema name (if any).
* @param TableSchema $table the table metadata object
* @param string $name the table name
*/
protected function resolveTableNames($table, $name) {
$parts = explode('.', str_replace('"', '', $name));
if (isset($parts[1])) {
$table->schemaName = $parts[0];
$table->name = $parts[1];
} else {
$table->name = $parts[0];
}
}
/**
* Quotes a table name for use in a query.
* A simple table name has no schema prefix.
* @param string $name table name
* @return string the properly quoted table name
*/
public function quoteSimpleTableName($name) {
return strpos($name, '"') !== false ? $name : '"' . $name . '"';
}
/** /**
* Loads the metadata for the specified table. * Loads the metadata for the specified table.
* @param string $name table name * @param string $name table name
* @return TableSchema|null driver dependent table metadata. Null if the table does not exist. * @return TableSchema|null driver dependent table metadata. Null if the table does not exist.
*/ */
public function loadTableSchema($name) public function loadTableSchema($name) {
{
$table = new TableSchema(); $table = new TableSchema();
$this->resolveTableNames($table, $name); $this->resolveTableNames($table, $name);
$this->findPrimaryKeys($table); $this->findPrimaryKeys($table);
...@@ -32,5 +57,6 @@ class Schema extends \yii\db\Schema ...@@ -32,5 +57,6 @@ class Schema extends \yii\db\Schema
$this->findForeignKeys($table); $this->findForeignKeys($table);
return $table; return $table;
} }
} }
} }
\ No newline at end of file
...@@ -21,9 +21,12 @@ return array( ...@@ -21,9 +21,12 @@ return array(
'fixture' => __DIR__ . '/mssql.sql', 'fixture' => __DIR__ . '/mssql.sql',
), ),
'pgsql' => array( 'pgsql' => array(
'dsn' => 'pgsql:host=localhost;dbname=yiitest;port=5432', 'dsn' => 'pgsql:host=localhost;dbname=yiitest;port=5432;',
'username' => 'postgres', 'username' => 'postgres',
'password' => 'postgres', 'password' => 'postgres',
'attributes' => array(
'search_path' => 'master,hello'
),
'fixture' => __DIR__ . '/postgres.sql', 'fixture' => __DIR__ . '/postgres.sql',
) )
) )
......
...@@ -37,6 +37,9 @@ abstract class DatabaseTestCase extends TestCase ...@@ -37,6 +37,9 @@ abstract class DatabaseTestCase extends TestCase
$db->username = $this->database['username']; $db->username = $this->database['username'];
$db->password = $this->database['password']; $db->password = $this->database['password'];
} }
if (isset($this->database['attributes'])) {
$db->attributes = $this->database['attributes'];
}
if ($open) { if ($open) {
$db->open(); $db->open();
$lines = explode(';', file_get_contents($this->database['fixture'])); $lines = explode(';', file_get_contents($this->database['fixture']));
......
...@@ -4,15 +4,48 @@ namespace yiiunit\framework\db\pgsql; ...@@ -4,15 +4,48 @@ namespace yiiunit\framework\db\pgsql;
use yiiunit\framework\db\ConnectionTest; use yiiunit\framework\db\ConnectionTest;
class PostgreSQLConnectionTest extends ConnectionTest class PostgreSQLConnectionTest extends ConnectionTest {
{
public function setUp() public function setUp() {
{ $this->driverName = 'pgsql';
$this->driverName = 'pgsql'; parent::setUp();
parent::setUp(); }
}
public function testConnection() {
public function testConnection() { $connection = $this->getConnection(true);
$connection = $this->getConnection(true); }
}
function testQuoteValue() {
$connection = $this->getConnection(false);
$this->assertEquals(123, $connection->quoteValue(123));
$this->assertEquals("'string'", $connection->quoteValue('string'));
$this->assertEquals("'It''s interesting'", $connection->quoteValue("It's interesting"));
}
function testQuoteTableName()
{
$connection = $this->getConnection(false);
$this->assertEquals('"table"', $connection->quoteTableName('table'));
$this->assertEquals('"table"', $connection->quoteTableName('"table"'));
$this->assertEquals('"schema"."table"', $connection->quoteTableName('schema.table'));
$this->assertEquals('"schema"."table"', $connection->quoteTableName('schema."table"'));
$this->assertEquals('"schema"."table"', $connection->quoteTableName('"schema"."table"'));
$this->assertEquals('{{table}}', $connection->quoteTableName('{{table}}'));
$this->assertEquals('(table)', $connection->quoteTableName('(table)'));
}
function testQuoteColumnName()
{
$connection = $this->getConnection(false);
$this->assertEquals('"column"', $connection->quoteColumnName('column'));
$this->assertEquals('"column"', $connection->quoteColumnName('"column"'));
$this->assertEquals('"table"."column"', $connection->quoteColumnName('table.column'));
$this->assertEquals('"table"."column"', $connection->quoteColumnName('table."column"'));
$this->assertEquals('"table"."column"', $connection->quoteColumnName('"table"."column"'));
$this->assertEquals('[[column]]', $connection->quoteColumnName('[[column]]'));
$this->assertEquals('{{column}}', $connection->quoteColumnName('{{column}}'));
$this->assertEquals('(column)', $connection->quoteColumnName('(column)'));
}
} }
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