Ejemplo n.º 1
0
        public string MakeTenantNew(string schema = "")
        {
            if (!string.IsNullOrEmpty(schema))
            {
                var s = "";
                var tenancy = new DataTenancy();
                List<string> functions = tenancy.GetAllFunction(schema);
                List<string> procedures = tenancy.GetAllProcedures(schema);
                var adoProvider = new ADOProvider(DataHelper.TenantInternal().ToLower());
                using (MiniProfiler.Current.Step("MakeTenantNew"))
                {
                    List<string> tables = tenancy.GetAllTablesInDbSchemaInternal();
                    foreach (var table in tables)
                    {
                        using (MiniProfiler.Current.Step("Create table " + table))
                        {

                            //s +=
                            s =
                                tenancy.GenScriptFromTable(table)
                                       .Replace("[" + DataHelper.TenantInternal().ToLower() + "]", "[" + schema + "]") +
                                "\n";

                            log.Info("-----------------------------Script Table---------------------");
                            log.Info(s);
                            log.Info("-----------------------------End Script Table---------------------");
                            tenancy.ExecuteScript(s);
                            log.Info("-----------------------------Complete Script Table---------------------");

                            //Copy data
                            var querySystemColumn = string.Format("SELECT * FROM sys.columns " +
                                                                  "WHERE object_id = object_id('[{0}].[{1}]')",
                                                                  DataHelper.TenantInternal().ToLower(), table);

                            var datas = adoProvider.Query<SystemColumn>(querySystemColumn);
                            log.Info("-----------------------------query SystemColumn---------------------");
                            log.Info(querySystemColumn);
                            log.Info("-----------------------------End query SystemColumn---------------------");
                            if (datas == null) continue;
                            var isIdentity = false;
                            var fields = datas.ToList();
                            var listFields = new List<string>();
                            foreach (SystemColumn field in fields)
                            {
                                if (field.is_identity == 1)
                                {
                                    isIdentity = true;
                                }
                                listFields.Add(string.Format("[{0}]", field.name));
                            }
                            var sql = string.Format("INSERT INTO [{0}].[{1}]({3}) SELECT * FROM [{2}].[{1}]", schema, table,
                                                    DataHelper.TenantInternal().ToLower(), string.Join(",", listFields));
                            if (isIdentity)
                            {
                                sql = string.Format("SET IDENTITY_INSERT [{0}].[{1}] ON "
                                    + sql
                                    + "SET IDENTITY_INSERT [{0}].[{1}] OFF "
                                    , schema, table);
                            }
                            log.Info("-----------------------------Create table---------------------");
                            log.Info(sql);
                            log.Info("-----------------------------End Create table---------------------");
                            tenancy.ExecuteScript(sql);
                            //+ "<br /><br /><br /><br />"
                        }
                    }

                    //if (procedures.Count > 0)
                    //{
                    //    var sProcedures = string.Join("\n\n", procedures);
                    //    log.Info("-----------------------------Create procedure---------------------");
                    //    log.Info(sProcedures);
                    //    log.Info("-----------------------------End Create procedure---------------------");
                    //    tenancy.ExecuteScript(sProcedures);
                    //}

                    foreach (var procedure in procedures)
                    {
                        var sProcedure = procedure
                            .Replace("SET ANSI_NULLS ON SET QUOTED_IDENTIFIER ON", "")
                            .Replace("[tenant].", "[" + schema + "].");
                        log.Info("-----------------------------Create procedure---------------------");
                        log.Info(sProcedure);
                        log.Info("-----------------------------End Create procedure---------------------");
                        tenancy.ExecuteScript(sProcedure);
                    }

                    foreach (var func in functions)
                    {
                        var sFunc = func
                            .Replace("SET ANSI_NULLS ON SET QUOTED_IDENTIFIER ON", "")
                            .Replace("[tenant].", "[" + schema + "].");
                        log.Info("-----------------------------Create function---------------------");
                        log.Info(sFunc);
                        log.Info("-----------------------------End Create function---------------------");
                        tenancy.ExecuteScript(sFunc);
                    }

                    return "";
                }
            }
            return "";
        }
