|
| 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