| 79 | } |
| 80 | |
| 81 | void V9Builder::AddV9Table(const std::string& table_name, |
| 82 | const bsatn::AlgebraicType& table_type, |
| 83 | const std::type_info* cpp_type, |
| 84 | bool is_public, |
| 85 | const std::vector<uint16_t>& primary_key, |
| 86 | const std::vector<RawIndexDefV9>& indexes, |
| 87 | const std::vector<RawConstraintDefV9>& constraints, |
| 88 | const std::vector<RawSequenceDefV9>& sequences, |
| 89 | const std::optional<RawScheduleDefV9>& schedule) { |
| 90 | |
| 91 | // Register the table type using the unified system |
| 92 | // Use empty string to let the system extract the struct name from cpp_type |
| 93 | AlgebraicType registered_type = registerType(table_type, "", cpp_type); |
| 94 | |
| 95 | // Extract the typespace index from the registered type |
| 96 | uint32_t type_ref; |
| 97 | if (registered_type.get_tag() == AlgebraicType::Tag::Ref) { |
| 98 | type_ref = registered_type.get<0>(); |
| 99 | } else { |
| 100 | // This shouldn't happen for a table type - tables should always be complex types |
| 101 | fprintf(stderr, "ERROR: Table '%s' did not register as a complex type\n", table_name.c_str()); |
| 102 | type_ref = 0; |
| 103 | } |
| 104 | |
| 105 | // RegisterTable now handles all constraint and index generation, |
| 106 | // so we just use what was passed in directly |
| 107 | |
| 108 | // Create the table definition |
| 109 | RawTableDefV9 table_def; |
| 110 | table_def.name = table_name; |
| 111 | table_def.product_type_ref = type_ref; |
| 112 | table_def.primary_key = primary_key; |
| 113 | table_def.indexes = indexes; // Use indexes passed from RegisterTable |
| 114 | table_def.constraints = constraints; // Use constraints passed from RegisterTable |
| 115 | table_def.sequences = sequences; |
| 116 | table_def.schedule = schedule; |
| 117 | table_def.table_type = TableType::User; // User-defined table |
| 118 | table_def.table_access = is_public ? TableAccess::Public : TableAccess::Private; |
| 119 | |
| 120 | // Check if table already exists to prevent duplicates |
| 121 | auto& module = GetV9Module(); |
| 122 | for (const auto& existing_table : module.tables) { |
| 123 | if (existing_table.name == table_name) { |
| 124 | fprintf(stdout, "DEBUG: Table '%s' already registered, skipping duplicate registration\n", |
| 125 | table_name.c_str()); |
| 126 | return; |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | // Debug: Show all existing table names before adding new one |
| 131 | //fprintf(stdout, "DEBUG: Adding table '%s'. Existing tables: [", table_name.c_str()); |
| 132 | // for (size_t i = 0; i < module.tables.size(); ++i) { |
| 133 | // if (i > 0) fprintf(stdout, ", "); |
| 134 | // fprintf(stdout, "'%s'", module.tables[i].name.c_str()); |
| 135 | // } |
| 136 | // fprintf(stdout, "]\n"); |
| 137 | |
| 138 | // Add table to the global V9 module |