Пример #1
0
        /// <summary>
        /// 读取整个类型对应的数据库的元数据。
        /// </summary>
        /// <returns></returns>
        public DestinationDatabase Read()
        {
            var tableEntityTypes = this.GetMappingEntityTypes();

            var result = new DestinationDatabase(this._dbSetting.Database);

            foreach (var item in this.IgnoreTables)
            {
                result.IgnoreTables.Add(item);
            }

            if (tableEntityTypes.Count == 0)
            {
                result.Removed = true;
            }
            else
            {
                var reader = new TypesMetaReader
                {
                    Database    = result,
                    Entities    = tableEntityTypes,
                    ReadComment = this.ReadComment,
                    AdditionalPropertiesComments = this.AdditionalPropertiesComments
                };
                reader.Read();
            }

            return(result);
        }
Пример #2
0
        /// <summary>
        /// Begins the transaction and returns the transaction object with open state connection.
        /// Make sure to call EndTransaction method to close transaction
        /// </summary>
        /// <param name="destinationDatabase"></param>
        /// <param name="err">The err.</param>
        /// <param name="givenIsolationLevel">The given isolation level.</param>
        /// <returns></returns>
        public iDB2Transaction BeginTransaction(DestinationDatabase destinationDatabase, ref ErrorObj err, IsolationLevel?givenIsolationLevel = null)
        {
            string          ModuleName = GetModuleNameByDestinationDB(destinationDatabase);
            iDB2Transaction db2Trans   = null;

            this.GetConnectionDetails(Settings.BusinessUnit, "", ModuleName);
            Settings.ModuleName = ModuleName;
            DBDB2Access DBDataAccessEntity = new DBDB2Access();

            DBDataAccessEntity.Settings       = Settings;
            DBDataAccessEntity.CommandElement = _commandElements;
            if (givenIsolationLevel == null)
            {
                db2Trans = DBDataAccessEntity.BeginTransaction(destinationDatabase, ref err);
            }
            else
            {
                db2Trans = DBDataAccessEntity.BeginTransaction(destinationDatabase, ref err);
            }
            if (err.HasError)
            {
                db2Trans = null;
            }
            //'End If
            return(db2Trans);
        }
Пример #3
0
        /// <summary>
        /// Functionality to provide the gateway to access the Data Access Layer
        /// </summary>
        /// <param name="destinationDatabase"></param>
        /// <param name="allowSerialize"></param>
        /// <returns></returns>
        /// <remarks></remarks>
        public ErrorObj SQLAccess(DestinationDatabase destinationDatabase, bool allowSerialize = true)
        {
            string   ModuleName = GetModuleNameByDestinationDB(destinationDatabase);
            ErrorObj err        = new ErrorObj();

            this.GetConnectionDetails(Settings.BusinessUnit, "", ModuleName);
            Settings.ModuleName = ModuleName;
            DBDataAccess DBDataAccessEntity = new DBDataAccess();

            DBDataAccessEntity.Settings       = Settings;
            DBDataAccessEntity.CommandElement = _commandElements;
            //Utilities.LogSQL(DBDataAccessEntity.CommandElement);
            err = DBDataAccessEntity.AccessDatabase();
            if (!err.HasError && !(DBDataAccessEntity.ResultDataSet == null))
            {
                ResultDataSet = DBDataAccessEntity.ResultDataSet;
                // Serialize the transaction
                if (allowSerialize && this.CommandElements.CommandExecutionType == CommandExecution.ExecuteNonQuery)
                {
                    SetConnectionStringParameters();
                    Utilities.SerializeTransaction(this, Settings);
                }
            }
            return(err);
        }
        public void DMDBT_DropDatabase()
        {
            //以下代码不能运行,会提示数据库正在被使用
            using (var context = new DbMigrationContext(DbSetting.FindOrCreate("Test_TestingDataBase")))
            {
                //context.HistoryRepository = new DbHistoryRepository();
                context.RunDataLossOperation = DataLossOperation.All;

                if (context.DatabaseExists() && !(context.DbVersionProvider is EmbadedDbVersionProvider))
                {
                    //context.DeleteDatabase();
                    var database = new DestinationDatabase("Test_TestingDataBase")
                    {
                        Removed = true
                    };
                    context.MigrateTo(database);

                    //历史记录
                    var histories = context.GetHistories();
                    Assert.IsTrue(histories[0] is DropDatabase);

                    //数据库结构
                    Assert.IsTrue(!context.DatabaseExists());
                }

                context.ResetHistory();
            }
        }
