SqliteSchemaManager.php 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. $keyName = $tableIndex['name'];
  90. $idx = array();
  91. $idx['key_name'] = $keyName;
  92. $idx['primary'] = false;
  93. $idx['non_unique'] = $tableIndex['unique']?false:true;
  94. $stmt = $this->_conn->executeQuery( "PRAGMA INDEX_INFO ( '{$keyName}' )" );
  95. $indexArray = $stmt->fetchAll(\PDO::FETCH_ASSOC);
  96. foreach ( $indexArray as $indexColumnRow ) {
  97. $idx['column_name'] = $indexColumnRow['name'];
  98. $indexBuffer[] = $idx;
  99. }
  100. }
  101. return parent::_getPortableTableIndexesList($indexBuffer, $tableName);
  102. }
  103. protected function _getPortableTableIndexDefinition($tableIndex)
  104. {
  105. return array(
  106. 'name' => $tableIndex['name'],
  107. 'unique' => (bool) $tableIndex['unique']
  108. );
  109. }
  110. protected function _getPortableTableColumnDefinition($tableColumn)
  111. {
  112. $e = explode('(', $tableColumn['type']);
  113. $tableColumn['type'] = $e[0];
  114. if (isset($e[1])) {
  115. $length = trim($e[1], ')');
  116. $tableColumn['length'] = $length;
  117. }
  118. $dbType = strtolower($tableColumn['type']);
  119. $length = isset($tableColumn['length']) ? $tableColumn['length'] : null;
  120. $unsigned = (boolean) isset($tableColumn['unsigned']) ? $tableColumn['unsigned'] : false;
  121. $fixed = false;
  122. $type = $this->_platform->getDoctrineTypeMapping($dbType);
  123. $default = $tableColumn['dflt_value'];
  124. if ($default == 'NULL') {
  125. $default = null;
  126. }
  127. $notnull = (bool) $tableColumn['notnull'];
  128. if ( ! isset($tableColumn['name'])) {
  129. $tableColumn['name'] = '';
  130. }
  131. $precision = null;
  132. $scale = null;
  133. switch ($dbType) {
  134. case 'char':
  135. $fixed = true;
  136. break;
  137. case 'float':
  138. case 'double':
  139. case 'real':
  140. case 'decimal':
  141. case 'numeric':
  142. if (isset($tableColumn['length'])) {
  143. list($precision, $scale) = array_map('trim', explode(', ', $tableColumn['length']));
  144. }
  145. $length = null;
  146. break;
  147. }
  148. $options = array(
  149. 'length' => $length,
  150. 'unsigned' => (bool) $unsigned,
  151. 'fixed' => $fixed,
  152. 'notnull' => $notnull,
  153. 'default' => $default,
  154. 'precision' => $precision,
  155. 'scale' => $scale,
  156. 'autoincrement' => (bool) $tableColumn['pk'],
  157. );
  158. return new Column($tableColumn['name'], \Doctrine\DBAL\Types\Type::getType($type), $options);
  159. }
  160. protected function _getPortableViewDefinition($view)
  161. {
  162. return new View($view['name'], $view['sql']);
  163. }
  164. }