|
| 1 | +#include "sql_engine/in_memory_catalog.h" |
| 2 | +#include <algorithm> |
| 3 | +#include <cstring> |
| 4 | + |
| 5 | +namespace sql_engine { |
| 6 | + |
| 7 | +std::string InMemoryCatalog::to_lower(const char* s, size_t len) { |
| 8 | + std::string result(len, '\0'); |
| 9 | + for (size_t i = 0; i < len; ++i) { |
| 10 | + char c = s[i]; |
| 11 | + if (c >= 'A' && c <= 'Z') c += 32; |
| 12 | + result[i] = c; |
| 13 | + } |
| 14 | + return result; |
| 15 | +} |
| 16 | + |
| 17 | +std::string InMemoryCatalog::make_key(const char* schema, const char* table) { |
| 18 | + std::string key; |
| 19 | + if (schema && schema[0] != '\0') { |
| 20 | + size_t slen = std::strlen(schema); |
| 21 | + key = to_lower(schema, slen); |
| 22 | + key += '.'; |
| 23 | + } |
| 24 | + size_t tlen = std::strlen(table); |
| 25 | + key += to_lower(table, tlen); |
| 26 | + return key; |
| 27 | +} |
| 28 | + |
| 29 | +void InMemoryCatalog::add_table(const char* schema, const char* table, |
| 30 | + std::initializer_list<ColumnDef> columns) { |
| 31 | + std::string key = make_key(schema, table); |
| 32 | + |
| 33 | + TableData& td = tables_[key]; |
| 34 | + td.schema_str = schema ? schema : ""; |
| 35 | + td.table_str = table; |
| 36 | + td.column_names.clear(); |
| 37 | + td.columns.clear(); |
| 38 | + |
| 39 | + uint16_t ordinal = 0; |
| 40 | + for (const auto& def : columns) { |
| 41 | + td.column_names.emplace_back(def.name); |
| 42 | + ColumnInfo ci{}; |
| 43 | + // StringRef will point into the stored string -- set after push_back |
| 44 | + ci.type = def.type; |
| 45 | + ci.ordinal = ordinal++; |
| 46 | + ci.nullable = def.nullable; |
| 47 | + td.columns.push_back(ci); |
| 48 | + } |
| 49 | + |
| 50 | + // Fix up StringRef pointers (must be done after all push_backs to avoid reallocation) |
| 51 | + for (size_t i = 0; i < td.columns.size(); ++i) { |
| 52 | + td.columns[i].name = sql_parser::StringRef{ |
| 53 | + td.column_names[i].c_str(), |
| 54 | + static_cast<uint32_t>(td.column_names[i].size()) |
| 55 | + }; |
| 56 | + } |
| 57 | + |
| 58 | + // Set up TableInfo |
| 59 | + td.info.schema_name = sql_parser::StringRef{ |
| 60 | + td.schema_str.c_str(), |
| 61 | + static_cast<uint32_t>(td.schema_str.size()) |
| 62 | + }; |
| 63 | + td.info.table_name = sql_parser::StringRef{ |
| 64 | + td.table_str.c_str(), |
| 65 | + static_cast<uint32_t>(td.table_str.size()) |
| 66 | + }; |
| 67 | + td.info.columns = td.columns.data(); |
| 68 | + td.info.column_count = static_cast<uint16_t>(td.columns.size()); |
| 69 | + |
| 70 | + // Also register under unqualified name if schema is provided |
| 71 | + if (schema && schema[0] != '\0') { |
| 72 | + std::string unqualified = to_lower(table, std::strlen(table)); |
| 73 | + // Only set unqualified key if it doesn't already exist or points to this same table |
| 74 | + if (tables_.find(unqualified) == tables_.end()) { |
| 75 | + // Store a reference entry: we don't duplicate, we just store another |
| 76 | + // entry pointing to the same data. But since we use value semantics, |
| 77 | + // we need to find via the qualified key. Instead, let get_table(name) |
| 78 | + // do a scan. Actually, for simplicity, store duplicate. |
| 79 | + // Better approach: unqualified lookup scans all entries. |
| 80 | + } |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +void InMemoryCatalog::drop_table(const char* schema, const char* table) { |
| 85 | + std::string key = make_key(schema, table); |
| 86 | + tables_.erase(key); |
| 87 | +} |
| 88 | + |
| 89 | +const TableInfo* InMemoryCatalog::get_table(sql_parser::StringRef name) const { |
| 90 | + // Try exact (unqualified) lookup |
| 91 | + std::string lower = to_lower(name.ptr, name.len); |
| 92 | + auto it = tables_.find(lower); |
| 93 | + if (it != tables_.end()) return &it->second.info; |
| 94 | + |
| 95 | + // Scan for matching table_name (for qualified entries) |
| 96 | + for (const auto& pair : tables_) { |
| 97 | + const TableData& td = pair.second; |
| 98 | + if (td.info.table_name.equals_ci(name.ptr, name.len)) { |
| 99 | + return &td.info; |
| 100 | + } |
| 101 | + } |
| 102 | + return nullptr; |
| 103 | +} |
| 104 | + |
| 105 | +const TableInfo* InMemoryCatalog::get_table(sql_parser::StringRef schema, |
| 106 | + sql_parser::StringRef table) const { |
| 107 | + // Build qualified key |
| 108 | + std::string key = to_lower(schema.ptr, schema.len); |
| 109 | + key += '.'; |
| 110 | + key += to_lower(table.ptr, table.len); |
| 111 | + auto it = tables_.find(key); |
| 112 | + if (it != tables_.end()) return &it->second.info; |
| 113 | + return nullptr; |
| 114 | +} |
| 115 | + |
| 116 | +const ColumnInfo* InMemoryCatalog::get_column(const TableInfo* table, |
| 117 | + sql_parser::StringRef column_name) const { |
| 118 | + if (!table) return nullptr; |
| 119 | + for (uint16_t i = 0; i < table->column_count; ++i) { |
| 120 | + if (table->columns[i].name.equals_ci(column_name.ptr, column_name.len)) { |
| 121 | + return &table->columns[i]; |
| 122 | + } |
| 123 | + } |
| 124 | + return nullptr; |
| 125 | +} |
| 126 | + |
| 127 | +} // namespace sql_engine |
0 commit comments