Пример #5
0
        /// <summary>
        /// 读取整个类型对应的数据库的元数据。
        /// </summary>
        /// <returns></returns>
        public DestinationDatabase Read()
        {
            var tableEntityTypes = this.GetMappingEntityTypes();

            var result = new DestinationDatabase(this._dbSetting.Database);

            foreach (var item in this.IgnoreTables)
            {
                result.IgnoreTables.Add(item);
            }

            if (tableEntityTypes.Count == 0)
            {
                result.Removed = true;
            }
            else
            {
                var reader = new TypesMetaReader
                {
                    _dbTypeConverter             = DbMigrationProviderFactory.GetDbTypeConverter(_dbSetting.ProviderName),
                    Database                     = result,
                    Entities                     = tableEntityTypes,
                    ReadComment                  = this.ReadComment,
                    IsGeneratingForeignKey       = this.IsGeneratingForeignKey,
                    AdditionalPropertiesComments = this.AdditionalPropertiesComments
                };
                reader.Read();
            }

            return(result);
        }
Пример #6
0
        /// <summary>
        /// Use this for Insert, Update and Delete
        /// Functionality to provide the gateway to access the Data Access Layer with Transaction object
        /// If any exception transaction will be rollbacked here
        /// </summary>
        /// <param name="destinationDatabase"></param>
        /// <param name="givenTransaction">The given transaction.</param>
        /// <returns></returns>
        public ErrorObj DB2Access(DestinationDatabase destinationDatabase, iDB2Transaction givenTransaction)
        {
            string   ModuleName = GetModuleNameByDestinationDB(destinationDatabase);
            ErrorObj err        = new ErrorObj();

            this.GetConnectionDetails(Settings.BusinessUnit, "", ModuleName);
            Settings.ModuleName = ModuleName;
            DBDB2Access DBDataAccessEntity = new DBDB2Access();

            DBDataAccessEntity.Settings       = Settings;
            DBDataAccessEntity.CommandElement = _commandElements;
            err = DBDataAccessEntity.AccessWithTransaction(givenTransaction);
            if (!err.HasError && !(DBDataAccessEntity.ResultDataSet == null))
            {
                ResultDataSet = DBDataAccessEntity.ResultDataSet;
            }
            else
            {
                givenTransaction.Rollback();
                //before call this get the previous error details
                string errMessage = err.ErrorMessage;
                err = EndTransaction(destinationDatabase, givenTransaction);
                if (err.HasError)
                {
                    errMessage = errMessage + ";" + err.ErrorMessage;
                }
                //whether end transaction gives error or not
                //always assign err object has error
                err.HasError     = true;
                err.ErrorMessage = errMessage;
            }
            return(err);
        }
Пример #7
0
        public void DMDBT_CreateDatabase()
        {
            using (var context = new RafyDbMigrationContext("Test_TestingDataBase"))
            {
                context.RunDataLossOperation = DataLossOperation.All;
                if (context.DbSetting.ProviderName != DbSetting.Provider_SQLite)
                {
                    context.HistoryRepository = new DbHistoryRepository();
                }

                if (!context.DatabaseExists())
                {
                    var destination = new DestinationDatabase("Test_TestingDataBase");
                    var tmpTable    = new Table("TestingTable", destination);
                    tmpTable.AddColumn("Id", DbType.Int32, isPrimaryKey: true);
                    tmpTable.AddColumn("Name", DbType.String);
                    destination.Tables.Add(tmpTable);

                    context.MigrateTo(destination);

                    //历史记录
                    if (context.SupportHistory)
                    {
                        var histories = context.GetHistories();
                        Assert.IsTrue(histories.Count == 3);
                        Assert.IsTrue(histories[2] is CreateDatabase);
                    }

                    //数据库结构
                    Assert.IsTrue(context.DatabaseExists());
                }
            }
        }
Пример #8
0
 /// <summary>
 /// Create SQLMulti Threading manager
 /// </summary>
 /// <param name="connectionString"></param>
 /// <param name="maxThread"></param>
 /// <param name="minTaskForThread"></param>
 public SQLMultiThreadingManager(DestinationDatabase destinationDatabase, int maxThread, int minTaskForThread)
 {
     this._destinationDatabase = destinationDatabase;
     this._maxThread           = maxThread;
     this._minTaskForThread    = minTaskForThread;
     this._locker = new object();
 }
