Exemplo n.º 1
0
        /// <summary>
        /// Dont map this table
        /// </summary>
        /// <param name="tableName"></param>
        public void IgnoreTable(string tableName)
        {
            MapTable table = this.GetMapTable(tableName);

            if (table != null)
            {
                table.Map();
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Get list reference of table
        /// </summary>
        /// <param name="tableName"></param>
        /// <returns></returns>
        public List <ReferenceInformation> GetListReference(string tableName)
        {
            //
            // Initialize result
            List <ReferenceInformation> result = new List <ReferenceInformation>();

            //
            // Get list reference
            MapTable table = this.GetMapTable(tableName);

            if (table != null)
            {
                result = table.GetReference();
            }

            //
            // Return result
            return(result);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Initialize list primary key
        /// </summary>
        /// <param name="listTableName"></param>
        private void InitializeListPrimaryKey(List <string> listTableName)
        {
            //
            // Initialize
            this._listTablePrimaryKeyName = new Dictionary <string, List <string> >();


            //
            // Try to get primary key information for each table
            foreach (string table in listTableName)
            {
                try
                {
                    string        sqlQuery = @"SELECT column_name 
                                        FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE 
                                        WHERE OBJECTPROPERTY(OBJECT_ID(constraint_name), 'IsPrimaryKey') = 1
                                        AND table_name = '" + table + "'";
                    List <string> pks      = this._dbContext.Database.SqlQuery <string>(sqlQuery).ToList();
                    this._listTablePrimaryKeyName.Add(table, pks);


                    //
                    // Add new table
                    if (pks.Count > 0)
                    {
                        MapTable mapTable = new MapTable(table, true);
                        this._listTable.Add(mapTable);
                    }
                    else
                    {
                        MapTable mapTable = new MapTable(table, false);
                        this._listTable.Add(mapTable);
                    }
                }
                catch (Exception exc)
                {
                    LogService.Log.Error("Can not get primary key list from table.", exc);
                }
            }
        }
        /// <summary>
        /// Does this table have reference to "reference" table ?
        /// </summary>
        /// <param name="reference"></param>
        /// <returns></returns>
        public bool HasReference(MapTable table)
        {
            //
            // Initialize result
            bool result = false;


            //
            // Check
            foreach (ReferenceInformation reference in this._listReference)
            {
                if (reference.PKTableName.Equals(table.Name))
                {
                    result = true;
                    break;
                }
            }


            //
            // Return result
            return(result);
        }
        /// <summary>
        /// Get next table to map
        /// </summary>
        /// <returns></returns>
        public MapTable NextTable()
        {
            //
            // Initialize result
            MapTable result = null;


            //
            // Check
            foreach (MapTable table in this._listTable)
            {
                if (this.CanMap(table))
                {
                    result = table;
                    break;
                }
            }


            //
            // Return result
            return(result);
        }
Exemplo n.º 6
0
        /// <summary>
        /// Tracking circle references
        /// </summary>
        /// <param name="tableName"></param>
        /// <param name="tracking"></param>
        /// <returns></returns>
        public bool HasCircleReference(string tableName, List <string> tracking)
        {
            //
            // First, try to get table information
            // and try to get table's reference
            MapTable table = this.GetMapTable(tableName);
            List <ReferenceInformation> listReference = table.GetReference();
            bool result = false;


            //
            // If we can not detect circle reference in this level
            // Try to access deeper level (using recursion algorithm)
            List <string> listTableHasCheck = new List <string>();

            listTableHasCheck.Add(tableName);
            foreach (ReferenceInformation reference in listReference)
            {
                bool response = HasCircleReference(tableName, reference, listTableHasCheck, tracking);
                if (response == true)
                {
                    //
                    // Add tracking if needed
                    if (tracking != null)
                    {
                        tracking.Add(tableName);
                    }
                    result = true;
                    break;
                }
            }


            //
            // Return result
            return(result);
        }
Exemplo n.º 7
0
 /// <summary>
 /// Add new table
 /// </summary>
 /// <param name="table"></param>
 public void AddTable(MapTable table)
 {
     this._listTable.Add(table);
 }
Exemplo n.º 8
0
        /// <summary>
        /// Has circle reference
        /// </summary>
        /// <param name="tableName"></param>
        /// <param name="referenceTable"></param>
        /// <param name="listTableHasCheck"></param>
        /// <returns></returns>
        private bool HasCircleReference(string tableName, ReferenceInformation reference, List <string> listTableHasCheck, List <string> tracking)
        {
            //
            // Initialize result
            bool result = false;

            //
            // Check current reference
            if (reference.PKTableName.Equals(tableName))
            {
                result = true;
            }
            else
            {
                //
                // Try to get referenced table
                // And go deeper (using recursion algorithm)
                MapTable referencedTable = this.GetMapTable(reference.PKTableName);
                List <ReferenceInformation> listReference = referencedTable.GetReference();
                listTableHasCheck.Add(referencedTable.Name);
                foreach (ReferenceInformation childReference in listReference)
                {
                    //
                    // Check
                    if (childReference.PKTableName.Equals(tableName))
                    {
                        // Add to tracking list if needed
                        if (tracking != null)
                        {
                            tracking.Add(referencedTable.Name);
                        }
                        result = true;
                        break;
                    }


                    //
                    // If referenced table is checked
                    // We don't need to go further
                    if (listTableHasCheck.Contains(childReference.PKTableName))
                    {
                        continue;
                    }


                    //
                    // Else try to go deeper
                    bool response = this.HasCircleReference(tableName, childReference, listTableHasCheck, tracking);
                    if (response)
                    {
                        // Add to tracking list if needed
                        if (tracking != null)
                        {
                            tracking.Add(referencedTable.Name);
                        }
                        result = true;
                        break;
                    }
                }
            }



            //
            // Return result
            return(result);
        }
        /// <summary>
        /// Check we can map this table right now or not
        /// </summary>
        /// <param name="table"></param>
        protected virtual bool CanMap(MapTable table)
        {
            //
            // Initialize result
            bool result = true;


            //
            // Already mapped or not ?
            // If it is already mapped
            // Return false
            if (table.IsMapped)
            {
                result = false;
            }
            else
            {
                //
                // Get config maporder
                // And config ingore reference for this table
                List <string> configMapOrder        = null;
                List <string> configIngoreReference = null;
                if (this._configMapOrder.ContainsKey(table.Name))
                {
                    configMapOrder = this._configMapOrder[table.Name];
                }
                if (this._configIgnoreReference.ContainsKey(table.Name))
                {
                    configIngoreReference = this._configIgnoreReference[table.Name];
                }


                //
                // Check map order configuration first
                if (configMapOrder != null)
                {
                    foreach (string prerequisiteTable in configMapOrder)
                    {
                        if (this.GetMapTable(prerequisiteTable).IsMapped == false)
                        {
                            return(false);
                        }
                    }
                }


                //
                // Check
                List <ReferenceInformation> listReferences = table.GetReference();
                foreach (ReferenceInformation reference in listReferences)
                {
                    //
                    // Do we ignore this reference ?
                    if (configIngoreReference != null && configIngoreReference.Contains(reference.PKTableName))
                    {
                        continue;
                    }


                    //
                    // If not, try to check
                    // Referenced table is mapped or not
                    // If not, we can not map this table right now
                    MapTable referencedTable = this.GetMapTable(reference.PKTableName);
                    if (referencedTable != null && referencedTable.IsMapped == false)
                    {
                        result = false;
                        break;
                    }
                }
            }


            //
            // Return result
            return(result);
        }
        /// <summary>
        /// Initialize list primary key
        /// </summary>
        /// <param name="listTableName"></param>
        private void InitializeListPrimaryKey(List<string> listTableName)
        {
            //
            // Initialize
            this._listTablePrimaryKeyName = new Dictionary<string, List<string>>();

            //
            // Try to get primary key information for each table
            foreach (string table in listTableName)
            {
                try
                {
                    string sqlQuery = @"SELECT column_name
                                        FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE
                                        WHERE OBJECTPROPERTY(OBJECT_ID(constraint_name), 'IsPrimaryKey') = 1
                                        AND table_name = '" + table + "'";
                    List<string> pks = this._dbContext.Database.SqlQuery<string>(sqlQuery).ToList();
                    this._listTablePrimaryKeyName.Add(table, pks);

                    //
                    // Add new table
                    if (pks.Count > 0)
                    {
                        MapTable mapTable = new MapTable(table, true);
                        this._listTable.Add(mapTable);
                    }
                    else
                    {
                        MapTable mapTable = new MapTable(table, false);
                        this._listTable.Add(mapTable);
                    }
                }
                catch (Exception exc)
                {
                    LogService.Log.Error("Can not get primary key list from table.", exc);
                }
            }
        }
 /// <summary>
 /// Add new table
 /// </summary>
 /// <param name="table"></param>
 public void AddTable(MapTable table)
 {
     this._listTable.Add(table);
 }
Exemplo n.º 12
0
        /// <summary>
        /// Start map database from source to destination database
        /// </summary>
        public void Map()
        {
            //
            // Validate data first
            //if (this._databaseSentinel.ValidateData(this, destination, @"D:\ValidateResult.txt") == false)
            //{
            //    Console.WriteLine("Validation failed. Terminated.");
            //    return;
            //}


            //
            // Initialize for DatabaseSentinel (checking duplicated data)
            // And DataConversionReflection
            LogService.Log.Info("Number of table in destination database : " + this._destinationDatabase.NumberOfTable.ToString());
            LogService.Log.Info("Creating snapshot of database.");
            this._databaseSentinel.Initialize(this._sourceDatabase, this._destinationDatabase);
            this._multiThreadManager    = new MultiThreadManager(this, this._sourceDatabase, this._destinationDatabase, this._databaseSentinel, this.MaximumNumberOfThread, this.MinimumTaskForThread);
            this._sqlMultiThreadManager = new SQLMultiThreadingManager(this._destinationDatabase, this.MaximumNumberOfThread, this.MinimumTaskForThread);


            //
            // Ok, start map
            LogService.Log.Info("Start mapping.");
            while (this._destinationDatabase.IsCompleted() == false)
            {
                //
                // Get next table to map
                MapTable nextTable = this._destinationDatabase.NextTable();
                if (nextTable == null)
                {
                    LogService.Log.Error("Can not get next table to map. Please check circle references again.");
                    break;
                }



                //
                // Is manual mapping or auto mapping
                nextTable.Map();
                LogService.Log.Info("Start mapping " + nextTable.Name);
                try
                {
                    if (this.IsManualMapping(nextTable.Name))
                    {
                        this.MapManual(nextTable.Name);
                    }
                    else
                    {
                        this.MapAutomatically(nextTable.Name);
                    }
                }
                catch (Exception exc)
                {
                    LogService.Log.Error("Error occurs when mapping with table " + nextTable.Name, exc);
                }


                //
                // Append to batch file to run SQL script
                if (this.IsGeneratedScript)
                {
                    this._batFileGenerator.RunSQLFile(Path.GetFileName(this.GetFilePath(nextTable.Name)));
                }
            }


            //
            // Run post migration script
            this.RunPostMigrationScript(this._destinationDatabase);


            //
            // Report, done mapping
            if (this.IsGeneratedScript)
            {
                this._batFileGenerator.ShowMessage("Done.");
                this._batFileGenerator.EchoOn();
            }
            LogService.Log.Info("Mapping completed.");
        }
 /// <summary>
 /// Get reference information
 /// </summary>
 /// <param name="table"></param>
 /// <returns></returns>
 public List <ReferenceInformation> GetReference(MapTable table)
 {
     return(this._listReference.Where(x => x.PKTableName.Equals(table.Name)).ToList());
 }
        /// <summary>
        /// Does this table have reference to "reference" table ?
        /// </summary>
        /// <param name="reference"></param>
        /// <returns></returns>
        public bool HasReference(MapTable table)
        {
            //
            // Initialize result
            bool result = false;

            //
            // Check
            foreach (ReferenceInformation reference in this._listReference)
            {
                if (reference.PKTableName.Equals(table.Name))
                {
                    result = true;
                    break;
                }
            }

            //
            // Return result
            return result;
        }
 /// <summary>
 /// Get reference information
 /// </summary>
 /// <param name="table"></param>
 /// <returns></returns>
 public List<ReferenceInformation> GetReference(MapTable table)
 {
     return this._listReference.Where(x => x.PKTableName.Equals(table.Name)).ToList();
 }
        /// <summary>
        /// Check we can map this table right now or not
        /// </summary>
        /// <param name="table"></param>
        protected virtual bool CanMap(MapTable table)
        {
            //
            // Initialize result
            bool result = true;

            //
            // Already mapped or not ?
            // If it is already mapped
            // Return false
            if (table.IsMapped)
            {
                result = false;
            }
            else
            {
                //
                // Get config maporder
                // And config ingore reference for this table
                List<string> configMapOrder = null;
                List<string> configIngoreReference = null;
                if (this._configMapOrder.ContainsKey(table.Name))
                {
                    configMapOrder = this._configMapOrder[table.Name];
                }
                if (this._configIgnoreReference.ContainsKey(table.Name))
                {
                    configIngoreReference = this._configIgnoreReference[table.Name];
                }

                //
                // Check map order configuration first
                if (configMapOrder != null)
                {
                    foreach (string prerequisiteTable in configMapOrder)
                    {
                        if (this.GetMapTable(prerequisiteTable).IsMapped == false)
                        {
                            return false;
                        }
                    }
                }

                //
                // Check
                List<ReferenceInformation> listReferences = table.GetReference();
                foreach (ReferenceInformation reference in listReferences)
                {
                    //
                    // Do we ignore this reference ?
                    if (configIngoreReference != null && configIngoreReference.Contains(reference.PKTableName))
                    {
                        continue;
                    }

                    //
                    // If not, try to check
                    // Referenced table is mapped or not
                    // If not, we can not map this table right now
                    MapTable referencedTable = this.GetMapTable(reference.PKTableName);
                    if (referencedTable != null && referencedTable.IsMapped == false)
                    {
                        result = false;
                        break;
                    }
                }
            }

            //
            // Return result
            return result;
        }