public void LoadSingleTriggerHistory(BLL.TriggerHistory oTriggerHID)
        {
            StringBuilder sSQL = new StringBuilder();

            sSQL.AppendLine("select")
            .AppendLine("	*")
            .AppendLine(" from")
            .AppendLine("   [dbTriggerhistory] a")
            .AppendLine(" where")
            .AppendFormat(" TriggerHistoryID='{0}'", oTriggerHID.TriggerHistoryID);

            DataTable oDT = base.ExecuteSQLToDatatable(sSQL.ToString());

            if (oDT.Rows.Count > 0)
            {
                oTriggerHID.TriggerID     = (Guid)(oDT.Rows[0]["TriggerID"]);
                oTriggerHID.VersionID     = DBHelper.NullToString(oDT.Rows[0]["VersionID"]);
                oTriggerHID.Description   = DBHelper.NullToString(oDT.Rows[0]["description"]);
                oTriggerHID.Configuration = oDT.Rows[0]["configuration"].ToString();

                oTriggerHID.ChangeDateTime = oDT.Rows[0]["change_date_time"].ToString();
                oTriggerHID.ChangeUID      = oDT.Rows[0]["change_uid"].ToString();
            }
            else
            {
                oTriggerHID = null;
            }
        }
        public void InsertSingleTriggerHistory(BLL.TriggerHistory oTriggerHID)
        {
            StringBuilder sSQL = new StringBuilder();

            sSQL.AppendLine("insert into [dbTriggerhistory] (")
            .AppendLine("   TriggerHistoryID,")
            .AppendLine("   TriggerID,")
            .AppendLine("   VersionID,")
            .AppendLine("   description,")
            .AppendLine("   configuration,")
            .AppendLine("   currentlyexists,")

            .AppendLine("   Change_Date_Time,")
            .AppendLine("   Change_UID) values (")

            .AppendFormat("'{0}',", oTriggerHID.TriggerHistoryID)
            .AppendFormat("'{0}',", oTriggerHID.TriggerID)
            .AppendFormat("'{0}',", oTriggerHID.VersionID)
            .AppendFormat("'{0}',", oTriggerHID.Description)
            .AppendFormat("'{0}',", oTriggerHID.Configuration.Replace("'", "''"))
            .AppendFormat("'{0}',", DBHelper.BoolToBit(oTriggerHID.CurrentlyExists))

            .AppendFormat("'{0}',", oTriggerHID.ChangeDateTime)
            .AppendFormat("'{0}')", oTriggerHID.ChangeUID);

            base.ExecuteNonQuery(sSQL.ToString());
        }
        public void UpdateSingleTriggerHistory(BLL.TriggerHistory oTriggerHID)
        {
            StringBuilder sSQL = new StringBuilder();

            sSQL.AppendLine("update [dbTriggerhistory] set")
            .AppendFormat("   TriggerID='{0}',", oTriggerHID.TriggerID)
            .AppendFormat("   VersionID='{0}',", oTriggerHID.VersionID)
            .AppendFormat("   description='{0}',", oTriggerHID.Description)
            .AppendFormat("   configuration='{0}',", oTriggerHID.Configuration.Replace("'", "''"))
            .AppendFormat("   currentlyexists='{0}',", DBHelper.BoolToBit(oTriggerHID.CurrentlyExists))

            .AppendFormat("   Change_Date_Time='{0}',", oTriggerHID.ChangeDateTime)
            .AppendFormat("   Change_UID='{0}'", oTriggerHID.ChangeUID)
            .AppendLine(" where")
            .AppendFormat(" TriggerHistoryID='{0}'", oTriggerHID.TriggerHistoryID);

            base.ExecuteNonQuery(sSQL.ToString());
        }
        /// <summary>
        /// Scan the database associated with a given connection string.
        /// If no data exists in the current sqlautodoc database for this
        /// server and database, then records are created, otherwise
        /// the current records are used.
        /// </summary>
        /// <param name="sConnectionString"></param>
        public void Scan(string sConnectionString)
        {
            DBL.DBTrigger_Factory oFactory = new DBL.DBTrigger_Factory();

            //Delete any Trigger history records associated with incomplete versions.
            //These records would be left over from a previously uncompleted scan.
            oFactory.DeleteIncompleteHistory(sDBID: m_DBID);

            //Indicate that all known Triggers (in the database) are being re-verified, in case a table has been dropped.
            oFactory.MarkScanningInProgress(sDBID: m_DBID);

            //Get list of Triggers from database
            using (AGENT.DBTriggerAgent_Factory oTriggerAgent = new AGENT.DBTriggerAgent_Factory(sConnectionString: sConnectionString))
            {
                DataTable oAllTriggers = oTriggerAgent.GetList();
                ScanCount(SQLAutoDocLib.UTIL.Constants.TRIGGERNODE, oAllTriggers.Rows.Count);

                //For each Trigger...
                foreach (DataRow oTrigger in oAllTriggers.Rows)
                {
                    string sTriggerName = oTrigger[0].ToString();

                    //Alert that we're beginning the scan
                    ScanBegin(SQLAutoDocLib.UTIL.Constants.TRIGGERNODE, sTriggerName);

                    //Open a connection to the target server
                    oTriggerAgent.OpenConnection();

                    //Create temporary stored Triggers
                    //  These will go away when the connection is closed
                    oTriggerAgent.make_TemporaryStoredTriggers();

                    //Get an xml summary of table schema
                    string sXML = oTriggerAgent.GetTriggerSchema(oTrigger[0].ToString());

                    //Close the connection to the target server
                    oTriggerAgent.CloseConnection();

                    //Record with same name and xml summary already exists in database?
                    bool bIsMatch = false;

                    Guid oFoundTrigger = oFactory.FindWithSchemaMatch(
                        DBID: m_DBID,
                        sName: sTriggerName,
                        sSchemaXML: sXML,
                        IsMatch: out bIsMatch);

                    if (oFoundTrigger == Guid.Empty)
                    {
                        // table not found

                        // create new Trigger record
                        BLL.Trigger oNewTrigger = new BLL.Trigger(DBID: m_DBID);
                        oNewTrigger.Name              = sTriggerName;
                        oNewTrigger.Configuration     = sXML;
                        oNewTrigger.FindFirstVersion  = m_VersionID;
                        oNewTrigger.FindLastVersion   = m_VersionID;
                        oNewTrigger.ScanInProgress    = false;
                        oNewTrigger.ChangedInLastScan = true;

                        oNewTrigger.Save();

                        // create new Trigger version record
                        BLL.TriggerHistory oNewTableHistory = new BLL.TriggerHistory(TriggerID: oNewTrigger.TriggerID, VersionID: m_VersionID);
                        oNewTableHistory.Configuration = sXML;

                        oNewTableHistory.Save();
                        this.ChangesFound = true;

                        ScanNotFound(SQLAutoDocLib.UTIL.Constants.TRIGGERNODE, sTriggerName);
                    }
                    else if (bIsMatch == false)
                    {
                        // Trigger found but schema has changed

                        // update Trigger record
                        BLL.Trigger oNewTrigger = new BLL.Trigger(DBID: m_DBID, TriggerID: oFoundTrigger);
                        oNewTrigger.Load();

                        oNewTrigger.Configuration     = sXML;
                        oNewTrigger.FindLastVersion   = m_VersionID;
                        oNewTrigger.ScanInProgress    = false;
                        oNewTrigger.ChangedInLastScan = true;

                        oNewTrigger.Save();

                        // create new table version record
                        BLL.TriggerHistory oNewTriggerHistory = new BLL.TriggerHistory(TriggerID: oNewTrigger.TriggerID, VersionID: m_VersionID);
                        oNewTriggerHistory.Configuration = sXML;

                        oNewTriggerHistory.Save();
                        this.ChangesFound = true;

                        ScanChanged(SQLAutoDocLib.UTIL.Constants.TRIGGERNODE, sTriggerName);
                    }
                    else
                    {
                        // Trigger found and schema is the same.

                        // update Trigger record
                        BLL.Trigger oNewTrigger = new BLL.Trigger(DBID: m_DBID, TriggerID: oFoundTrigger);
                        oNewTrigger.Load();

                        oNewTrigger.FindLastVersion   = m_VersionID;
                        oNewTrigger.ScanInProgress    = false;
                        oNewTrigger.ChangedInLastScan = false;

                        oNewTrigger.Save();
                    }
                }
            }

            //After scan is complete...
            //Note that any tables not found have been dropped
            DataTable oDT = oFactory.FindUnfound(DBID: m_DBID);

            foreach (DataRow oDroppedTrigger in oDT.Rows)
            {
                Guid oTriggerID = (Guid)oDroppedTrigger["Triggerid"];

                // update Trigger record
                BLL.Trigger oNewTrigger = new BLL.Trigger(DBID: m_DBID, TriggerID: oTriggerID);
                oNewTrigger.Load();

                oNewTrigger.FindLastVersion   = m_VersionID;
                oNewTrigger.ScanInProgress    = false;
                oNewTrigger.ChangedInLastScan = true;
                oNewTrigger.CurrentlyExists   = false;

                oNewTrigger.Save();

                // create new Trigger version record
                BLL.TriggerHistory oNewTriggerHistory = new BLL.TriggerHistory(TriggerID: oNewTrigger.TriggerID, VersionID: m_VersionID);
                oNewTriggerHistory.Configuration   = "";
                oNewTriggerHistory.CurrentlyExists = false;

                oNewTriggerHistory.Save();
                this.ChangesFound = true;

                ScanDeleted(SQLAutoDocLib.UTIL.Constants.TRIGGERNODE, oNewTrigger.Name);
            }
        }