Пример #9
0
        public void verify_destination_apollo_database()
        {
            // TODO: Add assumption of DB name & such to README.md.
            var apolloDb      = new ApolloDb("adron", "superSecret!", "secrets.zip", "testks");
            var destinationDb = new DestinationDatabase(apolloDb);

            Assert.True(destinationDb.VerifyConnection());
        }
 /// <summary>
 /// Create new orphan record validation manager
 /// </summary>
 /// <param name="database"></param>
 public OrphanRecordValidationThreadManager(SourceDatabase sourceDatabase, DestinationDatabase destinationDatabase)
 {
     this._sourceDatabase      = sourceDatabase;
     this._destinationDatabase = destinationDatabase;
     this._locker = new object();
     this.MaximumNumberOfThread = 60;
     this.MinimumTaskForThread  = 10;
 }
Пример #11
0
 /// <summary>
 /// Ends the transaction and disposes any unhandled transaction object
 /// </summary>
 /// <param name="err">The error object as ref</param>
 /// <param name="givenTransaction">The given transaction.</param>
 public void EndTransaction(DestinationDatabase destinationDatabase, ref ErrorObj err, SqlTransaction givenTransaction)
 {
     if (!(givenTransaction.Connection == null))
     {
         givenTransaction.Dispose();
         givenTransaction = null;
     }
     err = ConnectionByDestinationDBClose(destinationDatabase);
 }
Пример #12
0
        private string GetModuleNameByDestinationDB(DestinationDatabase destinationDatabase)
        {
            string moduleName = GlobalConstants.DBACCESS_TALENT_TICKETING;

            if (destinationDatabase == TalentSystemDefaults.DestinationDatabase.TALENTTKT)
            {
                moduleName = GlobalConstants.DBACCESS_TALENT_TICKETING;
            }
            return(moduleName);
        }
Пример #13
0
        /// <summary>
        ///  XML Comment
        /// </summary>
        /// <param name="destinationDatabase"></param>
        /// <returns></returns>
        /// <remarks></remarks>
        private ErrorObj ConnectionByDestinationDBClose(DestinationDatabase destinationDatabase)
        {
            ErrorObj err = new ErrorObj();

            if (destinationDatabase == TalentSystemDefaults.DestinationDatabase.TALENTTKT)
            {
                err = TALENTTKTClose();
            }
            return(err);
        }
        public ConversionManager(string srcConnectionString, string destConnectionString, ConversionOption options)
        {
            _packageOutputPath = Path.Combine(ConfigurationManager.AppSettings["PackageOutputFolder"]);
            _options = options;

            _destinationDatabase = new DestinationDatabase(destConnectionString);
            _destinationDatabase.Initialize();
            _sourceDatabase = new SourceDatabase(srcConnectionString);
            _sourceDatabase.Initialize();
            _sourceDatabase.LearnPrimaryKeys(_destinationDatabase, options.ExplicitTableMappings);

            CreatePackageFolders();
        }
Пример #15
0
        public MigrationManager(string sourceConnectionString, string destinationConnectionString, MigrationOptions options)
        {
            _options = options;

            _sourceDatabase = new SourceDatabase(sourceConnectionString);
            _sourceDatabase.Initialize();

            _destinationDatabase = new DestinationDatabase(destinationConnectionString);
            _destinationDatabase.Initialize();

            _sourceDatabase.LearnDestinationDatabaseReference(_destinationDatabase, options.ExplicitTableMappings);

            ConfigIgnoreCircleReferences();
        }
        public MigrationManager(string sourceConnectionString, string destinationConnectionString, MigrationOptions options)
        {
            _options = options;

            _sourceDatabase = new SourceDatabase(sourceConnectionString);
            _sourceDatabase.Initialize();

            _destinationDatabase = new DestinationDatabase(destinationConnectionString);
            _destinationDatabase.Initialize();

            _sourceDatabase.LearnDestinationDatabaseReference(_destinationDatabase, options.ExplicitTableMappings);

            ConfigIgnoreCircleReferences();
        }
