Exemplo n.º 1
0
 /// <summary>Initializes a new instance of <b>ClassItemNames</b> class.</summary>
 /// <param name="item">Item mappet to class members.</param>
 /// <param name="template">Naming convention template.</param>
 internal ClassItemNames(string item, IClassItemNamingConvention template)
 {
     this.PrivateMember = template.GeneratePrivateMember(item);
     this.ProtectedMember = template.GenerateProtectedMember(item);
     this.PublicMember = template.GeneratePublicMember(item);
     this.LocalVariable = template.GenerateLocalVariable(item);
     this.ClassName = template.GenerateClassName(item);
 }
Exemplo n.º 2
0
        /// <summary>Renders member/property that contains parent entity.</summary>
        /// <param name="fk">Foreign key meta data.</param>
        /// <param name="memberType">Class item type.</param>
        /// <param name="classItemTemplate">Naming convention template.</param>
        /// <param name="prefixesToRemove">Table name prefixes to remove from item name.</param>
        /// <param name="tableSuffix">Sufix to append.</param>
        /// <returns>Class member mapped to foreign key.</returns>
        public string RenderParentMember(ForeignKey fk, NetClassItem memberType, IClassItemNamingConvention classItemTemplate, string[] prefixesToRemove, string tableSuffix)
        {
            // Used by template based generator.
            StringBuilder memberName = new StringBuilder();
            ApplyPrefix(memberName);
            RenderTableNameWithoutSpecifiedPrefixes(fk, classItemTemplate, memberName, prefixesToRemove);
            ApplySuffixes(fk, classItemTemplate, memberName, tableSuffix, this.delimiter, this.appendChildFields);
            if (memberName.Length < 1)
                throw new Exception("Specified options yielded with an empty parent member name.");

            ApplyNetClassItem(memberType, memberName);
            return memberName.ToString();
        }
Exemplo n.º 3
0
        private void RenderTableNameWithoutSpecifiedPrefixes(ForeignKey fk, IClassItemNamingConvention classItemTemplate, StringBuilder memberName, string[] prefixesToRemove)
        {
            if (this.renderTableName)
            {
                string table = "";
                if ((prefixesToRemove != null) && (prefixesToRemove.Length > 0))
                    table = RemovePrefix(fk.ParentTableName, prefixesToRemove);
                else
                    table = fk.ParentTableName;

                table = classItemTemplate.GenerateClassName(table);
                memberName.Append(table);
            }
        }
