PageRenderTime 43ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/framework/db/schema/sqlite/CSqliteSchema.php

https://github.com/ereslibre/documentrepository
PHP | 288 lines | 141 code | 19 blank | 128 comment | 10 complexity | 9495b22006e436c85bb6bb24dca347ab MD5 | raw file
  1. <?php
  2. /**
  3. * CSqliteSchema class file.
  4. *
  5. * @author Qiang Xue <qiang.xue@gmail.com>
  6. * @link http://www.yiiframework.com/
  7. * @copyright Copyright &copy; 2008-2011 Yii Software LLC
  8. * @license http://www.yiiframework.com/license/
  9. */
  10. /**
  11. * CSqliteSchema is the class for retrieving metadata information from a SQLite (2/3) database.
  12. *
  13. * @author Qiang Xue <qiang.xue@gmail.com>
  14. * @version $Id: CSqliteSchema.php 3304 2011-06-23 14:53:50Z qiang.xue $
  15. * @package system.db.schema.sqlite
  16. * @since 1.0
  17. */
  18. class CSqliteSchema extends CDbSchema
  19. {
  20. /**
  21. * @var array the abstract column types mapped to physical column types.
  22. * @since 1.1.6
  23. */
  24. public $columnTypes=array(
  25. 'pk' => 'integer PRIMARY KEY AUTOINCREMENT NOT NULL',
  26. 'string' => 'varchar(255)',
  27. 'text' => 'text',
  28. 'integer' => 'integer',
  29. 'float' => 'float',
  30. 'decimal' => 'decimal',
  31. 'datetime' => 'datetime',
  32. 'timestamp' => 'timestamp',
  33. 'time' => 'time',
  34. 'date' => 'date',
  35. 'binary' => 'blob',
  36. 'boolean' => 'tinyint(1)',
  37. 'money' => 'decimal(19,4)',
  38. );
  39. /**
  40. * Resets the sequence value of a table's primary key.
  41. * The sequence will be reset such that the primary key of the next new row inserted
  42. * will have the specified value or 1.
  43. * @param CDbTableSchema $table the table schema whose primary key sequence will be reset
  44. * @param mixed $value the value for the primary key of the next new row inserted. If this is not set,
  45. * the next new row's primary key will have a value 1.
  46. * @since 1.1
  47. */
  48. public function resetSequence($table,$value=null)
  49. {
  50. if($table->sequenceName!==null)
  51. {
  52. if($value===null)
  53. $value=$this->getDbConnection()->createCommand("SELECT MAX(`{$table->primaryKey}`) FROM {$table->rawName}")->queryScalar();
  54. else
  55. $value=(int)$value-1;
  56. try
  57. {
  58. // it's possible sqlite_sequence does not exist
  59. $this->getDbConnection()->createCommand("UPDATE sqlite_sequence SET seq='$value' WHERE name='{$table->name}'")->execute();
  60. }
  61. catch(Exception $e)
  62. {
  63. }
  64. }
  65. }
  66. /**
  67. * Enables or disables integrity check.
  68. * @param boolean $check whether to turn on or off the integrity check.
  69. * @param string $schema the schema of the tables. Defaults to empty string, meaning the current or default schema.
  70. * @since 1.1
  71. */
  72. public function checkIntegrity($check=true,$schema='')
  73. {
  74. // SQLite doesn't enforce integrity
  75. return;
  76. }
  77. /**
  78. * Returns all table names in the database.
  79. * @param string $schema the schema of the tables. This is not used for sqlite database.
  80. * @return array all table names in the database.
  81. * @since 1.0.2
  82. */
  83. protected function findTableNames($schema='')
  84. {
  85. $sql="SELECT DISTINCT tbl_name FROM sqlite_master WHERE tbl_name<>'sqlite_sequence'";
  86. return $this->getDbConnection()->createCommand($sql)->queryColumn();
  87. }
  88. /**
  89. * Creates a command builder for the database.
  90. * @return CSqliteCommandBuilder command builder instance
  91. */
  92. protected function createCommandBuilder()
  93. {
  94. return new CSqliteCommandBuilder($this);
  95. }
  96. /**
  97. * Loads the metadata for the specified table.
  98. * @param string $name table name
  99. * @return CDbTableSchema driver dependent table metadata. Null if the table does not exist.
  100. */
  101. protected function loadTable($name)
  102. {
  103. $table=new CDbTableSchema;
  104. $table->name=$name;
  105. $table->rawName=$this->quoteTableName($name);
  106. if($this->findColumns($table))
  107. {
  108. $this->findConstraints($table);
  109. return $table;
  110. }
  111. else
  112. return null;
  113. }
  114. /**
  115. * Collects the table column metadata.
  116. * @param CDbTableSchema $table the table metadata
  117. * @return boolean whether the table exists in the database
  118. */
  119. protected function findColumns($table)
  120. {
  121. $sql="PRAGMA table_info({$table->rawName})";
  122. $columns=$this->getDbConnection()->createCommand($sql)->queryAll();
  123. if(empty($columns))
  124. return false;
  125. foreach($columns as $column)
  126. {
  127. $c=$this->createColumn($column);
  128. $table->columns[$c->name]=$c;
  129. if($c->isPrimaryKey)
  130. {
  131. if($table->primaryKey===null)
  132. $table->primaryKey=$c->name;
  133. else if(is_string($table->primaryKey))
  134. $table->primaryKey=array($table->primaryKey,$c->name);
  135. else
  136. $table->primaryKey[]=$c->name;
  137. }
  138. }
  139. if(is_string($table->primaryKey) && !strncasecmp($table->columns[$table->primaryKey]->dbType,'int',3))
  140. {
  141. $table->sequenceName='';
  142. $table->columns[$table->primaryKey]->autoIncrement=true;
  143. }
  144. return true;
  145. }
  146. /**
  147. * Collects the foreign key column details for the given table.
  148. * @param CDbTableSchema $table the table metadata
  149. */
  150. protected function findConstraints($table)
  151. {
  152. $foreignKeys=array();
  153. $sql="PRAGMA foreign_key_list({$table->rawName})";
  154. $keys=$this->getDbConnection()->createCommand($sql)->queryAll();
  155. foreach($keys as $key)
  156. {
  157. $column=$table->columns[$key['from']];
  158. $column->isForeignKey=true;
  159. $foreignKeys[$key['from']]=array($key['table'],$key['to']);
  160. }
  161. $table->foreignKeys=$foreignKeys;
  162. }
  163. /**
  164. * Creates a table column.
  165. * @param array $column column metadata
  166. * @return CDbColumnSchema normalized column metadata
  167. */
  168. protected function createColumn($column)
  169. {
  170. $c=new CSqliteColumnSchema;
  171. $c->name=$column['name'];
  172. $c->rawName=$this->quoteColumnName($c->name);
  173. $c->allowNull=!$column['notnull'];
  174. $c->isPrimaryKey=$column['pk']!=0;
  175. $c->isForeignKey=false;
  176. $c->init(strtolower($column['type']),$column['dflt_value']);
  177. return $c;
  178. }
  179. /**
  180. * Builds a SQL statement for truncating a DB table.
  181. * @param string $table the table to be truncated. The name will be properly quoted by the method.
  182. * @return string the SQL statement for truncating a DB table.
  183. * @since 1.1.6
  184. */
  185. public function truncateTable($table)
  186. {
  187. return "DELETE FROM ".$this->quoteTableName($table);
  188. }
  189. /**
  190. * Builds a SQL statement for dropping a DB column.
  191. * Because SQLite does not support dropping a DB column, calling this method will throw an exception.
  192. * @param string $table the table whose column is to be dropped. The name will be properly quoted by the method.
  193. * @param string $column the name of the column to be dropped. The name will be properly quoted by the method.
  194. * @return string the SQL statement for dropping a DB column.
  195. * @since 1.1.6
  196. */
  197. public function dropColumn($table, $column)
  198. {
  199. throw new CDbException(Yii::t('yii', 'Dropping DB column is not supported by SQLite.'));
  200. }
  201. /**
  202. * Builds a SQL statement for renaming a column.
  203. * Because SQLite does not support renaming a DB column, calling this method will throw an exception.
  204. * @param string $table the table whose column is to be renamed. The name will be properly quoted by the method.
  205. * @param string $name the old name of the column. The name will be properly quoted by the method.
  206. * @param string $newName the new name of the column. The name will be properly quoted by the method.
  207. * @return string the SQL statement for renaming a DB column.
  208. * @since 1.1.6
  209. */
  210. public function renameColumn($table, $name, $newName)
  211. {
  212. throw new CDbException(Yii::t('yii', 'Renaming a DB column is not supported by SQLite.'));
  213. }
  214. /**
  215. * Builds a SQL statement for adding a foreign key constraint to an existing table.
  216. * Because SQLite does not support adding foreign key to an existing table, calling this method will throw an exception.
  217. * @param string $name the name of the foreign key constraint.
  218. * @param string $table the table that the foreign key constraint will be added to.
  219. * @param string $columns the name of the column to that the constraint will be added on. If there are multiple columns, separate them with commas.
  220. * @param string $refTable the table that the foreign key references to.
  221. * @param string $refColumns the name of the column that the foreign key references to. If there are multiple columns, separate them with commas.
  222. * @param string $delete the ON DELETE option. Most DBMS support these options: RESTRICT, CASCADE, NO ACTION, SET DEFAULT, SET NULL
  223. * @param string $update the ON UPDATE option. Most DBMS support these options: RESTRICT, CASCADE, NO ACTION, SET DEFAULT, SET NULL
  224. * @return string the SQL statement for adding a foreign key constraint to an existing table.
  225. * @since 1.1.6
  226. */
  227. public function addForeignKey($name, $table, $columns, $refTable, $refColumns, $delete=null, $update=null)
  228. {
  229. throw new CDbException(Yii::t('yii', 'Adding a foreign key constraint to an existing table is not supported by SQLite.'));
  230. }
  231. /**
  232. * Builds a SQL statement for dropping a foreign key constraint.
  233. * Because SQLite does not support dropping a foreign key constraint, calling this method will throw an exception.
  234. * @param string $name the name of the foreign key constraint to be dropped. The name will be properly quoted by the method.
  235. * @param string $table the table whose foreign is to be dropped. The name will be properly quoted by the method.
  236. * @return string the SQL statement for dropping a foreign key constraint.
  237. * @since 1.1.6
  238. */
  239. public function dropForeignKey($name, $table)
  240. {
  241. throw new CDbException(Yii::t('yii', 'Dropping a foreign key constraint is not supported by SQLite.'));
  242. }
  243. /**
  244. * Builds a SQL statement for changing the definition of a column.
  245. * Because SQLite does not support altering a DB column, calling this method will throw an exception.
  246. * @param string $table the table whose column is to be changed. The table name will be properly quoted by the method.
  247. * @param string $column the name of the column to be changed. The name will be properly quoted by the method.
  248. * @param string $type the new column type. The {@link getColumnType} method will be invoked to convert abstract column type (if any)
  249. * into the physical one. Anything that is not recognized as abstract type will be kept in the generated SQL.
  250. * For example, 'string' will be turned into 'varchar(255)', while 'string not null' will become 'varchar(255) not null'.
  251. * @return string the SQL statement for changing the definition of a column.
  252. * @since 1.1.6
  253. */
  254. public function alterColumn($table, $column, $type)
  255. {
  256. throw new CDbException(Yii::t('yii', 'Altering a DB column is not supported by SQLite.'));
  257. }
  258. /**
  259. * Builds a SQL statement for dropping an index.
  260. * @param string $name the name of the index to be dropped. The name will be properly quoted by the method.
  261. * @param string $table the table whose index is to be dropped. The name will be properly quoted by the method.
  262. * @return string the SQL statement for dropping an index.
  263. * @since 1.1.6
  264. */
  265. public function dropIndex($name, $table)
  266. {
  267. return 'DROP INDEX '.$this->quoteTableName($name);
  268. }
  269. }