Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

String constants for component naming #699

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions QueryBuilder.Tests/GeneralTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ public void AddOrReplace_Works(string table, string engine)
var query = new Query();
if (table != null)
query.From(table);
query.AddOrReplaceComponent("from", new FromClause() { Table = "updated", Engine = engine });
query.AddOrReplaceComponent(ComponentName.From, new FromClause() { Table = "updated", Engine = engine });
var froms = query.Clauses.OfType<FromClause>();

Assert.Single(froms);
Expand All @@ -262,7 +262,7 @@ public void GetOneComponent_Prefers_Engine(string engine, string column)
.Where("generic", "foo")
.ForSqlServer(q => q.Where("mssql", "foo"));

var where = query.GetOneComponent("where", engine) as BasicCondition;
var where = query.GetOneComponent(ComponentName.Where, engine) as BasicCondition;

Assert.NotNull(where);
Assert.Equal(column, where.Column);
Expand All @@ -275,7 +275,7 @@ public void AddOrReplace_Throws_MoreThanOne()
.Where("a", "b")
.Where("c", "d");

Action act = () => query.AddOrReplaceComponent("where", new BasicCondition());
Action act = () => query.AddOrReplaceComponent(ComponentName.Where, new BasicCondition());
Assert.Throws<InvalidOperationException>(act);
}

Expand All @@ -286,7 +286,7 @@ public void OneLimitPerEngine()
.ForSqlServer(q => q.Limit(5))
.ForSqlServer(q => q.Limit(10));

var limits = query.GetComponents<LimitClause>("limit", EngineCodes.SqlServer);
var limits = query.GetComponents<LimitClause>(ComponentName.Limit, EngineCodes.SqlServer);
Assert.Single(limits);
Assert.Equal(10, limits.Single().Limit);
}
Expand Down Expand Up @@ -314,7 +314,7 @@ public void OneOffsetPerEngine()
.ForSqlServer(q => q.Offset(5))
.ForSqlServer(q => q.Offset(10));

var limits = query.GetComponents<OffsetClause>("offset", EngineCodes.SqlServer);
var limits = query.GetComponents<OffsetClause>(ComponentName.Offset, EngineCodes.SqlServer);
Assert.Single(limits);
Assert.Equal(10, limits.Single().Offset);
}
Expand All @@ -329,7 +329,7 @@ public void CompilerSpecificOffset()
var engines = new[] { EngineCodes.SqlServer, EngineCodes.MySql, EngineCodes.PostgreSql };
var c = Compilers.Compile(engines, query);

Assert.Equal(2, query.GetComponents("offset").Count);
Assert.Equal(2, query.GetComponents(ComponentName.Offset).Count);
Assert.Equal("SELECT * FROM `mytable` LIMIT 18446744073709551615 OFFSET 5", c[EngineCodes.MySql].ToString());
Assert.Equal("SELECT * FROM \"mytable\" OFFSET 10", c[EngineCodes.PostgreSql].ToString());
Assert.Equal("SELECT * FROM [mytable]", c[EngineCodes.SqlServer].ToString());
Expand Down
42 changes: 21 additions & 21 deletions QueryBuilder/Base.Where.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public Q Where(string column, string op, object value)
return boolValue ? WhereTrue(column) : WhereFalse(column);
}

