@@ -20514,18 +20514,36 @@ namespace sqlite_orm::internal {
2051420514 }
2051520515
2051620516 void drop_trigger_internal(const std::string& triggerName, bool ifExists) {
20517- std::string sql;
20518- {
20519- std::stringstream ss;
20520- ss << "DROP TRIGGER";
20521- if (ifExists) {
20522- ss << " IF EXISTS";
20523- }
20524- ss << ' ' << quote_identifier(triggerName) << std::flush;
20525- sql = ss.str();
20526- }
2052720517 auto connection = this->get_connection();
20528- this->executor.perform_void_exec(connection.get(), sql.c_str());
20518+ this->drop_trigger_internal(triggerName, ifExists, connection.get());
20519+ }
20520+
20521+ void drop_trigger_internal(const std::string& triggerName, bool ifExists, sqlite3* db) {
20522+ std::stringstream ss;
20523+ ss << "DROP TRIGGER";
20524+ if (ifExists) {
20525+ ss << " IF EXISTS";
20526+ }
20527+ ss << ' ' << quote_identifier(triggerName) << std::flush;
20528+ this->executor.perform_void_exec(db, ss.str().c_str());
20529+ }
20530+
20531+ std::string retrieve_object_sql(sqlite3* db, const std::string& type, const std::string& name) const {
20532+ std::string result;
20533+ std::stringstream ss;
20534+ ss << "SELECT sql FROM sqlite_master WHERE type = " << quote_string_literal(type)
20535+ << " AND name = " << quote_string_literal(name);
20536+ this->executor.perform_exec(
20537+ db,
20538+ ss.str(),
20539+ [](void* userData, int /*argc*/, orm_gsl::zstring* argv, orm_gsl::zstring* /*columnName*/) -> int {
20540+ if (argv[0]) {
20541+ *static_cast<std::string*>(userData) = argv[0];
20542+ }
20543+ return 0;
20544+ },
20545+ &result);
20546+ return result;
2052920547 }
2053020548
2053120549 static int collate_callback(void* argument, int leftLength, const void* lhs, int rightLength, const void* rhs) {
@@ -24224,8 +24242,7 @@ namespace sqlite_orm::internal {
2422424242 std::stringstream ss;
2422524243 ss << "CREATE ";
2422624244
24227- ss << "TRIGGER IF NOT EXISTS " << streaming_identifier(statement.name) << " "
24228- << serialize(statement.base, context);
24245+ ss << "TRIGGER " << streaming_identifier(statement.name) << " " << serialize(statement.base, context);
2422924246 ss << " BEGIN ";
2423024247 iterate_tuple(statement.elements, [&ss, &context](auto& element) {
2423124248 using element_type = polyfill::remove_cvref_t<decltype(element)>;
@@ -26108,8 +26125,19 @@ namespace sqlite_orm::internal {
2610826125 }
2610926126
2611026127 template<class T, class... S>
26111- sync_schema_result schema_status(const trigger_t<T, S...>&, sqlite3*, bool, bool*) {
26112- return sync_schema_result::already_in_sync;
26128+ sync_schema_result schema_status(const trigger_t<T, S...>& trigger, sqlite3* db, bool, bool*) {
26129+ auto dbTriggerSql = this->retrieve_object_sql(db, "trigger", trigger.name);
26130+ if (dbTriggerSql.empty()) {
26131+ return sync_schema_result::new_table_created;
26132+ }
26133+
26134+ const serializer_context<db_objects_type> context{this->db_objects};
26135+ auto storageSql = serialize(trigger, context);
26136+
26137+ if (dbTriggerSql == storageSql) {
26138+ return sync_schema_result::already_in_sync;
26139+ }
26140+ return sync_schema_result::dropped_and_recreated;
2611326141 }
2611426142
2611526143 template<class... Cols>
@@ -26243,11 +26271,16 @@ namespace sqlite_orm::internal {
2624326271 }
2624426272
2624526273 template<class... Cols>
26246- sync_schema_result sync_dbo(const trigger_t<Cols...>& trigger, sqlite3* db, bool) {
26247- const auto res = sync_schema_result::already_in_sync; // TODO Change accordingly
26248- const serializer_context<db_objects_type> context{this->db_objects};
26249- const auto sql = serialize(trigger, context);
26250- this->executor.perform_void_exec(db, sql.c_str());
26274+ sync_schema_result sync_dbo(const trigger_t<Cols...>& trigger, sqlite3* db, bool preserve) {
26275+ auto res = this->schema_status(trigger, db, preserve, nullptr);
26276+ if (res != sync_schema_result::already_in_sync) {
26277+ if (res == sync_schema_result::dropped_and_recreated) {
26278+ this->drop_trigger_internal(trigger.name, true, db);
26279+ }
26280+ const serializer_context<db_objects_type> context{this->db_objects};
26281+ const auto sql = serialize(trigger, context);
26282+ this->executor.perform_void_exec(db, sql.c_str());
26283+ }
2625126284 return res;
2625226285 }
2625326286
0 commit comments