2008-08-09 13:45:28 +04:00
|
|
|
<?php
|
|
|
|
/*
|
|
|
|
* $Id$
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
* This software consists of voluntary contributions made by many individuals
|
|
|
|
* and is licensed under the LGPL. For more information, see
|
2009-02-05 20:34:44 +03:00
|
|
|
* <http://www.doctrine-project.org>.
|
2008-08-09 13:45:28 +04:00
|
|
|
*/
|
|
|
|
|
2009-01-22 22:38:10 +03:00
|
|
|
namespace Doctrine\DBAL\Schema;
|
2008-08-09 13:45:28 +04:00
|
|
|
|
|
|
|
/**
|
2009-05-27 22:54:40 +04:00
|
|
|
* SqliteSchemaManager
|
2008-08-09 13:45:28 +04:00
|
|
|
*
|
|
|
|
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
|
|
|
|
* @author Konsta Vesterinen <kvesteri@cc.hut.fi>
|
|
|
|
* @author Lukas Smith <smith@pooteeweet.org> (PEAR MDB2 library)
|
2009-05-27 22:54:40 +04:00
|
|
|
* @author Jonathan H. Wage <jonwage@gmail.com>
|
2008-08-09 13:45:28 +04:00
|
|
|
* @version $Revision$
|
|
|
|
* @since 2.0
|
|
|
|
*/
|
2009-01-22 22:38:10 +03:00
|
|
|
class SqliteSchemaManager extends AbstractSchemaManager
|
2009-05-27 22:54:40 +04:00
|
|
|
{
|
2009-05-30 22:38:00 +04:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*
|
|
|
|
* @override
|
|
|
|
*/
|
|
|
|
public function dropDatabase($database)
|
2009-05-28 06:04:51 +04:00
|
|
|
{
|
2009-05-30 06:27:50 +04:00
|
|
|
if (file_exists($database)) {
|
|
|
|
unlink($database);
|
2009-05-28 06:04:51 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-05-30 22:38:00 +04:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*
|
|
|
|
* @override
|
|
|
|
*/
|
|
|
|
public function createDatabase($database)
|
2009-05-28 06:04:51 +04:00
|
|
|
{
|
2009-06-20 21:02:02 +04:00
|
|
|
$params = $this->_conn->getParams();
|
|
|
|
$driver = $params['driver'];
|
|
|
|
$options = array(
|
|
|
|
'driver' => $driver,
|
|
|
|
'path' => $database
|
|
|
|
);
|
|
|
|
$conn = \Doctrine\DBAL\DriverManager::getConnection($options);
|
|
|
|
$conn->connect();
|
|
|
|
$conn->close();
|
2009-05-28 06:04:51 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
protected function _getPortableTableDefinition($table)
|
|
|
|
{
|
|
|
|
return $table['name'];
|
|
|
|
}
|
|
|
|
|
2009-11-03 11:58:03 +03:00
|
|
|
/**
|
|
|
|
* @license New BSD License
|
|
|
|
* @link http://ezcomponents.org/docs/api/trunk/DatabaseSchema/ezcDbSchemaPgsqlReader.html
|
|
|
|
* @param array $tableIndexes
|
|
|
|
* @param string $tableName
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
protected function _getPortableTableIndexesList($tableIndexes, $tableName=null)
|
|
|
|
{
|
|
|
|
$indexBuffer = array();
|
|
|
|
|
|
|
|
// fetch primary
|
|
|
|
$stmt = $this->_conn->execute( "PRAGMA TABLE_INFO ('$tableName')" );
|
|
|
|
$indexArray = $stmt->fetchAll(\Doctrine\DBAL\Connection::FETCH_ASSOC);
|
|
|
|
foreach($indexArray AS $indexColumnRow) {
|
|
|
|
if($indexColumnRow['pk'] == "1") {
|
|
|
|
$indexBuffer[] = array(
|
|
|
|
'key_name' => 'primary',
|
|
|
|
'primary' => true,
|
|
|
|
'non_unique' => false,
|
|
|
|
'column_name' => $indexColumnRow['name']
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// fetch regular indexes
|
|
|
|
foreach($tableIndexes AS $tableIndex) {
|
|
|
|
$keyName = $tableIndex['name'];
|
|
|
|
$idx = array();
|
|
|
|
$idx['key_name'] = $keyName;
|
|
|
|
$idx['primary'] = false;
|
|
|
|
$idx['non_unique'] = $tableIndex['unique']?false:true;
|
|
|
|
|
|
|
|
$stmt = $this->_conn->execute( "PRAGMA INDEX_INFO ( '{$keyName}' )" );
|
|
|
|
$indexArray = $stmt->fetchAll(\Doctrine\DBAL\Connection::FETCH_ASSOC);
|
|
|
|
|
|
|
|
foreach ( $indexArray as $indexColumnRow ) {
|
|
|
|
$idx['column_name'] = $indexColumnRow['name'];
|
|
|
|
$indexBuffer[] = $idx;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return parent::_getPortableTableIndexesList($indexBuffer, $tableName);
|
|
|
|
}
|
|
|
|
|
2009-05-28 06:04:51 +04:00
|
|
|
protected function _getPortableTableIndexDefinition($tableIndex)
|
|
|
|
{
|
|
|
|
return array(
|
|
|
|
'name' => $tableIndex['name'],
|
|
|
|
'unique' => (bool) $tableIndex['unique']
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2009-05-28 02:14:27 +04:00
|
|
|
protected function _getPortableTableColumnDefinition($tableColumn)
|
|
|
|
{
|
|
|
|
$e = explode('(', $tableColumn['type']);
|
|
|
|
$tableColumn['type'] = $e[0];
|
|
|
|
if (isset($e[1])) {
|
|
|
|
$length = trim($e[1], ')');
|
|
|
|
$tableColumn['length'] = $length;
|
|
|
|
}
|
|
|
|
|
|
|
|
$dbType = strtolower($tableColumn['type']);
|
|
|
|
|
|
|
|
$length = isset($tableColumn['length']) ? $tableColumn['length'] : null;
|
|
|
|
$unsigned = (boolean) isset($tableColumn['unsigned']) ? $tableColumn['unsigned'] : false;
|
|
|
|
$fixed = false;
|
|
|
|
$type = null;
|
|
|
|
$default = $tableColumn['dflt_value'];
|
|
|
|
if ($default == 'NULL') {
|
|
|
|
$default = null;
|
|
|
|
}
|
|
|
|
$notnull = (bool) $tableColumn['notnull'];
|
|
|
|
|
|
|
|
if ( ! isset($tableColumn['name'])) {
|
|
|
|
$tableColumn['name'] = '';
|
|
|
|
}
|
|
|
|
|
2009-11-03 19:56:05 +03:00
|
|
|
$precision = null;
|
|
|
|
$scale = null;
|
|
|
|
|
2009-05-28 02:14:27 +04:00
|
|
|
switch ($dbType) {
|
|
|
|
case 'boolean':
|
|
|
|
$type = 'boolean';
|
|
|
|
break;
|
|
|
|
case 'tinyint':
|
2009-10-15 00:18:36 +04:00
|
|
|
$type = 'boolean';
|
|
|
|
$length = null;
|
2009-05-28 02:14:27 +04:00
|
|
|
break;
|
|
|
|
case 'smallint':
|
2009-10-15 00:18:36 +04:00
|
|
|
$type = 'smallint';
|
|
|
|
$length = null;
|
2009-05-28 02:14:27 +04:00
|
|
|
break;
|
|
|
|
case 'mediumint':
|
|
|
|
case 'int':
|
|
|
|
case 'integer':
|
|
|
|
case 'serial':
|
|
|
|
$type = 'integer';
|
2009-10-15 00:18:36 +04:00
|
|
|
$length = null;
|
2009-05-28 02:14:27 +04:00
|
|
|
break;
|
|
|
|
case 'bigint':
|
|
|
|
case 'bigserial':
|
2009-10-15 00:18:36 +04:00
|
|
|
$type = 'bigint';
|
|
|
|
$length = null;
|
2009-05-28 02:14:27 +04:00
|
|
|
break;
|
|
|
|
case 'clob':
|
2009-10-01 16:00:14 +04:00
|
|
|
$fixed = false;
|
|
|
|
$type = 'text';
|
|
|
|
break;
|
2009-05-28 02:14:27 +04:00
|
|
|
case 'tinytext':
|
|
|
|
case 'mediumtext':
|
|
|
|
case 'longtext':
|
|
|
|
case 'text':
|
2009-10-15 00:18:36 +04:00
|
|
|
$type = 'text';
|
|
|
|
break;
|
2009-05-28 02:14:27 +04:00
|
|
|
case 'varchar':
|
|
|
|
case 'varchar2':
|
|
|
|
case 'nvarchar':
|
|
|
|
case 'ntext':
|
|
|
|
case 'image':
|
|
|
|
case 'nchar':
|
|
|
|
$fixed = false;
|
|
|
|
case 'char':
|
|
|
|
$type = 'string';
|
|
|
|
if ($length == '1') {
|
|
|
|
$type = 'boolean';
|
|
|
|
if (preg_match('/^(is|has)/', $tableColumn['name'])) {
|
|
|
|
$type = array_reverse($type);
|
|
|
|
}
|
|
|
|
} elseif (strstr($dbType, 'text')) {
|
|
|
|
$type = 'clob';
|
|
|
|
}
|
|
|
|
if ($fixed !== false) {
|
|
|
|
$fixed = true;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'date':
|
|
|
|
$type = 'date';
|
|
|
|
$length = null;
|
|
|
|
break;
|
|
|
|
case 'datetime':
|
|
|
|
case 'timestamp':
|
2009-10-15 00:18:36 +04:00
|
|
|
$type = 'datetime';
|
2009-05-28 02:14:27 +04:00
|
|
|
$length = null;
|
|
|
|
break;
|
|
|
|
case 'time':
|
|
|
|
$type = 'time';
|
|
|
|
$length = null;
|
|
|
|
break;
|
|
|
|
case 'float':
|
|
|
|
case 'double':
|
|
|
|
case 'real':
|
|
|
|
case 'decimal':
|
|
|
|
case 'numeric':
|
2009-11-03 19:56:05 +03:00
|
|
|
list($precision, $scale) = array_map('trim', explode(', ', $tableColumn['length']));
|
2009-05-28 02:14:27 +04:00
|
|
|
$type = 'decimal';
|
|
|
|
$length = null;
|
|
|
|
break;
|
|
|
|
case 'tinyblob':
|
|
|
|
case 'mediumblob':
|
|
|
|
case 'longblob':
|
|
|
|
case 'blob':
|
|
|
|
$type = 'blob';
|
|
|
|
$length = null;
|
|
|
|
break;
|
|
|
|
case 'year':
|
|
|
|
$type = 'date';
|
|
|
|
$length = null;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
$type = 'string';
|
|
|
|
$length = null;
|
|
|
|
}
|
|
|
|
|
2009-11-03 19:56:05 +03:00
|
|
|
return array(
|
|
|
|
'name' => $tableColumn['name'],
|
|
|
|
'type' => $type,
|
|
|
|
'length' => $length,
|
|
|
|
'unsigned' => (bool) $unsigned,
|
|
|
|
'fixed' => $fixed,
|
|
|
|
'notnull' => $notnull,
|
|
|
|
'default' => $default,
|
|
|
|
'precision' => $precision,
|
|
|
|
'scale' => $scale,
|
|
|
|
'platformDetails' => array(
|
|
|
|
'primary' => (bool) $tableColumn['pk'],
|
|
|
|
),
|
|
|
|
);
|
2009-05-28 02:14:27 +04:00
|
|
|
}
|
2009-02-20 08:46:20 +03:00
|
|
|
}
|