|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq.Expressions; |
| 4 | +using System.Diagnostics.Contracts; |
| 5 | + |
| 6 | +namespace SQLitePCL.pretty.Orm |
| 7 | +{ |
| 8 | + public static partial class SqlQuery |
| 9 | + { |
| 10 | + public sealed class SelectClause<T> : ISqlQuery |
| 11 | + { |
| 12 | + // FIXME: Long term, prefer some sort of expression syntax |
| 13 | + private readonly IReadOnlyList<String> select; |
| 14 | + private readonly FromClause<T> from; |
| 15 | + |
| 16 | + internal SelectClause(FromClause<T> from, IReadOnlyList<String> select) |
| 17 | + { |
| 18 | + this.from = from; |
| 19 | + this.select = select; |
| 20 | + } |
| 21 | + |
| 22 | + public WhereClause<T> Where<U,V,W,X,Y,Z>(Expression<Func<T,U,V,W,X,Y,Z,bool>> predExpr) |
| 23 | + { |
| 24 | + Contract.Requires(predExpr != null); |
| 25 | + return this.Where((LambdaExpression) predExpr); |
| 26 | + } |
| 27 | + |
| 28 | + public WhereClause<T> Where<U,V,W,X,Y>(Expression<Func<T,U,V,W,X,Y,bool>> predExpr) |
| 29 | + { |
| 30 | + Contract.Requires(predExpr != null); |
| 31 | + return this.Where((LambdaExpression) predExpr); |
| 32 | + } |
| 33 | + |
| 34 | + public WhereClause<T> Where<U,V,W,X>(Expression<Func<T,U,V,W,X,bool>> predExpr) |
| 35 | + { |
| 36 | + Contract.Requires(predExpr != null); |
| 37 | + return this.Where((LambdaExpression) predExpr); |
| 38 | + } |
| 39 | + |
| 40 | + public WhereClause<T> Where<U,V,W>(Expression<Func<T,U,V,W,bool>> predExpr) |
| 41 | + { |
| 42 | + Contract.Requires(predExpr != null); |
| 43 | + return this.Where((LambdaExpression) predExpr); |
| 44 | + } |
| 45 | + |
| 46 | + public WhereClause<T> Where<U,V>(Expression<Func<T,U,V,bool>> predExpr) |
| 47 | + { |
| 48 | + Contract.Requires(predExpr != null); |
| 49 | + return this.Where((LambdaExpression) predExpr); |
| 50 | + } |
| 51 | + |
| 52 | + public WhereClause<T> Where<U>(Expression<Func<T,U,bool>> predExpr) |
| 53 | + { |
| 54 | + Contract.Requires(predExpr != null); |
| 55 | + return this.Where((LambdaExpression) predExpr); |
| 56 | + } |
| 57 | + |
| 58 | + public WhereClause<T> Where(Expression<Func<T, bool>> predExpr) |
| 59 | + { |
| 60 | + Contract.Requires(predExpr != null); |
| 61 | + return this.Where((LambdaExpression) predExpr); |
| 62 | + } |
| 63 | + |
| 64 | + private WhereClause<T> Where(LambdaExpression lambda) |
| 65 | + { |
| 66 | + var pred = lambda.Body; |
| 67 | + return new WhereClause<T>(this, pred); |
| 68 | + } |
| 69 | + |
| 70 | + private WhereClause<T> Where() |
| 71 | + { |
| 72 | + return new WhereClause<T>(this, null); |
| 73 | + } |
| 74 | + |
| 75 | + /// <summary> |
| 76 | + /// Performs a subsequent ordering of the elements in a sequence in ascending order according to a key. |
| 77 | + /// </summary> |
| 78 | + /// <returns>An <seealso cref="OrderByClause<T>"/>.</returns> |
| 79 | + /// <param name="orderExpr">A function to extract a key from each element.</param> |
| 80 | + /// <typeparam name="TValue">The type of the key</typeparam> |
| 81 | + public OrderByClause<T> OrderBy<TValue>(Expression<Func<T, TValue>> orderExpr) |
| 82 | + { |
| 83 | + return this.Where().OrderBy(orderExpr); |
| 84 | + } |
| 85 | + |
| 86 | + /// <summary> |
| 87 | + /// Performs a subsequent ordering of the elements in a sequence in descending order according to a key. |
| 88 | + /// </summary> |
| 89 | + /// <returns>An <seealso cref="OrderByClause<T>"/>.</returns> |
| 90 | + /// <param name="orderExpr">A function to extract a key from each element.</param> |
| 91 | + /// <typeparam name="TValue">The type of the key</typeparam> |
| 92 | + public OrderByClause<T> OrderByDescending<TValue>(Expression<Func<T, TValue>> orderExpr) |
| 93 | + { |
| 94 | + return this.Where().OrderByDescending(orderExpr); |
| 95 | + } |
| 96 | + |
| 97 | + /// <summary> |
| 98 | + /// Returns a <see cref="LimitClause<T>"/> that limits the result set to a specified number of contiguous elements. |
| 99 | + /// </summary> |
| 100 | + /// <param name="n">The number of elements to return.</param> |
| 101 | + /// <returns>A new <see cref="LimitClause<T>"/>.</returns> |
| 102 | + public LimitClause<T> Take(int n) |
| 103 | + { |
| 104 | + return this.Where().Take(n); |
| 105 | + } |
| 106 | + |
| 107 | + /// <summary> |
| 108 | + /// Returns a <see cref="LimitClause<T>"/> that skips a specified number of elements in the result set and then returns the remaining elements. |
| 109 | + /// </summary> |
| 110 | + /// <param name="n">The number of elements to skip before returning the remaining elements.</param> |
| 111 | + /// <returns>A new <see cref="LimitClause<T>"/>.</returns> |
| 112 | + public LimitClause<T> Skip(int n) |
| 113 | + { |
| 114 | + return this.Where().Skip(n); |
| 115 | + } |
| 116 | + |
| 117 | + /// <summary> |
| 118 | + /// Returns a <see cref="LimitClause<T>"/> that returns the element at a specified index in the result set. |
| 119 | + /// </summary> |
| 120 | + /// <returns>The <see cref="LimitClause<T>"/>.</returns> |
| 121 | + /// <param name="index">Index.</param> |
| 122 | + /// <returns>A new <see cref="LimitClause<T>"/>.</returns> |
| 123 | + public LimitClause<T> ElementAt(int index) |
| 124 | + { |
| 125 | + return this.Where().ElementAt(index); |
| 126 | + } |
| 127 | + |
| 128 | + /// <summary> |
| 129 | + /// Returns a <see cref="System.String"/> that represents the current <see cref="SelectClause<T>"/>. |
| 130 | + /// </summary> |
| 131 | + /// <returns>A <see cref="System.String"/> that represents the current <see cref="SelectClause<T>"/>.</returns> |
| 132 | + public override string ToString() |
| 133 | + { |
| 134 | + return |
| 135 | + "SELECT " + string.Join(", ", select) + |
| 136 | + "\r\n" + from.ToString(); |
| 137 | + } |
| 138 | + } |
| 139 | + } |
| 140 | +} |
| 141 | + |
0 commit comments