Пример #1
0
 public void CatalogCompare()
 {
     Database.Model model1   = GetModel(ConnectionString1);
     Database.Model model2   = GetModel(ConnectionString2);
     var            comparer = new SqlComparer();
     var            catalogComparisonResult = comparer.Compare(model1.DefaultServer.DefaultCatalog, model2.DefaultServer.DefaultCatalog, null);
 }
        public static string GetSqlComparor(this SqlComparer c)
        {
            string sql = null;

            if (comparerLookup.TryGetValue(c, out sql) == false)
            {
                return(string.Empty);
            }

            return(sql);
        }
Пример #3
0
        public void NavigatorTest()
        {
            Database.Model model1   = GetModel(ConnectionString1);
            Database.Model model2   = GetModel(ConnectionString2);
            Catalog        catalog1 = model1.DefaultServer.DefaultCatalog;
            Catalog        catalog2 = model2.DefaultServer.DefaultCatalog;
            var            result   = new SqlComparer().Compare(catalog1, catalog2, null);

            navigator = new ComparisonResultNavigator(result);
            CheckCatalog(catalog1, true);
            CheckCatalog(catalog2, false);
        }
Пример #4
0
        private void RestoreFromDump(IDataReadOperator dataReader)
        {
            var keyBase  = KeyHelper.GetDatabaseSchema();
            var keys     = dataReader.GetEntries(keyBase).Select(r => Path.GetFileName(r)).ToList();
            var upgrades = new List <string>();

            if (!string.IsNullOrEmpty(UpgradesPath) && Directory.Exists(UpgradesPath))
            {
                upgrades = Directory.GetFiles(UpgradesPath).ToList();
            }

            var stepscount = keys.Count * 2 + upgrades.Count;

            SetStepsCount(ProcessStorage ? stepscount + 1 : stepscount);

            if (ProcessStorage)
            {
                var storageModules = StorageFactoryConfig.GetModuleList(ConfigPath).Where(IsStorageModuleAllowed);
                var tenants        = TenantManager.GetTenants(false);

                stepscount += storageModules.Count() * tenants.Count;

                SetStepsCount(stepscount + 1);

                DoDeleteStorage(storageModules, tenants);
            }
            else
            {
                SetStepsCount(stepscount);
            }

            for (var i = 0; i < keys.Count; i += TasksLimit)
            {
                var tasks = new List <Task>(TasksLimit * 2);

                for (var j = 0; j < TasksLimit && i + j < keys.Count; j++)
                {
                    var key1 = Path.Combine(KeyHelper.GetDatabaseSchema(), keys[i + j]);
                    tasks.Add(RestoreFromDumpFile(dataReader, key1).ContinueWith(r => RestoreFromDumpFile(dataReader, KeyHelper.GetDatabaseData(key1.Substring(keyBase.Length + 1)))));
                }

                Task.WaitAll(tasks.ToArray());
            }

            var comparer = new SqlComparer();

            foreach (var u in upgrades.OrderBy(Path.GetFileName, comparer))
            {
                RunMysqlFile(u, true);
                SetStepCompleted();
            }
        }
Пример #5
0
        public void CompareEqualNodes()
        {
            Database.Model model1   = GetModel(ConnectionString1);
            var            comparer = new SqlComparer();

            comparer.Compare(model1.DefaultServer.DefaultCatalog, model1.DefaultServer.DefaultCatalog, null);
            var singleResult = comparer.Compare(model1.DefaultServer.DefaultCatalog, model1.DefaultServer.DefaultCatalog, null);

            foreach (IComparisonResult comparisonResult in singleResult.NestedComparisons)
            {
                Assert.IsTrue(comparisonResult.ResultType == ComparisonResultType.Unchanged);
            }
        }
 private SqlColumn FindColumnWithExpression(IEnumerable <SqlColumn> columns, SqlExpression expr)
 {
     foreach (SqlColumn c in columns)
     {
         if (c == expr)
         {
             return(c);
         }
         if (SqlComparer.AreEqual(c.Expression, expr))
         {
             return(c);
         }
     }
     return(null);
 }
