Skip to content

Commit a171ffc

Browse files
committed
Add join and SELECT DISTINCT support
1 parent 707d361 commit a171ffc

5 files changed

Lines changed: 300 additions & 32 deletions

File tree

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,9 @@
187187
<Compile Include="SqlCompiler.cs">
188188
<DependentUpon>SqlQuery.cs</DependentUpon>
189189
</Compile>
190+
<Compile Include="SqlQuery.Join.cs">
191+
<DependentUpon>SqlQuery.cs</DependentUpon>
192+
</Compile>
190193
</ItemGroup>
191194
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\Portable\$(TargetFrameworkVersion)\Microsoft.Portable.CSharp.targets" />
192195
<ItemGroup>

SQLitePCL.pretty.Orm/SqlQuery.From.cs

Lines changed: 79 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Linq;
33
using System.Linq.Expressions;
4+
using System.Collections.Generic;
45

56
namespace SQLitePCL.pretty.Orm.Sql
67
{
@@ -43,14 +44,34 @@ internal FromClause(string clause) : base(clause)
4344
{
4445
}
4546

47+
public JoinClause<T,T2> Join<T2>(Expression<Func<T,T2,bool>> predExpr)
48+
{
49+
var join = new List<string>();
50+
join.Add(SqlCompiler.CompileJoinClause(false, typeof(T2),predExpr));
51+
return new JoinClause<T,T2>(this, join);
52+
}
53+
54+
public JoinClause<T,T2> LeftJoin<T2>(Expression<Func<T,T2,bool>> predExpr)
55+
{
56+
var join = new List<string>();
57+
join.Add(SqlCompiler.CompileJoinClause(true, typeof(T2),predExpr));
58+
return new JoinClause<T,T2>(this, join);
59+
}
60+
4661
/// <summary>
4762
/// Select all columns from the table.
4863
/// </summary>
4964
public SelectClause<T> Select()
5065
{
51-
return new SelectClause<T>(
52-
this,
53-
SqlCompiler.SelectAllColumnsClause(typeof(T)));
66+
return new JoinClause<T>(this, new List<string>()).Select();
67+
}
68+
69+
/// <summary>
70+
/// Select all columns from the table returning only distinct rows.
71+
/// </summary>
72+
public SelectClause<T> SelectDistinct()
73+
{
74+
return new JoinClause<T>(this, new List<string>()).SelectDistinct();
5475
}
5576
}
5677

@@ -63,14 +84,34 @@ internal FromClause(string clause) : base(clause)
6384
{
6485
}
6586

87+
public JoinClause<T1,T2,T3> Join<T3>(Expression<Func<T1,T2,T3,bool>> predExpr)
88+
{
89+
var join = new List<string>();
90+
join.Add(SqlCompiler.CompileJoinClause(false, typeof(T3),predExpr));
91+
return new JoinClause<T1,T2,T3>(this, join);
92+
}
93+
94+
public JoinClause<T1,T2,T3> LeftJoin<T3>(Expression<Func<T1,T2,T3,bool>> predExpr)
95+
{
96+
var join = new List<string>();
97+
join.Add(SqlCompiler.CompileJoinClause(true, typeof(T3),predExpr));
98+
return new JoinClause<T1,T2,T3>(this, join);
99+
}
100+
66101
/// <summary>
67102
/// Select all columns from the table.
68103
/// </summary>
69104
public SelectClause<T1,T2> Select()
70105
{
71-
return new SelectClause<T1,T2>(
72-
this,
73-
SqlCompiler.SelectAllColumnsClause(typeof(T1), typeof(T2)));
106+
return new JoinClause<T1,T2>(this, new List<string>()).Select();
107+
}
108+
109+
/// <summary>
110+
/// Select all columns from the table returning only distinct rows.
111+
/// </summary>
112+
public SelectClause<T1,T2> SelectDistinct()
113+
{
114+
return new JoinClause<T1,T2>(this, new List<string>()).SelectDistinct();
74115
}
75116
}
76117

@@ -83,14 +124,34 @@ internal FromClause(string clause) : base(clause)
83124
{
84125
}
85126