Пример #17
0
        /// <summary>
        /// Ends the transaction and close the reader by passing the object to DBDataAccess
        /// </summary>
        /// <param name="destinationDatabase"></param>
        /// <param name="givenTransaction">The given transaction.</param>
        /// <param name="readerToClose">The reader to close.</param>
        /// <returns>Error Object</returns>
        public ErrorObj EndTransaction(DestinationDatabase destinationDatabase, iDB2Transaction givenTransaction, iDB2DataReader readerToClose)
        {
            string   ModuleName = GetModuleNameByDestinationDB(destinationDatabase);
            ErrorObj err        = new ErrorObj();

            this.GetConnectionDetails(Settings.BusinessUnit, "", ModuleName);
            Settings.ModuleName = ModuleName;
            DBDB2Access DBDataAccessEntity = new DBDB2Access();

            DBDataAccessEntity.Settings       = Settings;
            DBDataAccessEntity.CommandElement = _commandElements;
            DBDataAccessEntity.EndTransaction(destinationDatabase, ref err, givenTransaction, readerToClose);
            return(err);
        }
        public ConversionManager(string srcConnectionString, string destConnectionString, ConversionOption options)
        {
            _packageOutputPath = Path.Combine(ConfigurationManager.AppSettings["PackageOutputFolder"]);
            _options           = options;

            _destinationDatabase = new DestinationDatabase(destConnectionString);
            _destinationDatabase.Initialize();
            _sourceDatabase = new SourceDatabase(srcConnectionString);
            _sourceDatabase.Initialize();
            _sourceDatabase.LearnPrimaryKeys(_destinationDatabase, options.ExplicitTableMappings);


            CreatePackageFolders();
        }
Пример #19
0
        /// <summary>
        ///  XML Comment
        /// </summary>
        /// <param name="destinationDatabase"></param>
        /// <returns></returns>
        /// <remarks></remarks>
        private ErrorObj ConnectionByDestinationDBClose(DestinationDatabase destinationDatabase)
        {
            ErrorObj err = new ErrorObj();

            if (destinationDatabase == TalentSystemDefaults.DestinationDatabase.SQL2005)
            {
                err = Sql2005Close();
            }
            else if (destinationDatabase == TalentSystemDefaults.DestinationDatabase.TALENT_CONFIG |
                     destinationDatabase == TalentSystemDefaults.DestinationDatabase.TALENT_DEFINITION)
            {
                err = TalentSQLClose();
            }
            return(err);
        }
 /// <summary>
 /// Create auto mapper thread
 /// </summary>
 /// <param name="manager"></param>
 /// <param name="multiThreadMapper"></param>
 /// <param name="sourceDatabase"></param>
 /// <param name="destinationDatabase"></param>
 /// <param name="databaseSentinel"></param>
 /// <param name="sourceTableName"></param>
 /// <param name="destTableName"></param>
 /// <param name="sourceTableType"></param>
 /// <param name="destTableType"></param>
 /// <param name="manualMapping"></param>
 public AutoMapperThread(MappingManager manager,
                         MultiThreadManager multiThreadMapper,
                         SourceDatabase sourceDatabase, DestinationDatabase destinationDatabase, DatabaseSentinel databaseSentinel,
                         string sourceTableName, string destTableName, Type sourceTableType, Type destTableType, ManualMapping manualMapping)
 {
     this._multiThreadMapper   = multiThreadMapper;
     this._sourceDatabase      = sourceDatabase;
     this._destinationDatabase = destinationDatabase;
     this._databaseSentinel    = databaseSentinel;
     this._sourceTableName     = sourceTableName;
     this._destTableType       = destTableType;
     this._sourceTableType     = sourceTableType;
     this._destTableName       = destTableName;
     this._manualMapping       = manualMapping;
 }
Пример #21
0
        /// <summary>
        /// 不删除库,只是删除其中的所有的表。
        /// </summary>
        public void DeleteAllTables()
        {
            var old = _ManualMigrations;

            _ManualMigrations = new ManualMigrationsContainer();
            try
            {
                var emptyDb = new DestinationDatabase(this.DbSetting.Name);
                this.MigrateTo(emptyDb);
            }
            finally
            {
                _ManualMigrations = old;
            }
        }
Пример #22
0
 /// <summary>
 /// Create multi threading mapper
 /// </summary>
 /// <param name="manager"></param>
 /// <param name="sourceDatabase"></param>
 /// <param name="destinationDatabase"></param>
 /// <param name="databaseSentinel"></param>
 /// <param name="maxThread"></param>
 /// <param name="minTaskForThread"></param>
 public MultiThreadManager(MappingManager manager,
                           SourceDatabase sourceDatabase,
                           DestinationDatabase destinationDatabase,
                           DatabaseSentinel databaseSentinel,
                           int maxThread,
                           int minTaskForThread)
 {
     this._manager             = manager;
     this._sourceDatabase      = sourceDatabase;
     this._destinationDatabase = destinationDatabase;
     this._databaseSentinel    = databaseSentinel;
     this._maxSubThread        = maxThread;
     this._minTaskForThread    = minTaskForThread;
     this._locker = new object();
 }
