Example #1
0
        protected internal virtual void WriteDaoSchema(TypeSchema typeSchema, SchemaManager schemaManager, List <KeyColumn> missingKeyColumns = null, List <ForeignKeyColumn> missingForeignKeyColumns = null, ITypeTableNameProvider tableNameProvider = null)
        {
            AddSchemaTables(typeSchema, schemaManager, tableNameProvider);

            HashSet <TypeFk>   foreignKeyTypes = typeSchema.ForeignKeys;
            HashSet <TypeXref> xrefTypes       = typeSchema.Xrefs;

            // accounting for missing columns
            // loop primary keys and fks separately to ensure
            // missing keys get recorded prior to trying to add the
            // fks
            foreach (TypeFk foreignKey in foreignKeyTypes)
            {
                TypeSchemaPropertyInfo keyInfo = foreignKey.PrimaryKeyProperty as TypeSchemaPropertyInfo;
                if (keyInfo != null)
                {
                    KeyColumn key = keyInfo.ToKeyColumn();
                    schemaManager.AddColumn(key.TableName, key);
                    schemaManager.SetKeyColumn(key.TableName, key.Name);
                    if (missingKeyColumns != null)
                    {
                        missingKeyColumns.Add(key);
                    }
                }
            }

            foreach (TypeFk foreignKey in foreignKeyTypes)
            {
                TypeSchemaPropertyInfo fkInfo = foreignKey.ForeignKeyProperty as TypeSchemaPropertyInfo;
                if (fkInfo != null)
                {
                    PropertyInfo keyProperty       = GetKeyProperty(foreignKey.PrimaryKeyType, missingKeyColumns);
                    string       referencedKeyName = keyProperty.Name;

                    ForeignKeyColumn fk = fkInfo.ToForeignKeyColumn(tableNameProvider);
                    fk.AllowNull = true;
                    schemaManager.AddColumn(fk.TableName, fk);
                    schemaManager.SetForeignKey(fk.ReferencedTable, fk.TableName, fk.Name, referencedKeyName);
                    if (missingForeignKeyColumns != null)
                    {
                        missingForeignKeyColumns.Add(fk);
                    }
                }
            }
            // /end - accounting for missing columns

            foreach (TypeFk foreignKey in foreignKeyTypes)
            {
                schemaManager.SetForeignKey(
                    GetTableNameForType(foreignKey.PrimaryKeyType, tableNameProvider),
                    GetTableNameForType(foreignKey.ForeignKeyType, tableNameProvider),
                    foreignKey.ForeignKeyProperty.Name);
            }

            foreach (TypeXref xref in xrefTypes)
            {
                schemaManager.SetXref(GetTableNameForType(xref.Left, tableNameProvider), GetTableNameForType(xref.Right, tableNameProvider));
            }
        }
Example #2
0
        /// <summary>
        /// Get the types for each IEnumerable property of the specified type
        /// </summary>
        /// <param name="parentType"></param>
        /// <returns></returns>
        protected internal IEnumerable <TypeFk> GetReferencingForeignKeyTypesFor(Type parentType)
        {
            HashSet <TypeFk> results = new HashSet <TypeFk>();

            foreach (PropertyInfo property in parentType.GetProperties().Where(p => p.CanWrite))
            {
                Type propertyType = property.PropertyType;
                if (propertyType != typeof(byte[]) &&
                    propertyType != typeof(string) &&
                    property.IsEnumerable() &&
                    property.GetEnumerableType() != typeof(string) &&
                    !AreXrefs(parentType, property.GetEnumerableType()))
                {
                    PropertyInfo keyProperty         = GetKeyProperty(parentType);
                    Type         foreignKeyType      = property.GetEnumerableType();
                    PropertyInfo referencingProperty = null;
                    if (keyProperty == null)
                    {
                        Message = "KeyProperty not found for type {0}"._Format(parentType.FullName);
                        FireEvent(KeyPropertyNotFound, EventArgs.Empty);
                        keyProperty = new TypeSchemaPropertyInfo("Id", parentType, TableNameProvider);
                    }

                    string referencingPropertyName = "{0}{1}"._Format(parentType.Name, keyProperty.Name);
                    referencingProperty = foreignKeyType.GetProperty(referencingPropertyName);

                    if (referencingProperty == null)
                    {
                        Message = "Referencing property not found {0}: Parent type ({1}), ForeignKeyType ({2})"._Format(referencingPropertyName, parentType.FullName, foreignKeyType.FullName);
                        FireEvent(ReferencingPropertyNotFound, EventArgs.Empty);
                        referencingProperty = new TypeSchemaPropertyInfo(referencingPropertyName, parentType, foreignKeyType, TableNameProvider);
                    }

                    PropertyInfo childParentProperty = foreignKeyType.GetProperty(parentType.Name);
                    if (childParentProperty == null)
                    {
                        Message = "ChildParentProperty was not found {0}.{1}: Parent type({2}), ForeignKeyType ({3})"._Format(foreignKeyType.Name, parentType.Name, parentType.FullName, foreignKeyType.FullName);
                        FireEvent(ChildParentPropertyNotFound, EventArgs.Empty);
                        childParentProperty = new TypeSchemaPropertyInfo(parentType.Name, foreignKeyType, TableNameProvider);
                    }

                    results.Add(new TypeFk
                    {
                        PrimaryKeyType      = parentType,
                        PrimaryKeyProperty  = keyProperty,
                        ForeignKeyType      = foreignKeyType,
                        ForeignKeyProperty  = referencingProperty,
                        ChildParentProperty = childParentProperty,
                        CollectionProperty  = property
                    });
                }
            }

            return(results);
        }
Example #3
0
        protected internal static PropertyInfo GetKeyProperty(Type type, List <KeyColumn> keyColumnsToCheck = null, ITypeTableNameProvider tableNameProvider = null)
        {
            tableNameProvider = tableNameProvider ?? new EchoTypeTableNameProvider();
            PropertyInfo keyProperty = type.GetFirstProperyWithAttributeOfType <KeyAttribute>();

            if (keyProperty == null)
            {
                keyProperty = type.GetProperty("Id");
            }

            if (keyProperty == null && keyColumnsToCheck != null)
            {
                KeyColumn keyColumn = keyColumnsToCheck.FirstOrDefault(kc => kc.TableName.Equals(GetTableNameForType(type, tableNameProvider)));
                if (keyColumn != null)
                {
                    keyProperty = new TypeSchemaPropertyInfo(keyColumn.Name, type, tableNameProvider);
                }
            }
            return(keyProperty);
        }