Example #1
0
 private bool ValidateStep(DbContextStep step)
 {
     if (String.IsNullOrWhiteSpace(step.OutputPath))
     {
         Log.Warning("Invalid DbContextStep step: empty OutputPath");
         return(false);
     }
     if (String.IsNullOrWhiteSpace(step.ClassName))
     {
         Log.Warning("Invalid DbContextStep step: empty ClassName");
         return(false);
     }
     if (String.IsNullOrWhiteSpace(step.Namespace))
     {
         Log.Warning("Invalid DbContextStep step: empty Namespace");
         return(false);
     }
     return(true);
 }
Example #2
0
 public DbContextEngine(DbContextStep settings, ISqlEngine sqlEngine)
 {
     _settings  = settings;
     _sqlEngine = sqlEngine;
 }
Example #3
0
        private bool LoadJobs()
        {
            if (_settingPaths == null || !_settingPaths.Any())
            {
                Log.Warning("No json settings path specified");
                return(false);
            }

            Log.Information(String.Format("Parsing {0} setting files", _settingPaths.Count()));
            foreach (var inputPath in _settingPaths)
            {
                var path = Path.GetFullPath(inputPath);
                Log.Debug(String.Format("Parsing file {0}", path));
                var settings = Newtonsoft.Json.JsonConvert.DeserializeObject <JsonSettings>(File.ReadAllText(path));

                _jobs = new List <Job>();
                int i = 1;
                Log.Debug(String.Format("Parsing job #{0}", i));
                foreach (var jsonJob in settings.Jobs)
                {
                    int  state        = 0;
                    bool preDbContext = false;
                    var  job          = new Job()
                    {
                        SettingsFile     = path,
                        WorkingDirectory = Path.GetDirectoryName(path),
                        Provider         = jsonJob.Provider,
                        ConnectionString = jsonJob.ConnectionString
                    };
                    foreach (var step in jsonJob.Steps)
                    {
                        Log.Debug(String.Format("Parsing step: {0}", step.StepType));
                        if (state == -1)
                        {
                            Log.Warning("Invalid step: no step is allowed after DbContext");
                            return(false);
                        }
                        switch (step.StepType.ToLowerInvariant())
                        {
                        case "load":
                            if (state != 0 && state != 1)
                            {
                                Log.Warning("Invalid Load Step: unallowed position in step flow");
                                return(false);
                            }
                            var loadStep = new LoadStep()
                            {
                                Name        = step.Name,
                                EntityTypes = step.EntityTypes.Select(s => s?.ToLowerInvariant()),
                                Schemas     = step.Schemas?.Select(s => s?.ToLowerInvariant()),
                                Exclude     = step.Exclude.Convert()
                            };
                            if (!ValidateStep(loadStep))
                            {
                                return(false);
                            }
                            job.Steps.Add(loadStep);
                            state = 1;
                            break;

                        case "pocogenerate":
                            if (state != 1)
                            {
                                Log.Warning("Invalid PocoGenerate Step: unallowed position in step flow");
                                return(false);
                            }
                            var pocoGenerateStep = new PocoGenerateStep()
                            {
                                Name                              = step.Name,
                                Namespace                         = step.Namespace,
                                ClassAccessModifier               = step.ClassAccessModifier,
                                ClassPartial                      = step.ClassPartial,
                                VirtualNavigationProperties       = step.VirtualNavigationProperties,
                                Usings                            = step.Usings,
                                Extends                           = step.Extends,
                                ClassNameForcePascalCase          = step.ClassNameForcePascalCase,
                                ClassNameReplace                  = step.ClassNameReplace.Select(c => c.Convert()),
                                PropertyNameForcePascalCase       = step.PropertyNameForcePascalCase,
                                PropertyNameReplace               = step.PropertyNameReplace.Select(c => c.Convert()),
                                PropertyNullableIfDefaultAndNotPk = step.PropertyNullableIfDefaultAndNotPk
                            };
                            if (!ValidateStep(pocoGenerateStep))
                            {
                                return(false);
                            }
                            job.Steps.Add(pocoGenerateStep);
                            state = 2;
                            break;

                        case "pocowrite":
                            if (state != 2)
                            {
                                Log.Warning("Invalid PocoWrite Step: unallowed position in step flow");
                                return(false);
                            }
                            var pocoWriteStep = new PocoWriteStep()
                            {
                                Name         = step.Name,
                                OutputPath   = step.OutputPath,
                                CleanFolder  = step.CleanFolder,
                                PocosExclude = step.Exclude.Convert()
                            };
                            if (!ValidateStep(pocoWriteStep))
                            {
                                return(false);
                            }
                            job.Steps.Add(pocoWriteStep);
                            state        = 0;
                            preDbContext = true;
                            break;

                        case "dbcontext":
                            if (state != 0 || !preDbContext)
                            {
                                Log.Warning("Invalid DbContext Step: unallowed position in step flow");
                                return(false);
                            }
                            var dbContextStep = new DbContextStep()
                            {
                                Name                       = step.Name,
                                OutputPath                 = step.OutputPath,
                                StubOutputPath             = step.StubOutputPath,
                                Namespace                  = step.Namespace,
                                ClassName                  = step.ClassName,
                                ClassAbstract              = step.ClassAbstract,
                                Extends                    = step.Extends,
                                IncludeIndices             = step.IncludeIndices,
                                IncludeOnModelCreatingStub = step.IncludeOnModelCreatingStub,
                                IncludeOptionalStubs       = step.IncludeOptionalStubs,
                                IncludeViews               = step.IncludeViews,
                                IncludeTablesWithoutPK     = step.IncludeTablesWithoutPK
                            };
                            if (!ValidateStep(dbContextStep))
                            {
                                return(false);
                            }
                            job.Steps.Add(dbContextStep);
                            state = -1;
                            break;
                        }
                    }
                    if (!ValidateJob(job))
                    {
                        return(false);
                    }

                    _jobs.Add(job);
                    i++;
                }
            }
            Log.Information(String.Format("Loaded {0} jobs", _settingPaths.Count()));
            return(true);
        }