PageRenderTime 28ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/Doctrine/DBAL/Schema/SqliteSchemaManager.php

http://github.com/doctrine/dbal
PHP | 552 lines | 449 code | 52 blank | 51 comment | 27 complexity | 337c950ba1d6b2f274bdb7e02fcb91c8 MD5 | raw file
Possible License(s): Unlicense
  1. <?php
  2. namespace Doctrine\DBAL\Schema;
  3. use Doctrine\DBAL\DBALException;
  4. use Doctrine\DBAL\DriverManager;
  5. use Doctrine\DBAL\FetchMode;
  6. use Doctrine\DBAL\Types\StringType;
  7. use Doctrine\DBAL\Types\TextType;
  8. use Doctrine\DBAL\Types\Type;
  9. use function array_change_key_case;
  10. use function array_map;
  11. use function array_reverse;
  12. use function array_values;
  13. use function explode;
  14. use function file_exists;
  15. use function preg_match;
  16. use function preg_match_all;
  17. use function preg_quote;
  18. use function preg_replace;
  19. use function rtrim;
  20. use function sprintf;
  21. use function str_replace;
  22. use function strpos;
  23. use function strtolower;
  24. use function trim;
  25. use function unlink;
  26. use function usort;
  27. use const CASE_LOWER;
  28. /**
  29. * Sqlite SchemaManager.
  30. */
  31. class SqliteSchemaManager extends AbstractSchemaManager
  32. {
  33. /**
  34. * {@inheritdoc}
  35. */
  36. public function dropDatabase($database)
  37. {
  38. if (! file_exists($database)) {
  39. return;
  40. }
  41. unlink($database);
  42. }
  43. /**
  44. * {@inheritdoc}
  45. */
  46. public function createDatabase($database)
  47. {
  48. $params = $this->_conn->getParams();
  49. $driver = $params['driver'];
  50. $options = [
  51. 'driver' => $driver,
  52. 'path' => $database,
  53. ];
  54. $conn = DriverManager::getConnection($options);
  55. $conn->connect();
  56. $conn->close();
  57. }
  58. /**
  59. * {@inheritdoc}
  60. */
  61. public function renameTable($name, $newName)
  62. {
  63. $tableDiff = new TableDiff($name);
  64. $tableDiff->fromTable = $this->listTableDetails($name);
  65. $tableDiff->newName = $newName;
  66. $this->alterTable($tableDiff);
  67. }
  68. /**
  69. * {@inheritdoc}
  70. */
  71. public function createForeignKey(ForeignKeyConstraint $foreignKey, $table)
  72. {
  73. $tableDiff = $this->getTableDiffForAlterForeignKey($table);
  74. $tableDiff->addedForeignKeys[] = $foreignKey;
  75. $this->alterTable($tableDiff);
  76. }
  77. /**
  78. * {@inheritdoc}
  79. */
  80. public function dropAndCreateForeignKey(ForeignKeyConstraint $foreignKey, $table)
  81. {
  82. $tableDiff = $this->getTableDiffForAlterForeignKey($table);
  83. $tableDiff->changedForeignKeys[] = $foreignKey;
  84. $this->alterTable($tableDiff);
  85. }
  86. /**
  87. * {@inheritdoc}
  88. */
  89. public function dropForeignKey($foreignKey, $table)
  90. {
  91. $tableDiff = $this->getTableDiffForAlterForeignKey($table);
  92. $tableDiff->removedForeignKeys[] = $foreignKey;
  93. $this->alterTable($tableDiff);
  94. }
  95. /**
  96. * {@inheritdoc}
  97. */
  98. public function listTableForeignKeys($table, $database = null)
  99. {
  100. if ($database === null) {
  101. $database = $this->_conn->getDatabase();
  102. }
  103. $sql = $this->_platform->getListTableForeignKeysSQL($table, $database);
  104. $tableForeignKeys = $this->_conn->fetchAll($sql);
  105. if (! empty($tableForeignKeys)) {
  106. $createSql = $this->getCreateTableSQL($table);
  107. if ($createSql !== null && preg_match_all(
  108. '#
  109. (?:CONSTRAINT\s+([^\s]+)\s+)?
  110. (?:FOREIGN\s+KEY[^\)]+\)\s*)?
  111. REFERENCES\s+[^\s]+\s+(?:\([^\)]+\))?
  112. (?:
  113. [^,]*?
  114. (NOT\s+DEFERRABLE|DEFERRABLE)
  115. (?:\s+INITIALLY\s+(DEFERRED|IMMEDIATE))?
  116. )?#isx',
  117. $createSql,
  118. $match
  119. )) {
  120. $names = array_reverse($match[1]);
  121. $deferrable = array_reverse($match[2]);
  122. $deferred = array_reverse($match[3]);
  123. } else {
  124. $names = $deferrable = $deferred = [];
  125. }
  126. foreach ($tableForeignKeys as $key => $value) {
  127. $id = $value['id'];
  128. $tableForeignKeys[$key]['constraint_name'] = isset($names[$id]) && $names[$id] !== '' ? $names[$id] : $id;
  129. $tableForeignKeys[$key]['deferrable'] = isset($deferrable[$id]) && strtolower($deferrable[$id]) === 'deferrable';
  130. $tableForeignKeys[$key]['deferred'] = isset($deferred[$id]) && strtolower($deferred[$id]) === 'deferred';
  131. }
  132. }
  133. return $this->_getPortableTableForeignKeysList($tableForeignKeys);
  134. }
  135. /**
  136. * {@inheritdoc}
  137. */
  138. protected function _getPortableTableDefinition($table)
  139. {
  140. return $table['name'];
  141. }
  142. /**
  143. * {@inheritdoc}
  144. *
  145. * @link http://ezcomponents.org/docs/api/trunk/DatabaseSchema/ezcDbSchemaPgsqlReader.html
  146. */
  147. protected function _getPortableTableIndexesList($tableIndexes, $tableName = null)
  148. {
  149. $indexBuffer = [];
  150. // fetch primary
  151. $stmt = $this->_conn->executeQuery(sprintf(
  152. 'PRAGMA TABLE_INFO (%s)',
  153. $this->_conn->quote($tableName)
  154. ));
  155. $indexArray = $stmt->fetchAll(FetchMode::ASSOCIATIVE);
  156. usort($indexArray, static function ($a, $b) {
  157. if ($a['pk'] === $b['pk']) {
  158. return $a['cid'] - $b['cid'];
  159. }
  160. return $a['pk'] - $b['pk'];
  161. });
  162. foreach ($indexArray as $indexColumnRow) {
  163. if ($indexColumnRow['pk'] === '0') {
  164. continue;
  165. }
  166. $indexBuffer[] = [
  167. 'key_name' => 'primary',
  168. 'primary' => true,
  169. 'non_unique' => false,
  170. 'column_name' => $indexColumnRow['name'],
  171. ];
  172. }
  173. // fetch regular indexes
  174. foreach ($tableIndexes as $tableIndex) {
  175. // Ignore indexes with reserved names, e.g. autoindexes
  176. if (strpos($tableIndex['name'], 'sqlite_') === 0) {
  177. continue;
  178. }
  179. $keyName = $tableIndex['name'];
  180. $idx = [];
  181. $idx['key_name'] = $keyName;
  182. $idx['primary'] = false;
  183. $idx['non_unique'] = ! $tableIndex['unique'];
  184. $stmt = $this->_conn->executeQuery(sprintf(
  185. 'PRAGMA INDEX_INFO (%s)',
  186. $this->_conn->quote($keyName)
  187. ));
  188. $indexArray = $stmt->fetchAll(FetchMode::ASSOCIATIVE);
  189. foreach ($indexArray as $indexColumnRow) {
  190. $idx['column_name'] = $indexColumnRow['name'];
  191. $indexBuffer[] = $idx;
  192. }
  193. }
  194. return parent::_getPortableTableIndexesList($indexBuffer, $tableName);
  195. }
  196. /**
  197. * @deprecated
  198. *
  199. * @param array<string, mixed> $tableIndex
  200. *
  201. * @return array<string, bool|string>
  202. */
  203. protected function _getPortableTableIndexDefinition($tableIndex)
  204. {
  205. return [
  206. 'name' => $tableIndex['name'],
  207. 'unique' => (bool) $tableIndex['unique'],
  208. ];
  209. }
  210. /**
  211. * {@inheritdoc}
  212. */
  213. protected function _getPortableTableColumnList($table, $database, $tableColumns)
  214. {
  215. $list = parent::_getPortableTableColumnList($table, $database, $tableColumns);
  216. // find column with autoincrement
  217. $autoincrementColumn = null;
  218. $autoincrementCount = 0;
  219. foreach ($tableColumns as $tableColumn) {
  220. if ($tableColumn['pk'] === '0') {
  221. continue;
  222. }
  223. $autoincrementCount++;
  224. if ($autoincrementColumn !== null || strtolower($tableColumn['type']) !== 'integer') {
  225. continue;
  226. }
  227. $autoincrementColumn = $tableColumn['name'];
  228. }
  229. if ($autoincrementCount === 1 && $autoincrementColumn !== null) {
  230. foreach ($list as $column) {
  231. if ($autoincrementColumn !== $column->getName()) {
  232. continue;
  233. }
  234. $column->setAutoincrement(true);
  235. }
  236. }
  237. // inspect column collation and comments
  238. $createSql = $this->getCreateTableSQL($table) ?? '';
  239. foreach ($list as $columnName => $column) {
  240. $type = $column->getType();
  241. if ($type instanceof StringType || $type instanceof TextType) {
  242. $column->setPlatformOption('collation', $this->parseColumnCollationFromSQL($columnName, $createSql) ?: 'BINARY');
  243. }
  244. $comment = $this->parseColumnCommentFromSQL($columnName, $createSql);
  245. if ($comment === null) {
  246. continue;
  247. }
  248. $type = $this->extractDoctrineTypeFromComment($comment, '');
  249. if ($type !== '') {
  250. $column->setType(Type::getType($type));
  251. $comment = $this->removeDoctrineTypeFromComment($comment, $type);
  252. }
  253. $column->setComment($comment);
  254. }
  255. return $list;
  256. }
  257. /**
  258. * {@inheritdoc}
  259. */
  260. protected function _getPortableTableColumnDefinition($tableColumn)
  261. {
  262. $parts = explode('(', $tableColumn['type']);
  263. $tableColumn['type'] = trim($parts[0]);
  264. if (isset($parts[1])) {
  265. $length = trim($parts[1], ')');
  266. $tableColumn['length'] = $length;
  267. }
  268. $dbType = strtolower($tableColumn['type']);
  269. $length = $tableColumn['length'] ?? null;
  270. $unsigned = false;
  271. if (strpos($dbType, ' unsigned') !== false) {
  272. $dbType = str_replace(' unsigned', '', $dbType);
  273. $unsigned = true;
  274. }
  275. $fixed = false;
  276. $type = $this->_platform->getDoctrineTypeMapping($dbType);
  277. $default = $tableColumn['dflt_value'];
  278. if ($default === 'NULL') {
  279. $default = null;
  280. }
  281. if ($default !== null) {
  282. // SQLite returns the default value as a literal expression, so we need to parse it
  283. if (preg_match('/^\'(.*)\'$/s', $default, $matches)) {
  284. $default = str_replace("''", "'", $matches[1]);
  285. }
  286. }
  287. $notnull = (bool) $tableColumn['notnull'];
  288. if (! isset($tableColumn['name'])) {
  289. $tableColumn['name'] = '';
  290. }
  291. $precision = null;
  292. $scale = null;
  293. switch ($dbType) {
  294. case 'char':
  295. $fixed = true;
  296. break;
  297. case 'float':
  298. case 'double':
  299. case 'real':
  300. case 'decimal':
  301. case 'numeric':
  302. if (isset($tableColumn['length'])) {
  303. if (strpos($tableColumn['length'], ',') === false) {
  304. $tableColumn['length'] .= ',0';
  305. }
  306. [$precision, $scale] = array_map('trim', explode(',', $tableColumn['length']));
  307. }
  308. $length = null;
  309. break;
  310. }
  311. $options = [
  312. 'length' => $length,
  313. 'unsigned' => (bool) $unsigned,
  314. 'fixed' => $fixed,
  315. 'notnull' => $notnull,
  316. 'default' => $default,
  317. 'precision' => $precision,
  318. 'scale' => $scale,
  319. 'autoincrement' => false,
  320. ];
  321. return new Column($tableColumn['name'], Type::getType($type), $options);
  322. }
  323. /**
  324. * {@inheritdoc}
  325. */
  326. protected function _getPortableViewDefinition($view)
  327. {
  328. return new View($view['name'], $view['sql']);
  329. }
  330. /**
  331. * {@inheritdoc}
  332. */
  333. protected function _getPortableTableForeignKeysList($tableForeignKeys)
  334. {
  335. $list = [];
  336. foreach ($tableForeignKeys as $value) {
  337. $value = array_change_key_case($value, CASE_LOWER);
  338. $name = $value['constraint_name'];
  339. if (! isset($list[$name])) {
  340. if (! isset($value['on_delete']) || $value['on_delete'] === 'RESTRICT') {
  341. $value['on_delete'] = null;
  342. }
  343. if (! isset($value['on_update']) || $value['on_update'] === 'RESTRICT') {
  344. $value['on_update'] = null;
  345. }
  346. $list[$name] = [
  347. 'name' => $name,
  348. 'local' => [],
  349. 'foreign' => [],
  350. 'foreignTable' => $value['table'],
  351. 'onDelete' => $value['on_delete'],
  352. 'onUpdate' => $value['on_update'],
  353. 'deferrable' => $value['deferrable'],
  354. 'deferred'=> $value['deferred'],
  355. ];
  356. }
  357. $list[$name]['local'][] = $value['from'];
  358. $list[$name]['foreign'][] = $value['to'];
  359. }
  360. $result = [];
  361. foreach ($list as $constraint) {
  362. $result[] = new ForeignKeyConstraint(
  363. array_values($constraint['local']),
  364. $constraint['foreignTable'],
  365. array_values($constraint['foreign']),
  366. $constraint['name'],
  367. [
  368. 'onDelete' => $constraint['onDelete'],
  369. 'onUpdate' => $constraint['onUpdate'],
  370. 'deferrable' => $constraint['deferrable'],
  371. 'deferred'=> $constraint['deferred'],
  372. ]
  373. );
  374. }
  375. return $result;
  376. }
  377. /**
  378. * @param Table|string $table
  379. *
  380. * @return TableDiff
  381. *
  382. * @throws DBALException
  383. */
  384. private function getTableDiffForAlterForeignKey($table)
  385. {
  386. if (! $table instanceof Table) {
  387. $tableDetails = $this->tryMethod('listTableDetails', $table);
  388. if ($tableDetails === false) {
  389. throw new DBALException(sprintf('Sqlite schema manager requires to modify foreign keys table definition "%s".', $table));
  390. }
  391. $table = $tableDetails;
  392. }
  393. $tableDiff = new TableDiff($table->getName());
  394. $tableDiff->fromTable = $table;
  395. return $tableDiff;
  396. }
  397. private function parseColumnCollationFromSQL(string $column, string $sql) : ?string
  398. {
  399. $pattern = '{(?:\W' . preg_quote($column) . '\W|\W' . preg_quote($this->_platform->quoteSingleIdentifier($column))
  400. . '\W)[^,(]+(?:\([^()]+\)[^,]*)?(?:(?:DEFAULT|CHECK)\s*(?:\(.*?\))?[^,]*)*COLLATE\s+["\']?([^\s,"\')]+)}is';
  401. if (preg_match($pattern, $sql, $match) !== 1) {
  402. return null;
  403. }
  404. return $match[1];
  405. }
  406. private function parseTableCommentFromSQL(string $table, string $sql) : ?string
  407. {
  408. $pattern = '/\s* # Allow whitespace characters at start of line
  409. CREATE\sTABLE # Match "CREATE TABLE"
  410. (?:\W"' . preg_quote($this->_platform->quoteSingleIdentifier($table), '/') . '"\W|\W' . preg_quote($table, '/')
  411. . '\W) # Match table name (quoted and unquoted)
  412. ( # Start capture
  413. (?:\s*--[^\n]*\n?)+ # Capture anything that starts with whitespaces followed by -- until the end of the line(s)
  414. )/ix';
  415. if (preg_match($pattern, $sql, $match) !== 1) {
  416. return null;
  417. }
  418. $comment = preg_replace('{^\s*--}m', '', rtrim($match[1], "\n"));
  419. return $comment === '' ? null : $comment;
  420. }
  421. private function parseColumnCommentFromSQL(string $column, string $sql) : ?string
  422. {
  423. $pattern = '{[\s(,](?:\W' . preg_quote($this->_platform->quoteSingleIdentifier($column)) . '\W|\W' . preg_quote($column)
  424. . '\W)(?:\([^)]*?\)|[^,(])*?,?((?:(?!\n))(?:\s*--[^\n]*\n?)+)}i';
  425. if (preg_match($pattern, $sql, $match) !== 1) {
  426. return null;
  427. }
  428. $comment = preg_replace('{^\s*--}m', '', rtrim($match[1], "\n"));
  429. return $comment === '' ? null : $comment;
  430. }
  431. private function getCreateTableSQL(string $table) : ?string
  432. {
  433. return $this->_conn->fetchColumn(
  434. <<<'SQL'
  435. SELECT sql
  436. FROM (
  437. SELECT *
  438. FROM sqlite_master
  439. UNION ALL
  440. SELECT *
  441. FROM sqlite_temp_master
  442. )
  443. WHERE type = 'table'
  444. AND name = ?
  445. SQL
  446. ,
  447. [$table]
  448. ) ?: null;
  449. }
  450. /**
  451. * @param string $tableName
  452. */
  453. public function listTableDetails($tableName) : Table
  454. {
  455. $table = parent::listTableDetails($tableName);
  456. $tableCreateSql = $this->getCreateTableSQL($tableName) ?? '';
  457. $comment = $this->parseTableCommentFromSQL($tableName, $tableCreateSql);
  458. if ($comment !== null) {
  459. $table->addOption('comment', $comment);
  460. }
  461. return $table;
  462. }
  463. }