Commit 75e0c1ed authored by jwage's avatar jwage

[2.0] More general work on the SchemaManager and Platform classes. Making API...

[2.0] More general work on the SchemaManager and Platform classes. Making API more complete and adding sqlite and mysql test coverage.
parent 746d9bc3
......@@ -192,6 +192,26 @@ class Connection
$this->_platform->setQuoteIdentifiers($this->_quoteIdentifiers);
}
/**
* Get the array of parameters used to instantiated this connection instance
*
* @return array $params
*/
public function getParams()
{
return $this->_params;
}
/**
* Get the name of the database connected to for this Connection instance
*
* @return string $database
*/
public function getDatabase()
{
return $this->_driver->getDatabase($this);
}
/**
* Gets the DBAL driver instance.
*
......@@ -584,7 +604,6 @@ class Connection
*/
public function close()
{
$this->clear();
unset($this->_conn);
$this->_isConnected = false;
}
......
......@@ -33,6 +33,7 @@ interface Driver
* Gets the SchemaManager that can be used to inspect and change the underlying
* database schema of the platform this driver connects to.
*
* @param Doctrine\DBAL\Connection $conn
* @return Doctrine\DBAL\SchemaManager
*/
public function getSchemaManager(Connection $conn);
......@@ -43,4 +44,12 @@ interface Driver
* @return string The name of the driver
*/
public function getName();
/**
* Get the name of the database connected to for this driver instance
*
* @param Doctrine\DBAL\Connection $conn
* @return string $database
*/
public function getDatabase(\Doctrine\DBAL\Connection $conn);
}
\ No newline at end of file
......@@ -62,4 +62,10 @@ class Driver implements \Doctrine\DBAL\Driver
{
return 'pdo_mssql';
}
public function getDatabase(\Doctrine\DBAL\Connection $conn)
{
$params = $conn->getParams();
return $params['dbname'];
}
}
\ No newline at end of file
......@@ -87,4 +87,10 @@ class Driver implements \Doctrine\DBAL\Driver
{
return 'pdo_mysql';
}
public function getDatabase(\Doctrine\DBAL\Connection $conn)
{
$params = $conn->getParams();
return $params['dbname'];
}
}
\ No newline at end of file
......@@ -79,4 +79,10 @@ class Driver implements \Doctrine\DBAL\Driver
{
return 'pdo_oracle';
}
public function getDatabase(\Doctrine\DBAL\Connection $conn)
{
$params = $conn->getParams();
return $params['dbname'];
}
}
\ No newline at end of file
......@@ -56,4 +56,10 @@ class Driver implements \Doctrine\DBAL\Driver
{
return 'pdo_pgsql';
}
public function getDatabase(\Doctrine\DBAL\Connection $conn)
{
$params = $conn->getParams();
return $params['dbname'];
}
}
\ No newline at end of file
......@@ -88,4 +88,10 @@ class Driver implements \Doctrine\DBAL\Driver
{
return 'pdo_sqlite';
}
public function getDatabase(\Doctrine\DBAL\Connection $conn)
{
$params = $conn->getParams();
return isset($params['path']) ? $params['path'] : null;
}
}
\ No newline at end of file
......@@ -662,6 +662,10 @@ abstract class AbstractPlatform
*/
public function getCreateIndexSql($table, $name, array $definition)
{
if ( ! isset($definition['fields'])) {
throw DoctrineException::updateMe('You must specify an array of fields to create the index for');
}
$type = '';
if (isset($definition['type'])) {
switch (strtolower($definition['type'])) {
......@@ -1300,7 +1304,7 @@ abstract class AbstractPlatform
throw DoctrineException::updateMe('List triggers not supported by this driver.');
}
public function getListSequencesSql()
public function getListSequencesSql($database)
{
throw DoctrineException::updateMe('List sequences not supported by this driver.');
}
......@@ -1330,6 +1334,21 @@ abstract class AbstractPlatform
throw DoctrineException::updateMe('List views not supported by this driver.');
}
public function getListTableIndexesSql($table)
{
throw DoctrineException::updateMe('List table indexes not supported by this driver.');
}
public function getCreateViewSql($name, $sql)
{
throw DoctrineException::updateMe('Create view not supported by this driver');
}
public function getDropViewSql($name)
{
throw DoctrineException::updateMe('Drop view not supported by this driver');
}
public function getDropSequenceSql($sequenceName)
{
throw DoctrineException::updateMe('Drop sequence not supported by this driver.');
......
......@@ -146,7 +146,55 @@ class MySqlPlatform extends AbstractPlatform
$args = func_get_args();
return 'CONCAT(' . join(', ', (array) $args) . ')';
}
public function getListDatabasesSql()
{
return 'SHOW DATABASES';
}
public function getListSequencesSql($database)
{
$query = 'SHOW TABLES';
if ( ! is_null($database)) {
$query .= ' FROM ' . $this->quoteIdentifier($database);
}
return $query;
}
public function getListTableConstraintsSql($table)
{
return 'SHOW INDEX FROM ' . $this->quoteIdentifier($table);
}
public function getListTableIndexesSql($table)
{
return 'SHOW INDEX FROM ' . $this->quoteIdentifier($table);
}
public function getListUsersSql()
{
return "SELECT * FROM mysql.user WHERE user != '' GROUP BY user";
}
public function getListViewsSql($database = null)
{
if (is_null($database)) {
return 'SELECT * FROM information_schema.VIEWS';
} else {
return "SHOW FULL TABLES FROM " . $database . " WHERE Table_type = 'VIEW'";
}
}
public function getCreateViewSql($name, $sql)
{
return 'CREATE VIEW ' . $name . ' AS ' . $sql;
}
public function getDropViewSql($name)
{
return 'DROP VIEW '. $name;
}
/**
* Gets the SQL snippet used to declare a VARCHAR column on the MySql platform.
*
......@@ -739,7 +787,6 @@ class MySqlPlatform extends AbstractPlatform
*/
public function getIndexDeclarationSql($name, array $definition)
{
$name = $this->formatter->getIndexName($name);
$type = '';
if (isset($definition['type'])) {
switch (strtolower($definition['type'])) {
......
......@@ -520,7 +520,7 @@ class PostgreSqlPlatform extends AbstractPlatform
*
* @override
*/
public function getListSequencesSql()
public function getListSequencesSql($database)
{
return "SELECT
relname
......@@ -599,7 +599,7 @@ class PostgreSqlPlatform extends AbstractPlatform
*
* @override
*/
public function getListTableIndexesSql()
public function getListTableIndexesSql($table)
{
return "SELECT
relname
......@@ -608,7 +608,7 @@ class PostgreSqlPlatform extends AbstractPlatform
WHERE oid IN (
SELECT indexrelid
FROM pg_index, pg_class
WHERE pg_class.relname = %s
WHERE pg_class.relname = '$table'
AND pg_class.oid=pg_index.indrelid
AND indisunique != 't'
AND indisprimary != 't'
......
......@@ -346,14 +346,14 @@ class SqlitePlatform extends AbstractPlatform
: ($length ? 'VARCHAR(' . $length . ')' : 'TEXT');
}
public function getListSequencesSql()
public function getListSequencesSql($database)
{
return "SELECT name FROM sqlite_master WHERE type='table' AND sql NOT NULL ORDER BY name";
}
public function getListTableConstraintsSql($table)
{
return "SELECT sql FROM sqlite_master WHERE type='index' AND tbl_name = $table AND sql NOT NULL ORDER BY name";
return "SELECT sql FROM sqlite_master WHERE type='index' AND tbl_name = '$table' AND sql NOT NULL ORDER BY name";
}
public function getListTableColumnsSql($table)
......@@ -383,6 +383,16 @@ class SqlitePlatform extends AbstractPlatform
return "SELECT name, sql FROM sqlite_master WHERE type='view' AND sql NOT NULL";
}
public function getCreateViewSql($name, $sql)
{
return 'CREATE VIEW ' . $name . ' AS ' . $sql;
}
public function getDropViewSql($name)
{
return 'DROP VIEW '. $name;
}
/**
* SQLite does support foreign key constraints, but only in CREATE TABLE statements...
* This really limits their usefulness and requires SQLite specific handling, so
......
......@@ -109,9 +109,12 @@ abstract class AbstractSchemaManager
*
* @return array $sequences
*/
public function listSequences()
public function listSequences($database = null)
{
$sql = $this->_platform->getListSequencesSql();
if (is_null($database)) {
$database = $this->_conn->getDatabase();
}
$sql = $this->_platform->getListSequencesSql($database);
$sequences = $this->_conn->fetchAll($sql);
......@@ -128,7 +131,7 @@ abstract class AbstractSchemaManager
{
$sql = $this->_platform->getListTableConstraintsSql($table);
$tableContraints = $this->_conn->fetchAll($sql);
$tableConstraints = $this->_conn->fetchAll($sql);
return $this->_getPortableTableConstraintsList($tableConstraints);
}
......@@ -208,11 +211,15 @@ abstract class AbstractSchemaManager
/**
* Drop the database for this connection
*
* @param string $database The name of the database to drop
* @return boolean $result
*/
public function dropDatabase()
public function dropDatabase($database = null)
{
$sql = $this->_platform->getDropDatabaseSql();
if (is_null($database)) {
$database = $this->_conn->getDatabase();
}
$sql = $this->_platform->getDropDatabaseSql($database);
return $this->_executeSql($sql, 'execute');
}
......@@ -289,11 +296,14 @@ abstract class AbstractSchemaManager
/**
* Create the given database on the connection
*
* @param string $database The name of the database
* @param string $database The name of the database to create
* @return boolean $result
*/
public function createDatabase($database)
public function createDatabase($database = null)
{
if (is_null($database)) {
$database = $this->_conn->getDatabase();
}
$sql = $this->_platform->getCreateDatabaseSql($database);
return $this->_executeSql($sql, 'exec');
......@@ -429,6 +439,33 @@ abstract class AbstractSchemaManager
return $this->_executeSql($sql, 'exec');
}
/**
* Create a new view
*
* @param string $name The name of the view
* @param string $sql The sql to set to the view
* @return boolean $result
*/
public function createView($name, $sql)
{
$sql = $this->_platform->getCreateViewSql($name, $sql);
return $this->_executeSql($sql, 'exec');
}
/**
* Drop a view
*
* @param string $name The name of the view
* @return boolean $result
*/
public function dropView($name)
{
$sql = $this->_platform->getDropViewSql($name);
return $this->_executeSql($sql, 'exec');
}
/**
* Alter an existing tables schema
*
......@@ -524,12 +561,113 @@ abstract class AbstractSchemaManager
return $this->_executeSql($sql, 'exec');
}
/**
* Rename a given table to another name
*
* @param string $name The current name of the table
* @param string $newName The new name of the table
* @return boolean $result
*/
public function renameTable($name, $newName)
{
$change = array(
'name' => $newName
);
return $this->alterTable($name, $change);
}
/**
* Add a new table column
*
* @param string $name The name of the table
* @param array $definition The definition of the column to add
* @return boolean $result
*/
public function addTableColumn($name, $definition)
{
$change = array(
'add' => array(
$name => $definition
)
);
return $this->alterTable($name, $change);
}
/**
* Remove a column from a table
*
* @param string $tableName The name of the table
* @param array|string $column The column name or array of names
* @return boolean $result
*/
public function removeTableColumn($name, $column)
{
$change = array(
'remove' => is_array($column) ? $column : array($column => array())
);
return $this->alterTable($name, $change);
}
/**
* Change a given table column. You can change the type, length, etc.
*
* @param string $name The name of the table
* @param string $type The type of the column
* @param string $length The length of the column
* @param string $definition The definition array for the column
* @return boolean $result
*/
public function changeTableColumn($name, $type, $length = null, $definition = array())
{
$definition['type'] = $type;
$change = array(
'change' => array(
$name => array(
'length' => $length,
'definition' => $definition
)
)
);
return $this->alterTable($name, $change);
}
/**
* Rename a given table column
*
* @param string $name The name of the table
* @param string $oldName The old column name
* @param string $newName The new column
* @param string $definition The column definition array if you want to change something
* @return boolean $result
*/
public function renameTableColumn($name, $oldName, $newName, $definition = array())
{
$change = array(
'rename' => array(
$oldName => array(
'name' => $newName,
'definition' => $definition
)
)
);
return $this->alterTable($name, $change);
}
protected function _getPortableDatabasesList($databases)
{
$list = array();
foreach ($databases as $key => $value) {
$databases[$key] = $this->_getPortableDatabaseDefinition($value);
if ($value = $this->_getPortableDatabaseDefinition($value)) {
$list[] = $value;
}
}
return $databases;
return $list;
}
protected function _getPortableDatabaseDefinition($database)
......@@ -539,10 +677,13 @@ abstract class AbstractSchemaManager
protected function _getPortableFunctionsList($functions)
{
$list = array();
foreach ($functions as $key => $value) {
$functions[$key] = $this->_getPortableFunctionDefinition($value);
if ($value = $this->_getPortableFunctionDefinition($value)) {
$list[] = $value;
}
}
return $functions;
return $list;
}
protected function _getPortableFunctionDefinition($function)
......@@ -552,10 +693,13 @@ abstract class AbstractSchemaManager
protected function _getPortableTriggersList($triggers)
{
$list = array();
foreach ($triggers as $key => $value) {
$triggers[$key] = $this->_getPortableTriggerDefinition($value);
if ($value = $this->_getPortableTriggerDefinition($value)) {
$list[] = $value;
}
}
return $triggers;
return $list;
}
protected function _getPortableTriggerDefinition($trigger)
......@@ -565,10 +709,13 @@ abstract class AbstractSchemaManager
protected function _getPortableSequencesList($sequences)
{
$list = array();
foreach ($sequences as $key => $value) {
$sequences[$key] = $this->_getPortableSequenceDefinition($value);
if ($value = $this->_getPortableSequenceDefinition($value)) {
$list[] = $value;
}
}
return $sequences;
return $list;
}
protected function _getPortableSequenceDefinition($sequence)
......@@ -578,10 +725,13 @@ abstract class AbstractSchemaManager
protected function _getPortableTableConstraintsList($tableConstraints)
{
$list = array();
foreach ($tableConstraints as $key => $value) {
$tableConstraints[$key] = $this->_getPortableTableConstraintDefinition($value);
if ($value = $this->_getPortableTableConstraintDefinition($value)) {
$list[] = $value;
}
}
return $tableConstraints;
return $list;
}
protected function _getPortableTableConstraintDefinition($tableConstraint)
......@@ -591,10 +741,13 @@ abstract class AbstractSchemaManager
protected function _getPortableTableColumnList($tableColumns)
{
$list = array();
foreach ($tableColumns as $key => $value) {
$tableColumns[$key] = $this->_getPortableTableColumnDefinition($value);
if ($value = $this->_getPortableTableColumnDefinition($value)) {
$list[] = $value;
}
}
return $tableColumns;
return $list;
}
protected function _getPortableTableColumnDefinition($tableColumn)
......@@ -604,10 +757,13 @@ abstract class AbstractSchemaManager
protected function _getPortableTableIndexesList($tableIndexes)
{
$list = array();
foreach ($tableIndexes as $key => $value) {
$tableIndexes[$key] = $this->_getPortableTableIndexDefinition($value);
if ($value = $this->_getPortableTableIndexDefinition($value)) {
$list[] = $value;
}
}
return $tableIndexes;
return $list;
}
protected function _getPortableTableIndexDefinition($tableIndex)
......@@ -617,10 +773,13 @@ abstract class AbstractSchemaManager
protected function _getPortableTablesList($tables)
{
$list = array();
foreach ($tables as $key => $value) {
$tables[$key] = $this->_getPortableTableDefinition($value);
if ($value = $this->_getPortableTableDefinition($value)) {
$list[] = $value;
}
}
return $tables;
return $list;
}
protected function _getPortableTableDefinition($table)
......@@ -630,10 +789,13 @@ abstract class AbstractSchemaManager
protected function _getPortableUsersList($users)
{
$list = array();
foreach ($users as $key => $value) {
$users[$key] = $this->_getPortableUserDefinition($value);
if ($value = $this->_getPortableUserDefinition($value)) {
$list[] = $value;
}
}
return $users;
return $list;
}
protected function _getPortableUserDefinition($user)
......@@ -643,10 +805,13 @@ abstract class AbstractSchemaManager
protected function _getPortableViewsList($views)
{
$list = array();
foreach ($views as $key => $value) {
$views[$key] = $this->_getPortableViewDefinition($value);
if ($value = $this->_getPortableViewDefinition($value)) {
$list[] = $value;
}
}
return $views;
return $list;
}
protected function _getPortableViewDefinition($view)
......
......@@ -32,6 +32,66 @@ namespace Doctrine\DBAL\Schema;
*/
class MySqlSchemaManager extends AbstractSchemaManager
{
protected function _getPortableViewDefinition($view)
{
return array(
'name' => $view['table_name'],
'sql' => $view['view_definition']
);
}
protected function _getPortableTableDefinition($table)
{
return end($table);
}
protected function _getPortableUserDefinition($user)
{
return array(
'user' => $user['user'],
'password' => $user['password'],
);
}
protected function _getPortableTableIndexDefinition($tableIndex)
{
$tableIndex = array_change_key_case($tableIndex, CASE_LOWER);
$result = array();
if ($tableIndex['key_name'] != 'PRIMARY' && ($index = $tableIndex['key_name'])) {
$result['name'] = $index;
$result['column'] = $tableIndex['column_name'];
$result['unique'] = $tableIndex['non_unique'] ? false : true;
}
return $result;
}
protected function _getPortableTableConstraintDefinition($tableConstraint)
{
$tableConstraint = array_change_key_case($tableConstraint, CASE_LOWER);
$result = array();
if ( ! $tableConstraint['non_unique']) {
$index = $tableConstraint['key_name'];
if ( ! empty($index)) {
$result[] = $index;
}
}
return $result;
}
protected function _getPortableSequenceDefinition($sequence)
{
return end($sequence);
}
protected function _getPortableDatabaseDefinition($database)
{
return $database['database'];
}
protected function _getPortableTableColumnDefinition($tableColumn)
{
$dbType = strtolower($tableColumn['type']);
......@@ -231,65 +291,6 @@ class MySqlSchemaManager extends AbstractSchemaManager
return $column;
}
/**
* lists all database sequences
*
* @param string|null $database
* @return array
* @override
*/
public function listSequences($database = null)
{
$query = 'SHOW TABLES';
if ( ! is_null($database)) {
$query .= ' FROM ' . $database;
}
$tableNames = $this->_conn->fetchColumn($query);
return array_map(array($this->_conn->formatter, 'fixSequenceName'), $tableNames);
}
/**
* lists table constraints
*
* @param string $table database table name
* @return array
* @override
*/
public function listTableConstraints($table)
{
$keyName = 'Key_name';
$nonUnique = 'Non_unique';
if ($this->_conn->getAttribute(Doctrine::ATTR_PORTABILITY) & Doctrine::PORTABILITY_FIX_CASE) {
if ($this->_conn->getAttribute(Doctrine::ATTR_FIELD_CASE) == CASE_LOWER) {
$keyName = strtolower($keyName);
$nonUnique = strtolower($nonUnique);
} else {
$keyName = strtoupper($keyName);
$nonUnique = strtoupper($nonUnique);
}
}
$table = $this->_conn->quoteIdentifier($table, true);
$query = 'SHOW INDEX FROM ' . $table;
$indexes = $this->_conn->fetchAssoc($query);
$result = array();
foreach ($indexes as $indexData) {
if ( ! $indexData[$nonUnique]) {
if ($indexData[$keyName] !== 'PRIMARY') {
$index = $this->_conn->formatter->fixIndexName($indexData[$keyName]);
} else {
$index = 'PRIMARY';
}
if ( ! empty($index)) {
$result[] = $index;
}
}
}
return $result;
}
/**
* lists table foreign keys
*
......@@ -317,70 +318,6 @@ class MySqlSchemaManager extends AbstractSchemaManager
return $result;
}
/**
* lists table constraints
*
* @param string $table database table name
* @return array
* @override
*/
public function listTableIndexes($table)
{
$keyName = 'Key_name';
$nonUnique = 'Non_unique';
if ($this->_conn->getAttribute(Doctrine::ATTR_PORTABILITY) & Doctrine::PORTABILITY_FIX_CASE) {
if ($this->_conn->getAttribute(Doctrine::ATTR_FIELD_CASE) == CASE_LOWER) {
$keyName = strtolower($keyName);
$nonUnique = strtolower($nonUnique);
} else {
$keyName = strtoupper($keyName);
$nonUnique = strtoupper($nonUnique);
}
}
$table = $this->_conn->quoteIdentifier($table, true);
$query = 'SHOW INDEX FROM ' . $table;
$indexes = $this->_conn->fetchAssoc($query);
$result = array();
foreach ($indexes as $indexData) {
if ($indexData[$nonUnique] && ($index = $this->_conn->formatter->fixIndexName($indexData[$keyName]))) {
$result[] = $index;
}
}
return $result;
}
/**
* lists tables
*
* @param string|null $database
* @return array
* @override
*/
public function listTables($database = null)
{
return $this->_conn->fetchColumn($this->_conn->getDatabasePlatform()
->getListTablesSql());
}
/**
* lists database views
*
* @param string|null $database
* @return array
* @override
*/
public function listViews($database = null)
{
if ( ! is_null($database)) {
$query = sprintf($this->sql['listViews'], ' FROM ' . $database);
}
return $this->_conn->fetchColumn($query);
}
/**
* create sequence
*
......
......@@ -33,6 +33,37 @@ namespace Doctrine\DBAL\Schema;
*/
class SqliteSchemaManager extends AbstractSchemaManager
{
public function dropDatabase($database = null)
{
if (is_null($database)) {
$database = $this->_conn->getDriver()->getDatabase($this->_conn);
}
unlink($database);
}
public function createDatabase($database = null)
{
if (is_null($database)) {
$database = $this->_conn->getDriver()->getDatabase($this->_conn);
}
// TODO: Can we do this better?
$this->_conn->close();
$this->_conn->connect();
}
protected function _getPortableTableDefinition($table)
{
return $table['name'];
}
protected function _getPortableTableIndexDefinition($tableIndex)
{
return array(
'name' => $tableIndex['name'],
'unique' => (bool) $tableIndex['unique']
);
}
protected function _getPortableTableColumnDefinition($tableColumn)
{
$e = explode('(', $tableColumn['type']);
......
......@@ -21,8 +21,8 @@ class AllTests
{
$suite = new \Doctrine\Tests\DbalFunctionalTestSuite('Doctrine Dbal Functional');
$suite->addTestSuite('Doctrine\Tests\DBAL\Functional\Schema\SqliteSchemaTest');
$suite->addTestSuite('Doctrine\Tests\DBAL\Functional\Schema\MySqlSchemaTest');
$suite->addTestSuite('Doctrine\Tests\DBAL\Functional\Schema\SqliteSchemaManagerTest');
$suite->addTestSuite('Doctrine\Tests\DBAL\Functional\Schema\MySqlSchemaManagerTest');
return $suite;
}
......
<?php
namespace Doctrine\Tests\DBAL\Functional\Schema;
use Doctrine\Tests\TestUtil;
use Doctrine\DBAL\Schema;
require_once __DIR__ . '/../../../TestInit.php';
class MysqlSchemaManagerTest extends \Doctrine\Tests\DbalFunctionalTestCase
{
private $_conn;
protected function setUp()
{
$this->_conn = TestUtil::getConnection();
if ($this->_conn->getDatabasePlatform()->getName() !== 'mysql')
{
$this->markTestSkipped('The MySqlSchemaTest requires the use of mysql');
}
$this->_sm = new Schema\MySqlSchemaManager($this->_conn);
}
public function testListDatabases()
{
try {
$this->_sm->dropDatabase('test_mysql_create_database');
} catch (\Exception $e) {}
$this->_sm->createDatabase('test_mysql_create_database');
$databases = $this->_sm->listDatabases();
$this->assertEquals(in_array('test_mysql_create_database', $databases), true);
}
public function testListFunctions()
{
try {
$this->_sm->listFunctions();
} catch (\Exception $e) {
return;
}
$this->fail('Sqlite listFunctions() should throw an exception because it is not supported');
}
public function testListTriggers()
{
try {
$this->_sm->listTriggers();
} catch (\Exception $e) {
return;
}
$this->fail('Sqlite listTriggers() should throw an exception because it is not supported');
}
public function testListSequences()
{
$columns = array(
'id' => array(
'type' => new \Doctrine\DBAL\Types\IntegerType,
'autoincrement' => true,
'primary' => true,
'notnull' => true
),
'test' => array(
'type' => new \Doctrine\DBAL\Types\StringType,
'length' => 255
)
);
$options = array();
try {
$this->_sm->dropTable('list_sequences_test');
} catch (\Exception $e) {}
$this->_sm->createTable('list_sequences_test', $columns, $options);
$sequences = $this->_sm->listSequences();
$this->assertEquals(in_array('list_sequences_test', $sequences), true);
}
public function testListTableConstraints()
{
$columns = array(
'id' => array(
'type' => new \Doctrine\DBAL\Types\IntegerType,
'autoincrement' => true,
'primary' => true,
'notnull' => true
),
'test' => array(
'type' => new \Doctrine\DBAL\Types\StringType,
'length' => 255
)
);
$options = array();
try {
$this->_sm->dropTable('list_table_constraints_test');
} catch (\Exception $e) {}
$this->_sm->createTable('list_table_constraints_test', $columns, $options);
$tableConstraints = $this->_sm->listTableConstraints('list_table_constraints_test');
$this->assertEquals($tableConstraints, array(array('PRIMARY')));
}
public function testListTableColumns()
{
$columns = array(
'id' => array(
'type' => new \Doctrine\DBAL\Types\IntegerType,
'autoincrement' => true,
'primary' => true,
'notnull' => true
),
'test' => array(
'type' => new \Doctrine\DBAL\Types\StringType,
'length' => 255
)
);
$options = array();
try {
$this->_sm->dropTable('list_tables_test');
} catch (\Exception $e) {}
$this->_sm->createTable('list_tables_test', $columns, $options);
$columns = $this->_sm->listTableColumns('list_tables_test');
$this->assertEquals($columns[0]['name'], 'id');
$this->assertEquals($columns[0]['primary'], true);
$this->assertEquals(get_class($columns[0]['type']), 'Doctrine\DBAL\Types\IntegerType');
$this->assertEquals($columns[0]['length'], 4);
$this->assertEquals($columns[0]['unsigned'], false);
$this->assertEquals($columns[0]['fixed'], false);
$this->assertEquals($columns[0]['notnull'], true);
$this->assertEquals($columns[0]['default'], null);
$this->assertEquals($columns[1]['name'], 'test');
$this->assertEquals($columns[1]['primary'], false);
$this->assertEquals(get_class($columns[1]['type']), 'Doctrine\DBAL\Types\StringType');
$this->assertEquals($columns[1]['length'], 255);
$this->assertEquals($columns[1]['unsigned'], false);
$this->assertEquals($columns[1]['fixed'], false);
$this->assertEquals($columns[1]['notnull'], false);
$this->assertEquals($columns[1]['default'], null);
}
public function testListTableIndexes()
{
$columns = array(
'id' => array(
'type' => new \Doctrine\DBAL\Types\IntegerType,
'autoincrement' => true,
'primary' => true,
'notnull' => true
),
'test' => array(
'type' => new \Doctrine\DBAL\Types\StringType,
'length' => 255
)
);
$options = array(
'indexes' => array(
'test_index_name' => array(
'fields' => array(
'test' => array()
),
'type' => 'unique'
)
)
);
try {
$this->_sm->dropTable('list_table_indexes_test');
} catch (\Exception $e) {}
$this->_sm->createTable('list_table_indexes_test', $columns, $options);
$tableIndexes = $this->_sm->listTableIndexes('list_table_indexes_test');
$this->assertEquals($tableIndexes[0]['name'], 'test_index_name');
$this->assertEquals($tableIndexes[0]['column'], 'test');
$this->assertEquals($tableIndexes[0]['unique'], true);
}
public function testListTable()
{
$columns = array(
'id' => array(
'type' => new \Doctrine\DBAL\Types\IntegerType,
'autoincrement' => true,
'primary' => true,
'notnull' => true
),
'test' => array(
'type' => new \Doctrine\DBAL\Types\StringType,
'length' => 255
)
);
$options = array();
try {
$this->_sm->dropTable('list_tables_test');
} catch (\Exception $e) {}
$this->_sm->createTable('list_tables_test', $columns, $options);
$tables = $this->_sm->listTables();
$this->assertEquals(in_array('list_tables_test', $tables), true);
}
public function testListUsers()
{
$users = $this->_sm->listUsers();
$this->assertEquals(is_array($users), true);
$params = $this->_conn->getParams();
$testUser = $params['user'];
$found = false;
foreach ($users as $user) {
if ($user['user'] == $testUser) {
$found = true;
}
}
$this->assertEquals($found, true);
}
public function testListViews()
{
try {
$this->_sm->dropView('test_create_view');
} catch (\Exception $e) {}
$this->_sm->createView('test_create_view', 'SELECT * from mysql.user');
$views = $this->_sm->listViews();
$this->assertEquals($views[0]['name'], 'test_create_view');
$this->assertEquals($views[0]['sql'], '/* ALGORITHM=UNDEFINED */ select `mysql`.`user`.`Host` AS `Host`,`mysql`.`user`.`User` AS `User`,`mysql`.`user`.`Password` AS `Password`,`mysql`.`user`.`Select_priv` AS `Select_priv`,`mysql`.`user`.`Insert_priv` AS `Insert_priv`,`mysql`.`user`.`Update_priv` AS `Update_priv`,`mysql`.`user`.`Delete_priv` AS `Delete_priv`,`mysql`.`user`.`Create_priv` AS `Create_priv`,`mysql`.`user`.`Drop_priv` AS `Drop_priv`,`mysql`.`user`.`Reload_priv` AS `Reload_priv`,`mysql`.`user`.`Shutdown_priv` AS `Shutdown_priv`,`mysql`.`user`.`Process_priv` AS `Process_priv`,`mysql`.`user`.`File_priv` AS `File_priv`,`mysql`.`user`.`Grant_priv` AS `Grant_priv`,`mysql`.`user`.`References_priv` AS `References_priv`,`mysql`.`user`.`Index_priv` AS `Index_priv`,`mysql`.`user`.`Alter_priv` AS `Alter_priv`,`mysql`.`user`.`Show_db_priv` AS `Show_db_priv`,`mysql`.`user`.`Super_priv` AS `Super_priv`,`mysql`.`user`.`Create_tmp_table_priv` AS `Create_tmp_table_priv`,`mysql`.`user`.`Lock_tables_priv` AS `Lock_tables_priv`,`mysql`.`user`.`Execute_priv` AS `Execute_priv`,`mysql`.`user`.`Repl_slave_priv` AS `Repl_slave_priv`,`mysql`.`user`.`Repl_client_priv` AS `Repl_client_priv`,`mysql`.`user`.`Create_view_priv` AS `Create_view_priv`,`mysql`.`user`.`Show_view_priv` AS `Show_view_priv`,`mysql`.`user`.`Create_routine_priv` AS `Create_routine_priv`,`mysql`.`user`.`Alter_routine_priv` AS `Alter_routine_priv`,`mysql`.`user`.`Create_user_priv` AS `Create_user_priv`,`mysql`.`user`.`ssl_type` AS `ssl_type`,`mysql`.`user`.`ssl_cipher` AS `ssl_cipher`,`mysql`.`user`.`x509_issuer` AS `x509_issuer`,`mysql`.`user`.`x509_subject` AS `x509_subject`,`mysql`.`user`.`max_questions` AS `max_questions`,`mysql`.`user`.`max_updates` AS `max_updates`,`mysql`.`user`.`max_connections` AS `max_connections`,`mysql`.`user`.`max_user_connections` AS `max_user_connections` from `mysql`.`user`');
}
}
\ No newline at end of file
<?php
namespace Doctrine\Tests\DBAL\Functional\Schema;
use Doctrine\Tests\TestUtil;
use Doctrine\DBAL\Schema;
require_once __DIR__ . '/../../../TestInit.php';
class MysqlSchemaTest extends \Doctrine\Tests\DbalFunctionalTestCase
{
private $_conn;
protected function setUp()
{
$this->_conn = TestUtil::getConnection();
if ($this->_conn->getDatabasePlatform()->getName() !== 'mysql')
{
$this->markTestSkipped('The MySqlSchemaTest requires the use of mysql');
}
$this->_sm = new Schema\MySqlSchemaManager($this->_conn);
}
public function testListTableColumns()
{
$columns = array(
'id' => array(
'type' => new \Doctrine\DBAL\Types\IntegerType,
'autoincrement' => true,
'primary' => true,
'notnull' => true
),
'test' => array(
'type' => new \Doctrine\DBAL\Types\StringType,
'length' => 255
)
);
$options = array();
try {
$this->_sm->dropTable('list_tables_test');
} catch (\Exception $e) {}
$this->_sm->createTable('list_tables_test', $columns, $options);
$columns = $this->_sm->listTableColumns('list_tables_test');
$this->assertEquals($columns[0]['name'], 'id');
$this->assertEquals($columns[0]['primary'], true);
$this->assertEquals(get_class($columns[0]['type']), 'Doctrine\DBAL\Types\IntegerType');
$this->assertEquals($columns[0]['length'], 4);
$this->assertEquals($columns[0]['unsigned'], false);
$this->assertEquals($columns[0]['fixed'], false);
$this->assertEquals($columns[0]['notnull'], true);
$this->assertEquals($columns[0]['default'], null);
$this->assertEquals($columns[1]['name'], 'test');
$this->assertEquals($columns[1]['primary'], false);
$this->assertEquals(get_class($columns[1]['type']), 'Doctrine\DBAL\Types\StringType');
$this->assertEquals($columns[1]['length'], 255);
$this->assertEquals($columns[1]['unsigned'], false);
$this->assertEquals($columns[1]['fixed'], false);
$this->assertEquals($columns[1]['notnull'], false);
$this->assertEquals($columns[1]['default'], null);
}
}
\ No newline at end of file
<?php
namespace Doctrine\Tests\DBAL\Functional\Schema;
use Doctrine\Tests\TestUtil;
use Doctrine\DBAL\Schema;
require_once __DIR__ . '/../../../TestInit.php';
class SqliteSchemaManagerTest extends \Doctrine\Tests\DbalFunctionalTestCase
{
private $_conn;
protected function setUp()
{
$this->_conn = TestUtil::getConnection();
if ($this->_conn->getDatabasePlatform()->getName() !== 'sqlite')
{
$this->markTestSkipped('The SqliteSchemaTest requires the use of sqlite');
}
$this->_sm = new Schema\SqliteSchemaManager($this->_conn);
}
public function testListDatabases()
{
try {
$this->_sm->listDatabases();
} catch (\Exception $e) {
return;
}
$this->fail('Sqlite listDatabases() should throw an exception because it is not supported');
}
public function testListFunctions()
{
try {
$this->_sm->listFunctions();
} catch (\Exception $e) {
return;
}
$this->fail('Sqlite listFunctions() should throw an exception because it is not supported');
}
public function testListTriggers()
{
try {
$this->_sm->listTriggers();
} catch (\Exception $e) {
return;
}
$this->fail('Sqlite listTriggers() should throw an exception because it is not supported');
}
public function testListSequences()
{
$columns = array(
'id' => array(
'type' => new \Doctrine\DBAL\Types\IntegerType,
'autoincrement' => true,
'primary' => true,
'notnull' => true
),
'test' => array(
'type' => new \Doctrine\DBAL\Types\StringType,
'length' => 255
)
);
$options = array();
$this->_sm->createTable('list_sequences_test', $columns, $options);
$sequences = $this->_sm->listSequences();
$this->assertEquals($sequences[0]['name'], 'list_sequences_test');
$this->assertEquals($sequences[1]['name'], 'sqlite_sequence');
}
public function testListTableConstraints()
{
// TODO: Implement support for constraints/foreign keys to be specified
// when creating tables. Sqlite does not support adding them after
// the table has already been created
$tableConstraints = $this->_sm->listTableConstraints('list_table_constraints_test');
$this->assertEquals($tableConstraints, array());
}
public function testListTableColumns()
{
$columns = array(
'id' => array(
'type' => new \Doctrine\DBAL\Types\IntegerType,
'autoincrement' => true,
'primary' => true,
'notnull' => true
),
'test' => array(
'type' => new \Doctrine\DBAL\Types\StringType,
'length' => 255
)
);
$options = array();
$this->_sm->createTable('list_table_columns_test', $columns, $options);
$tableColumns = $this->_sm->listTableColumns('list_table_columns_test');
$this->assertEquals($tableColumns[0]['name'], 'id');
$this->assertEquals($tableColumns[0]['primary'], true);
$this->assertEquals(get_class($tableColumns[0]['type']), 'Doctrine\DBAL\Types\IntegerType');
$this->assertEquals($tableColumns[0]['length'], 4);
$this->assertEquals($tableColumns[0]['unsigned'], false);
$this->assertEquals($tableColumns[0]['fixed'], false);
$this->assertEquals($tableColumns[0]['notnull'], true);
$this->assertEquals($tableColumns[0]['default'], null);
$this->assertEquals($tableColumns[1]['name'], 'test');
$this->assertEquals($tableColumns[1]['primary'], false);
$this->assertEquals(get_class($tableColumns[1]['type']), 'Doctrine\DBAL\Types\StringType');
$this->assertEquals($tableColumns[1]['length'], 255);
$this->assertEquals($tableColumns[1]['unsigned'], false);
$this->assertEquals($tableColumns[1]['fixed'], false);
$this->assertEquals($tableColumns[1]['notnull'], false);
$this->assertEquals($tableColumns[1]['default'], null);
}
public function testListTableIndexes()
{
$columns = array(
'id' => array(
'type' => new \Doctrine\DBAL\Types\IntegerType,
'autoincrement' => true,
'primary' => true,
'notnull' => true
),
'test' => array(
'type' => new \Doctrine\DBAL\Types\StringType,
'length' => 255
)
);
$options = array(
'indexes' => array(
'test' => array(
'fields' => array(
'test' => array()
),
'type' => 'unique'
)
)
);
$this->_sm->createTable('list_table_indexes_test', $columns, $options);
$tableIndexes = $this->_sm->listTableIndexes('list_table_indexes_test');
$this->assertEquals($tableIndexes[0]['name'], 'test');
$this->assertEquals($tableIndexes[0]['unique'], true);
}
public function testListTable()
{
$columns = array(
'id' => array(
'type' => new \Doctrine\DBAL\Types\IntegerType,
'autoincrement' => true,
'primary' => true,
'notnull' => true
),
'test' => array(
'type' => new \Doctrine\DBAL\Types\StringType,
'length' => 255
)
);
$options = array();
$this->_sm->createTable('list_tables_test', $columns, $options);
$tables = $this->_sm->listTables();
$this->assertEquals(in_array('list_tables_test', $tables), true);
}
public function testListUsers()
{
try {
$this->_sm->listUsers();
} catch (\Exception $e) {
return;
}
$this->fail('Sqlite listUsers() should throw an exception because it is not supported');
}
public function testListViews()
{
$columns = array(
'id' => array(
'type' => new \Doctrine\DBAL\Types\IntegerType,
'autoincrement' => true,
'primary' => true,
'notnull' => true
),
'test' => array(
'type' => new \Doctrine\DBAL\Types\StringType,
'length' => 255
)
);
$options = array();
$this->_sm->createTable('test_views', $columns, $options);
try {
$this->_sm->dropView('test_create_view');
} catch (\Exception $e) {}
$this->_sm->createView('test_create_view', 'SELECT * from test_views');
$views = $this->_sm->listViews();
$this->assertEquals($views[0]['name'], 'test_create_view');
$this->assertEquals($views[0]['sql'], 'CREATE VIEW test_create_view AS SELECT * from test_views');
}
public function testCreateAndDropDatabase()
{
$path = dirname(__FILE__).'/test_create_and_drop_sqlite_database.sqlite';
$config = new \Doctrine\ORM\Configuration();
$eventManager = new \Doctrine\Common\EventManager();
$connectionOptions = array(
'driver' => 'pdo_sqlite',
'path' => $path
);
$em = \Doctrine\ORM\EntityManager::create($connectionOptions, $config, $eventManager);
$conn = $em->getConnection();
$sm = $conn->getSchemaManager();
$sm->createDatabase();
$this->assertEquals(file_exists($path), true);
$sm->dropDatabase();
$this->assertEquals(file_exists($path), false);
$sm->createDatabase();
$this->assertEquals(file_exists($path), true);
$sm->dropDatabase();
}
public function testCreateTable()
{
$columns = array(
'id' => array(
'type' => new \Doctrine\DBAL\Types\IntegerType,
'autoincrement' => true,
'primary' => true,
'notnull' => true
),
'test' => array(
'type' => new \Doctrine\DBAL\Types\StringType,
'length' => 255
)
);
$options = array();
$this->_sm->createTable('test_create_table', $columns, $options);
$tables = $this->_sm->listTables();
$this->assertEquals(in_array('test_create_table', $tables), true);
$tableColumns = $this->_sm->listTableColumns('test_create_table');
$this->assertEquals($tableColumns[0]['name'], 'id');
$this->assertEquals($tableColumns[0]['primary'], true);
$this->assertEquals(get_class($tableColumns[0]['type']), 'Doctrine\DBAL\Types\IntegerType');
$this->assertEquals($tableColumns[0]['length'], 4);
$this->assertEquals($tableColumns[0]['unsigned'], false);
$this->assertEquals($tableColumns[0]['fixed'], false);
$this->assertEquals($tableColumns[0]['notnull'], true);
$this->assertEquals($tableColumns[0]['default'], null);
$this->assertEquals($tableColumns[1]['name'], 'test');
$this->assertEquals($tableColumns[1]['primary'], false);
$this->assertEquals(get_class($tableColumns[1]['type']), 'Doctrine\DBAL\Types\StringType');
$this->assertEquals($tableColumns[1]['length'], 255);
$this->assertEquals($tableColumns[1]['unsigned'], false);
$this->assertEquals($tableColumns[1]['fixed'], false);
$this->assertEquals($tableColumns[1]['notnull'], false);
$this->assertEquals($tableColumns[1]['default'], null);
}
public function testCreateSequence()
{
try {
$this->_sm->createSequence();
} catch (\Exception $e) {
return;
}
$this->fail('Sqlite createSequence() should throw an exception because it is not supported');
}
public function testCreateConstraint()
{
try {
$this->_sm->createConstraint();
} catch (\Exception $e) {
return;
}
$this->fail('Sqlite createConstraint() should throw an exception because it is not supported');
}
public function testCreateIndex()
{
$columns = array(
'id' => array(
'type' => new \Doctrine\DBAL\Types\IntegerType,
'autoincrement' => true,
'primary' => true,
'notnull' => true
),
'test' => array(
'type' => new \Doctrine\DBAL\Types\StringType,
'length' => 255
)
);
$options = array();
$this->_sm->createTable('test_create_index', $columns, $options);
$index = array(
'fields' => array(
'test' => array()
),
'type' => 'unique'
);
$this->_sm->createIndex('test_create_index', 'test', $index);
$tableIndexes = $this->_sm->listTableIndexes('test_create_index');
$this->assertEquals($tableIndexes[0]['name'], 'test');
$this->assertEquals($tableIndexes[0]['unique'], true);
}
public function testCreateForeignKey()
{
try {
$this->_sm->createForeignKey();
} catch (\Exception $e) {
return;
}
$this->fail('Sqlite createForeignKey() should throw an exception because it is not supported');
}
public function testRenameTable()
{
try {
$this->_sm->renameTable();
} catch (\Exception $e) {
return;
}
$this->fail('Sqlite renameTable() should throw an exception because it is not supported');
}
public function testAddTableColumn()
{
try {
$this->_sm->addTableColumn();
} catch (\Exception $e) {
return;
}
$this->fail('Sqlite addTableColumn() should throw an exception because it is not supported');
}
public function testRemoveTableColumn()
{
try {
$this->_sm->removeTableColumn();
} catch (\Exception $e) {
return;
}
$this->fail('Sqlite removeTableColumn() should throw an exception because it is not supported');
}
public function testChangeTableColumn()
{
try {
$this->_sm->changeTableColumn();
} catch (\Exception $e) {
return;
}
$this->fail('Sqlite changeTableColumn() should throw an exception because it is not supported');
}
public function testRenameTableColumn()
{
try {
$this->_sm->renameTableColumn();
} catch (\Exception $e) {
return;
}
$this->fail('Sqlite renameTableColumn() should throw an exception because it is not supported');
}
}
\ No newline at end of file
<?php
namespace Doctrine\Tests\DBAL\Functional\Schema;
use Doctrine\Tests\TestUtil;
use Doctrine\DBAL\Schema;
require_once __DIR__ . '/../../../TestInit.php';
class SqliteSchemaTest extends \Doctrine\Tests\DbalFunctionalTestCase
{
private $_conn;
protected function setUp()
{
$this->_conn = TestUtil::getConnection();
if ($this->_conn->getDatabasePlatform()->getName() !== 'sqlite')
{
$this->markTestSkipped('The SqliteSchemaTest requires the use of sqlite');
}
$this->_sm = new Schema\SqliteSchemaManager($this->_conn);
}
public function testListTableColumns()
{
$columns = array(
'id' => array(
'type' => new \Doctrine\DBAL\Types\IntegerType,
'autoincrement' => true,
'primary' => true,
'notnull' => true
),
'test' => array(
'type' => new \Doctrine\DBAL\Types\StringType,
'length' => 255
)
);
$options = array();
$this->_sm->createTable('list_tables_test', $columns, $options);
$columns = $this->_sm->listTableColumns('list_tables_test');
$this->assertEquals($columns[0]['name'], 'id');
$this->assertEquals($columns[0]['primary'], true);
$this->assertEquals(get_class($columns[0]['type']), 'Doctrine\DBAL\Types\IntegerType');
$this->assertEquals($columns[0]['length'], 4);
$this->assertEquals($columns[0]['unsigned'], false);
$this->assertEquals($columns[0]['fixed'], false);
$this->assertEquals($columns[0]['notnull'], true);
$this->assertEquals($columns[0]['default'], null);
$this->assertEquals($columns[1]['name'], 'test');
$this->assertEquals($columns[1]['primary'], false);
$this->assertEquals(get_class($columns[1]['type']), 'Doctrine\DBAL\Types\StringType');
$this->assertEquals($columns[1]['length'], 255);
$this->assertEquals($columns[1]['unsigned'], false);
$this->assertEquals($columns[1]['fixed'], false);
$this->assertEquals($columns[1]['notnull'], false);
$this->assertEquals($columns[1]['default'], null);
}
}
\ No newline at end of file
......@@ -53,4 +53,9 @@ class DriverMock implements \Doctrine\DBAL\Driver
{
return 'mock';
}
public function getDatabase(\Doctrine\DBAL\Connection $conn)
{
return;
}
}
\ No newline at end of file
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