127+
public JoinClause<T1,T2,T3,T4> Join<T4>(Expression<Func<T1,T2,T3,T4,bool>> predExpr)
128+
{
129+
var join = new List<string>();
130+
join.Add(SqlCompiler.CompileJoinClause(false, typeof(T4),predExpr));
131+
return new JoinClause<T1,T2,T3,T4>(this, join);
132+
}
133+
134+
public JoinClause<T1,T2,T3,T4> LeftJoin<T4>(Expression<Func<T1,T2,T3,T4,bool>> predExpr)
135+
{
136+
var join = new List<string>();
137+
join.Add(SqlCompiler.CompileJoinClause(true, typeof(T4),predExpr));
138+
return new JoinClause<T1,T2,T3,T4>(this, join);
139+
}
140+
86141
/// <summary>
87142
/// Select all columns from the table.
88143
/// </summary>
89144
public SelectClause<T1,T2,T3> Select()
90145
{
91-
return new SelectClause<T1,T2,T3>(
92-
this,
93-
SqlCompiler.SelectAllColumnsClause(typeof(T1), typeof(T2), typeof(T3)));
146+
return new JoinClause<T1,T2,T3>(this, new List<string>()).Select();
147+
}
148+
149+
/// <summary>
150+
/// Select all columns from the table returning only distinct rows.
151+
/// </summary>
152+
public SelectClause<T1,T2,T3> SelectDistinct()
153+
{
154+
return new JoinClause<T1,T2,T3>(this, new List<string>()).SelectDistinct();
94155
}
95156
}
96157

