Ejemplo n.º 1
0
        private string AssignNavigationPropertyName(PocoGenerateStep settings, Poco poco, ForeignKey fk)
        {
            string name;

            if ((poco == fk.FkTable.Poco && fk.FkTable.Dependencies != null && fk.FkTable.Dependencies.Count(f => f.PkTable == fk.PkTable) > 1) ||
                (poco == fk.PkTable.Poco && fk.PkTable.DependenciesFrom != null && fk.PkTable.DependenciesFrom.Count(f => f.FkTable == fk.FkTable) > 1))
            {
                name = fk.Name;
            }
            else if (poco == fk.FkTable.Poco)
            {
                name = fk.PkTable.Poco.Name;
            }
            else
            {
                name = fk.FkTable.Poco.Name;
            }

            if (settings.PropertyNameForcePascalCase)
            {
                name = ToPascalCase(name);
            }
            if (name.StartsWith("fk_", StringComparison.InvariantCultureIgnoreCase))
            {
                name = name.Substring(3);
            }
            name = name.Replace("_", String.Empty);

            if (poco == fk.FkTable.Poco && name.StartsWith(poco.Table.Name, StringComparison.InvariantCultureIgnoreCase))
            {
                name = name.Substring(poco.Table.Name.Length);
            }
            return(name);
        }
Ejemplo n.º 2
0
 private bool ValidateStep(PocoGenerateStep step)
 {
     if (String.IsNullOrWhiteSpace(step.Namespace))
     {
         Log.Warning("Invalid PocoWrite step: empty Namespace");
         return(false);
     }
     return(true);
 }
Ejemplo n.º 3
0
        public IEnumerable <Poco> GeneratePocos(IEnumerable <Table> entities, PocoGenerateStep settings)
        {
            if (settings == null)
            {
                throw new Exception("Cannot generate POCOs: no settings found");
            }
            if (entities == null || !entities.Any())
            {
                throw new Exception("Cannot generate POCOs: no entities loaded");
            }

            var pocos = new List <Poco>();

            //create POCOs and resolve name conflicts
            foreach (var entity in entities.OrderBy(e => e.Name))
            {
                var poco = new Poco()
                {
                    Table          = entity,
                    Name           = entity.Name,
                    Namespace      = settings.Namespace,
                    Usings         = settings.Usings,
                    Extends        = settings.Extends,
                    AccessModifier = settings.ClassAccessModifier,
                    IsPartial      = settings.ClassPartial,
                };

                //name replace & resolve
                poco.Name = Replace(settings.ClassNameReplace, poco.Name);
                if (settings.ClassNameForcePascalCase)
                {
                    poco.Name = ToPascalCase(poco.Name);
                }
                ResolvePocoName(pocos, poco);

                Log.Debug(String.Format("POCO {0} - Assigned name: {1}", entity.Name, poco.Name));

                //properties: creation, name replace & resolve
                foreach (var column in entity.Columns.Values.OrderBy(c => c.Position))
                {
                    var property = new PocoBaseProperty()
                    {
                        Name       = column.Name,
                        Column     = column,
                        CSharpType = _sqlEngine.GetCSharpType(column, settings.PropertyNullableIfDefaultAndNotPk)
                    };
                    column.PocoProperty = property;

                    property.Name = Replace(settings.PropertyNameReplace, property.Name);
                    if (settings.PropertyNameForcePascalCase)
                    {
                        property.Name = ToPascalCase(property.Name);
                    }
                    property.Name = ResolvePropertyName(poco, property, "Property");

                    poco.BaseProperties.Add(property);
                    poco.PropertyNames.Add(property.Name);
                }
                pocos.Add(poco);
                entity.Poco = poco;
            }

            //create navigation properties
            foreach (var entity in entities)
            {
                //output dependencies
                if (entity.Dependencies != null)
                {
                    foreach (var fk in entity.Dependencies.Where(fk => fk.PkTable.Poco != null))
                    {
                        var property = new PocoNavigationProperty()
                        {
                            Name           = AssignNavigationPropertyName(settings, entity.Poco, fk),
                            IsVirtual      = settings.VirtualNavigationProperties,
                            Poco           = fk.PkTable.Poco,
                            ForeignKey     = fk,
                            BaseProperties = fk.Columns.Values.Select(c => c.FkColumn.PocoProperty)
                        };
                        property.Name = ResolvePropertyName(entity.Poco, property, "Navigation");
                        entity.Poco.OutNavigationProperties.Add(property);
                        entity.Poco.PropertyNames.Add(property.Name);
                    }
                }
                //input dependencies
                if (entity.DependenciesFrom != null)
                {
                    foreach (var fk in entity.DependenciesFrom.Where(fk => fk.FkTable.Poco != null))
                    {
                        var property = new PocoNavigationProperty()
                        {
                            Name       = AssignNavigationPropertyName(settings, entity.Poco, fk),
                            IsVirtual  = settings.VirtualNavigationProperties,
                            Poco       = fk.FkTable.Poco,
                            ForeignKey = fk
                        };

                        property.Name = ResolvePropertyName(entity.Poco, property, "Navigation");
                        entity.Poco.InNavigationProperties.Add(property);
                        entity.Poco.PropertyNames.Add(property.Name);
                    }
                }
            }
            return(pocos);
        }
Ejemplo n.º 4
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);
        }