blob: 1c6ba78f44c71e7e8e5f2f6aaf138b8891aed3d3 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
|
<?php
/**
* TSqliteSchema class file.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @link http://www.yiiframework.com/
* @copyright Copyright © 2008-2009 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
prado::using('System.db.schema.TDbSchema');
prado::using('System.db.schema.TDbTableSchema');
prado::using('System.db.schema.sqlite.TSqliteColumnSchema');
prado::using('System.db.schema.sqlite.TSqliteCommandBuilder');
/**
* TSqliteSchema is the class for retrieving metadata information from a SQLite (2/3) database.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @version $Id$
* @package system.db.schema.sqlite
* @since 1.0
*/
class TSqliteSchema extends TDbSchema
{
/**
* Returns all table names in the database.
* @param string the schema of the tables. This is not used for sqlite database.
* @return array all table names in the database.
* @since 1.0.2
*/
protected function findTableNames($schema='')
{
$sql="SELECT DISTINCT tbl_name FROM sqlite_master WHERE tbl_name<>'sqlite_sequence'";
return $this->getDbConnection()->createCommand($sql)->queryColumn();
}
/**
* Creates a command builder for the database.
* @return TSqliteCommandBuilder command builder instance
*/
protected function createCommandBuilder()
{
return new TSqliteCommandBuilder($this);
}
/**
* Creates a table instance representing the metadata for the named table.
* @return TDbTableSchema driver dependent table metadata. Null if the table does not exist.
*/
protected function createTable($name)
{
$db=$this->getDbConnection();
$table=new TDbTableSchema;
$table->name=$name;
$table->rawName=$this->quoteTableName($name);
if($this->findColumns($table))
{
$this->findConstraints($table);
return $table;
}
else
return null;
}
/**
* Collects the table column metadata.
* @param TDbTableSchema the table metadata
* @return boolean whether the table exists in the database
*/
protected function findColumns($table)
{
$sql="PRAGMA table_info({$table->rawName})";
$columns=$this->getDbConnection()->createCommand($sql)->queryAll();
if(empty($columns))
return false;
foreach($columns as $column)
{
$c=$this->createColumn($column);
$table->columns[$c->name]=$c;
if($c->isPrimaryKey)
{
if($table->primaryKey===null)
$table->primaryKey=$c->name;
else if(is_string($table->primaryKey))
$table->primaryKey=array($table->primaryKey,$c->name);
else
$table->primaryKey[]=$c->name;
}
}
if(is_string($table->primaryKey) && !strncasecmp($table->columns[$table->primaryKey]->dbType,'int',3))
$table->sequenceName='';
return true;
}
/**
* Collects the foreign key column details for the given table.
* @param TDbTableSchema the table metadata
*/
protected function findConstraints($table)
{
$foreignKeys=array();
$sql="PRAGMA foreign_key_list({$table->rawName})";
$keys=$this->getDbConnection()->createCommand($sql)->queryAll();
foreach($keys as $key)
{
$column=$table->columns[$key['from']];
$column->isForeignKey=true;
$foreignKeys[$key['from']]=array($key['table'],$key['to']);
}
$table->foreignKeys=$foreignKeys;
}
/**
* Creates a table column.
* @param array column metadata
* @return TDbColumnSchema normalized column metadata
*/
protected function createColumn($column)
{
$c=new TSqliteColumnSchema;
$c->name=$column['name'];
$c->rawName=$this->quoteColumnName($c->name);
$c->allowNull=!$column['notnull'];
$c->isPrimaryKey=$column['pk']!=0;
$c->isForeignKey=false;
$c->init(strtolower($column['type']),$column['dflt_value']);
return $c;
}
}
|