Пример #7
0
        internal override SqlSelect VisitSelect(SqlSelect select)
        {
            select.From  = this.VisitSource(select.From);
            select.Where = this.VisitExpression(select.Where);
            for (int i = 0, n = select.GroupBy.Count; i < n; i++)
            {
                select.GroupBy[i] = this.VisitExpression(select.GroupBy[i]);
            }
            // remove duplicate group expressions
            for (int i = select.GroupBy.Count - 1; i >= 0; i--)
            {
                for (int j = i - 1; j >= 0; j--)
                {
                    if (SqlComparer.AreEqual(select.GroupBy[i], select.GroupBy[j]))
                    {
                        select.GroupBy.RemoveAt(i);
                        break;
                    }
                }
            }
            select.Having = this.VisitExpression(select.Having);
            for (int i = 0, n = select.OrderBy.Count; i < n; i++)
            {
                select.OrderBy[i].Expression = this.VisitExpression(select.OrderBy[i].Expression);
            }
            // remove duplicate order expressions
            if (select.OrderBy.Count > 0)
            {
                this.equalizer.BuildEqivalenceMap(select.From);

                for (int i = select.OrderBy.Count - 1; i >= 0; i--)
                {
                    for (int j = i - 1; j >= 0; j--)
                    {
                        if (this.equalizer.AreEquivalent(select.OrderBy[i].Expression, select.OrderBy[j].Expression))
                        {
                            select.OrderBy.RemoveAt(i);
                            break;
                        }
                    }
                }
            }
            select.Top       = this.VisitExpression(select.Top);
            select.Row       = (SqlRow)this.Visit(select.Row);
            select.Selection = this.VisitExpression(select.Selection);
            return(select);
        }
        internal bool AreEquivalent(SqlExpression e1, SqlExpression e2)
        {
            if (SqlComparer.AreEqual(e1, e2))
            {
                return(true);
            }

            SqlColumnRef cr1 = e1 as SqlColumnRef;
            SqlColumnRef cr2 = e2 as SqlColumnRef;

            if (cr1 != null && cr2 != null)
            {
                SqlColumn c1 = cr1.GetRootColumn();
                SqlColumn c2 = cr2.GetRootColumn();
                SqlColumn r;
                return(this._map.TryGetValue(c1, out r) && r == c2);
            }

            return(false);
        }
        private void RestoreFromDump(IDataReadOperator dataReader, DbFactory dbFactory)
        {
            var keyBase  = KeyHelper.GetDatabaseSchema();
            var keys     = dataReader.Entries.Where(r => r.StartsWith(keyBase)).ToList();
            var upgrades = new List <string>();

            if (!string.IsNullOrEmpty(UpgradesPath) && Directory.Exists(UpgradesPath))
            {
                upgrades = Directory.GetFiles(UpgradesPath).ToList();
            }

            var stepscount = keys.Count * 2 + upgrades.Count;

            SetStepsCount(ProcessStorage ? stepscount + 1 : stepscount);

            for (var i = 0; i < keys.Count; i += TasksLimit)
            {
                var tasks = new List <Task>(TasksLimit * 2);

                for (var j = 0; j < TasksLimit && i + j < keys.Count; j++)
                {
                    var key1 = keys[i + j];
                    tasks.Add(RestoreFromDumpFile(dataReader, key1).ContinueWith(r => RestoreFromDumpFile(dataReader, KeyHelper.GetDatabaseData(key1.Substring(keyBase.Length + 1)))));
                }

                Task.WaitAll(tasks.ToArray());
            }

            var comparer = new SqlComparer();

            foreach (var u in upgrades.OrderBy(Path.GetFileName, comparer))
            {
                RunMysqlFile(dbFactory, u, true);
                SetStepCompleted();
            }
        }
        public static SqlComparer ParseComparer(string s)
        {
            SqlComparer comparer = SqlComparer.None;

            foreach (char c in s)
            {
                switch (c)
                {
                case '>':
                    comparer |= SqlComparer.GreaterThan;
                    break;

                case '<':
                    comparer |= SqlComparer.LessThan;
                    break;

                case '=':
                    comparer |= SqlComparer.Equal;
                    break;

                case '&':
                    comparer |= SqlComparer.BitwiseAnd;
                    break;

                case '|':
                    comparer |= SqlComparer.BitwiseOr;
                    break;

                case '^':
                    comparer |= SqlComparer.BitwiseExclusiveOr;
                    break;
                }
            }

            return(comparer);
        }
