Skip to content

Commit f9d6c30

Browse files
committed
Move Query functions into Database class.
1 parent 3c4e6ab commit f9d6c30

4 files changed

Lines changed: 78 additions & 48 deletions

File tree

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Diagnostics.Contracts;
4+
5+
namespace SQLitePCL.pretty.Orm
6+
{
7+
public static partial class DatabaseConnection
8+
{
9+
public static IStatement PrepareStatement(this IDatabaseConnection This, ISqlQuery query)
10+
{
11+
Contract.Requires(This != null);
12+
Contract.Requires(query != null);
13+
return This.PrepareStatement(query.ToString());
14+
}
15+
16+
public static IEnumerable<IReadOnlyList<IResultSetValue>> Query(this IDatabaseConnection This, ISqlQuery query)
17+
{
18+
Contract.Requires(This != null);
19+
Contract.Requires(query != null);
20+
return This.Query(query.ToString());
21+
}
22+
23+
public static IEnumerable<IReadOnlyList<IResultSetValue>> Query(
24+
this IDatabaseConnection This, ISqlQuery query, params object[] values)
25+
{
26+
Contract.Requires(This != null);
27+
Contract.Requires(query != null);
28+
Contract.Requires(values != null);
29+
return This.Query(query.ToString(), values);
30+
}
31+
}
32+
33+
public static partial class AsyncDatabaseConnection
34+
{
35+
public static IObservable<IReadOnlyList<IResultSetValue>> Query(this IAsyncDatabaseConnection This, ISqlQuery query)
36+
{
37+
Contract.Requires(This != null);
38+
Contract.Requires(query != null);
39+
return This.Query(query.ToString());
40+
}
41+
42+
public static IObservable<IReadOnlyList<IResultSetValue>> Query(
43+
this IAsyncDatabaseConnection This, ISqlQuery query, params object[] values)
44+
{
45+
Contract.Requires(This != null);
46+
Contract.Requires(query != null);
47+
Contract.Requires(values != null);
48+
return This.Query(query.ToString(), values);
49+
}
50+
}
51+
}
52+

SQLitePCL.pretty.Orm/SQLitePCL.pretty.Orm.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@
154154
<Compile Include="DatabaseConnection.InsertOrReplace.cs" />
155155
<Compile Include="SqlQuery.From.cs" />
156156
<Compile Include="ResultSet.cs" />
157+
<Compile Include="DatabaseConnection.Query.cs" />
157158
</ItemGroup>
158159
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\Portable\$(TargetFrameworkVersion)\Microsoft.Portable.CSharp.targets" />
159160
<ItemGroup>

SQLitePCL.pretty.Orm/SqlQuery.OrderBy.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,31 @@ public override string ToString()
8282
(ordering.Count > 0 ? "\r\nORDER BY " + string.Join(", ", ordering.Select(o => "\"" + o.Item1 + "\"" + (o.Item2 ? "" : " DESC"))) : "");
8383
}
8484
}
85+
86+
private static Tuple<string, bool> CompileOrderByExpression<T, TValue>(this Expression<Func<T, TValue>> orderExpr, bool asc)
87+
{
88+
var lambda = orderExpr;
89+
90+
MemberExpression mem = null;
91+
92+
var unary = lambda.Body as UnaryExpression;
93+
if (unary != null && unary.NodeType == ExpressionType.Convert)
94+
{
95+
96+
mem = unary.Operand as MemberExpression;
97+
}
98+
else
99+
{
100+
mem = lambda.Body as MemberExpression;
101+
}
102+
103+
if (mem != null && (mem.Expression.NodeType == ExpressionType.Parameter))
104+
{
105+
return Tuple.Create(((PropertyInfo) mem.Member).GetColumnName(), asc);
106+
}
107+
108+
throw new NotSupportedException("Order By does not support: " + orderExpr);
109+
}
85110
}
86111
}
87112

SQLitePCL.pretty.Orm/SqlQuery.cs

Lines changed: 0 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -69,29 +69,6 @@ public static bool IsNot<T>(this Nullable<T> This, Nullable<T> other = null)
6969

7070
public static partial class SqlQuery
7171
{
72-
public static IEnumerable<IReadOnlyList<IResultSetValue>> Query(this IDatabaseConnection This, ISqlQuery query)
73-
{
74-
Contract.Requires(This != null);
75-
Contract.Requires(query != null);
76-
return This.Query(query.ToString());
77-
}
78-
79-
public static IEnumerable<IReadOnlyList<IResultSetValue>> Query(
80-
this IDatabaseConnection This, ISqlQuery query, params object[] values)
81-
{
82-
Contract.Requires(This != null);
83-
Contract.Requires(query != null);
84-
Contract.Requires(values != null);
85-
return This.Query(query.ToString(), values);
86-
}
87-
88-
public static IStatement PrepareStatement(this IDatabaseConnection This, ISqlQuery query)
89-
{
90-
Contract.Requires(This != null);
91-
Contract.Requires(query != null);
92-
return This.PrepareStatement(query.ToString());
93-
}
94-
9572
public static FromClause<T> From<T>()
9673
{
9774
var typ = typeof(T);
@@ -192,30 +169,5 @@ private static string GetSqlName(Expression prop)
192169
throw new NotSupportedException ("Cannot get SQL for: " + n);
193170
}
194171
}
195-
196-
private static Tuple<string, bool> CompileOrderByExpression<T, TValue>(this Expression<Func<T, TValue>> orderExpr, bool asc)
197-
{
198-
var lambda = orderExpr;
199-
200-
MemberExpression mem = null;
201-
202-
var unary = lambda.Body as UnaryExpression;
203-
if (unary != null && unary.NodeType == ExpressionType.Convert)
204-
{
205-
206-
mem = unary.Operand as MemberExpression;
207-
}
208-
else
209-
{
210-
mem = lambda.Body as MemberExpression;
211-
}
212-
213-
if (mem != null && (mem.Expression.NodeType == ExpressionType.Parameter))
214-
{
215-
return Tuple.Create(((PropertyInfo) mem.Member).GetColumnName(), asc);
216-
}
217-
218-
throw new NotSupportedException("Order By does not support: " + orderExpr);
219-
}
220172
}
221173
}

0 commit comments

Comments
 (0)