|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Data; |
| 4 | +using NUnit.Framework; |
| 5 | +using ServiceStack.DataAnnotations; |
| 6 | + |
| 7 | +namespace ServiceStack.OrmLite.Tests.UseCase |
| 8 | +{ |
| 9 | + public interface ITestPoco |
| 10 | + { |
| 11 | + int Id { get; set; } |
| 12 | + |
| 13 | + DateTime DateCreated { get; set; } |
| 14 | + } |
| 15 | + |
| 16 | + public class TestPocoImpl : ITestPoco |
| 17 | + { |
| 18 | + [AutoIncrement] |
| 19 | + public int Id { get; set; } |
| 20 | + |
| 21 | + public DateTime DateCreated { get; set; } |
| 22 | + |
| 23 | + public string Name { get; set; } |
| 24 | + } |
| 25 | + |
| 26 | + public class EntityAttribute |
| 27 | + { |
| 28 | + [AutoIncrement] |
| 29 | + public int Id { get; set; } |
| 30 | + |
| 31 | + public int EntityId { get; set; } |
| 32 | + |
| 33 | + [Required, StringLength(100)] |
| 34 | + public string EntityType { get; set; } |
| 35 | + |
| 36 | + [Required, StringLength(100)] |
| 37 | + public string AttributeName { get; set; } |
| 38 | + |
| 39 | + [Required, StringLength(100)] |
| 40 | + public string AttributeValue { get; set; } |
| 41 | + } |
| 42 | + |
| 43 | + [TestFixture] |
| 44 | + public class FieldFromInterfaceImplementationUseCase : OrmLiteTestBase |
| 45 | + { |
| 46 | + [Test] |
| 47 | + public void Can_select_on_generic_interface_implementation_properties_with_PrefixFieldWithTableName() |
| 48 | + { |
| 49 | + using (var db = OpenDbConnection()) |
| 50 | + { |
| 51 | + db.DropAndCreateTable<TestPocoImpl>(); |
| 52 | + |
| 53 | + var testPoco = new TestPocoImpl { DateCreated = DateTime.Now, Name = "Object 1" }; |
| 54 | + |
| 55 | + testPoco.Id = (int)db.Insert<TestPocoImpl>(testPoco, true); |
| 56 | + |
| 57 | + var rows = GetRows<TestPocoImpl>(db, testPoco.Id); |
| 58 | + |
| 59 | + Assert.That(rows.Count, Is.EqualTo(1)); |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + [Test] |
| 64 | + public void Can_select_on_generic_interface_implementation_properties_with_join() |
| 65 | + { |
| 66 | + using (var db = OpenDbConnection()) |
| 67 | + { |
| 68 | + db.DropAndCreateTable<TestPocoImpl>(); |
| 69 | + db.DropAndCreateTable<EntityAttribute>(); |
| 70 | + |
| 71 | + var testPoco = new TestPocoImpl { DateCreated = DateTime.Now, Name = "Object 1" }; |
| 72 | + |
| 73 | + testPoco.Id = (int)db.Insert<TestPocoImpl>(testPoco, true); |
| 74 | + |
| 75 | + var entityAttribute = new EntityAttribute { EntityType = "TestPocoImpl", EntityId = testPoco.Id, AttributeName = "Description", AttributeValue = "Some Object" }; |
| 76 | + |
| 77 | + db.Insert<EntityAttribute>(entityAttribute); |
| 78 | + |
| 79 | + var rows = GetRowsByIdWhereHasAnyAttributes<TestPocoImpl>(db, testPoco.Id); |
| 80 | + |
| 81 | + Assert.That(rows.Count, Is.EqualTo(1)); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + private List<T> GetRows<T>(IDbConnection db, int id) |
| 86 | + where T : ITestPoco |
| 87 | + { |
| 88 | + var ev = db.From<T>(); |
| 89 | + |
| 90 | + ev.PrefixFieldWithTableName = true; |
| 91 | + |
| 92 | + ev.Where(x => x.Id == id); |
| 93 | + |
| 94 | + return db.Select<T>(ev); |
| 95 | + } |
| 96 | + |
| 97 | + private List<T> GetRowsByIdWhereHasAnyAttributes<T>(IDbConnection db, int id) |
| 98 | + where T : ITestPoco |
| 99 | + { |
| 100 | + var typeName = typeof(T).Name; |
| 101 | + |
| 102 | + var ev = db.From<T, EntityAttribute>((x, y) => x.Id == y.EntityId && y.EntityType == typeName); |
| 103 | + |
| 104 | + ev.Select(x => new { x.Id, x.DateCreated }) |
| 105 | + .Where(x => x.Id == id); |
| 106 | + |
| 107 | + return db.Select<T>(ev); |
| 108 | + } |
| 109 | + } |
| 110 | +} |
0 commit comments