Exemplo n.º 4
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;
        }
        private static TableField CreateTableFieldFromFieldDataRow(Table table, IClassItemNamingConvention classItemTemplate, ITypeMapper typeTemplate, DataRow col, int indexInParentTable)
        {
            string columnName = col["COLUMN_NAME"].ToString();
            string nativeDbType = col["DATA_TYPE"].ToString();
            int precision = (col["NUMERIC_PRECISION"] != DBNull.Value) ? Convert.ToInt32(col["NUMERIC_PRECISION"]) : 0;
            int scale = (col["NUMERIC_SCALE"] != DBNull.Value) ? Convert.ToInt32(col["NUMERIC_SCALE"]) : 0;
            bool isNullable = (col["IS_NULLABLE"].ToString().ToLowerInvariant() == "yes");
            int maxLength;
            bool isTextualOrBinary = (col["CHARACTER_MAXIMUM_LENGTH"] != DBNull.Value);
            if (isTextualOrBinary)
            {
                int maxCharsOrBytes = Convert.ToInt32(col["CHARACTER_MAXIMUM_LENGTH"]);
                maxLength = maxCharsOrBytes;
            }
            else
            {
                // Numeric types.
                maxLength = (col["ColumnLength"] != DBNull.Value) ? Convert.ToInt32(col["ColumnLength"]) : 0;
            }

            bool autoIncrement = (col["IsIdentity"] != DBNull.Value) && (Convert.ToInt32(col["IsIdentity"]) >= 1);
            string dbDefaultValue = col["COLUMN_DEFAULT"].ToString();
            bool isPrimaryKeyPart = (Convert.ToInt32(col["IsPrimaryKey"]) == 1);
            // IsForeignKeyPart isn't working for some reason. It will be processed later when fetching relations.
            bool isForeignKeyPart = (Convert.ToInt32(col["IsForeignKey"]) == 1);
            bool isUniqueConstraintPart = (Convert.ToInt32(col["HasUniqueConstraint"]) == 1);
            // Auto-generated flag. Fields that are auto-generated may not be inserted or updated.
            bool isComputed = (Convert.ToInt32(col["IsComputed"]) == 1);
            bool isRowVersion = (nativeDbType == "timestamp") || (nativeDbType == "rowversion");
            bool isAutoGenerated = (isComputed || isRowVersion);
            string sequenceName = (col["SequenceName"] != DBNull.Value) ? (string)col["SequenceName"] : null;

            TableField field = new TableField(table, columnName, classItemTemplate, nativeDbType, typeTemplate, precision, scale,
                isNullable, indexInParentTable, autoIncrement, dbDefaultValue, maxLength, isPrimaryKeyPart, isForeignKeyPart, isUniqueConstraintPart,
                isAutoGenerated, sequenceName);

            //int colSizeInBytes = Convert.ToInt32(row["ColumnLength"]);
            //table.Fields[i].Ordinal = Convert.ToInt32(row["ORDINAL_POSITION"]);
            return field;
        }
 private static void CreateFields(IClassItemNamingConvention classItemTemplate, ITypeMapper typeTemplate, Table table, DataTable fieldsData)
 {
     table.Fields = new TableField[fieldsData.Rows.Count];
     for (int i = 0; i < table.Fields.Length; i++)
     {
         DataRow row = fieldsData.Rows[i];
         table.Fields[i] = CreateTableFieldFromFieldDataRow(table, classItemTemplate, typeTemplate, row, i);
         if (table.Fields[i].IsPrimaryKeyPart)
             table.NumberOfPrimaryKeyFields++;
     }
 }
        /// <summary>Fetches table and column metadata.</summary>
        /// <param name="tableName">Table name.</param>
        /// <param name="classItemTemplate">Naming convention.</param>
        /// <param name="typeTemplate">Type metadata.</param>
        /// <returns>An instance of <b>Table</b> class.</returns>
        public Table FetchTableMetadata(string tableName, IClassItemNamingConvention classItemTemplate, ITypeMapper typeTemplate)
        {
            // Algorithm:
            //	1. fetch table columns without constraints
            //	2. fetch all table constraints
            //	3. ensure there are no multiple FK constraints for a single column
            //	4. populate column info/table with constraints
            //	5. fetch parent info
            DataTable fieldsData = FetchColumnData(tableName);
            DataTable constraintData = FetchConstraintData(tableName);
            // Match table constraints with field info.
            EnsureNoMultipleConstraintsOfTheSameTypeForASingleColumn(tableName, constraintData);
            AssignConstraintsToColumns(fieldsData, constraintData);
            DataTable parentData = FetchParentRelations(tableName);

            Table table = new Table(tableName);
            CreateFields(classItemTemplate, typeTemplate, table, fieldsData);
            CreateFKs(table, parentData);
            return table;
        }
Exemplo n.º 8
0
        private void ApplySuffixes(ForeignKey fk, IClassItemNamingConvention classItemTemplate, StringBuilder memberName)
        {
            if (this.tableSuffix != null)
                memberName.Append(this.tableSuffix);

            if (this.delimiter != null)
                memberName.Append(this.delimiter);

            if (this.appendChildFields)
            {
                TableField[] fkFields = fk.GetChildFkFields();
                foreach (TableField field in fkFields)
                    memberName.Append(classItemTemplate.GeneratePublicMember(field.ColumnName));
            }
        }
        private static TableField CreateTableFieldFromFieldDataRow(Table table, IClassItemNamingConvention classItemTemplate, ITypeMapper typeTemplate, DataRow col, int indexInParentTable)
        {
            string columnName = col["COLUMN_NAME"].ToString();
            string nativeDbType = col["COLUMN_DATA_TYPE"].ToString();
            int precision = (col["NUMERIC_PRECISION"] != DBNull.Value) ? Convert.ToInt32(col["NUMERIC_PRECISION"]) : 0;
            int scale = (col["NUMERIC_SCALE"] != DBNull.Value) ? Convert.ToInt32(col["NUMERIC_SCALE"]) : 0;
            bool isNullable = Convert.ToBoolean(col["IS_NULLABLE"]);
            int maxLength = Convert.ToInt32(col["COLUMN_SIZE"]);
            bool autoIncrement = false;
            string dbDefaultValue = col["COLUMN_DEFAULT"].ToString();
            bool isPrimaryKeyPart = Convert.ToBoolean(col["IsPrimaryKey"]);
            bool isForeignKeyPart = Convert.ToBoolean(col["IsForeignKey"]);
            bool isUniqueConstraintPart = false;
            bool isAutoGenerated = false;
            string sequenceName = null;

            TableField field = new TableField(table, columnName, classItemTemplate, nativeDbType, typeTemplate, precision, scale,
                isNullable, indexInParentTable, autoIncrement, dbDefaultValue, maxLength, isPrimaryKeyPart, isForeignKeyPart, isUniqueConstraintPart,
                isAutoGenerated, sequenceName);

            return field;
        }
