protected virtual bool TryGetTable(Expression expression, out ITableConfig result) { var type = expression.Type; if (type.IsGenericType) { type = type.GetGenericArguments()[0]; } result = this.Database.Config.GetTable(TableConfig.By(type)); return(result != null); }
public static ITableConfig Table(this IConfig config, Type tableType, TableFlags?flags = null) { var selector = TableConfig.By(tableType, flags); var table = config.GetTable(selector); if (table == null) { table = config.CreateTable(selector); } return(table); }
protected virtual bool TryGetTable(MemberInfo member, out ITableConfig result) { var type = this.GetMemberType(member); if (type.IsGenericType) { result = default(ITableConfig); return(false); } result = this.Database.Config.GetTable(TableConfig.By(type)); return(result != null); }
protected TableConfig(TableConfig table, Type tableType) { this.Config = table.Config; this.Flags = table.Flags; this.Identifier = table.Identifier; this.TableName = table.TableName; this.TableType = tableType; this.Columns = new ConcurrentDictionary <string, IColumnConfig>(table.Columns, StringComparer.OrdinalIgnoreCase); this.Indexes = new ConcurrentDictionary <string, IIndexConfig>(table.Indexes, StringComparer.OrdinalIgnoreCase); this.Relations = new ConcurrentDictionary <string, IRelationConfig>(table.Relations, StringComparer.OrdinalIgnoreCase); this.Reset(); }
public static ITableConfig <T1, T2> Table <T1, T2>(this IConfig config, TableFlags?flags = null) { var leftTable = config.Table <T1>(flags); var rightTable = config.Table <T2>(flags); var selector = TableConfig.By(leftTable, rightTable, flags); var table = config.GetTable(selector); if (table == null) { table = config.CreateTable(selector); } return(table as ITableConfig <T1, T2>); }
public void CommonTableExpression_1() { switch (this.ProviderType) { case ProviderType.SqlCe: Assert.Ignore("The provider does not support common table expressions."); return; } var set = this.Database.Set <Test002>(this.Transaction); var data = new List <Test002>(); set.Clear(); set.AddRange(new[] { new Test002() { Name = "1" }, new Test002() { Name = "2" }, new Test002() { Name = "3" } }); var query = this.Database.QueryFactory.Build(); var subQuery = this.Database.QueryFactory.Build(); subQuery.Output.AddOperator(QueryOperator.Star); subQuery.Source.AddTable(set.Table); subQuery.Filter.AddColumn( set.Table.GetColumn(ColumnConfig.By("Name")) ).Right = query.Filter.CreateConstant("2"); query.With.AddCommonTableExpression(new[] { subQuery, subQuery }).Alias = "Extent1"; query.Output.AddOperator(QueryOperator.Star); query.Source.AddTable( this.Database.Config.Transient.CreateTable( TableConfig.By("Extent1", TableFlags.None) ) ); set.Fetch = query; foreach (var item in set) { Assert.AreEqual("2", item.Name); } }
protected virtual bool TryGetColumn(MemberInfo member, Expression expression, out IColumnConfig result) { var property = member as PropertyInfo; if (property == null) { result = default(IColumnConfig); return(false); } var table = this.Database.Config.GetTable(TableConfig.By(expression.Type)); if (table == null) { result = default(IColumnConfig); return(false); } result = table.GetColumn(ColumnConfig.By(property)); return(result != null); }
protected virtual bool TryGetRelation(MemberInfo member, Expression expression, out IRelationConfig result) { var type = this.GetMemberType(member); if (type.IsGenericType) { type = type.GetGenericArguments()[0]; } var table = this.Database.Config.GetTable(TableConfig.By(expression.Type)); if (table != null) { foreach (var relation in table.Relations) { if (relation.RelationType == type) { result = relation; return(true); } } } result = default(IRelationConfig); return(false); }
public void ExpressionDependency_A() { this.Database.Config.CreateTable(TableConfig.By(typeof(A), TableFlags.None)).With(a => { a.Column("Id", ColumnFlags.None).IsPrimaryKey = true; a.Column("B_Id", ColumnFlags.None).IsForeignKey = true; }); this.Database.Config.CreateTable(TableConfig.By(typeof(B), TableFlags.None)).With(b => { b.Column("Id", ColumnFlags.None).IsPrimaryKey = true; b.Column("A_Id", ColumnFlags.None).IsForeignKey = true; b.Column("C_Id", ColumnFlags.None).IsForeignKey = true; }); this.Database.Config.CreateTable(TableConfig.By(typeof(C), TableFlags.None)).With(c => { c.Column("Id", ColumnFlags.None).IsPrimaryKey = true; c.Column("B_Id", ColumnFlags.None).IsForeignKey = true; c.Column("D_Id", ColumnFlags.None).IsForeignKey = true; }); this.Database.Config.CreateTable(TableConfig.By(typeof(D), TableFlags.None)).With(d => { d.Column("Id", ColumnFlags.None).IsPrimaryKey = true; d.Column("A_Id", ColumnFlags.None).IsForeignKey = true; d.Column("C_Id", ColumnFlags.None).IsForeignKey = true; }); this.Database.Config.Table <A>().With(a => { a.Relation(item => item.B).With(relation => { relation.Expression = relation.CreateConstraint().With(constraint => { constraint.Left = relation.CreateConstraint( this.Database.Config.Table <B>().Column("Id"), this.Database.Config.Table <A>().Column("B_Id") ); constraint.Left = relation.CreateConstraint( this.Database.Config.Table <A>().Column("Id"), this.Database.Config.Table <B>().Column("A_Id") ); }); }); a.Relation(item => item.D).With(relation => { relation.Expression = relation.CreateConstraint( this.Database.Config.Table <A>().Column("Id"), this.Database.Config.Table <D>().Column("A_Id") ); }); }); this.Database.Config.Table <B>().With(b => { b.Relation(item => item.C).With(relation => { relation.Expression = relation.CreateConstraint( this.Database.Config.Table <C>().Column("Id"), this.Database.Config.Table <B>().Column("C_Id") ); }); }); this.Database.Config.Table <C>().With(c => { c.Relation(item => item.D).With(relation => { relation.Expression = relation.CreateConstraint( this.Database.Config.Table <D>().Column("Id"), this.Database.Config.Table <C>().Column("D_Id") ); }); }); var composer = new EntityRelationQueryComposer(this.Database, this.Database.Config.Table <A>()); var relations = composer.Fetch.RelationManager.CalculatedRelations; var query = composer.Fetch.Build(); }
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(); } }
public void OneTimeTearDown() { var tables = new[] { this.Database.Config.Transient.CreateTable( TableConfig.By(typeof(Test001), TableFlags.None) ), 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(Test003), TableFlags.None) ), this.Database.Config.Transient.CreateTable( TableConfig.By(typeof(Test004), TableFlags.None) ), this.Database.Config.Transient.CreateTable( TableConfig.By(typeof(Test002), TableFlags.None) ), this.Database.Config.Transient.CreateTable( TableConfig.By(typeof(Test005), TableFlags.None) ), this.Database.Config.Transient.CreateTable( TableConfig.By(typeof(Test006), TableFlags.None) ), 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 ) ), this.Database.Config.Transient.CreateTable( TableConfig.By(typeof(Test008), TableFlags.None) ), this.Database.Config.Transient.CreateTable( TableConfig.By(typeof(Test009), TableFlags.None) ), this.Database.Config.Transient.CreateTable( TableConfig.By(typeof(Test007), TableFlags.None) ) }; foreach (var table in tables) { var query = this.Database.SchemaFactory.Delete(table, Defaults.Schema.Flags).Build(); this.Database.Execute(query); this.Database.Schema.Reset(); } this.Database.Dispose(); this.Database = null; }