Beispiel #1
0
        public EntityCodeGenerator(EntityCodeGenerationModel model, GeneratorConfig config)
        {
            var kdiff3Paths = new[]
            {
                config.KDiff3Path,
                Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86), "KDiff3\\kdiff3.exe"),
                Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles), "KDiff3\\kdiff3.exe"),
            };

            this.model = model;
            CodeFileHelper.Kdiff3Path = kdiff3Paths.FirstOrDefault(File.Exists);

            if (config.TFSIntegration)
            {
                CodeFileHelper.SetupTFSIntegration(config.TFPath);
            }

            CodeFileHelper.SetupTSCPath(config.TSCPath);

            siteWebProj = Path.GetFullPath(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, config.ModelProjectFile));
            siteWebPath = Path.GetDirectoryName(siteWebProj);
            if (!string.IsNullOrEmpty(config.ScriptProjectFile))
            {
                scriptProject = Path.GetFullPath(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, config.ScriptProjectFile));
                scriptPath    = Path.GetDirectoryName(scriptProject);

                if (!File.Exists(scriptProject))
                {
                    scriptProject = null;
                    scriptPath    = null;
                }
            }

            this.config = config;
        }
Beispiel #2
0
        public static string Generate(IDbConnection connection, string tableSchema, string table, string module, string connectionKey, string entityClass, string permission,
                                      GeneratorConfig config)
        {
            var model = GenerateModel(connection, tableSchema, table, module, connectionKey, entityClass, permission, config);

            return("");
        }
Beispiel #3
0
        public MainWindow()
        {
            InitializeComponent();

            _connections = new BindingList <GeneratorConfig.Connection>();
            _tables      = new BindingList <string>();

            this.ConnectionsCombo.DataContext = _connections;
            this.DataContext = this;

            this.config = GeneratorConfig.Load();

            foreach (var connection in config.Connections)
            {
                _connections.Add(connection);
            }
        }
Beispiel #4
0
        private void ModelProjectFileBrowse(object sender, RoutedEventArgs e)
        {
            Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
            if (string.IsNullOrWhiteSpace(ModelProjectFile))
            {
                dlg.FileName         = "*.csproj";
                dlg.InitialDirectory = Path.GetDirectoryName(GeneratorConfig.GetConfigurationFilePath());
            }
            else
            {
                var webProjectFile = Path.GetFullPath(ModelProjectFile);
                dlg.FileName         = Path.GetFileName(webProjectFile);
                dlg.InitialDirectory = Path.GetDirectoryName(webProjectFile);
            }

            bool?result = dlg.ShowDialog();

            if (result.Value == true)
            {
                ModelProjectFile = GeneratorConfig.GetRelativePath(dlg.FileName, AppDomain.CurrentDomain.BaseDirectory);
                config.Save();
            }
        }
