Exemple #1
0
        /// <summary>
        /// Creates the models in the database.
        /// </summary>
        /// <param name="ctx"></param>
        /// <returns></returns>
        public BoolMessageItem <ModelContainer> Process(ModelContext ctx)
        {
            var      bufferTables = new StringBuilder();
            var      bufferProcs  = new StringBuilder();
            var      bufferBoth   = new StringBuilder();
            var      bufferDrop   = new StringBuilder();
            DBSchema schema       = new DBSchema(_conn);

            ctx.AllModels.Iterate(m => ctx.CanProcess(m, (model) => model.GenerateTable), currentModel =>
            {
                // Create the database table for all the models.
                List <Model> modelInheritanceChain = ctx.AllModels.InheritanceFor(currentModel.Name);
                Validate(ctx, currentModel);

                // Create table schema for model & create in database.
                DataTable modelTable  = ConvertModelChainToTable(currentModel, modelInheritanceChain, ctx);
                string sqlTableSchema = string.Empty, sqlProcs = string.Empty, sqlModel = string.Empty;
                string sqlDrop        = string.Empty, sqlDropProcs = string.Empty, sqlDropTable = string.Empty;

                // Build sql for
                // 1. create table
                // 2. create procs
                // 3. create table & procs
                // 4. drop procs & table
                sqlDropTable           = schema.GetDropTable(currentModel.TableName, true);
                var sqlDropTableNoGo   = schema.GetDropTable(currentModel.TableName, false);
                sqlTableSchema         = BuildTableSchema(ctx, modelInheritanceChain, currentModel, true);
                var sqlTableSchemaNoGo = BuildTableSchema(ctx, modelInheritanceChain, currentModel, false);
                List <string> sqlTable = new List <string>()
                {
                    sqlDropTableNoGo, sqlTableSchemaNoGo
                };
                List <string> procNames = CodeFileHelper.GetProcNames(ctx.AllModels.Settings.ModelDbStoredProcTemplates);
                BoolMessageItem <List <string> > storedProcSql     = CreateStoredProcs(ctx, currentModel, true);
                BoolMessageItem <List <string> > storedProcSqlNoGo = CreateStoredProcs(ctx, currentModel, false);
                if (storedProcSql.Success)
                {
                    storedProcSql.Item.ForEach(proc => sqlProcs += proc + Environment.NewLine);
                }
                procNames.ForEach(procName => sqlDropProcs += schema.GetDropProc(currentModel.TableName, procName) + Environment.NewLine);

                sqlModel = sqlDropTable + Environment.NewLine + sqlTableSchema + Environment.NewLine + sqlProcs;
                sqlDrop  = sqlDropProcs + sqlDropTable + Environment.NewLine;

                // Create in the database.
                ExecuteSqlInDb(sqlTable, ctx, currentModel);
                ExecuteSqlInDb(storedProcSqlNoGo.Item, ctx, currentModel);

                // Create model install file containing both the table/procs sql.
                CreateInstallSqlFile(ctx, currentModel, sqlModel);

                bufferTables.Append(sqlTable + Environment.NewLine);
                bufferProcs.Append(sqlProcs + Environment.NewLine + Environment.NewLine);
                bufferBoth.Append(sqlModel + Environment.NewLine);
                bufferDrop.Append(sqlDrop + Environment.NewLine);
            });

            // Create the files.
            string installPath = ctx.AllModels.Settings.ModelInstallLocation;

            Try.CatchLog(() => File.WriteAllText(installPath + "_install_models_tables.sql", bufferTables.ToString()));
            Try.CatchLog(() => File.WriteAllText(installPath + "_install_models_tables.sql", bufferTables.ToString()));
            Try.CatchLog(() => File.WriteAllText(installPath + "_install_models_procs.sql", bufferProcs.ToString()));
            Try.CatchLog(() => File.WriteAllText(installPath + "_install_models_all.sql", bufferBoth.ToString()));
            Try.CatchLog(() => File.WriteAllText(installPath + "_uninstall_models.sql", bufferDrop.ToString()));
            return(null);
        }