table-schema.vala 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. using Invercargill.DataStructures;
  2. namespace InvercargillSql.Orm {
  3. /**
  4. * Represents schema metadata for a database table.
  5. *
  6. * TableSchema contains information about the table's columns,
  7. * primary key, and other metadata discovered through database
  8. * introspection.
  9. *
  10. * This class is used by the ORM to discover schema metadata
  11. * at runtime rather than requiring explicit configuration.
  12. */
  13. public class TableSchema : Object {
  14. /**
  15. * The name of the table in the database.
  16. */
  17. public string table_name { get; set; }
  18. /**
  19. * The columns in this table.
  20. */
  21. public Vector<ColumnSchema> columns { get; set; }
  22. /**
  23. * The name of the primary key column, or null if no primary key.
  24. */
  25. public string? primary_key_column { get; set; }
  26. /**
  27. * Creates a new TableSchema instance.
  28. */
  29. public TableSchema() {
  30. columns = new Vector<ColumnSchema>();
  31. }
  32. /**
  33. * Gets the ColumnSchema for a specific column name.
  34. *
  35. * @param name The column name to find
  36. * @return The ColumnSchema, or null if not found
  37. */
  38. public ColumnSchema? get_column(string name) {
  39. foreach (var col in columns) {
  40. if (col.name == name) {
  41. return col;
  42. }
  43. }
  44. return null;
  45. }
  46. /**
  47. * Checks if a column is the primary key.
  48. *
  49. * @param column_name The column name to check
  50. * @return true if the column is the primary key
  51. */
  52. public bool is_primary_key(string column_name) {
  53. return primary_key_column == column_name;
  54. }
  55. /**
  56. * Gets all columns that are auto-incrementing.
  57. *
  58. * @return A vector of auto-increment column schemas
  59. */
  60. public Vector<ColumnSchema> get_auto_increment_columns() {
  61. var result = new Vector<ColumnSchema>();
  62. foreach (var col in columns) {
  63. if (col.auto_increment) {
  64. result.add(col);
  65. }
  66. }
  67. return result;
  68. }
  69. }
  70. /**
  71. * Represents schema metadata for a single database column.
  72. *
  73. * ColumnSchema contains information about a column's type,
  74. * constraints, and other properties discovered through
  75. * database introspection.
  76. */
  77. public class ColumnSchema : Object {
  78. /**
  79. * The name of the column in the database.
  80. */
  81. public string name { get; set; }
  82. /**
  83. * The column type.
  84. */
  85. public ColumnType column_type { get; set; }
  86. /**
  87. * Whether this column is the primary key.
  88. */
  89. public bool is_primary_key { get; set; default = false; }
  90. /**
  91. * Whether this column auto-increments.
  92. */
  93. public bool auto_increment { get; set; default = false; }
  94. /**
  95. * Whether this column is required (NOT NULL).
  96. */
  97. public bool is_required { get; set; default = false; }
  98. /**
  99. * Creates a new ColumnSchema instance.
  100. */
  101. public ColumnSchema() {
  102. column_type = ColumnType.TEXT;
  103. }
  104. }
  105. }