Пример #11
0
        private void RestoreFromDump(IDataReadOperator dataReader)
        {
            var keyBase  = KeyHelper.GetDatabaseSchema();
            var keys     = dataReader.Entries.Where(r => r.StartsWith(keyBase)).ToList();
            var upgrades = new List <string>();

            var upgradesPath = Path.GetFullPath(Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), UpgradesPath));

            if (!string.IsNullOrEmpty(upgradesPath) && Directory.Exists(upgradesPath))
            {
                upgrades = Directory.GetFiles(upgradesPath).ToList();
            }

            var stepscount = keys.Count * 2 + upgrades.Count;

            if (ProcessStorage)
            {
                var storageModules = StorageFactory.GetModuleList(ConfigPath).Where(IsStorageModuleAllowed);
                var tenants        = CoreContext.TenantManager.GetTenants(false);

                stepscount += storageModules.Count() * tenants.Count;

                SetStepsCount(stepscount + 1);

                DoDeleteStorage(storageModules, tenants);
            }
            else
            {
                SetStepsCount(stepscount);
            }


            for (var i = 0; i < keys.Count; i += TasksLimit)
            {
                var tasks = new List <Task>(TasksLimit * 2);

                for (var j = 0; j < TasksLimit && i + j < keys.Count; j++)
                {
                    var key1 = keys[i + j];
                    tasks.Add(RestoreFromDumpFile(dataReader, key1).ContinueWith(r => RestoreFromDumpFile(dataReader, KeyHelper.GetDatabaseData(key1.Substring(keyBase.Length + 1)))));
                }

                Task.WaitAll(tasks.ToArray());
            }

            var comparer = new SqlComparer();

            foreach (var u in upgrades.OrderBy(Path.GetFileName, comparer))
            {
                using (var s = File.OpenRead(u))
                {
                    if (u.Contains(".upgrade."))
                    {
                        RunMysqlFile(s, null).Wait();
                    }
                    else if (u.Contains(".data") || u.Contains(".upgrade"))
                    {
                        RunMysqlProcedure(s).Wait();
                    }
                    else
                    {
                        RunMysqlFile(s).Wait();
                    }
                }

                SetStepCompleted();
            }
        }
