3 * TSqliteMetaData class file.
5 * @author Wei Zhuo <weizhuo[at]gmail[dot]com>
6 * @link https://github.com/pradosoft/prado
7 * @copyright Copyright © 2005-2016 The PRADO Group
8 * @license https://github.com/pradosoft/prado/blob/master/COPYRIGHT
9 * @package System.Data.Common.Sqlite
13 * Load the base TDbMetaData class.
15 Prado::using('System.Data.Common.TDbMetaData');
16 Prado::using('System.Data.Common.Sqlite.TSqliteTableInfo');
19 * TSqliteMetaData loads SQLite database table and column information.
21 * @author Wei Zhuo <weizho[at]gmail[dot]com>
22 * @package System.Data.Commom.Sqlite
25 class TSqliteMetaData extends TDbMetaData
28 * @return string TDbTableInfo class name.
30 protected function getTableInfoClass()
32 return 'TSqliteTableInfo';
36 * Quotes a table name for use in a query.
37 * @param string $name table name
38 * @return string the properly quoted table name
40 public function quoteTableName($name)
42 return parent::quoteTableName($name, "'", "'");
46 * Quotes a column name for use in a query.
47 * @param string $name column name
48 * @return string the properly quoted column name
50 public function quoteColumnName($name)
52 return parent::quoteColumnName($name, '"', '"');
56 * Quotes a column alias for use in a query.
57 * @param string $name column alias
58 * @return string the properly quoted column alias
60 public function quoteColumnAlias($name)
62 return parent::quoteColumnAlias($name, '"', '"');
66 * Get the column definitions for given table.
67 * @param string table name.
68 * @return TPgsqlTableInfo table information.
70 protected function createTableInfo($tableName)
72 $tableName = str_replace("'",'',$tableName);
73 $this->getDbConnection()->setActive(true);
74 $table = $this->getDbConnection()->quoteString($tableName);
75 $sql = "PRAGMA table_info({$table})";
76 $command = $this->getDbConnection()->createCommand($sql);
77 $foreign = $this->getForeignKeys($table);
81 foreach($command->query() as $col)
83 $col['index'] = $index++;
84 $column = $this->processColumn($col, $foreign);
85 $columns[$col['name']] = $column;
86 if($column->getIsPrimaryKey())
87 $primary[] = $col['name'];
89 $info['TableName'] = $tableName;
90 if($this->getIsView($tableName))
91 $info['IsView'] = true;
92 if(count($columns)===0)
93 throw new TDbException('dbmetadata_invalid_table_view', $tableName);
94 $class = $this->getTableInfoClass();
95 $tableInfo = new $class($info,$primary,$foreign);
96 $tableInfo->getColumns()->copyFrom($columns);
101 * @param string table name.
102 * @return boolean true if the table is a view.
104 protected function getIsView($tableName)
106 $sql = 'SELECT count(*) FROM sqlite_master WHERE type="view" AND name= :table';
107 $this->getDbConnection()->setActive(true);
108 $command = $this->getDbConnection()->createCommand($sql);
109 $command->bindValue(':table', $tableName);
110 return intval($command->queryScalar()) === 1;
114 * @param array column information.
115 * @param array foreign key details.
116 * @return TSqliteTableColumn column details.
118 protected function processColumn($col, $foreign)
120 $columnId = $col['name']; //use column name as column Id
122 $info['ColumnName'] = '"'.$columnId.'"'; //quote the column names!
123 $info['ColumnId'] = $columnId;
124 $info['ColumnIndex'] = $col['index'];
126 if($col['notnull']!=='99')
127 $info['AllowNull'] = true;
130 $info['IsPrimaryKey'] = true;
131 if($this->isForeignKeyColumn($columnId, $foreign))
132 $info['IsForeignKey'] = true;
134 if($col['dflt_value']!==null)
135 $info['DefaultValue'] = $col['dflt_value'];
137 $type = strtolower($col['type']);
138 $info['AutoIncrement'] = $type==='integer' && $col['pk']==='1';
140 $info['DbType'] = $type;
142 if(is_int($pos=strpos($type, '(')) && preg_match('/\((.*)\)/', $type, $match))
144 $ps = explode(',', $match[1]);
147 $info['NumericPrecision'] = intval($ps[0]);
148 $info['NumericScale'] = intval($ps[1]);
151 $info['ColumnSize']=intval($match[1]);
152 $info['DbType'] = substr($type,0,$pos);
155 return new TSqliteTableColumn($info);
161 * @param string quoted table name.
162 * @return array foreign key details.
164 protected function getForeignKeys($table)
166 $sql = "PRAGMA foreign_key_list({$table})";
167 $command = $this->getDbConnection()->createCommand($sql);
169 foreach($command->query() as $col)
171 $fkeys[$col['table']]['keys'][$col['from']] = $col['to'];
172 $fkeys[$col['table']]['table'] = $col['table'];
174 return count($fkeys) > 0 ? array_values($fkeys) : $fkeys;
178 * @param string column name.
179 * @param array foreign key column names.
180 * @return boolean true if column is a foreign key.
182 protected function isForeignKeyColumn($columnId, $foreign)
184 foreach($foreign as $fk)
186 if(in_array($columnId, array_keys($fk['keys'])))
193 * Returns all table names in the database.
194 * @param string $schema the schema of the tables. This is not used for sqlite database.
195 * @return array all table names in the database.
197 public function findTableNames($schema='')
199 $sql="SELECT DISTINCT tbl_name FROM sqlite_master WHERE tbl_name<>'sqlite_sequence'";
200 return $this->getDbConnection()->createCommand($sql)->queryColumn();