Permalink
Browse files

added LIMIT to the LINQ expression

  • Loading branch information...
1 parent 7c8b121 commit ab42ba35c9c4c1db84121ae168a64fff107d2dd6 @nberardi nberardi committed Feb 13, 2012
View
20 src/Linq/CqlQueryEvaluator.cs
@@ -25,19 +25,25 @@ public string Query
var select = Fields;
var from = _columnFamily;
var where = WhereCriteria;
+ var limit = LimitCount;
var query = String.Format("SELECT {0} \nFROM {1}", select, from);
if (!String.IsNullOrWhiteSpace(where))
query += " \nWHERE " + where;
+ if (limit > 0)
+ query += " \nLIMIT " + limit;
+
return query;
}
}
private IList<string> FieldsArray { get; set; }
- private string FirstCount { get; set; }
+ private int FirstCount { get; set; }
+
+ private int LimitCount { get; set; }
private string Fields
{
@@ -46,7 +52,7 @@ private string Fields
var firstCount = FirstCount;
var query = "";
- if (!String.IsNullOrWhiteSpace(firstCount))
+ if (firstCount > 0)
query += "FIRST " + firstCount;
if (query.Length > 0)
@@ -203,10 +209,20 @@ private void VisitMethodCall(MethodCallExpression exp)
AddCriteria(exp.Arguments[1]);
else if (exp.Method.Name == "Select")
AddField(SimplifyExpression(exp.Arguments[1]));
+ else if (exp.Method.Name == "Take")
+ SetLimit(exp.Arguments[1]);
else
throw new NotSupportedException("Method call to " + exp.Method.Name + " is not supported.");
}
+ private void SetLimit(Expression exp)
+ {
+ if (exp.NodeType == ExpressionType.Constant)
+ {
+ LimitCount = (int)((ConstantExpression)exp).Value;
+ }
+ }
+
private IEnumerable<CassandraObject> VisitSelectExpression(Expression exp)
{
switch (exp.NodeType)
View
1 test/FluentCassandra.Sandbox/Program.cs
@@ -182,6 +182,7 @@ private static void ReadAllPosts()
{
var key = "first-blog-post";
+ var postFamily = db.GetColumnFamily("Posts");
var tagsFamily = db.GetColumnFamily("Tags");
// get the post back from the database
View
14 test/FluentCassandra.Tests/Linq/LinqTests.cs
@@ -58,6 +58,20 @@ public void SELECT()
}
[Test]
+ public void LIMIT()
+ {
+ var expected = "SELECT * FROM Users LIMIT 25";
+
+ var query = (
+ from f in _family
+ select f).Take(25);
+
+ var actual = query.ToString();
+
+ AreEqual(expected, actual);
+ }
+
+ [Test]
public void SELECT_One_Column()
{
var expected = "SELECT Age FROM Users";

0 comments on commit ab42ba3

Please sign in to comment.