GeezerVison is showing github.com — Converting works with object graphs · ThatRendle/Simple.Data@14cd9d4
File treeExpand file treeCollapse file tree Open diff view settings Simple.Data.BehaviourTest/Query
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: Simple.Data.Ado/QueryBuilder.cs +24-1Lines changed: 24 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -60,7 +60,14 @@ private void SetQueryContext(SimpleQuery query) | 60 | 60 | var selectClause = _query.Clauses.OfType<SelectClause>().SingleOrDefault(); | 61 | 61 | if (selectClause != null) | 62 | 62 | { | 63 | | - _columns = selectClause.Columns.ToArray(); | | 63 | + if (selectClause.Columns.OfType<AllColumnsSpecialReference>().Any()) | | 64 | + { | | 65 | + _columns = ExpandAllColumnsReferences(selectClause.Columns).ToArray(); | | 66 | + } | | 67 | + else | | 68 | + { | | 69 | + _columns = selectClause.Columns.ToArray(); | | 70 | + } | 64 | 71 | } | 65 | 72 | else | 66 | 73 | { | @@ -77,6 +84,22 @@ private void SetQueryContext(SimpleQuery query) | 77 | 84 | _commandBuilder.SetText(GetSelectClause(_tableName)); | 78 | 85 | } | 79 | 86 |
| | 87 | + private IEnumerable<SimpleReference> ExpandAllColumnsReferences(IEnumerable<SimpleReference> columns) | | 88 | + { | | 89 | + foreach (var column in columns) | | 90 | + { | | 91 | + var allColumns = column as AllColumnsSpecialReference; | | 92 | + if (ReferenceEquals(allColumns, null)) yield return column; | | 93 | + else | | 94 | + { | | 95 | + foreach (var allColumn in _schema.FindTable(allColumns.Table.GetName()).Columns) | | 96 | + { | | 97 | + yield return new ObjectReference(allColumn.ActualName, allColumns.Table); | | 98 | + } | | 99 | + } | | 100 | + } | | 101 | + } | | 102 | + | 80 | 103 | private void HandleWithClauses() | 81 | 104 | { | 82 | 105 | var withClauses = _query.Clauses.OfType<WithClause>().ToList(); | |
Collapse file Copy file name to clipboardExpand all lines: Simple.Data.Ado/SimpleReferenceFormatter.cs +13-1Lines changed: 13 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -33,13 +33,25 @@ private string FormatColumnClause(SimpleReference reference, bool excludeAlias) | 33 | 33 | ?? | 34 | 34 | TryFormatAsFunctionReference(reference as FunctionReference, excludeAlias) | 35 | 35 | ?? | 36 | | - TryFormatAsMathReference(reference as MathReference, excludeAlias); | | 36 | + TryFormatAsMathReference(reference as MathReference, excludeAlias) | | 37 | + ?? | | 38 | + TryFormatAsAllColumnsReference(reference as AllColumnsSpecialReference, excludeAlias); | 37 | 39 |
| 38 | 40 | if (formatted != null) return formatted; | 39 | 41 |
| 40 | 42 | throw new InvalidOperationException("SimpleReference type not supported."); | 41 | 43 | } | 42 | 44 |
| | 45 | + private string TryFormatAsAllColumnsReference(AllColumnsSpecialReference allColumnsSpecialReference, bool excludeAlias) | | 46 | + { | | 47 | + if (ReferenceEquals(allColumnsSpecialReference, null)) return null; | | 48 | + var table = _schema.FindTable(allColumnsSpecialReference.Table.GetAllObjectNamesDotted()); | | 49 | + var tableName = string.IsNullOrWhiteSpace(allColumnsSpecialReference.GetAlias()) | | 50 | + ? table.QualifiedName | | 51 | + : _schema.QuoteObjectName(allColumnsSpecialReference.GetAlias()); | | 52 | + return string.Format("{0}.*", tableName); | | 53 | + } | | 54 | + | 43 | 55 | private string FormatObject(object obj) | 44 | 56 | { | 45 | 57 | var reference = obj as SimpleReference; | |
Collapse file Copy file name to clipboardExpand all lines: Simple.Data.BehaviourTest/Query/ExplicitJoinTest.cs +102Lines changed: 102 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -56,6 +56,46 @@ public void JoinWithExplicitClauseUsingNamedParameters() | 56 | 56 | " join [dbo].[department] on ([dbo].[department].[id] = [dbo].[employee].[departmentid])"); | 57 | 57 | } | 58 | 58 |
| | 59 | + [Test] | | 60 | + public void OuterJoinWithExplicitClauseUsingNamedParameters() | | 61 | + { | | 62 | + var q = _db.Employees.Query() | | 63 | + .OuterJoin(_db.Department, Id: _db.Employees.DepartmentId) | | 64 | + .Select(_db.Employees.Name, _db.Department.Name.As("Department")); | | 65 | + | | 66 | + try | | 67 | + { | | 68 | + q.ToList(); | | 69 | + } | | 70 | + catch (InvalidOperationException) | | 71 | + { | | 72 | + // This won't work on Mock provider, but the SQL should be generated OK | | 73 | + } | | 74 | + | | 75 | + GeneratedSqlIs("select [dbo].[employee].[name],[dbo].[department].[name] as [Department] from [dbo].[employee]" + | | 76 | + " left join [dbo].[department] on ([dbo].[department].[id] = [dbo].[employee].[departmentid])"); | | 77 | + } | | 78 | + | | 79 | + [Test] | | 80 | + public void LeftJoinWithExplicitClauseUsingNamedParameters() | | 81 | + { | | 82 | + var q = _db.Employees.Query() | | 83 | + .LeftJoin(_db.Department, Id: _db.Employees.DepartmentId) | | 84 | + .Select(_db.Employees.Name, _db.Department.Name.As("Department")); | | 85 | + | | 86 | + try | | 87 | + { | | 88 | + q.ToList(); | | 89 | + } | | 90 | + catch (InvalidOperationException) | | 91 | + { | | 92 | + // This won't work on Mock provider, but the SQL should be generated OK | | 93 | + } | | 94 | + | | 95 | + GeneratedSqlIs("select [dbo].[employee].[name],[dbo].[department].[name] as [Department] from [dbo].[employee]" + | | 96 | + " left join [dbo].[department] on ([dbo].[department].[id] = [dbo].[employee].[departmentid])"); | | 97 | + } | | 98 | + | 59 | 99 | [Test] | 60 | 100 | public void JoinWithExplicitClauseUsingExpression() | 61 | 101 | { | @@ -76,6 +116,26 @@ public void JoinWithExplicitClauseUsingExpression() | 76 | 116 | " join [dbo].[department] on ([dbo].[department].[id] = [dbo].[employee].[departmentid])"); | 77 | 117 | } | 78 | 118 |
| | 119 | + [Test] | | 120 | + public void LeftJoinWithExplicitClauseUsingExpression() | | 121 | + { | | 122 | + var q = _db.Employees.Query() | | 123 | + .LeftJoin(_db.Department).On(_db.Department.Id == _db.Employees.DepartmentId) | | 124 | + .Select(_db.Employees.Name, _db.Department.Name.As("Department")); | | 125 | + | | 126 | + try | | 127 | + { | | 128 | + q.ToList(); | | 129 | + } | | 130 | + catch (InvalidOperationException) | | 131 | + { | | 132 | + // This won't work on Mock provider, but the SQL should be generated OK | | 133 | + } | | 134 | + | | 135 | + GeneratedSqlIs("select [dbo].[employee].[name],[dbo].[department].[name] as [Department] from [dbo].[employee]" + | | 136 | + " left join [dbo].[department] on ([dbo].[department].[id] = [dbo].[employee].[departmentid])"); | | 137 | + } | | 138 | + | 79 | 139 | [Test] | 80 | 140 | public void SelfJoinWithExplicitClauseUsingNamedParameters() | 81 | 141 | { | @@ -97,6 +157,27 @@ public void SelfJoinWithExplicitClauseUsingNamedParameters() | 97 | 157 | " join [dbo].[employee] [manager] on ([manager].[id] = [dbo].[employee].[managerid])"); | 98 | 158 | } | 99 | 159 |
| | 160 | + [Test] | | 161 | + public void LeftSelfJoinWithExplicitClauseUsingNamedParameters() | | 162 | + { | | 163 | + var q = _db.Employees.Query() | | 164 | + .LeftJoin(_db.Employees.As("Manager"), Id: _db.Employees.ManagerId); | | 165 | + | | 166 | + q = q.Select(_db.Employees.Name, q.Manager.Name.As("Manager")); | | 167 | + | | 168 | + try | | 169 | + { | | 170 | + q.ToList(); | | 171 | + } | | 172 | + catch (InvalidOperationException) | | 173 | + { | | 174 | + // This won't work on Mock provider, but the SQL should be generated OK | | 175 | + } | | 176 | + | | 177 | + GeneratedSqlIs("select [dbo].[employee].[name],[manager].[name] as [Manager] from [dbo].[employee]" + | | 178 | + " left join [dbo].[employee] [manager] on ([manager].[id] = [dbo].[employee].[managerid])"); | | 179 | + } | | 180 | + | 100 | 181 | [Test] | 101 | 182 | public void SelfJoinWithExplicitClauseUsingOutParameterAndNamedParameters() | 102 | 183 | { | @@ -118,6 +199,27 @@ public void SelfJoinWithExplicitClauseUsingOutParameterAndNamedParameters() | 118 | 199 | " join [dbo].[employee] [manager] on ([manager].[id] = [dbo].[employee].[managerid])"); | 119 | 200 | } | 120 | 201 |
| | 202 | + [Test] | | 203 | + public void LeftSelfJoinWithExplicitClauseUsingOutParameterAndNamedParameters() | | 204 | + { | | 205 | + dynamic manager; | | 206 | + var q = _db.Employees.Query() | | 207 | + .LeftJoin(_db.Employees.As("Manager"), out manager).On(Id: _db.Employees.ManagerId) | | 208 | + .Select(_db.Employees.Name, manager.Name.As("Manager")); | | 209 | + | | 210 | + try | | 211 | + { | | 212 | + q.ToList(); | | 213 | + } | | 214 | + catch (InvalidOperationException) | | 215 | + { | | 216 | + // This won't work on Mock provider, but the SQL should be generated OK | | 217 | + } | | 218 | + | | 219 | + GeneratedSqlIs("select [dbo].[employee].[name],[manager].[name] as [Manager] from [dbo].[employee]" + | | 220 | + " left join [dbo].[employee] [manager] on ([manager].[id] = [dbo].[employee].[managerid])"); | | 221 | + } | | 222 | + | 121 | 223 | [Test] | 122 | 224 | public void SelfJoinWithExplicitClauseUsingExpression() | 123 | 225 | { | |
Collapse file Copy file name to clipboardExpand all lines: Simple.Data.BehaviourTest/Query/QueryTest.cs +20Lines changed: 20 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -43,6 +43,26 @@ public void SpecifyingColumnsShouldRestrictSelect() | 43 | 43 | GeneratedSqlIs("select [dbo].[users].[name],[dbo].[users].[password] from [dbo].[users]"); | 44 | 44 | } | 45 | 45 |
| | 46 | + [Test] | | 47 | + public void SpecifyingColumnStarShouldSelectAllColumns() | | 48 | + { | | 49 | + _db.Users.All() | | 50 | + .Select(_db.Users.Name, _db.Users.UserBio.Star()) | | 51 | + .ToList(); | | 52 | + GeneratedSqlIs("select [dbo].[users].[name],[dbo].[userbio].[userid],[dbo].[userbio].[text] from [dbo].[users]" + | | 53 | + " left join [dbo].[userbio] on ([dbo].[users].[id] = [dbo].[userbio].[userid])"); | | 54 | + } | | 55 | + | | 56 | + [Test] | | 57 | + public void SpecifyingColumnAllColumnsShouldSelectAllColumns() | | 58 | + { | | 59 | + _db.Users.All() | | 60 | + .Select(_db.Users.Name, _db.Users.UserBio.AllColumns()) | | 61 | + .ToList(); | | 62 | + GeneratedSqlIs("select [dbo].[users].[name],[dbo].[userbio].[userid],[dbo].[userbio].[text] from [dbo].[users]" + | | 63 | + " left join [dbo].[userbio] on ([dbo].[users].[id] = [dbo].[userbio].[userid])"); | | 64 | + } | | 65 | + | 46 | 66 | [Test] | 47 | 67 | public void SpecifyingColumnWithAliasShouldAddAsClause() | 48 | 68 | { | |
Collapse file Copy file name to clipboardExpand all lines: Simple.Data.BehaviourTest/Query/WithTest.cs +41Lines changed: 41 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -115,5 +115,46 @@ public void SingleWithClauseUsingExplicitJoinShouldApplyAliasToSql() | 115 | 115 |
| 116 | 116 | GeneratedSqlIs(expectedSql); | 117 | 117 | } | | 118 | + | | 119 | + [Test] | | 120 | + public void SingleWithOneClauseUsingExplicitJoinShouldApplyAliasToSql() | | 121 | + { | | 122 | + const string expectedSql = "select [dbo].[employee].[id],[dbo].[employee].[name]," + | | 123 | + "[dbo].[employee].[managerid],[dbo].[employee].[departmentid]," + | | 124 | + "[manager].[id] as [__with1__manager__id],[manager].[name] as [__with1__manager__name]," + | | 125 | + "[manager].[managerid] as [__with1__manager__managerid],[manager].[departmentid] as [__with1__manager__departmentid]" + | | 126 | + " from [dbo].[employee] left join [dbo].[employee] [manager] on ([manager].[id] = [dbo].[employee].[managerid])"; | | 127 | + | | 128 | + dynamic manager; | | 129 | + var q = _db.Employees.All() | | 130 | + .OuterJoin(_db.Employees.As("Manager"), out manager).On(Id: _db.Employees.ManagerId) | | 131 | + .WithOne(manager); | | 132 | + | | 133 | + EatException(() => q.ToList()); | | 134 | + | | 135 | + GeneratedSqlIs(expectedSql); | | 136 | + } | | 137 | + | | 138 | + [Test] | | 139 | + public void MultipleWithClauseJustDoesEverythingYouWouldHope() | | 140 | + { | | 141 | + const string expectedSql = "select [dbo].[employee].[id],[dbo].[employee].[name]," + | | 142 | + "[dbo].[employee].[managerid],[dbo].[employee].[departmentid]," + | | 143 | + "[manager].[id] as [__withn__manager__id],[manager].[name] as [__withn__manager__name]," + | | 144 | + "[manager].[managerid] as [__withn__manager__managerid],[manager].[departmentid] as [__withn__manager__departmentid]," + | | 145 | + "[dbo].[department].[id] as [__with1__department__id],[dbo].[department].[name] as [__with1__department__name]" + | | 146 | + " from [dbo].[employee] left join [dbo].[employee] [manager] on ([manager].[id] = [dbo].[employee].[managerid])" + | | 147 | + " left join [dbo].[department] on ([dbo].[department].[id] = [dbo].[employee].[departmentid])"; | | 148 | + | | 149 | + dynamic manager; | | 150 | + var q = _db.Employees.All() | | 151 | + .OuterJoin(_db.Employees.As("Manager"), out manager).On(Id: _db.Employees.ManagerId) | | 152 | + .With(manager) | | 153 | + .WithDepartment(); | | 154 | + | | 155 | + EatException(() => q.ToList()); | | 156 | + | | 157 | + GeneratedSqlIs(expectedSql); | | 158 | + } | 118 | 159 | } | 119 | 160 | } |
Collapse file Copy file name to clipboardExpand all lines: Simple.Data.UnitTest/ConcreteTypeCreatorTest.cs +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -57,7 +57,7 @@ public void CanConvertStringToEnum() | 57 | 57 | Assert.IsInstanceOf<Int32ToEnum>(actual); | 58 | 58 | Assert.AreEqual(expected, ((Int32ToEnum)actual).Value); | 59 | 59 | } | 60 | | - | | 60 | + | 61 | 61 | public class DecimalToDouble | 62 | 62 | { | 63 | 63 | public double Value { get; set; } | |
Collapse file Copy file name to clipboardExpand all lines: Simple.Data.UnitTest/Simple.Data.UnitTest.csproj +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 102 | 102 | <Compile Include="SimpleQueryTest.cs" /> | 103 | 103 | <Compile Include="SimpleRecordAsDictionaryTest.cs" /> | 104 | 104 | <Compile Include="SimpleRecordCloneTest.cs" /> | | 105 | + <Compile Include="SimpleRecordConvertTest.cs" /> | 105 | 106 | <Compile Include="SimpleResultSetTest.cs" /> | 106 | 107 | <Compile Include="SpecialReferenceTests.cs" /> | 107 | 108 | <Compile Include="StringExtensionsTest.cs" /> | |
Collapse file Copy file name to clipboard +53Lines changed: 53 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +namespace Simple.Data.UnitTest | | 2 | +{ | | 3 | + using System.Collections.Generic; | | 4 | + using System.Linq; | | 5 | + using NUnit.Framework; | | 6 | + | | 7 | + [TestFixture] | | 8 | + public class SimpleRecordConvertTest | | 9 | + { | | 10 | + [Test] | | 11 | + public void CanConvertToFoo() | | 12 | + { | | 13 | + dynamic source = new SimpleRecord(new Dictionary<string, object> {{"X", "Bar"}}); | | 14 | + Foo actual = source; | | 15 | + Assert.AreEqual("Bar", actual.X); | | 16 | + } | | 17 | + | | 18 | + [Test] | | 19 | + public void CanConvertWithSubItemToFoo() | | 20 | + { | | 21 | + dynamic source = new SimpleRecord(new Dictionary<string, object> {{"X", "Bar"}, {"Y", new Dictionary<string,object> { {"X", "Quux"}}}}); | | 22 | + Foo actual = source; | | 23 | + Assert.AreEqual("Bar", actual.X); | | 24 | + Assert.IsNotNull(actual.Y); | | 25 | + Assert.AreEqual("Quux", actual.Y.X); | | 26 | + } | | 27 | + | | 28 | + [Test] | | 29 | + public void CanConvertWithSubItemAndCollectionToFoo() | | 30 | + { | | 31 | + dynamic source = | | 32 | + new SimpleRecord(new Dictionary<string, object> | | 33 | + {{"X", "Bar"}, | | 34 | + {"Y", new Dictionary<string, object> {{"X", "Quux"}}}, | | 35 | + {"Z", new[] { new Dictionary<string, object> {{"X", "Wibble"}}}} | | 36 | + }); | | 37 | + Foo actual = source; | | 38 | + Assert.AreEqual("Bar", actual.X); | | 39 | + Assert.IsNotNull(actual.Y); | | 40 | + Assert.AreEqual("Quux", actual.Y.X); | | 41 | + Assert.IsNotNull(actual.Z); | | 42 | + Assert.AreEqual(1, actual.Z.Count); | | 43 | + Assert.AreEqual("Wibble", actual.Z.Single().X); | | 44 | + } | | 45 | + | | 46 | + public class Foo | | 47 | + { | | 48 | + public string X { get; set; } | | 49 | + public Foo Y { get; set; } | | 50 | + public ICollection<Foo> Z { get; set; } | | 51 | + } | | 52 | + } | | 53 | +} |
Collapse file Copy file name to clipboardExpand all lines: Simple.Data/AllColumnsSpecialReference.cs +13-1Lines changed: 13 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -2,8 +2,20 @@ namespace Simple.Data | 2 | 2 | { | 3 | 3 | public class AllColumnsSpecialReference : SpecialReference | 4 | 4 | { | 5 | | - public AllColumnsSpecialReference() : base("*") | | 5 | + private readonly ObjectReference _table; | | 6 | + | | 7 | + public AllColumnsSpecialReference() : this(null) | 6 | 8 | { | 7 | 9 | } | | 10 | + | | 11 | + public AllColumnsSpecialReference(ObjectReference table) : base("*") | | 12 | + { | | 13 | + _table = table; | | 14 | + } | | 15 | + | | 16 | + public ObjectReference Table | | 17 | + { | | 18 | + get { return _table; } | | 19 | + } | 8 | 20 | } | 9 | 21 | } |
Collapse file Copy file name to clipboardExpand all lines: Simple.Data/ObjectReference.cs +10Lines changed: 10 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -78,6 +78,16 @@ public override string GetAliasOrName() | 78 | 78 | return GetAlias() ?? _name; | 79 | 79 | } | 80 | 80 |
| | 81 | + public AllColumnsSpecialReference AllColumns() | | 82 | + { | | 83 | + return new AllColumnsSpecialReference(this); | | 84 | + } | | 85 | + | | 86 | + public AllColumnsSpecialReference Star() | | 87 | + { | | 88 | + return new AllColumnsSpecialReference(this); | | 89 | + } | | 90 | + | 81 | 91 | public override bool TryInvoke(InvokeBinder binder, object[] args, out object result) | 82 | 92 | { | 83 | 93 | if (_dataStrategy != null) | |
|