@@ -108,9 +169,15 @@ internal FromClause(string clause) : base(clause)
108169
/// </summary>
109170
public SelectClause<T1,T2,T3,T4> Select()
110171
{
111-
return new SelectClause<T1,T2,T3,T4>(
112-
this,
113-
SqlCompiler.SelectAllColumnsClause(typeof(T1), typeof(T2), typeof(T3), typeof(T4)));
172+
return new JoinClause<T1,T2,T3,T4>(this, new List<string>()).Select();
173+
}
174+
175+
/// <summary>
176+
/// Select all columns from the table returning only distinct rows.
177+
/// </summary>
178+
public SelectClause<T1,T2,T3,T4> SelectDistinct()
179+
{
180+
return new JoinClause<T1,T2,T3,T4>(this, new List<string>()).SelectDistinct();
114181
}
115182
}
116183
}
Lines changed: 188 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,188 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq.Expressions;
4+
5+
namespace SQLitePCL.pretty.Orm.Sql
6+
{
7+
internal static partial class SqlCompiler
8+
{
9+
internal static string CompileJoinClause(bool left, Type table, Expression expr)
10+
{
11+
var tableName = TableMapping.Get(table).TableName;
12+
return (left ? "LEFT JOIN " : "INNER JOIN ") + "\"" + tableName + "\" ON " + expr.CompileExpr();
13+
}
14+
}
15+
16+
/// <summary>
17+
/// The JOIN clause of a SQL query.
18+
/// </summary>
19+
public abstract class JoinClause
20+
{
21+
private readonly FromClause from;
22+
private readonly IReadOnlyList<string> join;
23+
24+
internal JoinClause(FromClause from, IReadOnlyList<string> join)
25+
{
26+
this.from = from;
27+
this.join = join;
28+
}
29+
30+
internal FromClause From { get { return from; } }
31+
internal IReadOnlyList<string> JoinConstraints { get { return join; } }
32+
33+
/// <summary>
34+
/// Returns a <see cref="System.String"/> that represents the current <see cref="SQLitePCL.pretty.Orm.Sql.JoinClause"/>.
35+
/// </summary>
36+
/// <returns>A <see cref="System.String"/> that represents the current <see cref="SQLitePCL.pretty.Orm.Sql.JoinClause"/>.</returns>
37+
public override string ToString()
38+
{
39+
return from + (join.Count == 0 ? "" : "\r\n" + string.Join("\r\n", join));
40+
}
41+
}
42+
43+
internal sealed class JoinClause<T> : JoinClause
44+
{
45+
internal JoinClause(FromClause from, IReadOnlyList<string> join) : base(from, join)
46+
{
47+
}
48+
49+
/// <summary>
50+
/// Select all columns from the table.
51+
/// </summary>
52+
public SelectClause<T> Select()
53+
{
54+
return new SelectClause<T>(
55+
this,
56+
SqlCompiler.SelectAllColumnsClause(false, typeof(T)));
57+
}
58+
59+
/// <summary>
60+
/// Select all columns from the table returning only distinct rows.
61+
/// </summary>
62+
public SelectClause<T> SelectDistinct()
63+
{
64+
return new SelectClause<T>(
65+
this,
66+
SqlCompiler.SelectAllColumnsClause(true, typeof(T)));
67+
}
68+
}
69+
70+
/// <summary>
71+
/// The JOIN clause of a SQL query.
72+
/// </summary>
73+
public sealed class JoinClause<T1,T2> : JoinClause
74+
{
75+
internal JoinClause(FromClause from, IReadOnlyList<string> join) : base(from, join)
76+
{
77+
}
78+
79+
public JoinClause<T1,T2,T3> Join<T3>(Expression<Func<T1,T2,T3,bool>> predExpr)
80+
{
81+
var joins = new List<String>(this.JoinConstraints);
82+
joins.Add(SqlCompiler.CompileJoinClause(false, typeof(T3), predExpr));
83+
return new JoinClause<T1,T2,T3>(this.From, joins);
84+
}
85+
86+
public JoinClause<T1,T2,T3> LeftJoin<T3>(Expression<Func<T1,T2,T3,bool>> predExpr)
87+
{
88+
var joins = new List<String>(this.JoinConstraints);
89+
joins.Add(SqlCompiler.CompileJoinClause(true, typeof(T3), predExpr));
90+
return new JoinClause<T1,T2,T3>(this.From, joins);
91+
}
92+
93+
/// <summary>
94+
/// Select all columns from the table.
95+
/// </summary>
96+
public SelectClause<T1,T2> Select()
97+
{
98+
return new SelectClause<T1,T2>(
99+
this,
100+
SqlCompiler.SelectAllColumnsClause(false, typeof(T1), typeof(T2)));
101+
}
102+
103+
/// <summary>
104+
/// Select all columns from the table returning only distinct rows.
105+
/// </summary>
106+
public SelectClause<T1,T2> SelectDistinct()
107+
{
108+
return new SelectClause<T1,T2>(
109+
this,
110+
SqlCompiler.SelectAllColumnsClause(true, typeof(T1), typeof(T2)));
111+
}
112+
}
113+
114+
/// <summary>
115+
/// The JOIN clause of a SQL query.
116+
/// </summary>
117+
public sealed class JoinClause<T1,T2,T3> : JoinClause
118+
{
119+
internal JoinClause(FromClause from, IReadOnlyList<string> join) : base(from, join)
120+
{
121+
}
122+
123+
public JoinClause<T1,T2,T3,T4> Join<T4>(Expression<Func<T1,T2,T3,T4,bool>> predExpr)
124+
{
125+
var joins = new List<String>(this.JoinConstraints);
126+
joins.Add(SqlCompiler.CompileJoinClause(false, typeof(T4), predExpr));
127+
return new JoinClause<T1,T2,T3,T4>(this.From, joins);
128+
}
129+
130+
public JoinClause<T1,T2,T3,T4> LeftJoin<T4>(Expression<Func<T1,T2,T3,T4,bool>> predExpr)
131+
{
132+
var joins = new List<String>(this.JoinConstraints);
133+
joins.Add(SqlCompiler.CompileJoinClause(true, typeof(T4), predExpr));
134+
return new JoinClause<T1,T2,T3,T4>(this.From, joins);
135+
}
136+
137+
/// <summary>
138+
/// Select all columns from the table.
139+
/// </summary>
140+
public SelectClause<T1,T2,T3> Select()
141+
{
142+
return new SelectClause<T1,T2,T3>(
143+
this,
144+
SqlCompiler.SelectAllColumnsClause(false, typeof(T1), typeof(T2), typeof(T3)));
145+
}
146+
147+
/// <summary>
148+
/// Select all columns from the table returning only distinct rows.
149+
/// </summary>
150+
public SelectClause<T1,T2,T3> SelectDistinct()
151+
{
152+
return new SelectClause<T1,T2,T3>(
153+
this,
154+
SqlCompiler.SelectAllColumnsClause(true, typeof(T1), typeof(T2), typeof(T3)));
155+
}
156+
}
157+
158+
/// <summary>
159+
/// The JOIN clause of a SQL query.
160+
/// </summary>
161+
public sealed class JoinClause<T1,T2,T3,T4> : JoinClause
162+
{
163+
internal JoinClause(FromClause from, IReadOnlyList<string> join) : base(from, join)
164+
{
165+
}
166+
167+
/// <summary>
168+
/// Select all columns from the table.
169+
/// </summary>
170+
public SelectClause<T1,T2,T3,T4> Select()
171+
{
172+
return new SelectClause<T1,T2,T3,T4>(
173+
this,
174+
SqlCompiler.SelectAllColumnsClause(false, typeof(T1), typeof(T2), typeof(T3), typeof(T4)));
175+
}
176+
177+
/// <summary>
178+
/// Select all columns from the table returning only distinct rows.
179+
/// </summary>
180+
public SelectClause<T1,T2,T3,T4> SelectDistinct()
181+
{
182+
return new SelectClause<T1,T2,T3,T4>(
183+
this,
184+
SqlCompiler.SelectAllColumnsClause(true, typeof(T1), typeof(T2), typeof(T3), typeof(T4)));
185+
}
186+
}
187+
}
188+

0 commit comments

Comments
 (0)