Ejemplo n.º 2
0
        public string MakeTenantNew(string schema = "")
        {
            if (!string.IsNullOrEmpty(schema))
            {
                var           s           = "";
                var           tenancy     = new DataTenancy();
                List <string> functions   = tenancy.GetAllFunction(schema);
                List <string> procedures  = tenancy.GetAllProcedures(schema);
                var           adoProvider = new ADOProvider(DataHelper.TenantInternal().ToLower());
                using (MiniProfiler.Current.Step("MakeTenantNew"))
                {
                    List <string> tables = tenancy.GetAllTablesInDbSchemaInternal();
                    foreach (var table in tables)
                    {
                        using (MiniProfiler.Current.Step("Create table " + table))
                        {
                            //s +=
                            s =
                                tenancy.GenScriptFromTable(table)
                                .Replace("[" + DataHelper.TenantInternal().ToLower() + "]", "[" + schema + "]") +
                                "\n";

                            log.Info("-----------------------------Script Table---------------------");
                            log.Info(s);
                            log.Info("-----------------------------End Script Table---------------------");
                            tenancy.ExecuteScript(s);
                            log.Info("-----------------------------Complete Script Table---------------------");

                            //Copy data
                            var querySystemColumn = string.Format("SELECT * FROM sys.columns " +
                                                                  "WHERE object_id = object_id('[{0}].[{1}]')",
                                                                  DataHelper.TenantInternal().ToLower(), table);

                            var datas = adoProvider.Query <SystemColumn>(querySystemColumn);
                            log.Info("-----------------------------query SystemColumn---------------------");
                            log.Info(querySystemColumn);
                            log.Info("-----------------------------End query SystemColumn---------------------");
                            if (datas == null)
                            {
                                continue;
                            }
                            var isIdentity = false;
                            var fields     = datas.ToList();
                            var listFields = new List <string>();
                            foreach (SystemColumn field in fields)
                            {
                                if (field.is_identity == 1)
                                {
                                    isIdentity = true;
                                }
                                listFields.Add(string.Format("[{0}]", field.name));
                            }
                            var sql = string.Format("INSERT INTO [{0}].[{1}]({3}) SELECT * FROM [{2}].[{1}]", schema, table,
                                                    DataHelper.TenantInternal().ToLower(), string.Join(",", listFields));
                            if (isIdentity)
                            {
                                sql = string.Format("SET IDENTITY_INSERT [{0}].[{1}] ON "
                                                    + sql
                                                    + "SET IDENTITY_INSERT [{0}].[{1}] OFF "
                                                    , schema, table);
                            }
                            log.Info("-----------------------------Create table---------------------");
                            log.Info(sql);
                            log.Info("-----------------------------End Create table---------------------");
                            tenancy.ExecuteScript(sql);
                            //+ "<br /><br /><br /><br />"
                        }
                    }

                    //if (procedures.Count > 0)
                    //{
                    //    var sProcedures = string.Join("\n\n", procedures);
                    //    log.Info("-----------------------------Create procedure---------------------");
                    //    log.Info(sProcedures);
                    //    log.Info("-----------------------------End Create procedure---------------------");
                    //    tenancy.ExecuteScript(sProcedures);
                    //}

                    foreach (var procedure in procedures)
                    {
                        var sProcedure = procedure
                                         .Replace("SET ANSI_NULLS ON SET QUOTED_IDENTIFIER ON", "")
                                         .Replace("[tenant].", "[" + schema + "].");
                        log.Info("-----------------------------Create procedure---------------------");
                        log.Info(sProcedure);
                        log.Info("-----------------------------End Create procedure---------------------");
                        tenancy.ExecuteScript(sProcedure);
                    }

                    foreach (var func in functions)
                    {
                        var sFunc = func
                                    .Replace("SET ANSI_NULLS ON SET QUOTED_IDENTIFIER ON", "")
                                    .Replace("[tenant].", "[" + schema + "].");
                        log.Info("-----------------------------Create function---------------------");
                        log.Info(sFunc);
                        log.Info("-----------------------------End Create function---------------------");
                        tenancy.ExecuteScript(sFunc);
                    }


                    return("");
                }
            }
            return("");
        }