コード例 #1
0
ファイル: Program.cs プロジェクト: blockspacer/niveum
        public static void ExportSqlServer(Schema s, String ConnectionString, String DatabaseName, String MemoryDatabaseFile)
        {
            if (DatabaseName == "")
            {
                throw new InvalidOperationException("数据库名称没有指定。");
            }

            var bs = Niveum.ObjectSchema.BinarySerializerWithString.Create();

            var cf = GetConnectionFactory(DatabaseType.SqlServer);

            var TableInfo           = TableOperations.GetTableInfo(s);
            var EntityMetas         = TableInfo.EntityMetas;
            var EnumUnderlyingTypes = TableInfo.EnumUnderlyingTypes;

            var Value = new RelationVal {
                Tables = new List <TableVal>()
            };

            using (var c = cf(ConnectionString))
            {
                c.Open();
                c.ChangeDatabase(DatabaseName);
                using (var b = c.BeginTransaction())
                {
                    try
                    {
                        foreach (var t in EntityMetas)
                        {
                            var Table = TableOperations.ExportTable(EntityMetas, EnumUnderlyingTypes, c, b, t.Value.Name, DatabaseType.SqlServer);
                            Value.Tables.Add(Table);
                        }
                    }
                    finally
                    {
                        b.Rollback();
                    }
                }
            }

            SaveData(s, MemoryDatabaseFile, Value, bs);
        }
コード例 #2
0
ファイル: Program.cs プロジェクト: blockspacer/niveum
        public static void RegenMySQL(Schema s, String ConnectionString, String DatabaseName, String[] DataDirOrMemoryDatabaseFiles)
        {
            var Value = LoadData(s, DataDirOrMemoryDatabaseFiles);

            var GenSqls   = s.CompileToMySql(DatabaseName, true);
            var RegenSqls = Regex.Split(GenSqls, @"\r\n;(\r\n)+", RegexOptions.ExplicitCapture).ToList();
            var Creates   = RegenSqls.TakeWhile(q => !q.StartsWith("ALTER")).ToList();
            var Alters    = RegenSqls.SkipWhile(q => !q.StartsWith("ALTER")).ToList();

            var cf = GetConnectionFactory(DatabaseType.MySQL);

            using (var c = cf(ConnectionString))
            {
                c.Open();
                foreach (var Sql in Creates)
                {
                    if (Sql == "")
                    {
                        continue;
                    }

                    var cmd = c.CreateCommand();
                    cmd.CommandText = Sql;
                    cmd.CommandType = CommandType.Text;
                    cmd.ExecuteNonQuery();
                }
            }

            var TableInfo           = TableOperations.GetTableInfo(s);
            var EntityMetas         = TableInfo.EntityMetas;
            var EnumUnderlyingTypes = TableInfo.EnumUnderlyingTypes;
            var Tables = TableOperations.GetTableDictionary(s, EntityMetas, Value);

            using (var c = cf(ConnectionString))
            {
                c.Open();
                c.ChangeDatabase(DatabaseName);
                using (var b = c.BeginTransaction())
                {
                    var Success = false;
                    try
                    {
                        foreach (var t in Tables)
                        {
                            TableOperations.ImportTable(EntityMetas, EnumUnderlyingTypes, c, b, t, DatabaseType.MySQL);
                        }

                        b.Commit();
                        Success = true;
                    }
                    finally
                    {
                        if (!Success)
                        {
                            b.Rollback();
                        }
                    }
                }
            }

            using (var c = cf(ConnectionString))
            {
                c.Open();
                c.ChangeDatabase(DatabaseName);
                foreach (var Sql in Alters)
                {
                    if (Sql == "")
                    {
                        continue;
                    }

                    var cmd = c.CreateCommand();
                    cmd.CommandText = Sql;
                    cmd.CommandType = CommandType.Text;
                    cmd.ExecuteNonQuery();
                }
            }
        }
コード例 #3
0
ファイル: Program.cs プロジェクト: blockspacer/niveum
        public static void RegenSqlServer(Schema s, String ConnectionString, String DatabaseName, String[] DataDirOrMemoryDatabaseFiles)
        {
            if (DatabaseName == "")
            {
                throw new InvalidOperationException("数据库名称没有指定。");
            }

            var Value = LoadData(s, DataDirOrMemoryDatabaseFiles);

            var cf = GetConnectionFactory(DatabaseType.SqlServer);

            using (var c = cf(ConnectionString))
            {
                var RegenSqls = Regex.Split(s.CompileToTSql(DatabaseName, true), @"\r\nGO(\r\n)+", RegexOptions.ExplicitCapture);
                c.Open();
                foreach (var Sql in RegenSqls)
                {
                    if (Sql == "")
                    {
                        continue;
                    }

                    var cmd = c.CreateCommand();
                    cmd.CommandText = Sql;
                    cmd.CommandType = CommandType.Text;
                    cmd.ExecuteNonQuery();
                }
            }

            var TableInfo           = TableOperations.GetTableInfo(s);
            var EntityMetas         = TableInfo.EntityMetas;
            var EnumUnderlyingTypes = TableInfo.EnumUnderlyingTypes;
            var Tables = TableOperations.GetTableDictionary(s, EntityMetas, Value);

            using (var c = cf(ConnectionString))
            {
                c.Open();
                c.ChangeDatabase(DatabaseName);
                using (var b = c.BeginTransaction())
                {
                    var Success = false;
                    try
                    {
                        foreach (var t in EntityMetas)
                        {
                            var CollectionName = t.Value.CollectionName;

                            {
                                IDbCommand cmd = c.CreateCommand();
                                cmd.Transaction = b;
                                cmd.CommandText = String.Format("ALTER TABLE [{0}] NOCHECK CONSTRAINT ALL", CollectionName);
                                cmd.CommandType = CommandType.Text;
                                cmd.ExecuteNonQuery();
                            }
                        }
                        foreach (var t in Tables)
                        {
                            TableOperations.ImportTable(EntityMetas, EnumUnderlyingTypes, c, b, t, DatabaseType.SqlServer);
                        }
                        foreach (var t in EntityMetas)
                        {
                            var CollectionName = t.Value.CollectionName;

                            {
                                IDbCommand cmd = c.CreateCommand();
                                cmd.Transaction = b;
                                cmd.CommandText = String.Format("ALTER TABLE [{0}] WITH CHECK CHECK CONSTRAINT ALL", CollectionName);
                                cmd.CommandType = CommandType.Text;
                                cmd.ExecuteNonQuery();
                            }
                        }

                        b.Commit();
                        Success = true;
                    }
                    finally
                    {
                        if (!Success)
                        {
                            b.Rollback();
                        }
                    }
                }
            }
        }