public SyncLogReplica() { LogItems = new SyncLogItemList(); Tables = new SyncTableList(); DataSet = new DataSet(); DataSet.RemotingFormat = SerializationFormat.Binary; Created = DateTime.Now; }
public override SyncTableRelationList GetRelationsForTables(SyncTableList list, SyncDatabase db) { string sql = @"select rc.rdb$constraint_name constraint_name, rc.rdb$relation_name child_table, rc2.rdb$relation_name parent_table, sg.rdb$field_name child_column, sg2.rdb$field_name parent_column from rdb$relation_constraints rc left join rdb$ref_constraints rr on rr.rdb$constraint_name = rc.rdb$constraint_name left join rdb$relation_constraints rc2 on rc2.rdb$constraint_name = rr.rdb$const_name_uq left join rdb$index_segments sg on sg.rdb$index_name = rc.rdb$index_name left join rdb$index_segments sg2 on sg2.rdb$index_name = rc2.rdb$index_name where rc.rdb$constraint_type = 'FOREIGN KEY' union all select '', table_child, table_parent, '', '' from sync_table_dep "; SyncTableRelationList result = new SyncTableRelationList(); using (FbCommand cmd = db.CreateCommand() as FbCommand) { cmd.CommandText = sql; using (IDataReader reader = cmd.ExecuteReader()) while (reader.Read()) { SyncTableRelation rel = new SyncTableRelation(); rel.ParentTable = reader["PARENT_TABLE"].ToString().Trim(); rel.ChildTable = reader["CHILD_TABLE"].ToString().Trim(); rel.ParentColumns = new string [] { reader["PARENT_COLUMN"].ToString().Trim() }; rel.ChildColumns = new string[] { reader["CHILD_COLUMN"].ToString().Trim() }; result.Add(rel); } } return(result); }
public abstract void UpgradeAndDisableTriggers(SyncDatabase db, SyncTableList tables);
public abstract SyncTableRelationList GetRelationsForTables(SyncTableList list, SyncDatabase db);
public override void UpgradeAndDisableTriggers(SyncDatabase db, SyncTableList tables) { UpgradeTriggers(db, tables); ExecSql(db, "select rdb$set_context('USER_TRANSACTION', 'TRG_DISABLE', '1') from rdb$database"); }
private void UpgradeTriggers(SyncDatabase db, SyncTableList tables) { db.Log.Debug(db.FMsg("Выполняется обновление текста триггеров...")); StringBuilder sb = new StringBuilder(100); foreach (SyncTable t in tables) { if (sb.Length > 0) { sb.Append(","); } sb.Append("'"); sb.Append(t.TableName); sb.Append("'"); } if (sb.Length == 0) { return; } string triggers_disable_hook = "/*TRG_DIS*/if (rdb$get_context('USER_TRANSACTION', 'TRG_DISABLE') is not null) then exit;"; // для триггеров архивации другая строчка (их не отключаем string triggers_disable_hook_arc = "/*TRG_DIS*/if (rdb$get_context('USER_TRANSACTION', 'TRG_DISABLE_ARC') is not null) then exit;"; string sql = string.Format(@"select * from rdb$triggers where rdb$system_flag = 0 and rdb$trigger_name not like 'CHECK%' and rdb$relation_name <> 'SYNC_LOG' and rdb$relation_name in ('', {0})", sb.ToString()); FbTransaction trg_trans = db.Transaction as FbTransaction; //(db.Connection as FbConnection).BeginTransaction(FbTransactionOptions.NoWait | FbTransactionOptions.ReadCommitted); using (FbCommand cmd = new FbCommand(string.Empty, db.Connection as FbConnection, trg_trans)) { cmd.CommandText = sql; using (FbDataReader reader = cmd.ExecuteReader()) while (reader.Read()) { string trigger_name = reader["RDB$TRIGGER_NAME"].ToString().Trim(); string trigger_source = reader["RDB$TRIGGER_SOURCE"].ToString().Trim(); string l_trigger_source = trigger_source.ToLower(); string trg_hook = triggers_disable_hook; if (trigger_name.StartsWith("T_SYNC$")) { trg_hook = triggers_disable_hook_arc; } // закладка для кредитного клиента if ((db.Connection as FbConnection).ConnectionString.ToLower().Contains("kredclient") && trigger_source.ToLower().Contains("sp_write_log")) { trg_hook = triggers_disable_hook_arc; } if (!l_trigger_source.Contains(trg_hook.ToLower())) { int pos_begin = l_trigger_source.IndexOf("begin"); if (pos_begin >= 0) { trigger_source = trigger_source.Insert(pos_begin + 5, "\r\n" + trg_hook); if ((trg_hook == triggers_disable_hook_arc) && trigger_source.Contains(triggers_disable_hook)) { trigger_source = trigger_source.Replace(triggers_disable_hook, string.Empty); } using (FbCommand cmd_alter = db.CreateCommand() as FbCommand) { cmd_alter.CommandText = string.Format("ALTER TRIGGER {0}\r\n{1}", trigger_name, trigger_source); try { cmd_alter.ExecuteNonQuery(); trg_trans.CommitRetaining(); } catch (Exception ex) { trg_trans.RollbackRetaining(); db.Log.Warn(string.Format(db.FMsg("Ошибка при обновлении текста триггера ({0}): {1}"), trigger_name, ex.Message)); } } } } } trg_trans.CommitRetaining(); } db.Log.Debug(db.FMsg("Обновление текста триггеров выполнено.")); }