Skip to content

Commit 3346915

Browse files
committed
Refactored the shit out of SimpleQuery
1 parent 4a525ba commit 3346915

13 files changed

Lines changed: 251 additions & 81 deletions

Simple.Data.Ado/Joiner.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public IEnumerable<string> GetJoinClauses(ObjectName mainTableName, IEnumerable<
5858
return tablePairs.Select(tp => _done[tp.Item2]).Distinct();
5959
}
6060

61-
public IEnumerable<string> GetJoinClauses(IEnumerable<SimpleQueryJoin> joins, ICommandBuilder commandBuilder)
61+
public IEnumerable<string> GetJoinClauses(IEnumerable<JoinClause> joins, ICommandBuilder commandBuilder)
6262
{
6363
var expressionFormatter = new ExpressionFormatter(commandBuilder, _schema);
6464
foreach (var join in joins)

Simple.Data.Ado/QueryBuilder.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ private void HandleGrouping()
119119

120120
private void HandleOrderBy()
121121
{
122-
if (_query.Order == null) return;
122+
if (!_query.Order.Any()) return;
123123

124124
var orderNames = _query.Order.Select(ToOrderByDirective);
125125
_commandBuilder.Append(" ORDER BY " + string.Join(", ", orderNames));
@@ -141,7 +141,7 @@ private void HandlePaging()
141141
}
142142
}
143143

144-
private string ToOrderByDirective(SimpleOrderByItem item)
144+
private string ToOrderByDirective(OrderByClause item)
145145
{
146146
var col = _table.FindColumn(item.Reference.GetName());
147147
var direction = item.Direction == OrderByDirection.Descending ? " DESC" : string.Empty;
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
6+
namespace Simple.Data.Extensions
7+
{
8+
public static class ArrayExtensions
9+
{
10+
public static T[] Append<T>(this T[] array, T newItem)
11+
where T : class
12+
{
13+
if (array.Length == 0) return new[] {newItem};
14+
var newArray = new T[array.Length + 1];
15+
array.CopyTo(newArray, 0);
16+
newArray[array.Length] = newItem;
17+
return newArray;
18+
}
19+
20+
public static T[] Replace<T>(this T[] array, int index, T newItem)
21+
{
22+
var newArray = (T[])array.Clone();
23+
newArray[index] = newItem;
24+
return newArray;
25+
}
26+
}
27+
}

Simple.Data/HavingClause.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
namespace Simple.Data
2+
{
3+
public class HavingClause : SimpleQueryClauseBase
4+
{
5+
private readonly SimpleExpression _criteria;
6+
7+
public HavingClause(SimpleExpression criteria)
8+
{
9+
_criteria = criteria;
10+
}
11+
12+
public SimpleExpression Criteria
13+
{
14+
get { return _criteria; }
15+
}
16+
}
17+
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@
55

66
namespace Simple.Data
77
{
8-
public class SimpleQueryJoin
8+
public class JoinClause : SimpleQueryClauseBase
99
{
1010
private readonly ObjectReference _table;
1111
private readonly SimpleExpression _joinExpression;
1212

13-
public SimpleQueryJoin(ObjectReference table, SimpleExpression joinExpression)
13+
public JoinClause(ObjectReference table, SimpleExpression joinExpression)
1414
{
1515
_table = table;
1616
_joinExpression = joinExpression;

Simple.Data/OrderByClause.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
namespace Simple.Data
2+
{
3+
public class OrderByClause : SimpleQueryClauseBase
4+
{
5+
private readonly ObjectReference _reference;
6+
private readonly OrderByDirection _direction;
7+
8+
public OrderByClause(ObjectReference reference) : this(reference, OrderByDirection.Ascending)
9+
{
10+
}
11+
12+
public OrderByClause(ObjectReference reference, OrderByDirection direction)
13+
{
14+
_reference = reference;
15+
_direction = direction;
16+
}
17+
18+
public OrderByDirection Direction
19+
{
20+
get { return _direction; }
21+
}
22+
23+
public ObjectReference Reference
24+
{
25+
get { return _reference; }
26+
}
27+
28+
}
29+
}

Simple.Data/SelectClause.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using System.Collections.Generic;
2+
3+
namespace Simple.Data
4+
{
5+
public class SelectClause : SimpleQueryClauseBase
6+
{
7+
private readonly IEnumerable<SimpleReference> _columns;
8+
9+
public SelectClause(IEnumerable<SimpleReference> columns)
10+
{
11+
_columns = columns;
12+
}
13+
14+
public IEnumerable<SimpleReference> Columns
15+
{
16+
get { return _columns; }
17+
}
18+
}
19+
}

Simple.Data/Simple.Data.csproj

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,24 +77,29 @@
7777
<Compile Include="Database.Open.cs" />
7878
<Compile Include="EnumerableEx.cs" />
7979
<Compile Include="ExistsSpecialReference.cs" />
80+
<Compile Include="Extensions\ArrayExtensions.cs" />
8081
<Compile Include="Extensions\HomogenizeEx.cs" />
8182
<Compile Include="Extensions\ResultSetExtensions.cs" />
8283
<Compile Include="FunctionReference.cs" />
84+
<Compile Include="HavingClause.cs" />
8385
<Compile Include="HomogenizedEqualityComparer.cs" />
8486
<Compile Include="IPluralizer.cs" />
8587
<Compile Include="IRange.cs" />
8688
<Compile Include="MathReference.cs" />
8789
<Compile Include="Maybe.cs" />
90+
<Compile Include="OrderByClause.cs" />
8891
<Compile Include="OrderByDirection.cs" />
8992
<Compile Include="Range.cs" />
9093
<Compile Include="Range1.cs" />
94+
<Compile Include="SelectClause.cs" />
9195
<Compile Include="SimpleEmptyExpression.cs" />
9296
<Compile Include="SimpleFunction.cs" />
9397
<Compile Include="SimpleList.cs" />
9498
<Compile Include="SimpleObservable.cs" />
9599
<Compile Include="SimpleOrderByItem.cs" />
96100
<Compile Include="SimpleQuery.cs" />
97-
<Compile Include="SimpleQueryJoin.cs" />
101+
<Compile Include="SimpleQueryClauseBase.cs" />
102+
<Compile Include="JoinClause.cs" />
98103
<Compile Include="SimpleReference.cs" />
99104
<Compile Include="SimpleResultSet.cs" />
100105
<Compile Include="DynamicSchema.cs" />
@@ -132,9 +137,12 @@
132137
<Compile Include="SimpleExpressionType.cs" />
133138
<Compile Include="Extensions\StringExtensions.cs" />
134139
<Compile Include="SimpleTransaction.cs" />
140+
<Compile Include="SkipClause.cs" />
135141
<Compile Include="SpecialReference.cs" />
142+
<Compile Include="TakeClause.cs" />
136143
<Compile Include="UnresolvableObjectException.cs" />
137144
<Compile Include="Commands\UpdateByCommand.cs" />
145+
<Compile Include="WhereClause.cs" />
138146
</ItemGroup>
139147
<ItemGroup>
140148
<None Include="app.config" />

0 commit comments

Comments
 (0)