2006-12-28 15:27:44 +03: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
|
|
|
|
* <http://www.phpdoctrine.com>.
|
|
|
|
*/
|
|
|
|
Doctrine::autoload('Doctrine_Import');
|
|
|
|
/**
|
|
|
|
* @package Doctrine
|
|
|
|
* @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)
|
|
|
|
* @version $Revision$
|
|
|
|
* @category Object Relational Mapping
|
|
|
|
* @link www.phpdoctrine.com
|
|
|
|
* @since 1.0
|
|
|
|
*/
|
2006-12-29 17:40:47 +03:00
|
|
|
class Doctrine_Import_Sqlite extends Doctrine_Import
|
|
|
|
{
|
2006-12-28 15:27:44 +03:00
|
|
|
/**
|
|
|
|
* lists all databases
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
2006-12-29 17:40:47 +03:00
|
|
|
public function listDatabases()
|
|
|
|
{
|
2006-12-28 15:27:44 +03:00
|
|
|
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* lists all availible database functions
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
2006-12-29 17:40:47 +03:00
|
|
|
public function listFunctions()
|
|
|
|
{
|
|
|
|
|
2006-12-28 15:27:44 +03:00
|
|
|
}
|
|
|
|
/**
|
|
|
|
* lists all database triggers
|
|
|
|
*
|
|
|
|
* @param string|null $database
|
|
|
|
* @return array
|
|
|
|
*/
|
2006-12-29 17:40:47 +03:00
|
|
|
public function listTriggers($database = null)
|
|
|
|
{
|
2006-12-28 15:27:44 +03:00
|
|
|
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* lists all database sequences
|
|
|
|
*
|
|
|
|
* @param string|null $database
|
|
|
|
* @return array
|
|
|
|
*/
|
2006-12-29 17:40:47 +03:00
|
|
|
public function listSequences($database = null)
|
|
|
|
{
|
2007-01-15 01:44:01 +03:00
|
|
|
$query = "SELECT name FROM sqlite_master WHERE type='table' AND sql NOT NULL ORDER BY name";
|
|
|
|
$tableNames = $this->conn->fetchColumn($query);
|
2007-01-06 01:06:52 +03:00
|
|
|
|
2006-12-29 14:03:19 +03:00
|
|
|
$result = array();
|
2007-01-15 01:44:01 +03:00
|
|
|
foreach ($tableNames as $tableName) {
|
|
|
|
if ($sqn = $this->conn->fixSequenceName($tableName, true)) {
|
2006-12-29 14:03:19 +03:00
|
|
|
$result[] = $sqn;
|
|
|
|
}
|
|
|
|
}
|
2007-01-15 01:44:01 +03:00
|
|
|
if ($this->conn->getAttribute(Doctrine::ATTR_PORTABILITY) & Doctrine::PORTABILITY_FIX_CASE) {
|
|
|
|
$result = array_map(($this->conn->getAttribute(Doctrine::ATTR_FIELD_CASE) == CASE_LOWER ? 'strtolower' : 'strtoupper'), $result);
|
2006-12-29 14:03:19 +03:00
|
|
|
}
|
|
|
|
return $result;
|
2006-12-28 15:27:44 +03:00
|
|
|
}
|
|
|
|
/**
|
|
|
|
* lists table constraints
|
|
|
|
*
|
|
|
|
* @param string $table database table name
|
|
|
|
* @return array
|
|
|
|
*/
|
2006-12-29 17:40:47 +03:00
|
|
|
public function listTableConstraints($table)
|
|
|
|
{
|
2007-01-06 01:06:52 +03:00
|
|
|
$table = $this->conn->quote($table, 'text');
|
2007-01-15 01:44:01 +03:00
|
|
|
|
2006-12-29 14:03:19 +03:00
|
|
|
$query = "SELECT sql FROM sqlite_master WHERE type='index' AND ";
|
2007-01-15 01:44:01 +03:00
|
|
|
|
|
|
|
if ($this->conn->getAttribute(Doctrine::ATTR_PORTABILITY) & Doctrine::PORTABILITY_FIX_CASE) {
|
|
|
|
$query .= 'LOWER(tbl_name) = ' . strtolower($table);
|
2006-12-29 14:03:19 +03:00
|
|
|
} else {
|
2007-01-15 01:44:01 +03:00
|
|
|
$query .= 'tbl_name = ' . $table;
|
2006-12-29 14:03:19 +03:00
|
|
|
}
|
2007-01-15 01:44:01 +03:00
|
|
|
$query .= ' AND sql NOT NULL ORDER BY name';
|
2007-01-06 01:06:52 +03:00
|
|
|
$indexes = $this->conn->fetchColumn($query);
|
2006-12-29 14:03:19 +03:00
|
|
|
|
|
|
|
$result = array();
|
|
|
|
foreach ($indexes as $sql) {
|
|
|
|
if (preg_match("/^create unique index ([^ ]+) on /i", $sql, $tmp)) {
|
2007-01-15 01:44:01 +03:00
|
|
|
$index = $this->conn->fixIndexName($tmp[1]);
|
|
|
|
if ( ! empty($index)) {
|
2006-12-29 14:03:19 +03:00
|
|
|
$result[$index] = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-01-15 01:44:01 +03:00
|
|
|
if ($this->conn->getAttribute(Doctrine::ATTR_PORTABILITY) & Doctrine::PORTABILITY_FIX_CASE) {
|
|
|
|
$result = array_change_key_case($result, $this->conn->getAttribute(Doctrine::ATTR_FIELD_CASE));
|
2006-12-29 14:03:19 +03:00
|
|
|
}
|
2006-12-29 17:40:47 +03:00
|
|
|
return array_keys($result);
|
2006-12-28 15:27:44 +03:00
|
|
|
}
|
|
|
|
/**
|
|
|
|
* lists table constraints
|
|
|
|
*
|
|
|
|
* @param string $table database table name
|
|
|
|
* @return array
|
|
|
|
*/
|
2006-12-29 17:40:47 +03:00
|
|
|
public function listTableColumns($table)
|
|
|
|
{
|
2006-12-28 15:27:44 +03:00
|
|
|
$sql = 'PRAGMA table_info(' . $table . ')';
|
2006-12-30 13:58:58 +03:00
|
|
|
$result = $this->conn->fetchAll($sql);
|
2006-12-28 15:27:44 +03:00
|
|
|
|
|
|
|
$description = array();
|
|
|
|
$columns = array();
|
2006-12-29 17:01:31 +03:00
|
|
|
foreach ($result as $key => $val) {
|
2006-12-28 15:27:44 +03:00
|
|
|
$description = array(
|
2007-02-17 21:35:24 +03:00
|
|
|
'name' => $val['name'],
|
|
|
|
'type' => $val['type'],
|
|
|
|
'notnull' => (bool) $val['notnull'],
|
|
|
|
'default' => $val['dflt_value'],
|
|
|
|
'primary' => (bool) $val['pk'],
|
|
|
|
'length' => null,
|
|
|
|
'scale' => null,
|
|
|
|
'precision' => null,
|
|
|
|
'unsigned' => null,
|
2006-12-28 15:27:44 +03:00
|
|
|
);
|
2006-12-30 13:58:58 +03:00
|
|
|
$columns[$val['name']] = $description;
|
2006-12-28 15:27:44 +03:00
|
|
|
}
|
|
|
|
return $columns;
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* lists table constraints
|
|
|
|
*
|
|
|
|
* @param string $table database table name
|
|
|
|
* @return array
|
|
|
|
*/
|
2006-12-29 17:40:47 +03:00
|
|
|
public function listTableIndexes($table)
|
|
|
|
{
|
2007-02-10 14:02:29 +03:00
|
|
|
$sql = 'PRAGMA index_list(' . $table . ')';
|
2007-01-15 01:44:01 +03:00
|
|
|
return $this->conn->fetchColumn($sql);
|
|
|
|
}
|
2006-12-28 15:27:44 +03:00
|
|
|
/**
|
|
|
|
* lists tables
|
|
|
|
*
|
|
|
|
* @param string|null $database
|
|
|
|
* @return array
|
|
|
|
*/
|
2006-12-29 17:40:47 +03:00
|
|
|
public function listTables($database = null)
|
|
|
|
{
|
2006-12-28 15:27:44 +03:00
|
|
|
$sql = "SELECT name FROM sqlite_master WHERE type = 'table' "
|
|
|
|
. "UNION ALL SELECT name FROM sqlite_temp_master "
|
|
|
|
. "WHERE type = 'table' ORDER BY name";
|
|
|
|
|
2007-01-15 01:44:01 +03:00
|
|
|
return $this->conn->fetchColumn($sql);
|
2006-12-28 15:27:44 +03:00
|
|
|
}
|
|
|
|
/**
|
|
|
|
* lists table triggers
|
|
|
|
*
|
|
|
|
* @param string $table database table name
|
|
|
|
* @return array
|
|
|
|
*/
|
2006-12-29 17:40:47 +03:00
|
|
|
public function listTableTriggers($table)
|
|
|
|
{
|
|
|
|
|
2006-12-28 15:27:44 +03:00
|
|
|
}
|
|
|
|
/**
|
|
|
|
* lists table views
|
|
|
|
*
|
|
|
|
* @param string $table database table name
|
|
|
|
* @return array
|
|
|
|
*/
|
2006-12-29 17:40:47 +03:00
|
|
|
public function listTableViews($table)
|
|
|
|
{
|
|
|
|
|
2006-12-28 15:27:44 +03:00
|
|
|
}
|
|
|
|
/**
|
|
|
|
* lists database users
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
2006-12-29 17:40:47 +03:00
|
|
|
public function listUsers()
|
|
|
|
{
|
|
|
|
|
2006-12-28 15:27:44 +03:00
|
|
|
}
|
|
|
|
/**
|
|
|
|
* lists database views
|
|
|
|
*
|
|
|
|
* @param string|null $database
|
|
|
|
* @return array
|
|
|
|
*/
|
2006-12-29 17:40:47 +03:00
|
|
|
public function listViews($database = null)
|
|
|
|
{
|
|
|
|
|
2006-12-28 15:27:44 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|