Skip to content
This repository was archived by the owner on Sep 3, 2022. It is now read-only.

Commit 0fb3674

Browse files
committed
Ordering, constraining Take expressions, Fakes warning removal, cleanup
1 parent de96ebe commit 0fb3674

35 files changed

+684
-397
lines changed

CqlSharp/CqlSharp.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@
6767
<Compile Include="DiscoveryScope.cs" />
6868
<Compile Include="Extensions\Loader.cs" />
6969
<Compile Include="Linq\BuilderBase.cs" />
70+
<Compile Include="Linq\OrderBuilder.cs" />
7071
<Compile Include="Linq\CqlContext.cs" />
7172
<Compile Include="Linq\CqlLinqException.cs" />
7273
<Compile Include="Linq\Expressions\ReadOnlyExtensions.cs" />

CqlSharp/Linq/BuilderBase.cs

+31-17
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,26 @@
1-
using CqlSharp.Linq.Expressions;
1+
// CqlSharp - CqlSharp
2+
// Copyright (c) 2014 Joost Reuzel
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
216
using System.Collections.Generic;
317
using System.Linq.Expressions;
418
using System.Reflection;
19+
using CqlSharp.Linq.Expressions;
520

621
namespace CqlSharp.Linq
722
{
8-
abstract class BuilderBase : CqlExpressionVisitor
23+
internal abstract class BuilderBase : CqlExpressionVisitor
924
{
1025
private readonly Dictionary<Expression, Expression> _map;
1126

@@ -15,10 +30,10 @@ protected BuilderBase()
1530
}
1631

1732
/// <summary>
18-
/// Adds mappings between the arguments of the provided lambda and its replacement expressions
33+
/// Adds mappings between the arguments of the provided lambda and its replacement expressions
1934
/// </summary>
20-
/// <param name="lambda">The lambda.</param>
21-
/// <param name="replacements">The replacements.</param>
35+
/// <param name="lambda"> The lambda. </param>
36+
/// <param name="replacements"> The replacements. </param>
2237
protected void MapLambdaParameters(LambdaExpression lambda, params Expression[] replacements)
2338
{
2439
//map the lambdas input parameter to the "current" projection, allowing
@@ -38,10 +53,10 @@ protected override Expression VisitParameter(ParameterExpression node)
3853
}
3954

4055
/// <summary>
41-
/// Attempt to get the assigned value expression of the accessed property or field
56+
/// Attempt to get the assigned value expression of the accessed property or field
4257
/// </summary>
43-
/// <param name="node"></param>
44-
/// <returns></returns>
58+
/// <param name="node"> </param>
59+
/// <returns> </returns>
4560
protected override Expression VisitMember(MemberExpression node)
4661
{
4762
//visit the expression first, to make sure parameter values are replaced,
@@ -52,28 +67,27 @@ protected override Expression VisitMember(MemberExpression node)
5267
{
5368
case ExpressionType.MemberInit:
5469
//object/class initialization of a known class
55-
var initExpression = (MemberInitExpression)source;
70+
var initExpression = (MemberInitExpression) source;
5671

5772
//search for the assignment of a value to the given member and return the value if found
5873
foreach (var binding in initExpression.Bindings)
5974
{
60-
if (binding.BindingType == MemberBindingType.Assignment && MembersMatch(binding.Member, node.Member))
75+
if (binding.BindingType == MemberBindingType.Assignment &&
76+
MembersMatch(binding.Member, node.Member))
6177
{
62-
return ((MemberAssignment)binding).Expression;
78+
return ((MemberAssignment) binding).Expression;
6379
}
64-
6580
}
6681
break;
6782
case ExpressionType.New:
6883
//object/class initialization of a anonymous class
69-
var nex = (NewExpression)source;
84+
var nex = (NewExpression) source;
7085

7186
//search for the assignment of a value to the given member and return the value if found
7287
for (int i = 0, n = nex.Members.Count; i < n; i++)
7388
{
7489
if (MembersMatch(nex.Members[i], node.Member))
7590
return nex.Arguments[i];
76-
7791
}
7892
break;
7993
}
@@ -95,15 +109,15 @@ private bool MembersMatch(MemberInfo a, MemberInfo b)
95109

96110
if (a is MethodInfo && b is PropertyInfo)
97111
{
98-
return a == ((PropertyInfo)b).GetGetMethod();
112+
return a == ((PropertyInfo) b).GetGetMethod();
99113
}
100114

101115
if (a is PropertyInfo && b is MethodInfo)
102116
{
103-
return ((PropertyInfo)a).GetGetMethod() == b;
117+
return ((PropertyInfo) a).GetGetMethod() == b;
104118
}
105119

106120
return false;
107121
}
108122
}
109-
}
123+
}

CqlSharp/Linq/CqlContext.cs

