Exemplo n.º 1
0
        public string GenerateTriggerRemoveScript(string connectionString, bool generateOnly,
                                                  DatabaseRemoteUpdate remoteUpdate)
        {
            // get temp file for triggers
            string Result = Path.GetTempFileName();

            try
            {
                _xmlHashUpdates.Clear();

                //connect to local DB
                FbConnection db = new FbConnection(connectionString);
                db.Open();
                try
                {
                    FbTransaction tran = db.BeginTransaction(IsolationLevel.ReadCommitted);
                    try
                    {
                        StreamWriter updateFile = new StreamWriter(Result, false);
                        try
                        {
                            // have any tables been removed from the list since the last time this was run?
                            string SQL = "SELECT TRIM(a.RDB$TRIGGER_NAME) " +
                                         "FROM RDB$TRIGGERS a WHERE ((TRIM(a.RDB$TRIGGER_NAME) LIKE 'REPLICATE$%'));";
                            FbDataReader rdr = null;
                            FbCommand    cmd = new FbCommand(SQL, db, tran);
                            try
                            {
                                rdr = cmd.ExecuteReader();

                                while (rdr.Read())
                                {
                                    updateFile.WriteLine(String.Format("DROP TRIGGER {0};", rdr.GetString(0).Trim()));

                                    string hashDatabase = "D" + Shared.Utilities.HashStringMD5(GetDatabaseName(db));
                                    string hashCode     = "C";
                                    string triggerHash  = "T" + Shared.Utilities.HashStringMD5(
                                        rdr.GetString(0).Trim().Replace("REPLICATE$", ""));

                                    _xmlHashUpdates.Add(String.Format("{0}${1}${2}", hashDatabase, triggerHash, hashCode));
                                }
                            }
                            finally
                            {
                                CloseAndDispose(ref cmd, ref rdr);
                            }

                            if (!generateOnly)
                            {
                                foreach (string update in _xmlHashUpdates)
                                {
                                    string[] parts = update.Split('$');

                                    Shared.XML.SetXMLValue(parts[0], parts[1], parts[2]);
                                }
                            }

                            SQL = "SELECT TRIM(a.RDB$RELATION_NAME) FROM RDB$RELATION_FIELDS a " +
                                  "WHERE a.RDB$FIELD_NAME = 'REPLICATE$HASH'";
                            cmd = new FbCommand(SQL, db, tran);
                            try
                            {
                                rdr = cmd.ExecuteReader();

                                while (rdr.Read())
                                {
                                    updateFile.WriteLine(String.Format("ALTER TABLE {0} DROP REPLICATE$HASH;", rdr.GetString(0)));
                                }
                            }
                            finally
                            {
                                CloseAndDispose(ref cmd, ref rdr);
                            }
                        }
                        finally
                        {
                            updateFile.Flush();
                            updateFile.Close();
                            updateFile = null;
                        }

                        if (generateOnly)
                        {
                            return(Result);
                        }

                        bool tableUpdated = false;

                        if (remoteUpdate.UpdateDatabase(connectionString, Result, -1, ref tableUpdated))
                        {
                            File.Delete(Result);
                        }
                        else
                        {
                            throw new Exception("Error creating replication triggers");
                        }
                    }
                    finally
                    {
                        tran.Rollback();
                        tran.Dispose();
                    }
                }
                finally
                {
                    db.Close();
                    db.Dispose();
                    db = null;
                }
            }
            catch (Exception e)
            {
                Shared.EventLog.Add(e);
                throw;
            }

            return(Result);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Creates new replication triggers based on rules in REPLICATE$TABLES
        /// </summary>
        public bool PrepareDatabaseForReplication(string connectionString, bool dbUpdated,
                                                  bool generateOnly, ref string fileName, DatabaseRemoteUpdate remoteUpdate)
        {
            bool Result = false;

            try
            {
                _xmlHashUpdates.Clear();

                if (dbUpdated)
                {
                    Shared.EventLog.Add("Rebuilding Replication Triggers");
                    fileName = RebuildReplicationTriggers(connectionString, generateOnly);
                    bool tableUpdated = false;

                    if (generateOnly)
                    {
                        return(true);
                    }

                    if (remoteUpdate.UpdateDatabase(connectionString, fileName, -1, ref tableUpdated))
                    {
                        File.Delete(fileName);

                        foreach (string update in _xmlHashUpdates)
                        {
                            string[] parts = update.Split('$');

                            Shared.XML.SetXMLValue(parts[0], parts[1], parts[2]);
                        }
                    }
                    else
                    {
                        throw new Exception("Error creating replication triggers");
                    }

                    Result = true;
                }
            }
            catch (Exception err)
            {
                Shared.EventLog.Add(err);
            }

            return(Result);
        }