Пример #1
0
 /// <summary>Initializes a new instance of <b>ForeignKey</b> class.</summary>
 /// <param name="childTable">Child table. <see cref="Table.GeneratedTables"/> property doesn't have to be initialized at this time.</param>
 /// <param name="parentTableName">Parent table name.</param>
 /// <param name="constraintName">Optional constraint name.</param>
 public ForeignKey(Table childTable, string parentTableName, string constraintName)
 {            
     this.Index = -1;
     this.ChildTable = childTable;
     this.ParentTableName = parentTableName;
     this.ConstraintName = constraintName;
     this.ChildColumns = new List<string>();
     this.ParentColumns = new List<string>();
 }
Пример #2
0
        private static bool IsTableGenerated(Table[] allGeneratedTables, string tableName)
        {
            foreach (Table tbl in allGeneratedTables)
            {
                if (tbl.TableName == tableName)
                    return true;
            }

            return false;
        }
Пример #3
0
        /// <summary>Initializes a new instance of TableField class.</summary>
        /// <param name="table">Table or view to which this column belongs to.</param>
        /// <param name="columnName">Column name.</param>
        /// <param name="nameTemplate">Class item naming convention.</param>
        /// <param name="nativeDbType">Native DB type.</param>
        /// <param name="typeTemplate">Type metadata.</param>
        /// <param name="precision">Precision if numeric type.</param>
        /// <param name="scale">Scale if numeric type.</param>
        /// <param name="isNullable">Specifies whether the values of this column are nullable.</param>
        /// <param name="indexInParentTable">Index in parent table <see cref="FistCore.Generator.Table.Fields"/> array.</param>
        /// <param name="autoIncrement">Specifies whether the values of this column are automatically incremented.</param>
        /// <param name="dbDefaultValue">Optional textual representaion of default values configured in database.</param>
        /// <param name="maxLength">Maximum length of textual or byte array column.</param>
        /// <param name="isPrimaryKeyPart">Indicates whether the column is a part of the primary key.</param>
        /// <param name="isForeignKeyPart">Indicates whether the column is a part of a foreign key.</param>
        /// <param name="isUniqueConstraintPart">Indicates whether the column is a part of a unique constraint.</param>
        /// <param name="isAutoGenerated">Indicates whether the value of the field is automatically generated by database (eg. timestamp in SQL Server).</param>
        /// <param name="sequenceName">Optional sequence that increments value for this field. <b>Null</b> if the field is not auto-incremented or sequence does not exist.</param>
        public TableField(Table table, string columnName, IClassItemNamingConvention nameTemplate, string nativeDbType, ITypeMapper typeTemplate, int precision, int scale,
            bool isNullable, int indexInParentTable, bool autoIncrement, string dbDefaultValue, int maxLength, bool isPrimaryKeyPart, bool isForeignKeyPart, bool isUniqueConstraintPart,
            bool isAutoGenerated, string sequenceName)
        {
            this.Table = table;
            this.ColumnName = columnName;
            this.Precision = precision;
            this.Scale = scale;
            this.Names = new ClassItemNames(this.ColumnName, nameTemplate);
            if (nativeDbType != null && typeTemplate != null)
                this.Types = new TypeInfo(nativeDbType, typeTemplate, precision, scale);

            this.IsNullable = isNullable;
            this.Index = indexInParentTable;
            this.AutoIncrement = autoIncrement;
            this.DbDefaultValue = dbDefaultValue;
            this.Length = length;
            this.IsPrimaryKeyPart = isPrimaryKeyPart;
            this.IsForeignKeyPart = isForeignKeyPart;
            this.IsUniqueConstraintPart = isUniqueConstraintPart;
            this.IsAutoGenerated = isAutoGenerated;
            this.SequenceName = sequenceName;
            this.ExtendedProperties = null;
        }
Пример #4
0
        private static bool IsChild(string parentTableName, Table potentialChild)
        {
            foreach (ForeignKey fk in potentialChild.ForeignKeys)
            {
                if (fk.ParentTableName == parentTableName)
                    return true;
            }

            return false;
        }
Пример #5
0
        private static Table[] GetChildTables(Table parent, Table[] allGeneratedTables)        
        {
            string parentTableName = parent.TableName;
            List<Table> children = new List<Table>();
            foreach (Table candidate in allGeneratedTables)
            {
                if (IsChild(parentTableName, candidate))
                    children.Add(candidate);
            }

            return children.ToArray();
        }
Пример #6
0
        private static ForeignKey[] GetChildRelations(Table parent, Table[] allGeneratedTables)
        {
            List<ForeignKey> childFKs = new List<ForeignKey>();
            foreach (Table child in parent.GetChildTables())
            {
                var allChildFKs = child.GetGeneratedFKs();
                foreach (ForeignKey fk in allChildFKs)
                {
                    if (fk.ParentTableName == parent.TableName)
                        childFKs.Add(fk);
                }
            }

            return childFKs.ToArray();
        }
Пример #7
0
 private void GenerateAppConfigFile(Table[] tables)
 {
     FireItemGenerated("Writing application configuration file.", 100 - OtherCost);
     string filePath = CreateAppConfigFilePath();
     using (StreamWriter sw = new StreamWriter(filePath, /*overwrite existing file*/ false, Encoding.UTF8))
     {
         this.appConfigTemplate.Run(sw, tables);
     }
 }