+22-24
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// CqlSharp - CqlSharp
2-
// Copyright (c) 2013 Joost Reuzel
2+
// Copyright (c) 2014 Joost Reuzel
33
//
44
// Licensed under the Apache License, Version 2.0 (the "License");
55
// you may not use this file except in compliance with the License.
@@ -13,14 +13,14 @@
1313
// See the License for the specific language governing permissions and
1414
// limitations under the License.
1515

16-
using CqlSharp.Linq.Expressions;
1716
using System;
1817
using System.Collections.Generic;
1918
using System.Diagnostics;
2019
using System.IO;
2120
using System.Linq;
2221
using System.Linq.Expressions;
2322
using System.Reflection;
23+
using CqlSharp.Linq.Expressions;
2424

2525
namespace CqlSharp.Linq
2626
{
@@ -72,20 +72,16 @@ public string ConnectionString
7272
}
7373

7474
/// <summary>
75-
/// Gets or sets the log where executed CQL queries are written to
75+
/// Gets or sets the log where executed CQL queries are written to
7676
/// </summary>
77-
/// <value>
78-
/// The log.
79-
/// </value>
77+
/// <value> The log. </value>
8078
public TextWriter Log { get; set; }
8179

8280
#if DEBUG
8381
/// <summary>
84-
/// Gets or sets a value indicating whether execution of the query is skipped. This is for debugging purposes.
82+
/// Gets or sets a value indicating whether execution of the query is skipped. This is for debugging purposes.
8583
/// </summary>
86-
/// <value>
87-
/// <c>true</c> if execution is skipped; otherwise, <c>false</c>.
88-
/// </value>
84+
/// <value> <c>true</c> if execution is skipped; otherwise, <c>false</c> . </value>
8985
public bool SkipExecute { get; set; }
9086
#endif
9187

@@ -110,7 +106,7 @@ private void InitializeTables()
110106
foreach (var property in properties)
111107
{
112108
var propertyType = property.PropertyType;
113-
if (propertyType.IsGenericType && propertyType.GetGenericTypeDefinition() == typeof(CqlTable<>))
109+
if (propertyType.IsGenericType && propertyType.GetGenericTypeDefinition() == typeof (CqlTable<>))
114110
{
115111
var table = Activator.CreateInstance(propertyType, this);
116112
property.SetValue(this, table);
@@ -141,12 +137,12 @@ private object Execute(Expression expression)
141137

142138
Delegate projector = result.Projector.Compile();
143139

144-
var enm = (IEnumerable<object>)Activator.CreateInstance(
145-
typeof(ProjectionReader<>).MakeGenericType(result.Projector.ReturnType),
146-
BindingFlags.Instance | BindingFlags.NonPublic, null,
147-
new object[] { this, result.Cql, projector },
148-
null
149-
);
140+
var enm = (IEnumerable<object>) Activator.CreateInstance(
141+
typeof (ProjectionReader<>).MakeGenericType(result.Projector.ReturnType),
142+
BindingFlags.Instance | BindingFlags.NonPublic, null,
143+
new object[] {this, result.Cql, projector},
144+
null
145+
);
150146

151147
if (result.ResultFunction != null)
152148
return result.ResultFunction.Invoke(enm);
@@ -171,18 +167,21 @@ internal ParseResult ParseExpression(Expression expression)
171167

172168
//translate the expression to a cql expression and corresponding projection
173169
var translation = new ExpressionTranslator().Translate(cleanedExpression);
174-
170+
175171
//generate cql text
176172
var cql = new CqlTextBuilder().Build(translation.Select);
177173
Debug.WriteLine("Generated CQL: " + cql);
178174

179175
//get a projection delegate
180176
var projector = new ProjectorBuilder().BuildProjector(translation.Projection);
181177
Debug.WriteLine("Generated Projector: " + projector);
182-
Debug.WriteLine("Result processor: " + (translation.ResultFunction!=null ? translation.ResultFunction.GetMethodInfo().ToString() : "<none>"));
178+
Debug.WriteLine("Result processor: " +
179+
(translation.ResultFunction != null
180+
? translation.ResultFunction.GetMethodInfo().ToString()
181+
: "<none>"));
183182

184183
//return translation results
185-
return new ParseResult { Cql = cql, Projector = projector, ResultFunction = translation.ResultFunction };
184+
return new ParseResult {Cql = cql, Projector = projector, ResultFunction = translation.ResultFunction};
186185
}
187186

188187
private bool CanBeEvaluatedLocally(Expression expression)
@@ -193,7 +192,6 @@ private bool CanBeEvaluatedLocally(Expression expression)
193192
var query = cex.Value as IQueryable;
194193
if (query != null && query.Provider == this)
195194
return false;
196-
197195
}
198196

