Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

155 строки
4.3KB

  1. <?php
  2. namespace Framework\Database\Migration;
  3. use Framework\Database\Connection\SqliteConnection;
  4. use Framework\Database\Exception\MigrationException;
  5. use Framework\Database\Migration\Field\Field;
  6. use Framework\Database\Migration\Field\BoolField;
  7. use Framework\Database\Migration\Field\DateTimeField;
  8. use Framework\Database\Migration\Field\FloatField;
  9. use Framework\Database\Migration\Field\IdField;
  10. use Framework\Database\Migration\Field\IntField;
  11. use Framework\Database\Migration\Field\StringField;
  12. use Framework\Database\Migration\Field\TextField;
  13. class SqliteMigration extends Migration
  14. {
  15. protected SqliteConnection $connection;
  16. protected string $table;
  17. protected string $type;
  18. public function __construct(SqliteConnection $connection, string $table, string $type)
  19. {
  20. $this->connection = $connection;
  21. $this->table = $table;
  22. $this->type = $type;
  23. }
  24. public function execute()
  25. {
  26. $command = $this->type === 'create' ? '' : 'ALTER TABLE';
  27. $fields = array_map(fn($field) => $this->stringForField($field), $this->fields);
  28. if ($this->type === 'create') {
  29. $fields = join(',' . PHP_EOL, $fields);
  30. $query = "
  31. CREATE TABLE \"{$this->table}\" (
  32. {$fields}
  33. );
  34. ";
  35. }
  36. if ($this->type === 'alter') {
  37. $fields = join(';' . PHP_EOL, $fields);
  38. $query = "
  39. ALTER TABLE \"{$this->table}\"
  40. {$fields};
  41. ";
  42. }
  43. $statement = $this->connection->pdo()->prepare($query);
  44. $statement->execute();
  45. }
  46. private function stringForField(Field $field): string
  47. {
  48. $prefix = '';
  49. if ($this->type === 'alter') {
  50. $prefix = 'ADD COLUMN';
  51. }
  52. if ($field->alter) {
  53. throw new MigrationException('SQLite doesn\'t support altering columns');
  54. }
  55. if ($field instanceof BoolField) {
  56. $template = "{$prefix} \"{$field->name}\" INTEGER";
  57. if (!$field->nullable) {
  58. $template .= " NOT NULL";
  59. }
  60. if ($field->default !== null) {
  61. $default = (int) $field->default;
  62. $template .= " DEFAULT {$default}";
  63. }
  64. return $template;
  65. }
  66. if ($field instanceof DateTimeField) {
  67. $template = "{$prefix} \"{$field->name}\" TEXT";
  68. if (!$field->nullable) {
  69. $template .= " NOT NULL";
  70. }
  71. if ($field->default === 'CURRENT_TIMESTAMP') {
  72. $template .= " DEFAULT CURRENT_TIMESTAMP";
  73. } else if ($field->default !== null) {
  74. $template .= " DEFAULT '{$field->default}'";
  75. }
  76. return $template;
  77. }
  78. if ($field instanceof FloatField) {
  79. $template = "{$prefix} \"{$field->name}\" REAL";
  80. if (!$field->nullable) {
  81. $template .= " NOT NULL";
  82. }
  83. if ($field->default !== null) {
  84. $template .= " DEFAULT {$field->default}";
  85. }
  86. return $template;
  87. }
  88. if ($field instanceof IdField) {
  89. return "{$prefix} \"{$field->name}\" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE";
  90. }
  91. if ($field instanceof IntField) {
  92. $template = "{$prefix} \"{$field->name}\" INTEGER";
  93. if (!$field->nullable) {
  94. $template .= " NOT NULL";
  95. }
  96. if ($field->default !== null) {
  97. $template .= " DEFAULT {$field->default}";
  98. }
  99. return $template;
  100. }
  101. if ($field instanceof StringField || $field instanceof TextField) {
  102. $template = "{$prefix} \"{$field->name}\" TEXT";
  103. if (!$field->nullable) {
  104. $template .= " NOT NULL";
  105. }
  106. if ($field->default !== null) {
  107. $template .= " DEFAULT '{$field->default}'";
  108. }
  109. return $template;
  110. }
  111. throw new MigrationException("Unrecognised field type for {$field->name}");
  112. }
  113. public function dropColumn(string $name): static
  114. {
  115. throw new MigrationException('SQLite doesn\'t support dropping columns');
  116. }
  117. }

Powered by TurnKey Linux.