public override IRelationConfig AutoExpression() { if (this.LeftTable.Flags.HasFlag(TableFlags.AutoColumns) && this.LeftTable.PrimaryKey != null) { this.Expression.Left = this.Expression.CreateColumn(this.LeftTable.PrimaryKey); } if (this.RightTable.Flags.HasFlag(TableFlags.AutoColumns) && this.LeftTable.PrimaryKey != null) { var column = default(IColumnConfig); if (this.RightTable.TryCreateColumn( ColumnConfig.By( Conventions.RelationColumn(this.LeftTable), Factories.Type.Create( TypeConfig.By( this.LeftTable.PrimaryKey.ColumnType.Type, this.LeftTable.PrimaryKey.ColumnType.Size, this.LeftTable.PrimaryKey.ColumnType.Precision, this.LeftTable.PrimaryKey.ColumnType.Scale, this.Flags.HasFlag(RelationFlags.AllowNull) ) ) ), out column )) { this.Expression.Right = this.Expression.CreateColumn(column); column.IsForeignKey = true; } } return(this); }
public override IRelationConfig AutoExpression() { var left = this.Expression.SetLeft(this.CreateConstraint()); var right = this.Expression.SetRight(this.CreateConstraint()); if (this.LeftTable.Flags.HasFlag(TableFlags.AutoColumns) && this.LeftTable.PrimaryKey != null) { var column = default(IColumnConfig); left.Left = this.Expression.CreateColumn(this.LeftTable.PrimaryKey); if (this.MappingTable.TryCreateColumn( ColumnConfig.By( Conventions.RelationColumn(this.LeftTable), Factories.Type.Create( TypeConfig.By( this.LeftTable.PrimaryKey.ColumnType.Type, this.LeftTable.PrimaryKey.ColumnType.Size, this.LeftTable.PrimaryKey.ColumnType.Precision, this.LeftTable.PrimaryKey.ColumnType.Scale, this.Flags.HasFlag(RelationFlags.AllowNull) ) ) ), out column )) { left.Right = this.Expression.CreateColumn(column); column.IsForeignKey = true; } } this.Expression.Operator = this.Expression.CreateOperator(QueryOperator.AndAlso); if (this.RightTable.Flags.HasFlag(TableFlags.AutoColumns) && this.RightTable.PrimaryKey != null) { var column = default(IColumnConfig); right.Left = this.Expression.CreateColumn(this.RightTable.PrimaryKey); if (this.MappingTable.TryCreateColumn( ColumnConfig.By( Conventions.RelationColumn(this.RightTable), Factories.Type.Create( TypeConfig.By( this.RightTable.PrimaryKey.ColumnType.Type, this.RightTable.PrimaryKey.ColumnType.Size, this.RightTable.PrimaryKey.ColumnType.Precision, this.RightTable.PrimaryKey.ColumnType.Scale, this.Flags.HasFlag(RelationFlags.AllowNull) ) ) ), out column )) { right.Right = this.Expression.CreateColumn(column); column.IsForeignKey = true; } } return(this); }
public ITypeConfig Clone() { return(Factories.Type.Create( TypeConfig.By( this.Type, this.Size, this.Precision, this.Scale, this.IsNullable ) )); }
public IRelationConfig Create<T, TRelation>(ITableConfig<T> table, string identifier, PropertyInfo property, RelationFlags? flags) { var elementType = default(Type); var attribute = property.GetCustomAttribute<RelationAttribute>(true) ?? new RelationAttribute() { Identifier = identifier }; if (string.IsNullOrEmpty(attribute.Identifier)) { attribute.Identifier = string.Format("{0}_{1}", table.TableName, property.Name); } if (!attribute.IsFlagsSpecified) { if (flags.HasValue) { attribute.Flags = flags.Value; } else { attribute.Flags = Defaults.Relation.Flags; } } var relation = default(IRelationConfig); if (property.PropertyType.IsCollection(out elementType)) { switch (attribute.Flags.EnsureMultiplicity(RelationFlags.OneToMany).GetMultiplicity()) { case RelationFlags.OneToMany: relation = (IRelationConfig)this.Members.Invoke(this, "CreateOneToMany", new[] { typeof(T), elementType }, table, attribute, property); break; case RelationFlags.ManyToMany: relation = (IRelationConfig)this.Members.Invoke(this, "CreateManyToMany", new[] { typeof(T), elementType }, table, attribute, property); break; default: throw new NotImplementedException(); } } else { relation = this.CreateOneToOne<T, TRelation>(table, attribute, property); } if (!string.IsNullOrEmpty(attribute.LeftColumn)) { relation.Expression.Left = relation.Expression.CreateColumn( relation.LeftTable.CreateColumn( ColumnConfig.By( attribute.LeftColumn, Factories.Type.Create(TypeConfig.By(property)) ) ).With(column => column.IsForeignKey = true) ); } if (!string.IsNullOrEmpty(attribute.RightColumn)) { relation.Expression.Right = relation.Expression.CreateColumn( relation.RightTable.CreateColumn( ColumnConfig.By( attribute.RightColumn, Factories.Type.Create(TypeConfig.By(property)) ) ).With(column => column.IsForeignKey = true) ); } return relation; }
public IColumnConfig Create(ITableConfig table, string identifier, string columnName, ITypeConfig columnType, PropertyInfo property, ColumnFlags?flags) { if (columnType == null) { columnType = Factories.Type.Create(TypeConfig.By(property)); } var attribute = property.GetCustomAttribute <ColumnAttribute>(true) ?? new ColumnAttribute() { Name = columnName, Identifier = identifier }; if (string.IsNullOrEmpty(attribute.Name)) { attribute.Name = Conventions.ColumnName(property); } if (string.IsNullOrEmpty(attribute.Identifier)) { attribute.Identifier = string.Format("{0}_{1}", table.TableName, Conventions.ColumnName(property)); } var isPrimaryKey = attribute.IsPrimaryKeySpecified ? attribute.IsPrimaryKey : string.Equals(attribute.Name, Conventions.KeyColumn, StringComparison.OrdinalIgnoreCase); var isForeignKey = attribute.IsForeignKeySpecified ? attribute.IsForeignKey : false; if (!attribute.IsFlagsSpecified) { if (flags.HasValue) { attribute.Flags = flags.Value; } else { attribute.Flags = Defaults.Column.Flags; if (isPrimaryKey) { attribute.Flags |= Defaults.PrimaryKey.Flags; } if (isForeignKey) { attribute.Flags |= Defaults.ForeignKey.Flags; } } } var accessor = Factories.PropertyAccessor.Column.Create <object, object>(property, columnType); return(new ColumnConfig( table.Config, attribute.Flags, attribute.Identifier, table, attribute.Name, columnType, property, accessor.Get, accessor.Set, accessor.Increment ) { IsPrimaryKey = isPrimaryKey, IsForeignKey = isForeignKey }); }
public void OneTimeSetUp() { var provider = this.CreateProvider(); try { provider.DeleteDatabase(this.InitialCatalog); } catch { //Nothing to do. } provider.CreateDatabase(this.InitialCatalog); this.Database = this.CreateDatabase(); var tables = new[] { this.Database.Config.Transient.CreateTable( TableConfig.By(typeof(Test001), TableFlags.AutoColumns | TableFlags.AutoIndexes) ).With(table => { table.CreateColumn(ColumnConfig.By("Field4", ColumnFlags.None)).With(column => { column.ColumnType = Factories.Type.Create(TypeConfig.By(DbType.Int32, isNullable: true)); }); table.CreateColumn(ColumnConfig.By("Field5", ColumnFlags.None)).With(column => { column.ColumnType = Factories.Type.Create(TypeConfig.By(DbType.Double, isNullable: true)); }); }), this.Database.Config.Transient.CreateTable( TableConfig.By(typeof(Test003), TableFlags.AutoColumns | TableFlags.AutoIndexes) ).With(table => { table.CreateColumn(ColumnConfig.By("Test002_Id", ColumnFlags.None)).With(column => { column.ColumnType = Factories.Type.Create(TypeConfig.By(DbType.Int32, isNullable: true)); }); }), this.Database.Config.Transient.CreateTable( TableConfig.By(typeof(Test004), TableFlags.AutoColumns | TableFlags.AutoIndexes) ).With(table => { table.CreateColumn(ColumnConfig.By("Test002_Id", ColumnFlags.None)).With(column => { column.ColumnType = Factories.Type.Create(TypeConfig.By(DbType.Int32, isNullable: true)); }); }), this.Database.Config.Transient.CreateTable( TableConfig.By(typeof(Test002), TableFlags.AutoColumns | TableFlags.AutoIndexes | TableFlags.AutoRelations) ).With(table => { table.CreateColumn(ColumnConfig.By("Version", ColumnFlags.None)).With(column => { column.ColumnType = Factories.Type.Create(TypeConfig.By(DbType.Binary, size: 8, isNullable: true)); }); }), this.Database.Config.Transient.CreateTable( TableConfig.By( this.Database.Config.Transient.CreateTable( TableConfig.By(typeof(Test002), TableFlags.AutoColumns) ), this.Database.Config.Transient.CreateTable( TableConfig.By(typeof(Test004), TableFlags.AutoColumns) ), TableFlags.AutoColumns | TableFlags.AutoRelations ) ), this.Database.Config.Transient.CreateTable( TableConfig.By(typeof(Test005), TableFlags.AutoColumns | TableFlags.AutoIndexes) ), this.Database.Config.Transient.CreateTable( TableConfig.By(typeof(Test006), TableFlags.AutoColumns | TableFlags.AutoIndexes) ), this.Database.Config.Transient.CreateTable( TableConfig.By(typeof(Test008), TableFlags.AutoColumns | TableFlags.AutoIndexes) ).With(table => { table.CreateColumn(ColumnConfig.By("Test007_Id", ColumnFlags.None)).With(column => { column.ColumnType = Factories.Type.Create(TypeConfig.By(DbType.Guid, isNullable: true)); }); }), this.Database.Config.Transient.CreateTable( TableConfig.By(typeof(Test009), TableFlags.AutoColumns | TableFlags.AutoIndexes) ).With(table => { table.CreateColumn(ColumnConfig.By("Test007_Id", ColumnFlags.None)).With(column => { column.ColumnType = Factories.Type.Create(TypeConfig.By(DbType.Guid, isNullable: true)); }); }), this.Database.Config.Transient.CreateTable( TableConfig.By(typeof(Test007), TableFlags.AutoColumns | TableFlags.AutoIndexes | TableFlags.AutoRelations) ), this.Database.Config.Transient.CreateTable( TableConfig.By( this.Database.Config.Transient.CreateTable( TableConfig.By(typeof(Test007), TableFlags.AutoColumns) ), this.Database.Config.Transient.CreateTable( TableConfig.By(typeof(Test009), TableFlags.AutoColumns) ), TableFlags.AutoColumns | TableFlags.AutoRelations ) ) }; foreach (var table in tables) { var query = this.Database.SchemaFactory.Add(table, Defaults.Schema.Flags).Build(); this.Database.Execute(query); this.Database.Schema.Reset(); } }