Skip to content
This repository was archived by the owner on Dec 24, 2022. It is now read-only.

Commit bf5907d

Browse files
committed
Add overloads for appending free-text conditions
1 parent 17195c8 commit bf5907d

File tree

1 file changed

+29
-11
lines changed

1 file changed

+29
-11
lines changed

src/ServiceStack.OrmLite/Expressions/SqlExpression.cs

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -145,13 +145,25 @@ public virtual SqlExpression<T> Where()
145145

146146
public virtual SqlExpression<T> Where(string sqlFilter, params object[] filterParams)
147147
{
148-
whereExpression = !string.IsNullOrEmpty(sqlFilter)
149-
? sqlFilter.SqlFmt(filterParams)
150-
: string.Empty;
151-
152-
if (!string.IsNullOrEmpty(whereExpression))
153-
whereExpression = (WhereStatementWithoutWhereString ? "" : "WHERE ") + whereExpression;
154-
148+
AppendToWhere("AND", sqlFilter.SqlFmt(filterParams).SqlVerifyFragment());
149+
return this;
150+
}
151+
152+
public virtual SqlExpression<T> And(string sqlFilter, params object[] filterParams)
153+
{
154+
AppendToWhere("AND", sqlFilter.SqlFmt(filterParams).SqlVerifyFragment());
155+
return this;
156+
}
157+
158+
public virtual SqlExpression<T> Or(string sqlFilter, params object[] filterParams)
159+
{
160+
AppendToWhere("OR", sqlFilter.SqlFmt(filterParams).SqlVerifyFragment());
161+
return this;
162+
}
163+
164+
public virtual SqlExpression<T> AddCondition(string condition, string sqlFilter, params object[] filterParams)
165+
{
166+
AppendToWhere(condition, sqlFilter.SqlFmt(filterParams).SqlVerifyFragment());
155167
return this;
156168
}
157169

@@ -173,18 +185,24 @@ public virtual SqlExpression<T> Or(Expression<Func<T, bool>> predicate)
173185
return this;
174186
}
175187

176-
protected void AppendToWhere(string operand, Expression predicate)
188+
protected void AppendToWhere(string condition, Expression predicate)
177189
{
178190
if (predicate == null)
179191
return;
180192

181193
useFieldName = true;
182194
sep = " ";
183195
var newExpr = Visit(predicate).ToString();
196+
AppendToWhere(condition, newExpr);
197+
}
198+
199+
protected void AppendToWhere(string condition, string sqlExpression)
200+
{
184201
whereExpression = string.IsNullOrEmpty(whereExpression)
185-
? (WhereStatementWithoutWhereString ? "" : "WHERE ")
186-
: whereExpression + " " + operand + " ";
187-
whereExpression += newExpr;
202+
? (WhereStatementWithoutWhereString ? "" : "WHERE ")
203+
: whereExpression + " " + condition + " ";
204+
205+
whereExpression += sqlExpression;
188206
}
189207

190208
public virtual SqlExpression<T> GroupBy()

0 commit comments

Comments
 (0)