return AddComponent("where", new BasicCondition
return AddComponent(ComponentName.Where, new BasicCondition
{
Column = column,
Operator = op,
Expand Down Expand Up @@ -112,7 +112,7 @@ public Q Where(IEnumerable<KeyValuePair<string, object>> values)

public Q WhereRaw(string sql, params object[] bindings)
{
return AddComponent("where", new RawCondition
return AddComponent(ComponentName.Where, new RawCondition
{
Expression = sql,
Bindings = bindings,
Expand All @@ -136,12 +136,12 @@ public Q Where(Func<Q, Q> callback)
var query = callback.Invoke(NewChild());

// omit empty queries
if (!query.Clauses.Where(x => x.Component == "where").Any())
if (!query.Clauses.Where(x => x.Component == ComponentName.Where).Any())
{
return (Q)this;
}

return AddComponent("where", new NestedCondition<Q>
return AddComponent(ComponentName.Where, new NestedCondition<Q>
{
Query = query,
IsNot = GetNot(),
Expand All @@ -166,7 +166,7 @@ public Q OrWhereNot(Func<Q, Q> callback)

public Q WhereColumns(string first, string op, string second)
{
return AddComponent("where", new TwoColumnsCondition
return AddComponent(ComponentName.Where, new TwoColumnsCondition
{
First = first,
Second = second,
Expand All @@ -183,7 +183,7 @@ public Q OrWhereColumns(string first, string op, string second)

public Q WhereNull(string column)
{
return AddComponent("where", new NullCondition
return AddComponent(ComponentName.Where, new NullCondition
{
Column = column,
IsOr = GetOr(),
Expand All @@ -208,7 +208,7 @@ public Q OrWhereNotNull(string column)

public Q WhereTrue(string column)
{
return AddComponent("where", new BooleanCondition
return AddComponent(ComponentName.Where, new BooleanCondition
{
Column = column,
Value = true,
Expand All @@ -224,7 +224,7 @@ public Q OrWhereTrue(string column)

public Q WhereFalse(string column)
{
return AddComponent("where", new BooleanCondition
return AddComponent(ComponentName.Where, new BooleanCondition
{
Column = column,
Value = false,
Expand All @@ -240,7 +240,7 @@ public Q OrWhereFalse(string column)

public Q WhereLike(string column, object value, bool caseSensitive = false, string escapeCharacter = null)
{
return AddComponent("where", new BasicStringCondition
return AddComponent(ComponentName.Where, new BasicStringCondition
{
Operator = "like",
Column = column,
Expand Down Expand Up @@ -268,7 +268,7 @@ public Q OrWhereNotLike(string column, object value, bool caseSensitive = false,
}
public Q WhereStarts(string column, object value, bool caseSensitive = false, string escapeCharacter = null)
{
return AddComponent("where", new BasicStringCondition
return AddComponent(ComponentName.Where, new BasicStringCondition
{
Operator = "starts",
Column = column,
Expand Down Expand Up @@ -297,7 +297,7 @@ public Q OrWhereNotStarts(string column, object value, bool caseSensitive = fals

public Q WhereEnds(string column, object value, bool caseSensitive = false, string escapeCharacter = null)
{
return AddComponent("where", new BasicStringCondition
return AddComponent(ComponentName.Where, new BasicStringCondition
{
Operator = "ends",
Column = column,
Expand Down Expand Up @@ -326,7 +326,7 @@ public Q OrWhereNotEnds(string column, object value, bool caseSensitive = false,

public Q WhereContains(string column, object value, bool caseSensitive = false, string escapeCharacter = null)
{
return AddComponent("where", new BasicStringCondition
return AddComponent(ComponentName.Where, new BasicStringCondition
{
Operator = "contains",
Column = column,
Expand Down Expand Up @@ -355,7 +355,7 @@ public Q OrWhereNotContains(string column, object value, bool caseSensitive = fa

public Q WhereBetween<T>(string column, T lower, T higher)
{
return AddComponent("where", new BetweenCondition<T>
return AddComponent(ComponentName.Where, new BetweenCondition<T>
{
Column = column,
IsOr = GetOr(),
Expand Down Expand Up @@ -387,7 +387,7 @@ public Q WhereIn<T>(string column, IEnumerable<T> values)
{
string val = values as string;

return AddComponent("where", new InCondition<string>
return AddComponent(ComponentName.Where, new InCondition<string>
{
Column = column,
IsOr = GetOr(),
Expand All @@ -396,7 +396,7 @@ public Q WhereIn<T>(string column, IEnumerable<T> values)
});
}

return AddComponent("where", new InCondition<T>
return AddComponent(ComponentName.Where, new InCondition<T>
{
Column = column,
IsOr = GetOr(),
Expand Down Expand Up @@ -425,7 +425,7 @@ public Q OrWhereNotIn<T>(string column, IEnumerable<T> values)

public Q WhereIn(string column, Query query)
{
return AddComponent("where", new InQueryCondition
return AddComponent(ComponentName.Where, new InQueryCondition
{
Column = column,
IsOr = GetOr(),
Expand Down Expand Up @@ -486,7 +486,7 @@ public Q Where(string column, string op, Func<Q, Q> callback)

public Q Where(string column, string op, Query query)
{
return AddComponent("where", new QueryCondition<Query>
return AddComponent(ComponentName.Where, new QueryCondition<Query>
{
Column = column,
Operator = op,
Expand All @@ -503,7 +503,7 @@ public Q WhereSub(Query query, object value)

public Q WhereSub(Query query, string op, object value)
{
return AddComponent("where", new SubQueryCondition<Query>
return AddComponent(ComponentName.Where, new SubQueryCondition<Query>
{
Value = value,
Operator = op,
Expand Down Expand Up @@ -534,12 +534,12 @@ public Q OrWhere(string column, string op, Func<Query, Query> callback)

public Q WhereExists(Query query)
{
if (!query.HasComponent("from"))
if (!query.HasComponent(ComponentName.From))
{
throw new ArgumentException($"'{nameof(FromClause)}' cannot be empty if used inside a '{nameof(WhereExists)}' condition");
}

return AddComponent("where", new ExistsCondition
return AddComponent(ComponentName.Where, new ExistsCondition
{
Query = query,
IsNot = GetNot(),
Expand Down Expand Up @@ -582,7 +582,7 @@ public Q OrWhereNotExists(Func<Query, Query> callback)
#region date
public Q WhereDatePart(string part, string column, string op, object value)
{
return AddComponent("where", new BasicDateCondition
return AddComponent(ComponentName.Where, new BasicDateCondition
{
Operator = op,
Column = column,
Expand Down
6 changes: 3 additions & 3 deletions QueryBuilder/BaseQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ protected bool GetNot()
/// <returns></returns>
public Q From(string table)
{
return AddOrReplaceComponent("from", new FromClause
return AddOrReplaceComponent(ComponentName.From, new FromClause
{
Table = table,
});
Expand All @@ -282,15 +282,15 @@ public Q From(Query query, string alias = null)
query.As(alias);
};

return AddOrReplaceComponent("from", new QueryFromClause
return AddOrReplaceComponent(ComponentName.From, new QueryFromClause
{
Query = query
});
}

public Q FromRaw(string sql, params object[] bindings)
{
return AddOrReplaceComponent("from", new RawFromClause
return AddOrReplaceComponent(ComponentName.From, new RawFromClause
{
Expression = sql,
Bindings = bindings,
Expand Down
6 changes: 3 additions & 3 deletions QueryBuilder/Compilers/Compiler.Conditions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -170,12 +170,12 @@ protected virtual string CompileBasicDateCondition(SqlResult ctx, BasicDateCondi

protected virtual string CompileNestedCondition<Q>(SqlResult ctx, NestedCondition<Q> x) where Q : BaseQuery<Q>
{
if (!(x.Query.HasComponent("where", EngineCode) || x.Query.HasComponent("having", EngineCode)))
if (!(x.Query.HasComponent(ComponentName.Where, EngineCode) || x.Query.HasComponent(ComponentName.Having, EngineCode)))
{
return null;
}

var clause = x.Query.HasComponent("where", EngineCode) ? "where" : "having";
var clause = x.Query.HasComponent(ComponentName.Where, EngineCode) ? ComponentName.Where : ComponentName.Having;

var clauses = x.Query.GetComponents<AbstractCondition>(clause, EngineCode);

Expand Down Expand Up @@ -253,7 +253,7 @@ protected virtual string CompileExistsCondition(SqlResult ctx, ExistsCondition i

if (OmitSelectInsideExists)
{
query.ClearComponent("select").SelectRaw("1");
query.ClearComponent(ComponentName.Select).SelectRaw("1");
}

var subCtx = CompileSelectQuery(query);
Expand Down
Loading