public void TestDerivedLoadWithFilter() { //Arrange var insertCommand = SqlConnection.CreateCommand(); insertCommand.CommandText = "INSERT INTO BaseClass (ObjectId, _dscr, Prop1) VALUES(@p1, 2, 1); INSERT INTO DerivedClass (ObjectId, Prop2) VALUES(@p1, 3);"; insertCommand.Parameters.AddWithValue("@p1", Guid.NewGuid()); insertCommand.ExecuteNonQuery(); var types = FluentConfiguration.Start().DefaultIdProperty(IdentityField) .AddType<BaseClass>(z => z.AllProperties()) .AddType<DerivedClass>(z => z.AllProperties()) .GenerateTypeMappings(); var provider = new TestProvider(types); //Act var mapper = new SqlValueMapper(); var builder = new QueryBuilder(provider); var command = SqlConnection.CreateCommand(); var plan = builder.GetQuery("DerivedClass", new [] { "Prop2" }); BaseClass result; command.CommandText = plan.SqlString; command.Parameters.AddWithValue("@p0", 3); using (var reader = command.ExecuteReader()) { var classes = mapper.MapFromReader(reader, plan.SelectClause); result = classes.OfType<BaseClass>().First(); } //Assert Assert.IsInstanceOf<DerivedClass>(result); Assert.AreEqual(1, result.Prop1); }
public void TestCreateTableQuery() { var types = FluentConfiguration.Start().DefaultIdProperty(IdentityField).DefaultDiscriminatorColumnName(DefaultDiscriminator).AddType<BaseClass>(z => z.AllProperties()).GenerateTypeMappings(); var provider = new TestProvider(types); var queryBuilder = new QueryBuilder(provider); var result = queryBuilder.GetCreateTable(typeof(BaseClass)); Assert.AreEqual("CREATE TABLE [BaseClass] ([ObjectId] UNIQUEIDENTIFIER NOT NULL PRIMARY KEY, [_dscr] INT NOT NULL, [Prop1] INT NOT NULL);", result); }
public WriteMapper(QueryBuilder builder, IMetaInfoProvider metaInfoProvider) { if (builder == null) throw new ArgumentNullException("builder"); if (metaInfoProvider == null) throw new ArgumentNullException("metaInfoProvider"); _builder = builder; _metaInfoProvider = metaInfoProvider; }
public void InsertSimpleProperties() { const string type = "BaseClass"; var types = FluentConfiguration.Start().DefaultIdProperty(IdentityField). DefaultDiscriminatorColumnName(DefaultDiscriminator).AddType<BaseClass>(z => z.AllProperties()).GenerateTypeMappings(); var provider = new TestProvider(types); var queryBuilder = new QueryBuilder(provider); var plan = queryBuilder.GetInsert(type); Assert.AreEqual(1, plan.Parts.Count()); Assert.AreEqual("INSERT INTO [BaseClass] ([_dscr], [ObjectId], [Prop1]) VALUES(@p0, @p1, @p2)", plan.Parts.First().SqlString); }
public void InsertPrimitiveListProperties() { const string type = "ClassWithIntArr"; var types = FluentConfiguration.Start().DefaultIdProperty(IdentityField) .DefaultDiscriminatorColumnName(DefaultDiscriminator) .AddTypeAuto<ClassWithIntArr>() .GenerateTypeMappings(); var provider = new TestProvider(types); var queryBuilder = new QueryBuilder(provider); var plan = queryBuilder.GetInsert(type); Assert.AreEqual(2, plan.Parts.Count()); Assert.AreEqual("INSERT INTO [ClassWithIntArr] ([_dscr], [ObjectId]) VALUES(@p0, @p1)", plan.Parts.First().SqlString); Assert.AreEqual("INSERT INTO [ClassWithIntArr_Arr] ([ParentId], [Value], [Index]) VALUES(@p0, @p1, @p2)", plan.Parts.Last().SqlString); }
public void Test() { var mappings = FluentConfiguration.Start().DefaultDiscriminatorColumnName("_dscr") .DefaultIdProperty(IdentityField) .AddTypeAuto<FirstClass>() .AddTypeAuto<SecondClass>() .AddTypeAuto<ThirdClass>().GenerateTypeMappings(); var provider = new TestProvider(mappings); var queryBuilder = new QueryBuilder(provider); var createTable = SqlConnection.CreateCommand(); createTable.CommandText = string.Concat( queryBuilder.GetCreateTable(typeof(FirstClass)), queryBuilder.GetCreateTable(typeof(SecondClass)), queryBuilder.GetCreateTable(typeof(ThirdClass)) ); createTable.ExecuteNonQuery(); var thirdGuids = LoadTable("ThirdClass", (row, i) => { }); var secondGuids = LoadTable("SecondClass", (row, i) => { row["Third"] = thirdGuids[i]; }); LoadTable("FirstClass", (row, i) => { row["Second"] = secondGuids[i]; }); var stopWatch = new Stopwatch(); stopWatch.Start(); var plan = queryBuilder.GetQuery("FirstClass", new string[0], new[] { "Second.Third" }); Debug.WriteLine(plan.SqlString); var mapper = new SqlValueMapper(); var firstClasses = new List<FirstClass>(Count); using (var cmd = new SqlCommand(plan.SqlString, SqlConnection)) { using (var reader = cmd.ExecuteReader()) { firstClasses.AddRange(mapper.MapFromReader(reader, plan.SelectClause).OfType<FirstClass>()); } } stopWatch.Stop(); Assert.AreEqual(Count, firstClasses.Count(z => z.Second != null && z.Second.Third != null)); Assert.Pass(stopWatch.Elapsed.ToString()); }
public void UpdateDerivedroperties() { const string type = "DerivedClass"; var types = FluentConfiguration.Start().DefaultIdProperty(IdentityField) .AddTypeAuto<BaseClass>() .AddTypeAuto<DerivedClass>() .GenerateTypeMappings(); var provider = new TestProvider(types); var queryBuilder = new QueryBuilder(provider); var plan = queryBuilder.GetUpdate(type, new [] { "Prop1", "Prop2" }); Assert.AreEqual(2, plan.Parts.Count()); Assert.AreEqual("UPDATE [M1] SET [M1].[Prop1] = @p1 FROM [BaseClass] AS [M1] WHERE [M1].[ObjectId] = @p0", plan.Parts.First().SqlString); Assert.AreEqual("UPDATE [M1] SET [M1].[Prop2] = @p1 FROM [DerivedClass] AS [M1] WHERE [M1].[ObjectId] = @p0", plan.Parts.Last().SqlString); }
public void TestQueryNestedClassInclude() { var types = FluentConfiguration.Start() .DefaultIdProperty(IdentityField) .DefaultDiscriminatorColumnName(DefaultDiscriminator) .AddType<BaseWithNavigationClass>(z => z.AllProperties()) .AddType<NavigationedClass>(z => z.AllProperties()) .GenerateTypeMappings(); var provider = new TestProvider(types); var queryBuilder = new QueryBuilder(provider); var plan = queryBuilder.GetQuery("BaseWithNavigationClass", new[] { "Nav.Something" }, new[] { "Nav" }); Assert.AreEqual("SELECT [M1].*, [S1].* FROM [BaseWithNavigationClass] AS [M1] LEFT OUTER JOIN [NavigationedClass] AS [S1] ON [M1].[Nav] = [S1].[ObjectId] INNER JOIN [NavigationedClass] AS [T1] ON [M1].[Nav] = [T1].[ObjectId] WHERE [T1].[Something] = @p0", plan.SqlString); Assert.AreEqual(2, plan.SelectClause.Parts.OfType<TypePart>().Count()); Assert.AreEqual(1, plan.SelectClause.Parts.OfType<SubTypePart>().Count()); Assert.IsTrue(plan.SelectClause.Parts.OfType<TypePart>().All(z => z.Tables.Count == 1)); Assert.IsTrue(plan.SelectClause.Parts.OfType<SubTypePart>().All(z => z.CollectingProperty.Name == "Nav")); }
public void TestCreateTableWithArray() { var types = FluentConfiguration.Start().DefaultIdProperty(IdentityField).DefaultDiscriminatorColumnName(DefaultDiscriminator) .AddType<ClassWithIntArr>(z => z.AllProperties()).GenerateTypeMappings(); var provider = new TestProvider(types); var queryBuilder = new QueryBuilder(provider); var result = queryBuilder.GetCreateTable(typeof(ClassWithIntArr)); Assert.AreEqual("CREATE TABLE [ClassWithIntArr] ([ObjectId] UNIQUEIDENTIFIER NOT NULL PRIMARY KEY, [_dscr] INT NOT NULL);\n" +"CREATE TABLE [ClassWithIntArr_Arr] ([ParentId] UNIQUEIDENTIFIER NOT NULL, [Value] INT NOT NULL, [Index] BIGINT NOT NULL);", result); }
public void TestQueryNestedClassOneToOne() { var types = FluentConfiguration.Start() .DefaultIdProperty(IdentityField) .DefaultDiscriminatorColumnName(DefaultDiscriminator) .AddType<BaseWithNavigationClass>(z => z.AllProperties()) .AddType<NavigationedClass>(z => z.AllProperties()) .GenerateTypeMappings(); var provider = new TestProvider(types); var queryBuilder = new QueryBuilder(provider); var plan = queryBuilder.GetQuery("BaseWithNavigationClass", new[] { "Nav.Something" }); Assert.AreEqual("SELECT [M1].* FROM [BaseWithNavigationClass] AS [M1] INNER JOIN [NavigationedClass] AS [T1] ON [M1].[Nav] = [T1].[ObjectId] WHERE [T1].[Something] = @p0", plan.SqlString); }
public void TestIncludeLoad() { //Arrange var insertCommand = SqlConnection.CreateCommand(); insertCommand.CommandText = "INSERT INTO BaseWithNavigationClass (ObjectId, _dscr, Nav) VALUES(@p1, 1, @p2); INSERT INTO NavigationedClass (ObjectId, _dscr, Something) VALUES(@p2, 1, 'goody')"; insertCommand.Parameters.AddWithValue("@p1", Guid.NewGuid()); var navGuid = Guid.NewGuid(); insertCommand.Parameters.AddWithValue("@p2", navGuid); insertCommand.ExecuteNonQuery(); var types = FluentConfiguration.Start() .DefaultIdProperty(IdentityField) .DefaultDiscriminatorColumnName("_dscr") .AddType<BaseWithNavigationClass>(z => z.AllProperties()) .AddType<NavigationedClass>(z => z.AllProperties()) .GenerateTypeMappings(); var provider = new TestProvider(types); var mapper = new SqlValueMapper(); var builder = new QueryBuilder(provider); var command = SqlConnection.CreateCommand(); var plan = builder.GetQuery("BaseWithNavigationClass", new string[0], new []{ "Nav"} ); NavigationedClass result; command.CommandText = plan.SqlString; using (var reader = command.ExecuteReader()) { var classes = mapper.MapFromReader(reader, plan.SelectClause); result = classes.OfType<BaseWithNavigationClass>().First().Nav; } //Assert Assert.AreEqual("goody", result.Something); }
public void UpdateSimpleProperties() { const string type = "BaseClass"; const string path = "Prop1"; var types = FluentConfiguration.Start().DefaultIdProperty(IdentityField).AddType<BaseClass>(z => z.AllProperties()).GenerateTypeMappings(); var provider = new TestProvider(types); var queryBuilder = new QueryBuilder(provider); var plan = queryBuilder.GetUpdate(type, new [] { path }); Assert.AreEqual(1, plan.Parts.Count()); Assert.AreEqual("UPDATE [M1] SET [M1].[Prop1] = @p1 FROM [BaseClass] AS [M1] WHERE [M1].[ObjectId] = @p0", plan.Parts.First().SqlString); }
public void TestSimpleQueryWithDerived() { const string path = "Prop1"; const string derivedType = "DerivedClass"; var types = FluentConfiguration.Start() .DefaultIdProperty(IdentityField) .DefaultDiscriminatorColumnName(DefaultDiscriminator) .AddType<BaseClass>(z => z.AllProperties()) .AddType<DerivedClass>(z => z.AllProperties()) .GenerateTypeMappings(); var provider = new TestProvider(types); var queryBuilder = new QueryBuilder(provider); var plan = queryBuilder.GetQuery(derivedType, new[] { path }); Assert.AreEqual("SELECT [M1].*, [T1].* FROM [BaseClass] AS [M1] INNER JOIN [DerivedClass] AS [T1] ON [M1].[ObjectId] = [T1].[ObjectId] WHERE [M1].[Prop1] = @p0", plan.SqlString); Assert.AreEqual(1, plan.SelectClause.Parts.OfType<TypePart>().Count()); Assert.IsTrue(plan.SelectClause.Parts.OfType<TypePart>().All(z => z.Tables.Count == 2)); }
public void TestSimpleQuery() { const string type = "BaseClass"; const string path = "Prop1"; var types = FluentConfiguration.Start().DefaultIdProperty(IdentityField).AddType<BaseClass>(z => z.AllProperties()).GenerateTypeMappings(); var provider = new TestProvider(types); var queryBuilder = new QueryBuilder(provider); var plan = queryBuilder.GetQuery(type, new[] { path }); Assert.AreEqual("SELECT [M1].* FROM [BaseClass] AS [M1] WHERE [M1].[Prop1] = @p0", plan.SqlString); Assert.AreEqual(1, plan.SelectClause.Parts.OfType<TypePart>().Count()); Assert.IsTrue(plan.SelectClause.Parts.OfType<TypePart>().All(z => z.Tables.Count == 1)); }
public void TestQueryWithCollectionInInclude() { var types = FluentConfiguration.Start().DefaultIdProperty(IdentityField) .AddTypeAuto<ClassWithCollection>() .AddTypeAuto<CollectionItem>() .GenerateTypeMappings(); var provider = new TestProvider(types); var queryBuilder = new QueryBuilder(provider); var plan = queryBuilder.GetQuery("ClassWithCollection", new string[0], new[] { "Collection" }); Assert.AreEqual("SELECT [M1].*, [S1].* FROM [ClassWithCollection] AS [M1] LEFT OUTER JOIN [CollectionItem] AS [S1] ON [M1].[ObjectId] = [S1].[ClassWithCollection_Collection]", plan.SqlString); Assert.AreEqual(2, plan.SelectClause.Parts.OfType<TypePart>().Count()); Assert.IsTrue(plan.SelectClause.Parts.OfType<TypePart>().All(z => z.Tables.Count == 1)); }
public void TestQueryWithCollectionInFilter() { var types = FluentConfiguration.Start().DefaultIdProperty(IdentityField) .AddTypeAuto<ClassWithCollection>() .AddTypeAuto<CollectionItem>() .GenerateTypeMappings(); var provider = new TestProvider(types); var queryBuilder = new QueryBuilder(provider); var plan = queryBuilder.GetQuery("ClassWithCollection", new[] { "Collection.Property" }); Assert.AreEqual("SELECT [M1].* FROM [ClassWithCollection] AS [M1] INNER JOIN [CollectionItem] AS [T1] ON [M1].[ObjectId] = [T1].[ClassWithCollection_Collection] WHERE [T1].[Property] = @p0", plan.SqlString); }
public void TestQueryWithArrayFilter() { var types = FluentConfiguration.Start().DefaultIdProperty(IdentityField) .AddTypeAuto<ClassWithIntArr>() .GenerateTypeMappings(); var provider = new TestProvider(types); var queryBuilder = new QueryBuilder(provider); var plan = queryBuilder.GetQuery("ClassWithIntArr", new[] { "Arr" }); Assert.AreEqual("SELECT [M1].* FROM [ClassWithIntArr] AS [M1] INNER JOIN [ClassWithIntArr_Arr] AS [T1] ON [M1].[ObjectId] = [T1].[ParentId] WHERE [T1].[Value] = @p0", plan.SqlString); }