SqliteSchemaManager.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. <?php
  2. /*
  3. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  4. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  5. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  6. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  7. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  8. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  9. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  10. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  11. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  12. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  13. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  14. *
  15. * This software consists of voluntary contributions made by many individuals
  16. * and is licensed under the LGPL. For more information, see
  17. * <http://www.doctrine-project.org>.
  18. */
  19. namespace Doctrine\DBAL\Schema;
  20. /**
  21. * SqliteSchemaManager
  22. *
  23. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  24. * @author Konsta Vesterinen <kvesteri@cc.hut.fi>
  25. * @author Lukas Smith <smith@pooteeweet.org> (PEAR MDB2 library)
  26. * @author Jonathan H. Wage <jonwage@gmail.com>
  27. * @version $Revision$
  28. * @since 2.0
  29. */
  30. class SqliteSchemaManager extends AbstractSchemaManager
  31. {
  32. /**
  33. * {@inheritdoc}
  34. *
  35. * @override
  36. */
  37. public function dropDatabase($database)
  38. {
  39. if (file_exists($database)) {
  40. unlink($database);
  41. }
  42. }
  43. /**
  44. * {@inheritdoc}
  45. *
  46. * @override
  47. */
  48. public function createDatabase($database)
  49. {
  50. $params = $this->_conn->getParams();
  51. $driver = $params['driver'];
  52. $options = array(
  53. 'driver' => $driver,
  54. 'path' => $database
  55. );
  56. $conn = \Doctrine\DBAL\DriverManager::getConnection($options);
  57. $conn->connect();
  58. $conn->close();
  59. }
  60. protected function _getPortableTableDefinition($table)
  61. {
  62. return $table['name'];
  63. }
  64. /**
  65. * @license New BSD License
  66. * @link http://ezcomponents.org/docs/api/trunk/DatabaseSchema/ezcDbSchemaPgsqlReader.html
  67. * @param array $tableIndexes
  68. * @param string $tableName
  69. * @return array
  70. */
  71. protected function _getPortableTableIndexesList($tableIndexes, $tableName=null)
  72. {
  73. $indexBuffer = array();
  74. // fetch primary
  75. $stmt = $this->_conn->executeQuery( "PRAGMA TABLE_INFO ('$tableName')" );
  76. $indexArray = $stmt->fetchAll(\PDO::FETCH_ASSOC);
  77. foreach($indexArray AS $indexColumnRow) {
  78. if($indexColumnRow['pk'] == "1") {
  79. $indexBuffer[] = array(
  80. 'key_name' => 'primary',
  81. 'primary' => true,
  82. 'non_unique' => false,
  83. 'column_name' => $indexColumnRow['name']
  84. );
  85. }
  86. }
  87. // fetch regular indexes
  88. foreach($tableIndexes AS $tableIndex) {
  89. // Ignore indexes with reserved names, e.g. autoindexes
  90. if (strpos($tableIndex['name'], 'sqlite_') !== 0) {
  91. $keyName = $tableIndex['name'];
  92. $idx = array();
  93. $idx['key_name'] = $keyName;
  94. $idx['primary'] = false;
  95. $idx['non_unique'] = $tableIndex['unique']?false:true;
  96. $stmt = $this->_conn->executeQuery( "PRAGMA INDEX_INFO ( '{$keyName}' )" );
  97. $indexArray = $stmt->fetchAll(\PDO::FETCH_ASSOC);
  98. foreach ( $indexArray as $indexColumnRow ) {
  99. $idx['column_name'] = $indexColumnRow['name'];
  100. $indexBuffer[] = $idx;
  101. }
  102. }
  103. }
  104. return parent::_getPortableTableIndexesList($indexBuffer, $tableName);
  105. }
  106. protected function _getPortableTableIndexDefinition($tableIndex)
  107. {
  108. return array(
  109. 'name' => $tableIndex['name'],
  110. 'unique' => (bool) $tableIndex['unique']
  111. );
  112. }
  113. protected function _getPortableTableColumnDefinition($tableColumn)
  114. {
  115. $e = explode('(', $tableColumn['type']);
  116. $tableColumn['type'] = $e[0];
  117. if (isset($e[1])) {
  118. $length = trim($e[1], ')');
  119. $tableColumn['length'] = $length;
  120. }
  121. $dbType = strtolower($tableColumn['type']);
  122. $length = isset($tableColumn['length']) ? $tableColumn['length'] : null;
  123. $unsigned = (boolean) isset($tableColumn['unsigned']) ? $tableColumn['unsigned'] : false;
  124. $fixed = false;
  125. $type = $this->_platform->getDoctrineTypeMapping($dbType);
  126. $default = $tableColumn['dflt_value'];
  127. if ($default == 'NULL') {
  128. $default = null;
  129. }
  130. if ($default !== null) {
  131. // SQLite returns strings wrapped in single quotes, so we need to strip them
  132. $default = preg_replace("/^'(.*)'$/", '\1', $default);
  133. }
  134. $notnull = (bool) $tableColumn['notnull'];
  135. if ( ! isset($tableColumn['name'])) {
  136. $tableColumn['name'] = '';
  137. }
  138. $precision = null;
  139. $scale = null;
  140. switch ($dbType) {
  141. case 'char':
  142. $fixed = true;
  143. break;
  144. case 'float':
  145. case 'double':
  146. case 'real':
  147. case 'decimal':
  148. case 'numeric':
  149. if (isset($tableColumn['length'])) {
  150. list($precision, $scale) = array_map('trim', explode(', ', $tableColumn['length']));
  151. }
  152. $length = null;
  153. break;
  154. }
  155. $options = array(
  156. 'length' => $length,
  157. 'unsigned' => (bool) $unsigned,
  158. 'fixed' => $fixed,
  159. 'notnull' => $notnull,
  160. 'default' => $default,
  161. 'precision' => $precision,
  162. 'scale' => $scale,
  163. 'autoincrement' => false,
  164. );
  165. return new Column($tableColumn['name'], \Doctrine\DBAL\Types\Type::getType($type), $options);
  166. }
  167. protected function _getPortableViewDefinition($view)
  168. {
  169. return new View($view['name'], $view['sql']);
  170. }
  171. }