14#define ORM_LOG_ERROR(context, message) \
15 std::cerr << "[ORM:" << context << "] Error: " << message << std::endl
16#define ORM_LOG_WARNING(message) \
17 std::cerr << "[ORM] Warning: " << message << std::endl
18#define ORM_LOG_INFO(message) \
19 std::cout << "[ORM] Info: " << message << std::endl
25 const std::string& type_name,
27 const std::string& index_name,
28 const std::string& foreign_table,
29 const std::string& foreign_field)
31 , type_name_(type_name)
32 , constraints_(constraints)
33 , index_name_(index_name)
34 , foreign_table_(foreign_table)
35 , foreign_field_(foreign_field)
41 std::ostringstream oss;
50 oss <<
"DOUBLE PRECISION";
52 oss <<
"VARCHAR(255)";
55 }
else if (
type_name_.find(
"time_point") != std::string::npos) {
63 oss <<
" PRIMARY KEY";
66 oss <<
" AUTO_INCREMENT";
75 oss <<
" DEFAULT CURRENT_TIMESTAMP";
83 : table_name_(table_name)
96 return field.is_primary_key();
98 return (it !=
fields_.end()) ? &(*it) :
nullptr;
103 std::vector<const field_metadata*> indexes;
104 for (
const auto& field :
fields_) {
105 if (field.has_index()) {
106 indexes.push_back(&field);
114 std::vector<const field_metadata*> foreign_keys;
115 for (
const auto& field :
fields_) {
116 if (field.is_foreign_key()) {
117 foreign_keys.push_back(&field);
125 std::ostringstream oss;
126 oss <<
"CREATE TABLE IF NOT EXISTS " <<
table_name_ <<
" (\n";
129 for (
const auto& field :
fields_) {
130 if (!first) oss <<
",\n";
131 oss <<
" " << field.to_sql_definition();
137 for (
const auto* fk : foreign_keys) {
138 oss <<
",\n FOREIGN KEY (" << fk->name() <<
") REFERENCES "
139 << fk->foreign_table() <<
"(" << fk->foreign_field() <<
")";
148 std::ostringstream oss;
151 for (
const auto*
index : indexes) {
152 if (!
index->index_name().empty()) {
153 oss <<
"CREATE INDEX IF NOT EXISTS " <<
index->index_name()
156 oss <<
"CREATE INDEX IF NOT EXISTS idx_" <<
table_name_
158 <<
"(" <<
index->name() <<
");\n";
169 ORM_LOG_ERROR(
"create_tables",
"Database connection is null");
176 std::string create_sql = metadata->create_table_sql();
177 auto result = db->execute_query(create_sql);
178 if (result.is_err()) {
179 ORM_LOG_ERROR(
"create_tables",
"Failed to create table: " + name);
184 std::string index_sql = metadata->create_indexes_sql();
185 if (!index_sql.empty()) {
186 auto index_result = db->execute_query(index_sql);
187 if (index_result.is_err()) {
188 ORM_LOG_ERROR(
"create_tables",
"Failed to create indexes for table: " + name);
194 }
catch (
const std::exception& e) {
195 ORM_LOG_ERROR(
"create_tables", std::string(
"Exception during table creation: ") + e.what());
209 std::string drop_sql =
"DROP TABLE IF EXISTS " + metadata->table_name();
210 auto result = db->execute_query(drop_sql);
211 if (result.is_err()) {
212 ORM_LOG_ERROR(
"drop_tables",
"Failed to drop table: " + name);
217 }
catch (
const std::exception& e) {
218 ORM_LOG_ERROR(
"drop_tables", std::string(
"Exception during table dropping: ") + e.what());
234 ORM_LOG_ERROR(
"sync_schema",
"Failed to drop existing tables");
244 }
catch (
const std::exception& e) {
245 ORM_LOG_ERROR(
"sync_schema", std::string(
"Exception during schema sync: ") + e.what());
bool drop_tables(std::shared_ptr< core::database_backend > db)
bool sync_schema(std::shared_ptr< core::database_backend > db)
std::unordered_map< std::string, std::unique_ptr< entity_metadata > > metadata_cache_
bool create_tables(std::shared_ptr< core::database_backend > db)
#define ORM_LOG_ERROR(context, message)