Пример #23
0
 /// <summary>
 /// Ends the transaction and disposes any unhandled transaction object and closes the reader object
 /// </summary>
 /// <param name="err">The error object as ref</param>
 /// <param name="givenTransaction">The given transaction.</param>
 /// <param name="readerToClose">The reader to close.</param>
 public void EndTransaction(DestinationDatabase destinationDatabase, ref ErrorObj err, SqlTransaction givenTransaction, SqlDataReader readerToClose)
 {
     if (!(givenTransaction.Connection == null))
     {
         givenTransaction.Dispose();
         givenTransaction = null;
     }
     if (!(readerToClose == null))
     {
         if (!(readerToClose.IsClosed))
         {
             readerToClose.Close();
         }
         readerToClose.Dispose();
     }
     err = ConnectionByDestinationDBClose(destinationDatabase);
 }
Пример #24
0
        /// <summary>
        /// Functionality to provide the gateway to access the Data Access Layer for DB2
        /// </summary>
        /// <param name="destinationDatabase"></param>
        /// <returns></returns>
        /// <remarks></remarks>
        public ErrorObj DB2Access(DestinationDatabase destinationDatabase)
        {
            string   ModuleName = GetModuleNameByDestinationDB(destinationDatabase);
            ErrorObj err        = new ErrorObj();

            this.GetConnectionDetails(Settings.BusinessUnit, "", ModuleName);
            Settings.ModuleName = ModuleName;
            DBDB2Access DBDataAccessEntity = new DBDB2Access();

            DBDataAccessEntity.Settings       = Settings;
            DBDataAccessEntity.CommandElement = _commandElements;
            err = DBDataAccessEntity.AccessDatabase();
            if (!err.HasError && !(DBDataAccessEntity.ResultDataSet == null))
            {
                ResultDataSet = DBDataAccessEntity.ResultDataSet;
            }
            return(err);
        }
Пример #25
0
        public ErrorObj DB2DefaultsAccess(DestinationDatabase destinationDatabase, DEDB2Defaults deDB2Defaults)
        {
            string   ModuleName = GetModuleNameByDestinationDB(destinationDatabase);
            ErrorObj err        = new ErrorObj();

            this.GetConnectionDetails(Settings.BusinessUnit, "", ModuleName);
            Settings.ModuleName = ModuleName;
            DB2Defaults dbDB2Access = new DB2Defaults();

            dbDB2Access.Settings      = Settings;
            dbDB2Access.DEDB2Defaults = deDB2Defaults;
            err = dbDB2Access.AccessDatabase();
            if (!err.HasError && !(dbDB2Access.ResultDataSet == null))
            {
                ResultDataSet = dbDB2Access.ResultDataSet;
            }
            return(err);
        }
Пример #26
0
        public void DMDBT_DropDatabase()
        {
            //以下代码不能运行,会提示数据库正在被使用
            using (var context = new RafyDbMigrationContext("Test_TestingDataBase"))
            {
                context.HistoryRepository    = new DbHistoryRepository();
                context.RunDataLossOperation = DataLossOperation.All;

                if (context.DatabaseExists() && !(context.DbVersionProvider is EmbadedDbVersionProvider))
                {
                    //context.DeleteDatabase();
                    var database = new DestinationDatabase("Test_TestingDataBase")
                    {
                        Removed = true
                    };
                    context.MigrateTo(database);

                    //历史记录
                    var histories = context.GetHistories();
                    Assert.IsTrue(histories[0] is DropDatabase);

                    //数据库结构
                    Assert.IsTrue(!context.DatabaseExists());
                }

                context.ResetHistory();
            }

            //using (var context = new RafyDbMigrationContext("Test_TestingDataBase"))
            //{
            //    context.ManualMigrations.Add(new DMDBT_DropDatabase_Migration());
            //    context.MigrateManually();

            //    //历史记录
            //    var histories = context.GetHistories();
            //    Assert.IsTrue(histories[0] is DMDBT_DropDatabase_Migration);

            //    //数据库结构
            //    var database = context.DatabaseMetaReader.Read();
            //    Assert.IsTrue(database == null);

            //    context.ResetHistory();
            //}
        }
