| 1 |
lars |
1 |
<?php
|
|
|
2 |
/**
|
|
|
3 |
* TSqliteMetaData class file.
|
|
|
4 |
*
|
|
|
5 |
* @author Wei Zhuo <weizhuo[at]gmail[dot]com>
|
|
|
6 |
* @link http://www.pradosoft.com/
|
|
|
7 |
* @copyright Copyright © 2005-2008 PradoSoft
|
|
|
8 |
* @license http://www.pradosoft.com/license/
|
|
|
9 |
* @version $Id: TSqliteMetaData.php 1861 2007-04-12 08:05:03Z wei $
|
|
|
10 |
* @package System.Data.Common.Sqlite
|
|
|
11 |
*/
|
|
|
12 |
|
|
|
13 |
/**
|
|
|
14 |
* Load the base TDbMetaData class.
|
|
|
15 |
*/
|
|
|
16 |
Prado::using('System.Data.Common.TDbMetaData');
|
|
|
17 |
Prado::using('System.Data.Common.Sqlite.TSqliteTableInfo');
|
|
|
18 |
|
|
|
19 |
/**
|
|
|
20 |
* TSqliteMetaData loads SQLite database table and column information.
|
|
|
21 |
*
|
|
|
22 |
* @author Wei Zhuo <weizho[at]gmail[dot]com>
|
|
|
23 |
* @version $Id: TSqliteMetaData.php 1861 2007-04-12 08:05:03Z wei $
|
|
|
24 |
* @package System.Data.Commom.Sqlite
|
|
|
25 |
* @since 3.1
|
|
|
26 |
*/
|
|
|
27 |
class TSqliteMetaData extends TDbMetaData
|
|
|
28 |
{
|
|
|
29 |
/**
|
|
|
30 |
* @return string TDbTableInfo class name.
|
|
|
31 |
*/
|
|
|
32 |
protected function getTableInfoClass()
|
|
|
33 |
{
|
|
|
34 |
return 'TSqliteTableInfo';
|
|
|
35 |
}
|
|
|
36 |
|
|
|
37 |
/**
|
|
|
38 |
* Get the column definitions for given table.
|
|
|
39 |
* @param string table name.
|
|
|
40 |
* @return TPgsqlTableInfo table information.
|
|
|
41 |
*/
|
|
|
42 |
protected function createTableInfo($tableName)
|
|
|
43 |
{
|
|
|
44 |
$tableName = str_replace("'",'',$tableName);
|
|
|
45 |
$this->getDbConnection()->setActive(true);
|
|
|
46 |
$table = $this->getDbConnection()->quoteString($tableName);
|
|
|
47 |
$sql = "PRAGMA table_info({$table})";
|
|
|
48 |
$command = $this->getDbConnection()->createCommand($sql);
|
|
|
49 |
$foreign = $this->getForeignKeys($table);
|
|
|
50 |
$index=0;
|
|
|
51 |
$columns=array();
|
|
|
52 |
$primary=array();
|
|
|
53 |
foreach($command->query() as $col)
|
|
|
54 |
{
|
|
|
55 |
$col['index'] = $index++;
|
|
|
56 |
$column = $this->processColumn($col, $foreign);
|
|
|
57 |
$columns[$col['name']] = $column;
|
|
|
58 |
if($column->getIsPrimaryKey())
|
|
|
59 |
$primary[] = $col['name'];
|
|
|
60 |
}
|
|
|
61 |
$info['TableName'] = $tableName;
|
|
|
62 |
if($this->getIsView($tableName))
|
|
|
63 |
$info['IsView'] = true;
|
|
|
64 |
if(count($columns)===0)
|
|
|
65 |
throw new TDbException('dbmetadata_invalid_table_view', $tableName);
|
|
|
66 |
$class = $this->getTableInfoClass();
|
|
|
67 |
$tableInfo = new $class($info,$primary,$foreign);
|
|
|
68 |
$tableInfo->getColumns()->copyFrom($columns);
|
|
|
69 |
return $tableInfo;
|
|
|
70 |
}
|
|
|
71 |
|
|
|
72 |
/**
|
|
|
73 |
* @param string table name.
|
|
|
74 |
* @return boolean true if the table is a view.
|
|
|
75 |
*/
|
|
|
76 |
protected function getIsView($tableName)
|
|
|
77 |
{
|
|
|
78 |
$sql = 'SELECT count(*) FROM sqlite_master WHERE type="view" AND name= :table';
|
|
|
79 |
$this->getDbConnection()->setActive(true);
|
|
|
80 |
$command = $this->getDbConnection()->createCommand($sql);
|
|
|
81 |
$command->bindValue(':table', $tableName);
|
|
|
82 |
return intval($command->queryScalar()) === 1;
|
|
|
83 |
}
|
|
|
84 |
|
|
|
85 |
/**
|
|
|
86 |
* @param array column information.
|
|
|
87 |
* @param array foreign key details.
|
|
|
88 |
* @return TSqliteTableColumn column details.
|
|
|
89 |
*/
|
|
|
90 |
protected function processColumn($col, $foreign)
|
|
|
91 |
{
|
|
|
92 |
$columnId = $col['name']; //use column name as column Id
|
|
|
93 |
|
|
|
94 |
$info['ColumnName'] = '"'.$columnId.'"'; //quote the column names!
|
|
|
95 |
$info['ColumnId'] = $columnId;
|
|
|
96 |
$info['ColumnIndex'] = $col['index'];
|
|
|
97 |
|
|
|
98 |
if($col['notnull']!=='99')
|
|
|
99 |
$info['AllowNull'] = true;
|
|
|
100 |
|
|
|
101 |
if($col['pk']==='1')
|
|
|
102 |
$info['IsPrimaryKey'] = true;
|
|
|
103 |
if($this->isForeignKeyColumn($columnId, $foreign))
|
|
|
104 |
$info['IsForeignKey'] = true;
|
|
|
105 |
|
|
|
106 |
if($col['dflt_value']!==null)
|
|
|
107 |
$info['DefaultValue'] = $col['dflt_value'];
|
|
|
108 |
|
|
|
109 |
$type = strtolower($col['type']);
|
|
|
110 |
$info['AutoIncrement'] = $type==='integer' && $col['pk']==='1';
|
|
|
111 |
|
|
|
112 |
$info['DbType'] = $type;
|
|
|
113 |
$match=array();
|
|
|
114 |
if(is_int($pos=strpos($type, '(')) && preg_match('/\((.*)\)/', $type, $match))
|
|
|
115 |
{
|
|
|
116 |
$ps = explode(',', $match[1]);
|
|
|
117 |
if(count($ps)===2)
|
|
|
118 |
{
|
|
|
119 |
$info['NumericPrecision'] = intval($ps[0]);
|
|
|
120 |
$info['NumericScale'] = intval($ps[1]);
|
|
|
121 |
}
|
|
|
122 |
else
|
|
|
123 |
$info['ColumnSize']=intval($match[1]);
|
|
|
124 |
$info['DbType'] = substr($type,0,$pos);
|
|
|
125 |
}
|
|
|
126 |
|
|
|
127 |
return new TSqliteTableColumn($info);
|
|
|
128 |
}
|
|
|
129 |
|
|
|
130 |
/**
|
|
|
131 |
*
|
|
|
132 |
*
|
|
|
133 |
* @param string quoted table name.
|
|
|
134 |
* @return array foreign key details.
|
|
|
135 |
*/
|
|
|
136 |
protected function getForeignKeys($table)
|
|
|
137 |
{
|
|
|
138 |
$sql = "PRAGMA foreign_key_list({$table})";
|
|
|
139 |
$command = $this->getDbConnection()->createCommand($sql);
|
|
|
140 |
$fkeys = array();
|
|
|
141 |
foreach($command->query() as $col)
|
|
|
142 |
{
|
|
|
143 |
$fkeys[$col['table']]['keys'][$col['from']] = $col['to'];
|
|
|
144 |
$fkeys[$col['table']]['table'] = $col['table'];
|
|
|
145 |
}
|
|
|
146 |
return count($fkeys) > 0 ? array_values($fkeys) : $fkeys;
|
|
|
147 |
}
|
|
|
148 |
|
|
|
149 |
/**
|
|
|
150 |
* @param string column name.
|
|
|
151 |
* @param array foreign key column names.
|
|
|
152 |
* @return boolean true if column is a foreign key.
|
|
|
153 |
*/
|
|
|
154 |
protected function isForeignKeyColumn($columnId, $foreign)
|
|
|
155 |
{
|
|
|
156 |
foreach($foreign as $fk)
|
|
|
157 |
{
|
|
|
158 |
if(in_array($columnId, array_keys($fk['keys'])))
|
|
|
159 |
return true;
|
|
|
160 |
}
|
|
|
161 |
return false;
|
|
|
162 |
}
|
|
|
163 |
}
|
|
|
164 |
|
|
|
165 |
/**
|
|
|
166 |
|
|
|
167 |
CREATE TABLE foo
|
|
|
168 |
(
|
|
|
169 |
id INTEGER NOT NULL PRIMARY KEY,
|
|
|
170 |
id2 CHAR(2)
|
|
|
171 |
);
|
|
|
172 |
|
|
|
173 |
CREATE TABLE bar
|
|
|
174 |
(
|
|
|
175 |
id INTEGER NOT NULL PRIMARY KEY,
|
|
|
176 |
foo_id INTEGER
|
|
|
177 |
CONSTRAINT fk_foo_id REFERENCES foo(id) ON DELETE CASCADE
|
|
|
178 |
);
|
|
|
179 |
*/
|
|
|
180 |
|