|
9 | 9 |
|
10 | 10 | namespace ServiceStack.OrmLite
|
11 | 11 | {
|
12 |
| - public abstract class SqlExpression<T> |
| 12 | + public abstract class SqlExpression<T> : ISqlExpression |
13 | 13 | {
|
14 | 14 | private Expression<Func<T, bool>> underlyingExpression;
|
15 | 15 | private List<string> orderByProperties = new List<string>();
|
16 | 16 | private string selectExpression = string.Empty;
|
| 17 | + private string fromExpression = null; |
17 | 18 | private string whereExpression;
|
18 | 19 | private string groupBy = string.Empty;
|
19 | 20 | private string havingExpression;
|
@@ -47,6 +48,7 @@ protected virtual SqlExpression<T> CopyTo(SqlExpression<T> to)
|
47 | 48 | to.underlyingExpression = underlyingExpression;
|
48 | 49 | to.orderByProperties = orderByProperties;
|
49 | 50 | to.selectExpression = selectExpression;
|
| 51 | + to.fromExpression = fromExpression; |
50 | 52 | to.whereExpression = whereExpression;
|
51 | 53 | to.groupBy = groupBy;
|
52 | 54 | to.havingExpression = havingExpression;
|
@@ -112,6 +114,30 @@ public virtual SqlExpression<T> SelectDistinct<TKey>(Expression<Func<T, TKey>> f
|
112 | 114 | return this;
|
113 | 115 | }
|
114 | 116 |
|
| 117 | + public virtual SqlExpression<T> From(string tables) |
| 118 | + { |
| 119 | + if (string.IsNullOrEmpty(tables)) |
| 120 | + { |
| 121 | + FromExpression = null; |
| 122 | + } |
| 123 | + else |
| 124 | + { |
| 125 | + tables.SqlVerifyFragment(); |
| 126 | + var singleTable = tables.ToLower().IndexOfAny("join", ",") >= 0; |
| 127 | + FromExpression = singleTable |
| 128 | + ? " \nFROM " + OrmLiteConfig.DialectProvider.GetQuotedTableName(tables) |
| 129 | + : " \nFROM " + tables; |
| 130 | + } |
| 131 | + |
| 132 | + return this; |
| 133 | + } |
| 134 | + |
| 135 | + public virtual SqlExpression<T> From<Table>() |
| 136 | + { |
| 137 | + FromExpression = " \nFROM " + OrmLiteConfig.DialectProvider.GetQuotedTableName(typeof(Table).GetModelDefinition()); |
| 138 | + return this; |
| 139 | + } |
| 140 | + |
115 | 141 | public virtual SqlExpression<T> Where()
|
116 | 142 | {
|
117 | 143 | if (underlyingExpression != null) underlyingExpression = null; //Where() clears the expression
|
@@ -457,6 +483,9 @@ public virtual string ToSelectStatement()
|
457 | 483 | var sql = new StringBuilder();
|
458 | 484 |
|
459 | 485 | sql.Append(SelectExpression);
|
| 486 | + |
| 487 | + sql.Append(FromExpression); |
| 488 | + |
460 | 489 | sql.Append(string.IsNullOrEmpty(WhereExpression) ?
|
461 | 490 | "" :
|
462 | 491 | "\n" + WhereExpression);
|
@@ -492,6 +521,17 @@ public string SelectExpression
|
492 | 521 | }
|
493 | 522 | }
|
494 | 523 |
|
| 524 | + public string FromExpression |
| 525 | + { |
| 526 | + get |
| 527 | + { |
| 528 | + return string.IsNullOrEmpty(fromExpression) |
| 529 | + ? " \nFROM " + OrmLiteConfig.DialectProvider.GetQuotedTableName(modelDef) |
| 530 | + : fromExpression; |
| 531 | + } |
| 532 | + set { fromExpression = value; } |
| 533 | + } |
| 534 | + |
495 | 535 | public string WhereExpression
|
496 | 536 | {
|
497 | 537 | get
|
@@ -1035,13 +1075,11 @@ protected static object GetQuotedFalseValue()
|
1035 | 1075 |
|
1036 | 1076 | private void BuildSelectExpression(string fields, bool distinct)
|
1037 | 1077 | {
|
1038 |
| - |
1039 |
| - selectExpression = string.Format("SELECT {0}{1} \nFROM {2}", |
| 1078 | + selectExpression = string.Format("SELECT {0}{1}", |
1040 | 1079 | (distinct ? "DISTINCT " : ""),
|
1041 | 1080 | (string.IsNullOrEmpty(fields) ?
|
1042 | 1081 | OrmLiteConfig.DialectProvider.GetColumnNames(modelDef) :
|
1043 |
| - fields), |
1044 |
| - OrmLiteConfig.DialectProvider.GetQuotedTableName(modelDef)); |
| 1082 | + fields)); |
1045 | 1083 | }
|
1046 | 1084 |
|
1047 | 1085 | public IList<string> GetAllFields()
|
@@ -1263,6 +1301,11 @@ protected virtual object VisitColumnAccessMethod(MethodCallExpression m)
|
1263 | 1301 | }
|
1264 | 1302 | }
|
1265 | 1303 |
|
| 1304 | + public interface ISqlExpression |
| 1305 | + { |
| 1306 | + string ToSelectStatement(); |
| 1307 | + } |
| 1308 | + |
1266 | 1309 | public class PartialSqlString
|
1267 | 1310 | {
|
1268 | 1311 | public PartialSqlString(string text)
|
|
0 commit comments