Exemplo n.º 10
0
        /// <summary>Fetches table and column metadata.</summary>
        /// <param name="tableName">Table name.</param>
        /// <param name="classItemTemplate">Naming convention.</param>
        /// <param name="typeTemplate">Type metadata.</param>
        /// <returns>An instance of <b>Table</b> class.</returns>
        public Table FetchTableMetadata(string tableName, IClassItemNamingConvention classItemTemplate, ITypeMapper typeTemplate)
        {
            DataTable fieldData = FetchMeta("Columns", new string[] { null, null, tableName, null });
            RemoveExcludedFieldsData(tableName, "COLUMN_NAME", fieldData);

            DataTable pkData = FetchMeta("PrimaryKeys", new string[] { null, null, tableName });
            fieldData.Columns.Add("IsPrimaryKey", typeof(bool));
            foreach (DataRow field in fieldData.Rows)
                field["IsPrimaryKey"] = 0 < pkData.Select(string.Format("COLUMN_NAME = '{0}'", field["COLUMN_NAME"])).Length;

            DataTable fkData = FetchForeignKeysMeta(tableName);
            EnsureNoMultipleConstraintsOfTheSameTypeForASingleColumn(tableName, fkData);
            fieldData.Columns.Add("IsForeignKey", typeof(bool));
            foreach (DataRow field in fieldData.Rows)
                field["IsForeignKey"] = 0 < fkData.Select(string.Format("COLUMN_NAME = '{0}'", field["COLUMN_NAME"])).Length;

            Table table = new Table(tableName);
            CreateFields(classItemTemplate, typeTemplate, table, fieldData);
            CreateFKs(table, fkData);
            return table;
        }
Exemplo n.º 11
0
        private static TableField CreateTableFieldFromFieldDataRow(Table table, IClassItemNamingConvention classItemTemplate, ITypeMapper typeTemplate, DataRow col, DataTable constraintsData, int indexInParentTable)
        {
            string fieldName = Convert.ToString(col["COLUMN_NAME"]);
            string nativeDbType = Convert.ToString(col["DATA_TYPE"]);
            int precision = (col["DATA_PRECISION"] != DBNull.Value) ? Convert.ToInt32(col["DATA_PRECISION"]) : 0;
            int scale = (col["DATA_SCALE"] != DBNull.Value) ? Convert.ToInt32(col["DATA_SCALE"]) : 0;
            bool isNullable = ("Y" == Convert.ToString(col["NULLABLE"]));
            int maxLength = (col["DATA_LENGTH"] != DBNull.Value) ? Convert.ToInt32(col["DATA_LENGTH"]) : 0;
            string dbDefaultValue = Convert.ToString(col["DATA_DEFAULT"]);
            bool isPrimaryKeyPart, isForeignKeyPart, isUniqueConstraintPart;
            SetConstraintPartFlags(fieldName, constraintsData, out isPrimaryKeyPart, out isForeignKeyPart, out isUniqueConstraintPart);
            TableField field = new TableField(table, fieldName, classItemTemplate, nativeDbType, typeTemplate, precision, scale,
                isNullable, indexInParentTable, /*autoIncrement*/ false, dbDefaultValue, maxLength, isPrimaryKeyPart, isForeignKeyPart, isUniqueConstraintPart,
                /*isAutoGenerated*/ false, /*sequenceName*/ null);

            //field.Ordinal = Convert.ToInt32(col["COLUMN_ID"]);
            return field;
        }
Exemplo n.º 12
0
        private static void CreateFields(IClassItemNamingConvention classItemTemplate, ITypeMapper typeTemplate, Table table, DataTable fieldsData, DataTable constraintsData)
        {
            table.Fields = new TableField[fieldsData.Rows.Count];
            for (int i = 0; i < fieldsData.Rows.Count; i++)
            {
                DataRow col = fieldsData.Rows[i];
                TableField field = CreateTableFieldFromFieldDataRow(table, classItemTemplate, typeTemplate, col, constraintsData, i);
                //SetConstraintPartFlags(constraintsData, field);

                table.Fields[i] = field;
                if (field.IsPrimaryKeyPart)
                    table.NumberOfPrimaryKeyFields++;
            }
        }
Exemplo n.º 13
0
        /// <summary>Fetches table and column metadata.</summary>
        /// <param name="tableName">Table name.</param>
        /// <param name="classItemTemplate">Naming convention.</param>
        /// <param name="typeTemplate">Type metadata.</param>
        /// <returns>An instance of <b>Table</b> class.</returns>
        public Table FetchTableMetadata(string tableName, IClassItemNamingConvention classItemTemplate, ITypeMapper typeTemplate)
        {
            DataTable fieldsData = FetchColumnData(this.conn, tableName, this.excludedColumnsCriteria);
            DataTable constraintsData = FetchConstraintData(this.conn, tableName);
            DataTable parentData = FetchParentRelations(this.conn, tableName);

            Table table = new Table(tableName);
            CreateFields(classItemTemplate, typeTemplate, table, fieldsData, constraintsData);
            CreateFKs(table, parentData);

            return table;
        }