table-builder.vala 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. using Invercargill.DataStructures;
  2. using InvercargillSql.Orm;
  3. namespace InvercargillSql.Migrations {
  4. /**
  5. * Builder for CREATE TABLE operations that provides a fluent API for defining table structure.
  6. *
  7. * This builder is used within MigrationBuilder.create_table() to define columns, constraints,
  8. * and indexes. It follows the builder pattern, returning itself for chaining table-level operations.
  9. *
  10. * Example usage within a migration:
  11. * {{{
  12. * b.create_table("users", t => {
  13. * t.column<string>("username", c => c.type_text().not_null())
  14. * t.column<int>("id", c => c.type_int().primary_key().auto_increment())
  15. * t.index("idx_email").on_column("email")
  16. * t.unique("uq_users_username", {"username"})
  17. * });
  18. * }}}
  19. */
  20. public class TableBuilder : Object {
  21. private MigrationBuilder _parent;
  22. private string _table_name;
  23. private Vector<ColumnDefinition> _columns;
  24. private Vector<TableConstraint> _constraints;
  25. private Vector<CreateIndexOperation> _indexes;
  26. private Vector<MigrationColumnBuilder> _column_builders;
  27. /**
  28. * Creates a new TableBuilder for the specified table.
  29. *
  30. * @param parent the parent MigrationBuilder to return to after table definition
  31. * @param table_name the name of the table to create
  32. */
  33. internal TableBuilder(MigrationBuilder parent, string table_name) {
  34. _parent = parent;
  35. _table_name = table_name;
  36. _columns = new Vector<ColumnDefinition>();
  37. _constraints = new Vector<TableConstraint>();
  38. _indexes = new Vector<CreateIndexOperation>();
  39. _column_builders = new Vector<MigrationColumnBuilder>();
  40. }
  41. /**
  42. * Adds a new column to the table definition.
  43. *
  44. * The generic type T is used to infer the column type. The returned
  45. * MigrationColumnBuilder can be used to configure additional column properties.
  46. *
  47. * Note: The builder is tracked internally to collect FK constraints and index
  48. * operations when build_operation() is called.
  49. *
  50. * @param name the name of the column
  51. * @return a MigrationColumnBuilder for configuring the column
  52. */
  53. public MigrationColumnBuilder column<T>(string name) {
  54. var col = new ColumnDefinition() {
  55. name = name,
  56. column_type = ColumnType.from_gtype(typeof(T)) ?? ColumnType.TEXT
  57. };
  58. _columns.add(col);
  59. var builder = new MigrationColumnBuilder.for_table(this, col);
  60. _column_builders.add(builder);
  61. return builder;
  62. }
  63. /**
  64. * Creates an index on this table.
  65. *
  66. * Use the returned IndexBuilder to specify columns and uniqueness.
  67. *
  68. * @param name the name of the index
  69. * @return an IndexBuilder for configuring the index
  70. *
  71. * Example:
  72. * {{{
  73. * t.index("idx_email").on_column("email")
  74. * t.index("idx_composite").on_columns("email", "name").unique()
  75. * }}}
  76. */
  77. public IndexBuilder index(string name) {
  78. return new IndexBuilder.for_table(this, _table_name, name);
  79. }
  80. /**
  81. * Adds a PRIMARY KEY constraint to the table.
  82. *
  83. * For single-column primary keys, you can also use MigrationColumnBuilder.primary_key().
  84. *
  85. * @param columns the column names that form the primary key
  86. * @return this builder for method chaining
  87. */
  88. public TableBuilder primary_key(string[] columns) {
  89. var constraint = new TableConstraint() {
  90. constraint_type = "PRIMARY KEY"
  91. };
  92. foreach (var col in columns) {
  93. constraint.columns.add(col);
  94. }
  95. _constraints.add(constraint);
  96. return this;
  97. }
  98. /**
  99. * Adds a UNIQUE constraint to the table.
  100. *
  101. * @param name the name of the constraint
  102. * @param columns the column names that must be unique together
  103. * @return this builder for method chaining
  104. */
  105. public TableBuilder unique(string name, string[] columns) {
  106. var constraint = new TableConstraint() {
  107. name = name,
  108. constraint_type = "UNIQUE"
  109. };
  110. foreach (var col in columns) {
  111. constraint.columns.add(col);
  112. }
  113. _constraints.add(constraint);
  114. return this;
  115. }
  116. /**
  117. * Adds a FOREIGN KEY constraint to the table.
  118. *
  119. * @param name the name of the constraint
  120. * @param columns the column names in this table
  121. * @param ref_table the name of the referenced table
  122. * @param ref_columns the column names in the referenced table
  123. * @return this builder for method chaining
  124. */
  125. public TableBuilder foreign_key(string name, string[] columns,
  126. string ref_table, string[] ref_columns) {
  127. var constraint = new TableConstraint() {
  128. name = name,
  129. constraint_type = "FOREIGN KEY",
  130. reference_table = ref_table
  131. };
  132. foreach (var col in columns) {
  133. constraint.columns.add(col);
  134. }
  135. foreach (var col in ref_columns) {
  136. constraint.reference_columns.add(col);
  137. }
  138. _constraints.add(constraint);
  139. return this;
  140. }
  141. /**
  142. * Adds a column definition directly to the table.
  143. *
  144. * This is used internally by MigrationColumnBuilder.
  145. *
  146. * @param col the column definition to add
  147. */
  148. internal void add_column(ColumnDefinition col) {
  149. _columns.add(col);
  150. }
  151. /**
  152. * Adds an index operation to the table.
  153. *
  154. * This is used internally by IndexBuilder.
  155. *
  156. * @param op the index operation to add
  157. */
  158. internal void add_index_operation(CreateIndexOperation op) {
  159. _indexes.add(op);
  160. }
  161. /**
  162. * Returns to the parent MigrationBuilder.
  163. *
  164. * This method is used by MigrationColumnBuilder to navigate back to the
  165. * MigrationBuilder after configuring a column.
  166. *
  167. * @return the parent MigrationBuilder
  168. */
  169. internal MigrationBuilder return_to_migration() {
  170. return _parent;
  171. }
  172. /**
  173. * Builds the CreateTableOperation from this builder's configuration.
  174. *
  175. * This method also collects FK constraints and index operations from column builders.
  176. *
  177. * @return a new CreateTableOperation containing all columns and constraints
  178. */
  179. internal CreateTableOperation build_operation() {
  180. var op = new CreateTableOperation() {
  181. table_name = _table_name
  182. };
  183. foreach (var col in _columns) {
  184. op.columns.add(col);
  185. }
  186. foreach (var constraint in _constraints) {
  187. op.constraints.add(constraint);
  188. }
  189. // Collect FK constraints and indexes from column builders
  190. for (int i = 0; i < (int) _column_builders.length; i++) {
  191. var builder = _column_builders.get(i);
  192. var col = _columns.get(i);
  193. // Collect FK constraint if configured
  194. var fk_builder = builder.fk_builder;
  195. if (fk_builder != null) {
  196. var constraint = fk_builder.build_constraint(col.name, _table_name);
  197. op.constraints.add(constraint);
  198. }
  199. // Collect index operation if configured
  200. if (builder.should_create_index) {
  201. var index_op = new CreateIndexOperation() {
  202. table_name = _table_name,
  203. is_unique = col.is_unique
  204. };
  205. index_op.columns.add(col.name);
  206. // Use custom index name or auto-generate
  207. index_op.index_name = builder.index_name ?? @"idx_$(_table_name)_$(col.name)";
  208. _indexes.add(index_op);
  209. }
  210. }
  211. return op;
  212. }
  213. /**
  214. * Gets all index operations collected by this builder.
  215. *
  216. * This includes indexes created via index() and indexes from column definitions.
  217. *
  218. * @return a Vector of CreateIndexOperation objects
  219. */
  220. internal Vector<CreateIndexOperation> get_index_operations() {
  221. return _indexes;
  222. }
  223. }
  224. }