Пример #8
0
        private bool GenerateSingleFileTemplateForTables(Table[] tables)
        {
            List<ITemplate> singleFileTemplates = GenUtil.GetSingleFileTemplates(this.tableItemTemplates);
            if (singleFileTemplates.Count == 0)
                return true;

            int percentage = 100 - OtherCost;
            foreach (ITemplate template in singleFileTemplates)
            {
                if (this.stopGenerator)
                {
                    FireItemGenerated(Messages.DotNetProjectGenerator_GeneratorStopped, percentage);
                    return false;
                }

                string generatedFileName = GenerateSingleFileForTemplate(template, tables);
                string msg = string.Format(Messages.DotNetProjectGenerator_GeneratedFileX, generatedFileName);
                FireItemGenerated(msg, percentage);
            }

            return true;
        }
Пример #9
0
        private string GenerateSingleFileForTemplate(ITemplate template, Table[] tables)
        {
            string fileNameWithExt = template.GetVariable(SpecialVariable.SingleFile) ?? template.GetSetting(SpecialVariable.SingleFile);
            string filePath = GenUtil.CreateSingleFilePathForTemplate(template, this.outputFolder, fileNameWithExt);
            using (StreamWriter codeOutput = new StreamWriter(filePath, /*overwrite existing file*/ false, Encoding.UTF8))
            {
                template.Run(codeOutput, tables);
            }

            return fileNameWithExt;
        }
Пример #10
0
 private void GenerateCodeForTable(Table tbl, List<ITemplate> multiFileTemplates)
 {
     foreach (ITemplate template in multiFileTemplates)
     {
         string classFileName = GenUtil.CreateClassOutputFilePath(this.classItemTemplate.GenerateClassName(tbl.TableName), tbl.GetType(), template, this.outputFolder);
         using (StreamWriter classOutput = new StreamWriter(classFileName, /*overwrite existing file*/ false, Encoding.UTF8))
         {
             template.Run(classOutput, tbl);
         }
     }
 }
Пример #11
0
        private bool GenerateCodeForSelectedTables(Table[] tables)
        {
            List<ITemplate> multiFileTemplates = GenUtil.GetMultiFileTemplates(this.tableItemTemplates);
            if (multiFileTemplates.Count == 0)
                return true;

            DateTime start = DateTime.Now;
            int percentage = MetaCost;
            for (int i = 0; i < tables.Length; i++)
            {
                percentage = (int)(MetaCost + EntityClassesCost * i / tables.Length);
                if (this.stopGenerator)
                {
                    FireItemGenerated(Messages.DbProjectGenerator_GeneratorStopped, percentage);
                    return false;
                }

                GenerateCodeForTable(tables[i], multiFileTemplates);
                string msg = string.Format(Messages.DbProjectGenerator_GeneratedCodeForTableX, tables[i].TableName);
                FireItemGenerated(msg, percentage);
            }

            string msgStatistics = string.Format(Messages.DbProjectGenerator_GeneratedCodeForAllTablesTimesUsedX, GenUtil.GetDuration(start));
            FireItemGenerated(msgStatistics, percentage);
            return true;
        }
Пример #12
0
 private void CreateGeneratedTablesIndices(Table[] tables)
 {
     this.generatedTablesIndices = new Dictionary<string, int>();
     for (int i = 0; i < tables.Length; i++)
     {
         Table tbl = tables[i];
         this.generatedTablesIndices[tbl.TableName] = i;
     }
 }
Пример #13
0
 private void OptimizeMetaByIndexing(Table[] tables)
 {
     // Table-map speeds up code generation by reducing loops while generating code for ORM components.
     CreateGeneratedTablesIndices(tables);
     foreach (Table tbl in tables)
     {
         // Tables need to know for which tables the code is generated. Required when generating foreign table members.
         tbl.GeneratedTables = tables;
         tbl.IsTableGeneratedAlgorithm = this.IsTableGenerated;
         tbl.IndexOfGeneratedTableAlgorithm = this.IndexOfGeneratedTable;
     }
 }
Пример #14
0
        private Table[] RetrieveTableMetaData()
        {
            DateTime start = DateTime.Now;
            this.metadataSource.ExcludedColumnsCriteria = this.excludedColumnsCriteria;
            Table[] tables = new Table[this.selectedTables.Length];
            int percentage = 0;
            for (int i = 0; i < this.selectedTables.Length; i++)
            {
                percentage = (int)(TableMetaCost * i / tables.Length);
                if (this.stopGenerator)
                {
                    this.metadataSource.CloseConnection();
                    FireItemGenerated(Messages.DbProjectGenerator_GeneratorStopped, percentage);
                    return null;
                }

                string currTable = this.selectedTables[i];

                tables[i] = this.metadataSource.FetchTableMetadata(currTable, this.classItemTemplate, this.typeTemplate);
                tables[i].IsView = this.selectedViews.ContainsKey(currTable);
                string msg = string.Format(Messages.DbProjectGenerator_RetrievedMetaForTableX, currTable);
                FireItemGenerated(msg, percentage);
            }

            string msgStatistics = string.Format(Messages.DbProjectGenerator_RetrievedMetaForAllTablesTimeUsedX, GenUtil.GetDuration(start));
            FireItemGenerated(msgStatistics, percentage);
            return tables;
        }