Пример #27
0
        /// <summary>
        ///  XML Comment
        /// </summary>
        /// <param name="destinationDatabase"></param>
        /// <returns></returns>
        /// <remarks></remarks>
        private string GetModuleNameByDestinationDB(DestinationDatabase destinationDatabase)
        {
            string moduleName = GlobalConstants.DBACCESS_SQL;

            if (destinationDatabase == TalentSystemDefaults.DestinationDatabase.SQL2005)
            {
                moduleName = GlobalConstants.DBACCESS_SQL;
            }
            else if (destinationDatabase == TalentSystemDefaults.DestinationDatabase.TALENT_ADMIN)
            {
                moduleName = GlobalConstants.DBACCESS_TALENT_ADMIN;
            }
            else if (destinationDatabase == TalentSystemDefaults.DestinationDatabase.TALENT_DEFINITION)
            {
                moduleName = GlobalConstants.DBACCESS_TALENT_DEFINITIONS;
            }
            else if (destinationDatabase == TalentSystemDefaults.DestinationDatabase.TALENT_CONFIG)
            {
                moduleName = GlobalConstants.DBACCESS_TALENT_CONFIGURATION;
            }
            return(moduleName);
        }
Пример #28
0
        /// <summary>
        /// Opens the connection and create and returns the transaction object
        /// </summary>
        /// <param name="err">The error object as ref</param>
        /// <param name="givenIsolationLevel">The given isolation level.</param>
        /// <returns>iDB2Transaction instance</returns>
        public iDB2Transaction BeginTransaction(DestinationDatabase destinationDatabase, ref ErrorObj err, IsolationLevel?givenIsolationLevel = null)
        {
            iDB2Transaction db2Trans = null;

            err = ConnectionByDestinationDBOpen(destinationDatabase);
            if (!err.HasError)
            {
                if (givenIsolationLevel == null)
                {
                    db2Trans = conTALENTTKT.BeginTransaction();
                }
                else
                {
                    db2Trans = conTALENTTKT.BeginTransaction((IsolationLevel)givenIsolationLevel);
                }
            }
            else
            {
                db2Trans = null;
            }
            return(db2Trans);
        }
Пример #29
0
        /// <summary>
        /// Opens the connection and create and returns the transaction object
        /// </summary>
        /// <param name="err">The error object as ref</param>
        /// <param name="givenIsolationLevel">The given isolation level.</param>
        /// <returns>SQLTransaction instance</returns>
        public SqlTransaction BeginTransaction(DestinationDatabase destinationDatabase, ref ErrorObj err, IsolationLevel?givenIsolationLevel = null)
        {
            SqlTransaction SqlTrans = null;

            err = ConnectionByDestinationDBOpen(destinationDatabase);
            if (!err.HasError)
            {
                if (givenIsolationLevel == null)
                {
                    SqlTrans = conSql2005.BeginTransaction();
                }
                else
                {
                    SqlTrans = conSql2005.BeginTransaction((IsolationLevel)givenIsolationLevel);
                }
            }
            else
            {
                SqlTrans = null;
            }
            return(SqlTrans);
        }
Пример #30
0
        /// <summary>
        /// Use this for Insert, Update and Delete
        /// Functionality to provide the gateway to access the Data Access Layer with Transaction object
        /// If any exception transaction will be rollbacked here
        /// </summary>
        /// <param name="destinationDatabase"></param>
        /// <param name="givenTransaction">The given transaction.</param>
        /// <param name="allowSerialize">To allow serialization of TalentDataAccess instance </param>
        /// <returns></returns>
        public ErrorObj SQLAccess(DestinationDatabase destinationDatabase, SqlTransaction givenTransaction, bool allowSerialize = true)
        {
            string   ModuleName = GetModuleNameByDestinationDB(destinationDatabase);
            ErrorObj err        = new ErrorObj();

            this.GetConnectionDetails(Settings.BusinessUnit, "", ModuleName);
            Settings.ModuleName = ModuleName;
            DBDataAccess DBDataAccessEntity = new DBDataAccess();

            DBDataAccessEntity.Settings       = Settings;
            DBDataAccessEntity.CommandElement = _commandElements;
            err = DBDataAccessEntity.AccessWithTransaction(givenTransaction);
            if (!err.HasError && !(DBDataAccessEntity.ResultDataSet == null))
            {
                ResultDataSet = DBDataAccessEntity.ResultDataSet;
                // Serialize the transaction
                if (allowSerialize && this.CommandElements.CommandExecutionType == CommandExecution.ExecuteNonQuery)
                {
                    SetConnectionStringParameters();
                    Utilities.SerializeTransaction(this, Settings);
                }
            }
            else
            {
                givenTransaction.Rollback();
                //before call this get the previous error details
                string errMessage = err.ErrorMessage;
                err = EndTransaction(destinationDatabase, givenTransaction);
                if (err.HasError)
                {
                    errMessage = errMessage + ";" + err.ErrorMessage;
                }
                //whether end transaction gives error or not
                //always assign err object has error
                err.HasError     = true;
                err.ErrorMessage = errMessage;
            }
            return(err);
        }
