schema-operations.vala 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. using Invercargill.DataStructures;
  2. using InvercargillSql.Dialects;
  3. using InvercargillSql.Orm;
  4. namespace InvercargillSql.Migrations {
  5. /**
  6. * Represents the referential action to take when a referenced row is deleted or updated.
  7. * These actions correspond to SQL standard foreign key actions.
  8. */
  9. public enum ReferentialAction {
  10. NO_ACTION, // default - restricts the action but check is deferred
  11. CASCADE, // cascade the change to dependent rows
  12. SET_NULL, // set referencing column to NULL
  13. SET_DEFAULT, // set referencing column to its default value
  14. RESTRICT; // restricts the action, check is immediate
  15. /**
  16. * Converts the referential action to its SQL string representation.
  17. * @return The SQL keyword(s) for this action
  18. */
  19. public string to_sql() {
  20. switch (this) {
  21. case CASCADE: return "CASCADE";
  22. case SET_NULL: return "SET NULL";
  23. case SET_DEFAULT: return "SET DEFAULT";
  24. case RESTRICT: return "RESTRICT";
  25. default: return "NO ACTION";
  26. }
  27. }
  28. }
  29. public interface SchemaOperation : Object {
  30. public abstract string to_sql(SqlDialect dialect);
  31. }
  32. public class CreateTableOperation : Object, SchemaOperation {
  33. public string table_name { get; set; }
  34. public Vector<ColumnDefinition> columns { get; set; }
  35. public Vector<TableConstraint> constraints { get; set; }
  36. public CreateTableOperation() {
  37. columns = new Vector<ColumnDefinition>();
  38. constraints = new Vector<TableConstraint>();
  39. }
  40. public string to_sql(SqlDialect dialect) {
  41. return dialect.create_table_sql(this);
  42. }
  43. }
  44. public class DropTableOperation : Object, SchemaOperation {
  45. public string table_name { get; set; }
  46. public string to_sql(SqlDialect dialect) {
  47. return dialect.drop_table_sql(this);
  48. }
  49. }
  50. public class AddColumnOperation : Object, SchemaOperation {
  51. public string table_name { get; set; }
  52. public ColumnDefinition column { get; set; }
  53. /**
  54. * Optional foreign key constraint for the column being added.
  55. * When set, the dialect should include REFERENCES clause in the ADD COLUMN SQL.
  56. */
  57. public TableConstraint? foreign_key_constraint { get; set; }
  58. public string to_sql(SqlDialect dialect) {
  59. return dialect.add_column_sql(this);
  60. }
  61. }
  62. public class DropColumnOperation : Object, SchemaOperation {
  63. public string table_name { get; set; }
  64. public string column_name { get; set; }
  65. public string to_sql(SqlDialect dialect) {
  66. return dialect.drop_column_sql(this);
  67. }
  68. }
  69. public class RenameColumnOperation : Object, SchemaOperation {
  70. public string table_name { get; set; }
  71. public string old_name { get; set; }
  72. public string new_name { get; set; }
  73. public string to_sql(SqlDialect dialect) {
  74. return dialect.rename_column_sql(this);
  75. }
  76. }
  77. public class CreateIndexOperation : Object, SchemaOperation {
  78. public string index_name { get; set; }
  79. public string table_name { get; set; }
  80. public Vector<string> columns { get; set; }
  81. public bool is_unique { get; set; }
  82. public CreateIndexOperation() {
  83. columns = new Vector<string>();
  84. }
  85. public string to_sql(SqlDialect dialect) {
  86. return dialect.create_index_sql(this);
  87. }
  88. }
  89. public class DropIndexOperation : Object, SchemaOperation {
  90. public string index_name { get; set; }
  91. public string table_name { get; set; }
  92. public string to_sql(SqlDialect dialect) {
  93. return dialect.drop_index_sql(this);
  94. }
  95. }
  96. public class RawSqlOperation : Object, SchemaOperation {
  97. public string sql { get; set; }
  98. public string to_sql(SqlDialect dialect) {
  99. return sql;
  100. }
  101. }
  102. public class TableConstraint : Object {
  103. public string name { get; set; }
  104. public string constraint_type { get; set; } // PRIMARY KEY, UNIQUE, FOREIGN KEY, etc.
  105. public Vector<string> columns { get; set; }
  106. public string? reference_table { get; set; }
  107. public Vector<string> reference_columns { get; set; }
  108. /**
  109. * The action to take when a referenced row is deleted.
  110. * Default is NO_ACTION per SQL standard.
  111. */
  112. public ReferentialAction on_delete_action { get; set; default = ReferentialAction.NO_ACTION; }
  113. /**
  114. * The action to take when a referenced row is updated.
  115. * Default is NO_ACTION per SQL standard.
  116. */
  117. public ReferentialAction on_update_action { get; set; default = ReferentialAction.NO_ACTION; }
  118. public TableConstraint() {
  119. columns = new Vector<string>();
  120. reference_columns = new Vector<string>();
  121. }
  122. }
  123. /**
  124. * Schema operation for dropping a foreign key constraint.
  125. * Can identify the constraint by name or by column (for auto-generated names).
  126. */
  127. public class DropForeignKeyOperation : Object, SchemaOperation {
  128. /**
  129. * The name of the table containing the foreign key constraint.
  130. */
  131. public string table_name { get; construct set; }
  132. /**
  133. * The name of the constraint to drop (optional).
  134. * Use this when the constraint name is known.
  135. */
  136. public string? constraint_name { get; construct set; }
  137. /**
  138. * The column name to find and drop the FK for (optional).
  139. * Use this for auto-generated constraint names.
  140. */
  141. public string? column_name { get; construct set; }
  142. /**
  143. * Creates a new drop foreign key operation.
  144. * @param table_name The table containing the FK constraint
  145. */
  146. public DropForeignKeyOperation(string table_name) {
  147. Object(table_name: table_name);
  148. }
  149. /**
  150. * Generates the SQL to drop the foreign key constraint.
  151. * Note: SQLite requires table recreation, handled by the dialect.
  152. */
  153. public string to_sql(SqlDialect dialect) {
  154. return dialect.drop_foreign_key_sql(this);
  155. }
  156. }
  157. }