Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

165 lignes
4.7KB

  1. <?php
  2. namespace Framework\Database\Migration;
  3. use Framework\Database\Connection\MysqlConnection;
  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 MysqlMigration extends Migration
  14. {
  15. protected MysqlConnection $connection;
  16. protected string $table;
  17. protected string $type;
  18. protected array $drops = [];
  19. public function __construct(MysqlConnection $connection, string $table, string $type)
  20. {
  21. $this->connection = $connection;
  22. $this->table = $table;
  23. $this->type = $type;
  24. }
  25. public function execute()
  26. {
  27. $fields = array_map(fn($field) => $this->stringForField($field), $this->fields);
  28. $primary = array_filter($this->fields, fn($field) => $field instanceof IdField);
  29. $primaryKey = isset($primary[0]) ? "PRIMARY KEY (`{$primary[0]->name}`)" : '';
  30. if ($this->type === 'create') {
  31. $fields = join(PHP_EOL, array_map(fn($field) => "{$field},", $fields));
  32. $query = "
  33. CREATE TABLE `{$this->table}` (
  34. {$fields}
  35. {$primaryKey}
  36. ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
  37. ";
  38. }
  39. if ($this->type === 'alter') {
  40. $fields = join(PHP_EOL, array_map(fn($field) => "{$field};", $fields));
  41. $drops = join(PHP_EOL, array_map(fn($drop) => "DROP COLUMN `{$drop}`;", $this->drops));
  42. $query = "
  43. ALTER TABLE `{$this->table}`
  44. {$fields}
  45. {$drops}
  46. ";
  47. }
  48. $statement = $this->connection->pdo()->prepare($query);
  49. $statement->execute();
  50. }
  51. private function stringForField(Field $field): string
  52. {
  53. $prefix = '';
  54. if ($this->type === 'alter') {
  55. $prefix = 'ADD';
  56. }
  57. if ($field->alter) {
  58. $prefix = 'MODIFY';
  59. }
  60. if ($field instanceof BoolField) {
  61. $template = "{$prefix} `{$field->name}` tinyint(4)";
  62. if ($field->nullable) {
  63. $template .= " DEFAULT NULL";
  64. }
  65. if ($field->default !== null) {
  66. $default = (int) $field->default;
  67. $template .= " DEFAULT {$default}";
  68. }
  69. return $template;
  70. }
  71. if ($field instanceof DateTimeField) {
  72. $template = "{$prefix} `{$field->name}` datetime";
  73. if ($field->nullable) {
  74. $template .= " DEFAULT NULL";
  75. }
  76. if ($field->default === 'CURRENT_TIMESTAMP') {
  77. $template .= " DEFAULT CURRENT_TIMESTAMP";
  78. } else if ($field->default !== null) {
  79. $template .= " DEFAULT '{$field->default}'";
  80. }
  81. return $template;
  82. }
  83. if ($field instanceof FloatField) {
  84. $template = "{$prefix} `{$field->name}` float";
  85. if ($field->nullable) {
  86. $template .= " DEFAULT NULL";
  87. }
  88. if ($field->default !== null) {
  89. $template .= " DEFAULT '{$field->default}'";
  90. }
  91. return $template;
  92. }
  93. if ($field instanceof IdField) {
  94. return "{$prefix} `{$field->name}` int(11) unsigned NOT NULL AUTO_INCREMENT";
  95. }
  96. if ($field instanceof IntField) {
  97. $template = "{$prefix} `{$field->name}` int(11)";
  98. if ($field->nullable) {
  99. $template .= " DEFAULT NULL";
  100. }
  101. if ($field->default !== null) {
  102. $template .= " DEFAULT '{$field->default}'";
  103. }
  104. return $template;
  105. }
  106. if ($field instanceof StringField) {
  107. $template = "{$prefix} `{$field->name}` varchar(255)";
  108. if ($field->nullable) {
  109. $template .= " DEFAULT NULL";
  110. }
  111. if ($field->default !== null) {
  112. $template .= " DEFAULT '{$field->default}'";
  113. }
  114. return $template;
  115. }
  116. if ($field instanceof TextField) {
  117. return "{$prefix} `{$field->name}` text";
  118. }
  119. throw new MigrationException("Unrecognised field type for {$field->name}");
  120. }
  121. public function dropColumn(string $name): static
  122. {
  123. $this->drops[] = $name;
  124. return $this;
  125. }
  126. }

Powered by TurnKey Linux.