Beispiel #5
0
        public static EntityCodeGenerationModel GenerateModel(IDbConnection connection, string tableSchema, string table,
                                                              string module, string connectionKey, string entityClass, string permission, GeneratorConfig config)
        {
            var model = new EntityCodeGenerationModel();

            model.Module = module;

            model.Schema = tableSchema;

            model.Permission    = permission;
            model.ConnectionKey = connectionKey;
            model.RootNamespace = config.RootNamespace;
            var className = entityClass ?? ClassNameFromTableName(table);

            model.ClassName    = className;
            model.RowClassName = className + "Row";
            model.Tablename    = table;
            model.Fields       = new List <EntityCodeField>();
            model.Joins        = new List <EntityCodeJoin>();
            model.Instance     = true;

            var fields   = SqlSchemaInfo.GetTableFieldInfos(connection, tableSchema, table);
            var foreigns = SqlSchemaInfo.GetTableSingleFieldForeignKeys(connection, tableSchema, table);

            var prefix = DeterminePrefixLength(fields, x => x.FieldName);

            model.FieldPrefix = fields.First().FieldName.Substring(0, prefix);

            var identity = fields.Find(f => f.IsIdentity == true);

            if (identity == null)
            {
                identity = fields.Find(f => f.IsPrimaryKey == true);
            }
            if (identity != null)
            {
                model.Identity = GenerateVariableName(identity.FieldName.Substring(prefix));
            }
            else
            {
                identity = fields.Find(f => f.IsPrimaryKey == true);
                if (identity != null)
                {
                    model.Identity = GenerateVariableName(identity.FieldName.Substring(prefix));
                }
            }

            string           baseRowMatch     = null;
            HashSet <string> baseRowFieldset  = null;
            List <string>    baseRowFieldList = new List <string>();

            foreach (var k in config.BaseRowClasses ?? new List <GeneratorConfig.BaseRowClass>())
            {
                var  b    = k.ClassName;
                var  f    = new HashSet <string>(StringComparer.OrdinalIgnoreCase);
                var  fl   = new List <string>();
                bool skip = false;
                foreach (var s in k.Fields ?? new List <string>())
                {
                    string n = s.TrimToNull();
                    if (n == null || !fields.Exists(z => z.FieldName.Substring(prefix) == n))
                    {
                        skip = true;
                        break;
                    }
                    f.Add(n);
                    fl.Add(n);
                }

                if (skip)
                {
                    continue;
                }

                if (baseRowFieldset == null || f.Count > baseRowFieldset.Count)
                {
                    baseRowFieldset  = f;
                    baseRowFieldList = fl;
                    baseRowMatch     = b;
                }
            }

            var removeForeignFields = new HashSet <string>(StringComparer.OrdinalIgnoreCase);

            foreach (var s in config.RemoveForeignFields ?? new List <string>())
            {
                string n = s.TrimToNull();
                if (n != null)
                {
                    removeForeignFields.Add(n);
                }
            }

            if (baseRowFieldset != null &&
                baseRowFieldset.Count > 0)
            {
                model.RowBaseClass    = baseRowMatch;
                model.FieldsBaseClass = baseRowMatch + "Fields";
                model.RowBaseFields   = new List <EntityCodeField>();
                fields.RemoveAll(f =>
                {
                    if (baseRowFieldset.Contains(f.FieldName.Substring(prefix)))
                    {
                        var ft = SqlSchemaInfo.SqlTypeNameToFieldType(f.DataType);
                        model.RowBaseFields.Add(new EntityCodeField
                        {
                            Type        = ft,
                            TSType      = FieldTypeToTS(ft),
                            Ident       = GenerateVariableName(f.FieldName.Substring(prefix)),
                            Title       = Inflector.Inflector.Titleize(f.FieldName.Substring(prefix)),
                            Name        = f.FieldName,
                            IsValueType = ft != "String" && ft != "Stream",
                            Size        = f.Size == 0 ? (Int32?)null : f.Size,
                            Scale       = f.Scale
                        });

                        return(true);
                    }
                    return(false);
                });
            }
            else
            {
                model.RowBaseClass    = "Row";
                model.RowBaseFields   = new List <EntityCodeField>();
                model.FieldsBaseClass = "RowFieldsBase";
            }

            foreach (var field in fields)
            {
                string flags;
                if (field.IsIdentity)
                {
                    flags = "Identity";
                }
                else if (field.IsPrimaryKey)
                {
                    flags = "PrimaryKey";
                }
                else if (!field.IsNullable)
                {
                    flags = "NotNull";
                }
                else
                {
                    flags = null;
                }

                var fieldType = SqlSchemaInfo.SqlTypeNameToFieldType(field.DataType);

                var f = new EntityCodeField()
                {
                    Type        = fieldType,
                    TSType      = FieldTypeToTS(fieldType),
                    Ident       = GenerateVariableName(field.FieldName.Substring(prefix)),
                    Title       = Inflector.Inflector.Titleize(field.FieldName.Substring(prefix)),
                    Name        = field.FieldName,
                    Flags       = flags,
                    IsValueType = fieldType != "String" && fieldType != "Stream",
                    Size        = field.Size == 0 ? (Int32?)null : field.Size,
                    Scale       = field.Scale
                };

                if (f.Name == className && fieldType == "String")
                {
                    model.NameField = f.Name;
                }

                var foreign = foreigns.Find((k) => k.FKColumn.Equals(field.FieldName, StringComparison.InvariantCultureIgnoreCase));
                if (foreign != null)
                {
                    if (f.Title.EndsWith(" Id") && f.Title.Length > 3)
                    {
                        f.Title = f.Title.SafeSubstring(0, f.Title.Length - 3);
                    }

                    f.PKSchema = foreign.PKSchema;
                    f.PKTable  = foreign.PKTable;
                    f.PKColumn = foreign.PKColumn;

                    var frgfld    = SqlSchemaInfo.GetTableFieldInfos(connection, foreign.PKSchema, foreign.PKTable);
                    int frgPrefix = RowGenerator.DeterminePrefixLength(frgfld, z => z.FieldName);
                    var j         = new EntityCodeJoin();
                    j.Fields = new List <EntityCodeField>();
                    j.Name   = GenerateVariableName(f.Name.Substring(prefix));
                    if (j.Name.EndsWith("Id") || j.Name.EndsWith("ID"))
                    {
                        j.Name = j.Name.Substring(0, j.Name.Length - 2);
                    }
                    f.ForeignJoinAlias = j.Name;
                    j.SourceField      = f.Ident;

                    frgfld.RemoveAll(y => removeForeignFields.Contains(y.FieldName));

                    if (frgfld.Find(y => y.FieldName.Substring(frgPrefix) == "SonGuncelleyenID") != null)
                    {
                        //frgfld.RemoveAll(y => LoggingBaseFields.Contains(y.FieldName.Substring(frgPrefix)));
                    }

                    foreach (var frg in frgfld)
                    {
                        if (frg.FieldName.Equals(foreign.PKColumn, StringComparison.InvariantCultureIgnoreCase))
                        {
                            continue;
                        }

                        var kType = SqlSchemaInfo.SqlTypeNameToFieldType(frg.DataType);

                        var k = new EntityCodeField()
                        {
                            Type        = kType,
                            TSType      = FieldTypeToTS(kType),
                            Ident       = GenerateVariableName(frg.FieldName.Substring(frgPrefix)),
                            Title       = Inflector.Inflector.Titleize(JU(j.Name, frg.FieldName.Substring(frgPrefix))),
                            Name        = frg.FieldName,
                            Flags       = flags,
                            IsValueType = kType != "String" && kType != "Stream",
                            Size        = frg.Size == 0 ? (Int32?)null : frg.Size,
                            Scale       = frg.Scale
                        };

                        if (f.TextualField == null && kType == "String")
                        {
                            f.TextualField = JI(j.Name, k.Ident);
                        }

                        j.Fields.Add(k);
                    }

                    model.Joins.Add(j);
                }

                model.Fields.Add(f);
            }

            if (model.NameField == null)
            {
                var fld = model.Fields.FirstOrDefault(z => z.Type == "String");
                if (fld != null)
                {
                    model.NameField = fld.Ident;
                }
            }

            return(model);
        }
Beispiel #6
0
 public void Save()
 {
     Connections.Sort((x, y) => x.Key.CompareTo(y.Key));
     CodeFileHelper.CheckoutAndWrite(GeneratorConfig.GetConfigurationFilePath(),
                                     JSON.StringifyIndented(this), false);
 }