Пример #12
0
        private void RestoreFromDump(IDataReadOperator dataReader)
        {
            var keyBase  = KeyHelper.GetDatabaseSchema();
            var keys     = dataReader.GetEntries(keyBase).Select(r => Path.GetFileName(r)).ToList();
            var dbs      = dataReader.GetDirectories("").Where(r => Path.GetFileName(r).StartsWith("mailservice")).Select(r => Path.GetFileName(r)).ToList();
            var upgrades = new List <string>();

            var upgradesPath = Path.GetFullPath(Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), UpgradesPath));

            if (!string.IsNullOrEmpty(upgradesPath) && Directory.Exists(upgradesPath))
            {
                upgrades = Directory.GetFiles(upgradesPath).ToList();
            }

            var stepscount = keys.Count * 2 + upgrades.Count;

            Dictionary <string, List <string> > databases = new Dictionary <string, List <string> >();

            foreach (var db in dbs)
            {
                var keys1 = dataReader.GetEntries(db + "/" + keyBase).Select(k => Path.GetFileName(k)).ToList();
                stepscount += keys1.Count() * 2;
                databases.Add(db, keys1);
            }

            if (ProcessStorage)
            {
                var storageModules = StorageFactory.GetModuleList(ConfigPath).Where(IsStorageModuleAllowed);
                var tenants        = CoreContext.TenantManager.GetTenants(false);

                stepscount += storageModules.Count() * tenants.Count;

                SetStepsCount(stepscount + 1);

                DoDeleteStorage(storageModules, tenants);
            }
            else
            {
                SetStepsCount(stepscount);
            }

            for (var i = 0; i < keys.Count; i += TasksLimit)
            {
                var tasks = new List <Task>(TasksLimit * 2);

                for (var j = 0; j < TasksLimit && i + j < keys.Count; j++)
                {
                    var key1 = Path.Combine(KeyHelper.GetDatabaseSchema(), keys[i + j]);
                    var key2 = Path.Combine(KeyHelper.GetDatabaseData(), keys[i + j]);
                    tasks.Add(RestoreFromDumpFile(dataReader, key1, key2));
                }

                Task.WaitAll(tasks.ToArray());
            }

            using (var dbManager = DbManager.FromHttpContext("default", 100000))
            {
                dbManager.ExecuteList("select id, connection_string from mail_server_server").ForEach(r =>
                {
                    RegisterDatabase((int)r[0], JsonConvert.DeserializeObject <Dictionary <string, object> >(Convert.ToString(r[1]))["DbConnection"].ToString());
                });
            }

            foreach (var database in databases)
            {
                for (var i = 0; i < database.Value.Count; i += TasksLimit)
                {
                    var tasks = new List <Task>(TasksLimit * 2);

                    for (var j = 0; j < TasksLimit && i + j < database.Value.Count; j++)
                    {
                        var key1 = Path.Combine(database.Key, KeyHelper.GetDatabaseSchema(), database.Value[i + j]);
                        var key2 = Path.Combine(database.Key, KeyHelper.GetDatabaseData(), database.Value[i + j]);
                        tasks.Add(RestoreFromDumpFile(dataReader, key1, key2, database.Key));
                    }

                    Task.WaitAll(tasks.ToArray());
                }
            }

            var comparer = new SqlComparer();

            foreach (var u in upgrades.OrderBy(Path.GetFileName, comparer))
            {
                using (var s = File.OpenRead(u))
                {
                    if (u.Contains(".upgrade."))
                    {
                        RunMysqlFile(s, null).Wait();
                    }
                    else if (u.Contains(".data") || u.Contains(".upgrade"))
                    {
                        RunMysqlProcedure(s).Wait();
                    }
                    else
                    {
                        RunMysqlFile(s, "default").Wait();
                    }
                }

                SetStepCompleted();
            }
        }
Пример #13
0
        internal override SqlExpression VisitSimpleCase(SqlSimpleCase c)
        {
            c.Expression = this.VisitExpression(c.Expression);
            int compareWhen = 0;

            // Find the ELSE if it exists.
            for (int i = 0, n = c.Whens.Count; i < n; i++)
            {
                if (c.Whens[i].Match == null)
                {
                    compareWhen = i;
                    break;
                }
            }

            c.Whens[compareWhen].Match = VisitExpression(c.Whens[compareWhen].Match);
            c.Whens[compareWhen].Value = VisitExpression(c.Whens[compareWhen].Value);

            // Compare each other when value to the compare when
            List <SqlWhen> newWhens         = new List <SqlWhen>();
            bool           allValuesLiteral = true;

            for (int i = 0, n = c.Whens.Count; i < n; i++)
            {
                if (compareWhen != i)
                {
                    SqlWhen when = c.Whens[i];
                    when.Match = this.VisitExpression(when.Match);
                    when.Value = this.VisitExpression(when.Value);
                    if (!SqlComparer.AreEqual(c.Whens[compareWhen].Value, when.Value))
                    {
                        newWhens.Add(when);
                    }
                    allValuesLiteral = allValuesLiteral && when.Value.NodeType == SqlNodeType.Value;
                }
            }
            newWhens.Add(c.Whens[compareWhen]);

            // Did everything reduce to a single CASE?
            SqlExpression rewrite = TryToConsolidateAllValueExpressions(newWhens.Count, c.Whens[compareWhen].Value);

            if (rewrite != null)
            {
                return(rewrite);
            }

            // Can it be a conjuction (or disjunction) of clauses?
            rewrite = TryToWriteAsSimpleBooleanExpression(c.ClrType, c.Expression, newWhens, allValuesLiteral);
            if (rewrite != null)
            {
                return(rewrite);
            }

            // Can any WHEN clauses be reduced to fall into the ELSE clause?
            rewrite = TryToWriteAsReducedCase(c.ClrType, c.Expression, newWhens, c.Whens[compareWhen].Match, c.Whens.Count);
            if (rewrite != null)
            {
                return(rewrite);
            }

            return(c);
        }