199197
return expression.NodeType != ExpressionType.Parameter &&
@@ -225,8 +223,8 @@ IQueryable IQueryProvider.CreateQuery(Expression expression)
225223
{
226224
return
227225
(IQueryable)
228-
Activator.CreateInstance(typeof(CqlTable<>).MakeGenericType(elementType),
229-
new object[] { this, expression });
226+
Activator.CreateInstance(typeof (CqlTable<>).MakeGenericType(elementType),
227+
new object[] {this, expression});
230228
}
231229
catch (TargetInvocationException tie)
232230
{
@@ -242,7 +240,7 @@ IQueryable IQueryProvider.CreateQuery(Expression expression)
242240
/// <returns> </returns>
243241
TResult IQueryProvider.Execute<TResult>(Expression expression)
244242
{
245-
return (TResult)Execute(expression);
243+
return (TResult) Execute(expression);
246244
}
247245

248246
/// <summary>

CqlSharp/Linq/CqlLinqException.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// CqlSharp - CqlSharp
2-
// Copyright (c) 2013 Joost Reuzel
2+
// Copyright (c) 2014 Joost Reuzel
33
//
44
// Licensed under the Apache License, Version 2.0 (the "License");
55
// you may not use this file except in compliance with the License.
@@ -19,7 +19,7 @@
1919
namespace CqlSharp.Linq
2020
{
2121
/// <summary>
22-
/// Exception representing Linq parsing errors
22+
/// Exception representing Linq parsing errors
2323
/// </summary>
2424
[Serializable]
2525
public class CqlLinqException : Exception

CqlSharp/Linq/CqlTable.cs

+18-20
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// CqlSharp - CqlSharp
2-
// Copyright (c) 2013 Joost Reuzel
2+
// Copyright (c) 2014 Joost Reuzel
33
//
44
// Licensed under the Apache License, Version 2.0 (the "License");
55
// you may not use this file except in compliance with the License.
@@ -13,21 +13,21 @@
1313
// See the License for the specific language governing permissions and
1414
// limitations under the License.
1515

16-
using CqlSharp.Serialization;
1716
using System;
1817
using System.Collections;
1918
using System.Collections.Generic;
2019
using System.Linq;
2120
using System.Linq.Expressions;
2221
using System.Reflection;
22+
using CqlSharp.Serialization;
2323

2424
namespace CqlSharp.Linq
2525
{
2626
/// <summary>
27-
/// A table in a Cassandra Keyspace (database)
27+
/// A table in a Cassandra Keyspace (database)
2828
/// </summary>
29-
/// <typeparam name="T"></typeparam>
30-
public class CqlTable<T> : IQueryable<T>, ICqlTable
29+
/// <typeparam name="T"> </typeparam>
30+
public class CqlTable<T> : IOrderedQueryable<T>, ICqlTable
3131
{
3232
private readonly CqlContext _context;
3333
private readonly Expression _expression;
@@ -53,44 +53,42 @@ public CqlTable(CqlContext context, Expression expression)
5353
_expression = expression;
5454
}
5555

56+
#region ICqlTable Members
57+
5658
/// <summary>
57-
/// Gets the column names.
59+
/// Gets the column names.
5860
/// </summary>
59-
/// <value>
60-
/// The column names.
61-
/// </value>
61+
/// <value> The column names. </value>
6262
public Dictionary<MemberInfo, string> ColumnNames
6363
{
6464
get { return ObjectAccessor<T>.Instance.ColumnNames; }
6565
}
6666

6767
/// <summary>
68-
/// Gets the name of the Table.
68+
/// Gets the name of the Table.
6969
/// </summary>
70-
/// <value>
71-
/// The name.
72-
/// </value>
70+
/// <value> The name. </value>
7371
public string Name
7472
{
7573
get { return ObjectAccessor<T>.Instance.Table; }
7674
}
7775

7876
/// <summary>
79-
/// Gets the type of entity contained by this table.
77+
/// Gets the type of entity contained by this table.
8078
/// </summary>
81-
/// <value>
82-
/// The type.
83-
/// </value>
79+
/// <value> The type. </value>
8480
public Type Type
8581
{
8682
get { return ObjectAccessor<T>.Instance.Type; }
8783
}
8884

89-
#region IQueryable<T> Members
85+
#endregion
86+
87+
#region IOrderedQueryable<T> Members
9088

9189
public IEnumerator<T> GetEnumerator()
9290
{
93-
return ((IEnumerable<T>)Provider.Execute(_expression)).GetEnumerator();
91+
return ((IEnumerable<T>) Provider.Execute(_expression)).GetEnumerator();
9492
}
9593

9694
IEnumerator IEnumerable.GetEnumerator()
@@ -100,7 +98,7 @@ IEnumerator IEnumerable.GetEnumerator()
10098

10199
public Type ElementType
102100
{
103-
get { return typeof(T); }
101+
get { return typeof (T); }
104102
}
105103

106104
public Expression Expression

0 commit comments

Comments
 (0)