Пример #31
0
        /// <summary>
        /// Export script to delete orphaned record
        /// </summary>
        /// <param name="filePath"></param>
        public static void ExportScriptToDeleteOrphan(SourceDatabase sourceDatabase, DestinationDatabase destinationDatabase, string filePath)
        {
            //
            // Initialize
            StreamWriter writer = null;

            try
            {
                writer = new StreamWriter(filePath);
            }
            catch (Exception excCreateStream)
            {
                LogService.Log.Error("Can not create stream writer at " + filePath, excCreateStream);
                return;
            }



            //
            // Write out
            writer.WriteLine("USE [" + sourceDatabase.GetDatabaseName() + "]");
            writer.WriteLine("GO");
            foreach (ErrorRecord error in _listErrorRecords)
            {
                if (error.Type == ErrorType.Orphan)
                {
                    string script = GenerateDeleteScript(destinationDatabase, sourceDatabase.GetDatabaseName(), error);
                    writer.WriteLine(script);
                    writer.WriteLine("GO");
                }
            }


            //
            // Clean
            writer.Close();
        }
        /// <summary>
        /// Manual mapping function for vims_EarningsDiff
        /// </summary>
        /// <param name="manager"></param>
        /// <param name="sourceDatabase"></param>
        /// <param name="destinationDatabase"></param>
        public override void Map(MappingManager manager, SourceDatabase sourceDatabase, DestinationDatabase destinationDatabase)
        {
            //
            // Try to get source database name and destination database name
            DbContext sourceDbContext    = sourceDatabase.GetDbContext();
            DbContext destDbContext      = destinationDatabase.GetDbContext();
            string    sourceDatabaseName = sourceDbContext.Database.SqlQuery <string>(@"SELECT DB_NAME() AS [Current Database]").FirstOrDefault();
            string    destDatabaseName   = destDbContext.Database.SqlQuery <string>(@"SELECT DB_NAME() AS [Current Database]").FirstOrDefault();
            string    tableName          = "vims_EarningsDiff";


            //
            // Generate SQL script
            string sqlQuery = @"INSERT INTO {DESTINATION_DATABASE_NAME}.dbo.vims_EarningsDiff(Pol_tran_id, BrRep_EarningWithAdj, Paid, Payable, Liability, Adjustment, BrRep_EarningWithoutAdj, pol_insured, gentrans_name, gencob_Name, short_payment_broker_rep_poriton)" + Environment.NewLine +
                              "SELECT Pol_tran_id, BrRep_EarningWithAdj, Paid, Payable, Liability, Adjustment, BrRep_EarningWithoutAdj, pol_insured, gentrans_name, gencob_Name, short_payment_broker_rep_poriton FROM {SOURCE_DATABASE_NAME}.dbo.vims_EarningsDiff" + Environment.NewLine;

            sqlQuery = sqlQuery.Replace("{SOURCE_DATABASE_NAME}", sourceDatabaseName)
                       .Replace("{DESTINATION_DATABASE_NAME}", destDatabaseName);


            //
            // Exec SQL script or write to file
            if (manager.IsGeneratedScript)
            {
                sqlQuery = "USE [" + destDatabaseName + "]" + Environment.NewLine +
                           "GO" + Environment.NewLine +
                           sqlQuery;
                manager.WriteSQLScript(tableName, sqlQuery, false);
            }
            else
            {
                try
                {
                    destDbContext.Database.ExecuteSqlCommand(sqlQuery);
                }
                catch (Exception excExecScript)
                {
                    LogService.Log.Error("Can not run migration script for " + tableName, excExecScript);
                }
            }
        }