From 10cf7950a6c151b297c3133b589b69f22f23a044 Mon Sep 17 00:00:00 2001 From: Aleksey_N Date: Fri, 8 Dec 2023 17:01:30 +0300 Subject: [PATCH 1/2] add string constants as clause component names --- QueryBuilder/Base.Where.cs | 42 +++++------ QueryBuilder/BaseQuery.cs | 6 +- QueryBuilder/ClauseName.cs | 20 +++++ QueryBuilder/Compilers/Compiler.Conditions.cs | 6 +- QueryBuilder/Compilers/Compiler.cs | 74 +++++++++---------- QueryBuilder/Compilers/CteFinder.cs | 8 +- QueryBuilder/Compilers/FirebirdCompiler.cs | 6 +- QueryBuilder/Compilers/OracleCompiler.cs | 2 +- QueryBuilder/Compilers/SqlServerCompiler.cs | 8 +- QueryBuilder/Join.cs | 2 +- QueryBuilder/Query.Aggregate.cs | 4 +- QueryBuilder/Query.Combine.cs | 4 +- QueryBuilder/Query.Having.cs | 40 +++++----- QueryBuilder/Query.Insert.cs | 10 +-- QueryBuilder/Query.Join.cs | 2 +- QueryBuilder/Query.Select.cs | 8 +- QueryBuilder/Query.Update.cs | 6 +- QueryBuilder/Query.cs | 26 +++---- 18 files changed, 147 insertions(+), 127 deletions(-) create mode 100644 QueryBuilder/ClauseName.cs diff --git a/QueryBuilder/Base.Where.cs b/QueryBuilder/Base.Where.cs index c40cc40a..8a788735 100644 --- a/QueryBuilder/Base.Where.cs +++ b/QueryBuilder/Base.Where.cs @@ -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(ClauseName.Where, new BasicCondition { Column = column, Operator = op, @@ -112,7 +112,7 @@ public Q Where(IEnumerable> values) public Q WhereRaw(string sql, params object[] bindings) { - return AddComponent("where", new RawCondition + return AddComponent(ClauseName.Where, new RawCondition { Expression = sql, Bindings = bindings, @@ -136,12 +136,12 @@ public Q Where(Func 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 == ClauseName.Where).Any()) { return (Q)this; } - return AddComponent("where", new NestedCondition + return AddComponent(ClauseName.Where, new NestedCondition { Query = query, IsNot = GetNot(), @@ -166,7 +166,7 @@ public Q OrWhereNot(Func callback) public Q WhereColumns(string first, string op, string second) { - return AddComponent("where", new TwoColumnsCondition + return AddComponent(ClauseName.Where, new TwoColumnsCondition { First = first, Second = second, @@ -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(ClauseName.Where, new NullCondition { Column = column, IsOr = GetOr(), @@ -208,7 +208,7 @@ public Q OrWhereNotNull(string column) public Q WhereTrue(string column) { - return AddComponent("where", new BooleanCondition + return AddComponent(ClauseName.Where, new BooleanCondition { Column = column, Value = true, @@ -224,7 +224,7 @@ public Q OrWhereTrue(string column) public Q WhereFalse(string column) { - return AddComponent("where", new BooleanCondition + return AddComponent(ClauseName.Where, new BooleanCondition { Column = column, Value = false, @@ -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(ClauseName.Where, new BasicStringCondition { Operator = "like", Column = column, @@ -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(ClauseName.Where, new BasicStringCondition { Operator = "starts", Column = column, @@ -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(ClauseName.Where, new BasicStringCondition { Operator = "ends", Column = column, @@ -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(ClauseName.Where, new BasicStringCondition { Operator = "contains", Column = column, @@ -355,7 +355,7 @@ public Q OrWhereNotContains(string column, object value, bool caseSensitive = fa public Q WhereBetween(string column, T lower, T higher) { - return AddComponent("where", new BetweenCondition + return AddComponent(ClauseName.Where, new BetweenCondition { Column = column, IsOr = GetOr(), @@ -387,7 +387,7 @@ public Q WhereIn(string column, IEnumerable values) { string val = values as string; - return AddComponent("where", new InCondition + return AddComponent(ClauseName.Where, new InCondition { Column = column, IsOr = GetOr(), @@ -396,7 +396,7 @@ public Q WhereIn(string column, IEnumerable values) }); } - return AddComponent("where", new InCondition + return AddComponent(ClauseName.Where, new InCondition { Column = column, IsOr = GetOr(), @@ -425,7 +425,7 @@ public Q OrWhereNotIn(string column, IEnumerable values) public Q WhereIn(string column, Query query) { - return AddComponent("where", new InQueryCondition + return AddComponent(ClauseName.Where, new InQueryCondition { Column = column, IsOr = GetOr(), @@ -486,7 +486,7 @@ public Q Where(string column, string op, Func callback) public Q Where(string column, string op, Query query) { - return AddComponent("where", new QueryCondition + return AddComponent(ClauseName.Where, new QueryCondition { Column = column, Operator = op, @@ -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 + return AddComponent(ClauseName.Where, new SubQueryCondition { Value = value, Operator = op, @@ -534,12 +534,12 @@ public Q OrWhere(string column, string op, Func callback) public Q WhereExists(Query query) { - if (!query.HasComponent("from")) + if (!query.HasComponent(ClauseName.From)) { throw new ArgumentException($"'{nameof(FromClause)}' cannot be empty if used inside a '{nameof(WhereExists)}' condition"); } - return AddComponent("where", new ExistsCondition + return AddComponent(ClauseName.Where, new ExistsCondition { Query = query, IsNot = GetNot(), @@ -582,7 +582,7 @@ public Q OrWhereNotExists(Func callback) #region date public Q WhereDatePart(string part, string column, string op, object value) { - return AddComponent("where", new BasicDateCondition + return AddComponent(ClauseName.Where, new BasicDateCondition { Operator = op, Column = column, diff --git a/QueryBuilder/BaseQuery.cs b/QueryBuilder/BaseQuery.cs index 86b44a23..d1b91bb4 100644 --- a/QueryBuilder/BaseQuery.cs +++ b/QueryBuilder/BaseQuery.cs @@ -266,7 +266,7 @@ protected bool GetNot() /// public Q From(string table) { - return AddOrReplaceComponent("from", new FromClause + return AddOrReplaceComponent(ClauseName.From, new FromClause { Table = table, }); @@ -282,7 +282,7 @@ public Q From(Query query, string alias = null) query.As(alias); }; - return AddOrReplaceComponent("from", new QueryFromClause + return AddOrReplaceComponent(ClauseName.From, new QueryFromClause { Query = query }); @@ -290,7 +290,7 @@ public Q From(Query query, string alias = null) public Q FromRaw(string sql, params object[] bindings) { - return AddOrReplaceComponent("from", new RawFromClause + return AddOrReplaceComponent(ClauseName.From, new RawFromClause { Expression = sql, Bindings = bindings, diff --git a/QueryBuilder/ClauseName.cs b/QueryBuilder/ClauseName.cs new file mode 100644 index 00000000..5d8ff57e --- /dev/null +++ b/QueryBuilder/ClauseName.cs @@ -0,0 +1,20 @@ +namespace SqlKata +{ + public static class ClauseName + { + public static string Select { get; set; } = "select"; + public static string From { get; set; } = "from"; + public static string Insert { get; set; } = "insert"; + public static string Update { get; set; } = "update"; + public static string Join { get; set; } = "join"; + public static string Where { get; set; } = "where"; + public static string Having { get; set; } = "having"; + public static string Offset { get; set; } = "offset"; + public static string Limit { get; set; } = "limit"; + public static string Cte { get; set; } = "cte"; + public static string Order { get; set; } = "order"; + public static string Group { get; set; } = "group"; + public static string Combine { get; set; } = "combine"; + public static string Aggregate { get; set; } = "aggregate"; + } +} diff --git a/QueryBuilder/Compilers/Compiler.Conditions.cs b/QueryBuilder/Compilers/Compiler.Conditions.cs index 6615c246..02580519 100644 --- a/QueryBuilder/Compilers/Compiler.Conditions.cs +++ b/QueryBuilder/Compilers/Compiler.Conditions.cs @@ -170,12 +170,12 @@ protected virtual string CompileBasicDateCondition(SqlResult ctx, BasicDateCondi protected virtual string CompileNestedCondition(SqlResult ctx, NestedCondition x) where Q : BaseQuery { - if (!(x.Query.HasComponent("where", EngineCode) || x.Query.HasComponent("having", EngineCode))) + if (!(x.Query.HasComponent(ClauseName.Where, EngineCode) || x.Query.HasComponent(ClauseName.Having, EngineCode))) { return null; } - var clause = x.Query.HasComponent("where", EngineCode) ? "where" : "having"; + var clause = x.Query.HasComponent(ClauseName.Where, EngineCode) ? ClauseName.Where : ClauseName.Having; var clauses = x.Query.GetComponents(clause, EngineCode); @@ -253,7 +253,7 @@ protected virtual string CompileExistsCondition(SqlResult ctx, ExistsCondition i if (OmitSelectInsideExists) { - query.ClearComponent("select").SelectRaw("1"); + query.ClearComponent(ClauseName.Select).SelectRaw("1"); } var subCtx = CompileSelectQuery(query); diff --git a/QueryBuilder/Compilers/Compiler.cs b/QueryBuilder/Compilers/Compiler.cs index aa15c789..b8fc1028 100644 --- a/QueryBuilder/Compilers/Compiler.cs +++ b/QueryBuilder/Compilers/Compiler.cs @@ -79,14 +79,14 @@ protected SqlResult PrepareResult(SqlResult ctx) private Query TransformAggregateQuery(Query query) { - var clause = query.GetOneComponent("aggregate", EngineCode); + var clause = query.GetOneComponent(ClauseName.Aggregate, EngineCode); if (clause.Columns.Count == 1 && !query.IsDistinct) return query; if (query.IsDistinct) { - query.ClearComponent("aggregate", EngineCode); - query.ClearComponent("select", EngineCode); + query.ClearComponent(ClauseName.Aggregate, EngineCode); + query.ClearComponent(ClauseName.Select, EngineCode); query.Select(clause.Columns.ToArray()); } else @@ -104,7 +104,7 @@ private Query TransformAggregateQuery(Query query) }; return new Query() - .AddComponent("aggregate", outerClause) + .AddComponent(ClauseName.Aggregate, outerClause) .From(query, $"{clause.Type}Query"); } @@ -128,9 +128,9 @@ protected virtual SqlResult CompileRaw(Query query) { if (query.Method == "aggregate") { - query.ClearComponent("limit") - .ClearComponent("order") - .ClearComponent("group"); + query.ClearComponent(ClauseName.Limit) + .ClearComponent(ClauseName.Order) + .ClearComponent(ClauseName.Group); query = TransformAggregateQuery(query); } @@ -139,7 +139,7 @@ protected virtual SqlResult CompileRaw(Query query) } // handle CTEs - if (query.HasComponent("cte", EngineCode)) + if (query.HasComponent(ClauseName.Cte, EngineCode)) { ctx = CompileCteQuery(ctx, query); } @@ -255,12 +255,12 @@ protected virtual SqlResult CompileDeleteQuery(Query query) Query = query }; - if (!ctx.Query.HasComponent("from", EngineCode)) + if (!ctx.Query.HasComponent(ClauseName.From, EngineCode)) { throw new InvalidOperationException("No table set to delete"); } - var fromClause = ctx.Query.GetOneComponent("from", EngineCode); + var fromClause = ctx.Query.GetOneComponent(ClauseName.From, EngineCode); string table = null; @@ -317,12 +317,12 @@ protected virtual SqlResult CompileUpdateQuery(Query query) Query = query }; - if (!ctx.Query.HasComponent("from", EngineCode)) + if (!ctx.Query.HasComponent(ClauseName.From, EngineCode)) { throw new InvalidOperationException("No table set to update"); } - var fromClause = ctx.Query.GetOneComponent("from", EngineCode); + var fromClause = ctx.Query.GetOneComponent(ClauseName.From, EngineCode); string table = null; @@ -343,7 +343,7 @@ protected virtual SqlResult CompileUpdateQuery(Query query) } // check for increment statements - var clause = ctx.Query.GetOneComponent("update", EngineCode); + var clause = ctx.Query.GetOneComponent(ClauseName.Update, EngineCode); string wheres; @@ -366,7 +366,7 @@ protected virtual SqlResult CompileUpdateQuery(Query query) } - var toUpdate = ctx.Query.GetOneComponent("update", EngineCode); + var toUpdate = ctx.Query.GetOneComponent(ClauseName.Update, EngineCode); var parts = new List(); for (var i = 0; i < toUpdate.Columns.Count; i++) @@ -395,10 +395,10 @@ protected virtual SqlResult CompileInsertQuery(Query query) Query = query }; - if (!ctx.Query.HasComponent("from", EngineCode)) + if (!ctx.Query.HasComponent(ClauseName.From, EngineCode)) throw new InvalidOperationException("No table set to insert"); - var fromClause = ctx.Query.GetOneComponent("from", EngineCode); + var fromClause = ctx.Query.GetOneComponent(ClauseName.From, EngineCode); if (fromClause is null) throw new InvalidOperationException("Invalid table expression"); @@ -414,7 +414,7 @@ protected virtual SqlResult CompileInsertQuery(Query query) if (table is null) throw new InvalidOperationException("Invalid table expression"); - var inserts = ctx.Query.GetComponents("insert", EngineCode); + var inserts = ctx.Query.GetComponents(ClauseName.Insert, EngineCode); if (inserts[0] is InsertQueryClause insertQueryClause) return CompileInsertQueryClause(ctx, table, insertQueryClause); else @@ -566,7 +566,7 @@ protected virtual string CompileFilterConditions(SqlResult ctx, AggregatedColumn return null; } - var wheres = aggregatedColumn.Filter.GetComponents("where"); + var wheres = aggregatedColumn.Filter.GetComponents(ClauseName.Where); return CompileConditions(ctx, wheres); } @@ -610,9 +610,9 @@ protected virtual SqlResult OnBeforeSelect(SqlResult ctx) protected virtual string CompileColumns(SqlResult ctx) { - if (ctx.Query.HasComponent("aggregate", EngineCode)) + if (ctx.Query.HasComponent(ClauseName.Aggregate, EngineCode)) { - var aggregate = ctx.Query.GetOneComponent("aggregate", EngineCode); + var aggregate = ctx.Query.GetOneComponent(ClauseName.Aggregate, EngineCode); var aggregateColumns = aggregate.Columns .Select(x => CompileColumn(ctx, new Column { Name = x })) @@ -636,7 +636,7 @@ protected virtual string CompileColumns(SqlResult ctx) } var columns = ctx.Query - .GetComponents("select", EngineCode) + .GetComponents(ClauseName.Select, EngineCode) .Select(x => CompileColumn(ctx, x)) .ToList(); @@ -652,14 +652,14 @@ public virtual string CompileUnion(SqlResult ctx) { // Handle UNION, EXCEPT and INTERSECT - if (!ctx.Query.GetComponents("combine", EngineCode).Any()) + if (!ctx.Query.GetComponents(ClauseName.Combine, EngineCode).Any()) { return null; } var combinedQueries = new List(); - var clauses = ctx.Query.GetComponents("combine", EngineCode); + var clauses = ctx.Query.GetComponents(ClauseName.Combine, EngineCode); foreach (var clause in clauses) { @@ -719,9 +719,9 @@ public virtual string CompileTableExpression(SqlResult ctx, AbstractFrom from) public virtual string CompileFrom(SqlResult ctx) { - if (ctx.Query.HasComponent("from", EngineCode)) + if (ctx.Query.HasComponent(ClauseName.From, EngineCode)) { - var from = ctx.Query.GetOneComponent("from", EngineCode); + var from = ctx.Query.GetOneComponent(ClauseName.From, EngineCode); return "FROM " + CompileTableExpression(ctx, from); } @@ -731,13 +731,13 @@ public virtual string CompileFrom(SqlResult ctx) public virtual string CompileJoins(SqlResult ctx) { - if (!ctx.Query.HasComponent("join", EngineCode)) + if (!ctx.Query.HasComponent(ClauseName.Join, EngineCode)) { return null; } var joins = ctx.Query - .GetComponents("join", EngineCode) + .GetComponents(ClauseName.Join, EngineCode) .Select(x => CompileJoin(ctx, x.Join)); return "\n" + string.Join("\n", joins); @@ -746,8 +746,8 @@ public virtual string CompileJoins(SqlResult ctx) public virtual string CompileJoin(SqlResult ctx, Join join, bool isNested = false) { - var from = join.GetOneComponent("from", EngineCode); - var conditions = join.GetComponents("where", EngineCode); + var from = join.GetOneComponent(ClauseName.From, EngineCode); + var conditions = join.GetComponents(ClauseName.Where, EngineCode); var joinTable = CompileTableExpression(ctx, from); var constraints = CompileConditions(ctx, conditions); @@ -759,12 +759,12 @@ public virtual string CompileJoin(SqlResult ctx, Join join, bool isNested = fals public virtual string CompileWheres(SqlResult ctx) { - if (!ctx.Query.HasComponent("where", EngineCode)) + if (!ctx.Query.HasComponent(ClauseName.Where, EngineCode)) { return null; } - var conditions = ctx.Query.GetComponents("where", EngineCode); + var conditions = ctx.Query.GetComponents(ClauseName.Where, EngineCode); var sql = CompileConditions(ctx, conditions).Trim(); return string.IsNullOrEmpty(sql) ? null : $"WHERE {sql}"; @@ -772,13 +772,13 @@ public virtual string CompileWheres(SqlResult ctx) public virtual string CompileGroups(SqlResult ctx) { - if (!ctx.Query.HasComponent("group", EngineCode)) + if (!ctx.Query.HasComponent(ClauseName.Group, EngineCode)) { return null; } var columns = ctx.Query - .GetComponents("group", EngineCode) + .GetComponents(ClauseName.Group, EngineCode) .Select(x => CompileColumn(ctx, x)); return "GROUP BY " + string.Join(", ", columns); @@ -786,13 +786,13 @@ public virtual string CompileGroups(SqlResult ctx) public virtual string CompileOrders(SqlResult ctx) { - if (!ctx.Query.HasComponent("order", EngineCode)) + if (!ctx.Query.HasComponent(ClauseName.Order, EngineCode)) { return null; } var columns = ctx.Query - .GetComponents("order", EngineCode) + .GetComponents(ClauseName.Order, EngineCode) .Select(x => { @@ -812,7 +812,7 @@ public virtual string CompileOrders(SqlResult ctx) public virtual string CompileHaving(SqlResult ctx) { - if (!ctx.Query.HasComponent("having", EngineCode)) + if (!ctx.Query.HasComponent(ClauseName.Having, EngineCode)) { return null; } @@ -820,7 +820,7 @@ public virtual string CompileHaving(SqlResult ctx) var sql = new List(); string boolOperator; - var having = ctx.Query.GetComponents("having", EngineCode) + var having = ctx.Query.GetComponents(ClauseName.Having, EngineCode) .Cast() .ToList(); diff --git a/QueryBuilder/Compilers/CteFinder.cs b/QueryBuilder/Compilers/CteFinder.cs index fbb0800a..c17de322 100644 --- a/QueryBuilder/Compilers/CteFinder.cs +++ b/QueryBuilder/Compilers/CteFinder.cs @@ -22,7 +22,7 @@ public List Find() namesOfPreviousCtes = new HashSet(); - orderedCteList = findInternal(query); + orderedCteList = FindInternal(query); namesOfPreviousCtes.Clear(); namesOfPreviousCtes = null; @@ -30,9 +30,9 @@ public List Find() return orderedCteList; } - private List findInternal(Query queryToSearch) + private List FindInternal(Query queryToSearch) { - var cteList = queryToSearch.GetComponents("cte", engineCode); + var cteList = queryToSearch.GetComponents(ClauseName.Cte, engineCode); var resultList = new List(); @@ -46,7 +46,7 @@ private List findInternal(Query queryToSearch) if (cte is QueryFromClause queryFromClause) { - resultList.InsertRange(0, findInternal(queryFromClause.Query)); + resultList.InsertRange(0, FindInternal(queryFromClause.Query)); } } diff --git a/QueryBuilder/Compilers/FirebirdCompiler.cs b/QueryBuilder/Compilers/FirebirdCompiler.cs index 61ab9547..0e645305 100644 --- a/QueryBuilder/Compilers/FirebirdCompiler.cs +++ b/QueryBuilder/Compilers/FirebirdCompiler.cs @@ -17,7 +17,7 @@ protected override SqlResult CompileInsertQuery(Query query) { var ctx = base.CompileInsertQuery(query); - var inserts = ctx.Query.GetComponents("insert", EngineCode); + var inserts = ctx.Query.GetComponents(ClauseName.Insert, EngineCode); if (inserts.Count > 1) { @@ -57,7 +57,7 @@ protected override string CompileColumns(SqlResult ctx) { ctx.Bindings.Insert(0, limit); - ctx.Query.ClearComponent("limit"); + ctx.Query.ClearComponent(ClauseName.Limit); return $"SELECT FIRST {parameterPlaceholder}" + compiled.Substring(6); } @@ -65,7 +65,7 @@ protected override string CompileColumns(SqlResult ctx) { ctx.Bindings.Insert(0, offset); - ctx.Query.ClearComponent("offset"); + ctx.Query.ClearComponent(ClauseName.Offset); return $"SELECT SKIP {parameterPlaceholder}" + compiled.Substring(6); } diff --git a/QueryBuilder/Compilers/OracleCompiler.cs b/QueryBuilder/Compilers/OracleCompiler.cs index 610ec20d..c5725f67 100644 --- a/QueryBuilder/Compilers/OracleCompiler.cs +++ b/QueryBuilder/Compilers/OracleCompiler.cs @@ -52,7 +52,7 @@ public override string CompileLimit(SqlResult ctx) var safeOrder = ""; - if (!ctx.Query.HasComponent("order")) + if (!ctx.Query.HasComponent(ClauseName.Order)) { safeOrder = "ORDER BY (SELECT 0 FROM DUAL) "; } diff --git a/QueryBuilder/Compilers/SqlServerCompiler.cs b/QueryBuilder/Compilers/SqlServerCompiler.cs index 0202f0f1..6148ba8e 100644 --- a/QueryBuilder/Compilers/SqlServerCompiler.cs +++ b/QueryBuilder/Compilers/SqlServerCompiler.cs @@ -32,7 +32,7 @@ protected override SqlResult CompileSelectQuery(Query query) var offset = query.GetOffset(EngineCode); - if (!query.HasComponent("select")) + if (!query.HasComponent(ClauseName.Select)) { query.Select("*"); } @@ -41,7 +41,7 @@ protected override SqlResult CompileSelectQuery(Query query) query.SelectRaw($"ROW_NUMBER() OVER ({order}) AS [row_num]", ctx.Bindings.ToArray()); - query.ClearComponent("order"); + query.ClearComponent(ClauseName.Order); var result = base.CompileSelectQuery(query); @@ -81,7 +81,7 @@ protected override string CompileColumns(SqlResult ctx) // top bindings should be inserted first ctx.Bindings.Insert(0, limit); - ctx.Query.ClearComponent("limit"); + ctx.Query.ClearComponent(ClauseName.Limit); // handle distinct if (compiled.IndexOf("SELECT DISTINCT") == 0) @@ -113,7 +113,7 @@ public override string CompileLimit(SqlResult ctx) } var safeOrder = ""; - if (!ctx.Query.HasComponent("order")) + if (!ctx.Query.HasComponent(ClauseName.Order)) { safeOrder = "ORDER BY (SELECT 0) "; } diff --git a/QueryBuilder/Join.cs b/QueryBuilder/Join.cs index ae0969a6..920f8a00 100644 --- a/QueryBuilder/Join.cs +++ b/QueryBuilder/Join.cs @@ -53,7 +53,7 @@ public Join AsType(string type) public Join On(string first, string second, string op = "=") { - return AddComponent("where", new TwoColumnsCondition + return AddComponent(ClauseName.Where, new TwoColumnsCondition { First = first, Second = second, diff --git a/QueryBuilder/Query.Aggregate.cs b/QueryBuilder/Query.Aggregate.cs index d4fc5057..fb348caf 100644 --- a/QueryBuilder/Query.Aggregate.cs +++ b/QueryBuilder/Query.Aggregate.cs @@ -10,8 +10,8 @@ public Query AsAggregate(string type, string[] columns = null) Method = "aggregate"; - this.ClearComponent("aggregate") - .AddComponent("aggregate", new AggregateClause + this.ClearComponent(ClauseName.Aggregate) + .AddComponent(ClauseName.Aggregate, new AggregateClause { Type = type, Columns = columns?.ToList() ?? new List(), diff --git a/QueryBuilder/Query.Combine.cs b/QueryBuilder/Query.Combine.cs index b7238264..0596182c 100644 --- a/QueryBuilder/Query.Combine.cs +++ b/QueryBuilder/Query.Combine.cs @@ -13,7 +13,7 @@ public Query Combine(string operation, bool all, Query query) throw new InvalidOperationException("Only select queries can be combined."); } - return AddComponent("combine", new Combine + return AddComponent(ClauseName.Combine, new Combine { Query = query, Operation = operation, @@ -28,7 +28,7 @@ public Query CombineRaw(string sql, params object[] bindings) throw new InvalidOperationException("Only select queries can be combined."); } - return AddComponent("combine", new RawCombine + return AddComponent(ClauseName.Combine, new RawCombine { Expression = sql, Bindings = bindings, diff --git a/QueryBuilder/Query.Having.cs b/QueryBuilder/Query.Having.cs index ee367941..d5773b3d 100644 --- a/QueryBuilder/Query.Having.cs +++ b/QueryBuilder/Query.Having.cs @@ -18,7 +18,7 @@ public Query Having(string column, string op, object value) return Not(op != "=").HavingNull(column); } - return AddComponent("having", new BasicCondition + return AddComponent(ClauseName.Having, new BasicCondition { Column = column, Operator = op, @@ -102,7 +102,7 @@ public Query Having(IEnumerable> values) public Query HavingRaw(string sql, params object[] bindings) { - return AddComponent("having", new RawCondition + return AddComponent(ClauseName.Having, new RawCondition { Expression = sql, Bindings = bindings, @@ -125,7 +125,7 @@ public Query Having(Func callback) { var query = callback.Invoke(NewChild()); - return AddComponent("having", new NestedCondition + return AddComponent(ClauseName.Having, new NestedCondition { Query = query, IsNot = GetNot(), @@ -150,7 +150,7 @@ public Query OrHavingNot(Func callback) public Query HavingColumns(string first, string op, string second) { - return AddComponent("having", new TwoColumnsCondition + return AddComponent(ClauseName.Having, new TwoColumnsCondition { First = first, Second = second, @@ -167,7 +167,7 @@ public Query OrHavingColumns(string first, string op, string second) public Query HavingNull(string column) { - return AddComponent("having", new NullCondition + return AddComponent(ClauseName.Having, new NullCondition { Column = column, IsOr = GetOr(), @@ -192,7 +192,7 @@ public Query OrHavingNotNull(string column) public Query HavingTrue(string column) { - return AddComponent("having", new BooleanCondition + return AddComponent(ClauseName.Having, new BooleanCondition { Column = column, Value = true, @@ -206,7 +206,7 @@ public Query OrHavingTrue(string column) public Query HavingFalse(string column) { - return AddComponent("having", new BooleanCondition + return AddComponent(ClauseName.Having, new BooleanCondition { Column = column, Value = false, @@ -220,7 +220,7 @@ public Query OrHavingFalse(string column) public Query HavingLike(string column, object value, bool caseSensitive = false, string escapeCharacter = null) { - return AddComponent("having", new BasicStringCondition + return AddComponent(ClauseName.Having, new BasicStringCondition { Operator = "like", Column = column, @@ -248,7 +248,7 @@ public Query OrHavingNotLike(string column, object value, bool caseSensitive = f } public Query HavingStarts(string column, object value, bool caseSensitive = false, string escapeCharacter = null) { - return AddComponent("having", new BasicStringCondition + return AddComponent(ClauseName.Having, new BasicStringCondition { Operator = "starts", Column = column, @@ -277,7 +277,7 @@ public Query OrHavingNotStarts(string column, object value, bool caseSensitive = public Query HavingEnds(string column, object value, bool caseSensitive = false, string escapeCharacter = null) { - return AddComponent("having", new BasicStringCondition + return AddComponent(ClauseName.Having, new BasicStringCondition { Operator = "ends", Column = column, @@ -306,7 +306,7 @@ public Query OrHavingNotEnds(string column, object value, bool caseSensitive = f public Query HavingContains(string column, object value, bool caseSensitive = false, string escapeCharacter = null) { - return AddComponent("having", new BasicStringCondition + return AddComponent(ClauseName.Having, new BasicStringCondition { Operator = "contains", Column = column, @@ -335,7 +335,7 @@ public Query OrHavingNotContains(string column, object value, bool caseSensitive public Query HavingBetween(string column, T lower, T higher) { - return AddComponent("having", new BetweenCondition + return AddComponent(ClauseName.Having, new BetweenCondition { Column = column, IsOr = GetOr(), @@ -366,7 +366,7 @@ public Query HavingIn(string column, IEnumerable values) { string val = values as string; - return AddComponent("having", new InCondition + return AddComponent(ClauseName.Having, new InCondition { Column = column, IsOr = GetOr(), @@ -375,7 +375,7 @@ public Query HavingIn(string column, IEnumerable values) }); } - return AddComponent("having", new InCondition + return AddComponent(ClauseName.Having, new InCondition { Column = column, IsOr = GetOr(), @@ -404,7 +404,7 @@ public Query OrHavingNotIn(string column, IEnumerable values) public Query HavingIn(string column, Query query) { - return AddComponent("having", new InQueryCondition + return AddComponent(ClauseName.Having, new InQueryCondition { Column = column, IsOr = GetOr(), @@ -465,7 +465,7 @@ public Query Having(string column, string op, Func callback) public Query Having(string column, string op, Query query) { - return AddComponent("having", new QueryCondition + return AddComponent(ClauseName.Having, new QueryCondition { Column = column, Operator = op, @@ -486,17 +486,17 @@ public Query OrHaving(string column, string op, Func callback) public Query HavingExists(Query query) { - if (!query.HasComponent("from")) + if (!query.HasComponent(ClauseName.From)) { throw new ArgumentException($"{nameof(FromClause)} cannot be empty if used inside a {nameof(HavingExists)} condition"); } // simplify the query as much as possible - query = query.Clone().ClearComponent("select") + query = query.Clone().ClearComponent(ClauseName.Select) .SelectRaw("1") .Limit(1); - return AddComponent("having", new ExistsCondition + return AddComponent(ClauseName.Having, new ExistsCondition { Query = query, IsNot = GetNot(), @@ -539,7 +539,7 @@ public Query OrHavingNotExists(Func callback) #region date public Query HavingDatePart(string part, string column, string op, object value) { - return AddComponent("having", new BasicDateCondition + return AddComponent(ClauseName.Having, new BasicDateCondition { Operator = op, Column = column, diff --git a/QueryBuilder/Query.Insert.cs b/QueryBuilder/Query.Insert.cs index dbec60af..f4d53844 100644 --- a/QueryBuilder/Query.Insert.cs +++ b/QueryBuilder/Query.Insert.cs @@ -31,7 +31,7 @@ public Query AsInsert(IEnumerable columns, IEnumerable values) Method = "insert"; - ClearComponent("insert").AddComponent("insert", new InsertClause + ClearComponent(ClauseName.Insert).AddComponent(ClauseName.Insert, new InsertClause { Columns = columnsList, Values = valuesList @@ -49,7 +49,7 @@ public Query AsInsert(IEnumerable> values, bool ret Method = "insert"; - ClearComponent("insert").AddComponent("insert", new InsertClause + ClearComponent(ClauseName.Insert).AddComponent(ClauseName.Insert, new InsertClause { Columns = values.Select(x => x.Key).ToList(), Values = values.Select(x => x.Value).ToList(), @@ -77,7 +77,7 @@ public Query AsInsert(IEnumerable columns, IEnumerable columns, IEnumerable columns, Query query) { Method = "insert"; - ClearComponent("insert").AddComponent("insert", new InsertQueryClause + ClearComponent(ClauseName.Insert).AddComponent(ClauseName.Insert, new InsertQueryClause { Columns = columns.ToList(), Query = query.Clone(), diff --git a/QueryBuilder/Query.Join.cs b/QueryBuilder/Query.Join.cs index 3fe3bbb1..34101120 100644 --- a/QueryBuilder/Query.Join.cs +++ b/QueryBuilder/Query.Join.cs @@ -9,7 +9,7 @@ private Query Join(Func callback) { var join = callback.Invoke(new Join().AsInner()); - return AddComponent("join", new BaseJoin + return AddComponent(ClauseName.Join, new BaseJoin { Join = join }); diff --git a/QueryBuilder/Query.Select.cs b/QueryBuilder/Query.Select.cs index 9502c024..84bd98a2 100644 --- a/QueryBuilder/Query.Select.cs +++ b/QueryBuilder/Query.Select.cs @@ -24,7 +24,7 @@ public Query Select(IEnumerable columns) foreach (var column in columns) { - AddComponent("select", new Column + AddComponent(ClauseName.Select, new Column { Name = column }); @@ -41,7 +41,7 @@ public Query SelectRaw(string sql, params object[] bindings) { Method = "select"; - AddComponent("select", new RawColumn + AddComponent(ClauseName.Select, new RawColumn { Expression = sql, Bindings = bindings, @@ -56,7 +56,7 @@ public Query Select(Query query, string alias) query = query.Clone(); - AddComponent("select", new QueryColumn + AddComponent(ClauseName.Select, new QueryColumn { Query = query.As(alias), }); @@ -73,7 +73,7 @@ public Query SelectAggregate(string aggregate, string column, Query filter = nul { Method = "select"; - AddComponent("select", new AggregatedColumn + AddComponent(ClauseName.Select, new AggregatedColumn { Column = new Column { Name = column }, Aggregate = aggregate, diff --git a/QueryBuilder/Query.Update.cs b/QueryBuilder/Query.Update.cs index d88aeb00..398a77cd 100644 --- a/QueryBuilder/Query.Update.cs +++ b/QueryBuilder/Query.Update.cs @@ -28,7 +28,7 @@ public Query AsUpdate(IEnumerable columns, IEnumerable values) Method = "update"; - ClearComponent("update").AddComponent("update", new InsertClause + ClearComponent(ClauseName.Update).AddComponent(ClauseName.Update, new InsertClause { Columns = columns.ToList(), Values = values.ToList() @@ -46,7 +46,7 @@ public Query AsUpdate(IEnumerable> values) Method = "update"; - ClearComponent("update").AddComponent("update", new InsertClause + ClearComponent(ClauseName.Update).AddComponent(ClauseName.Update, new InsertClause { Columns = values.Select(x => x.Key).ToList(), Values = values.Select(x => x.Value).ToList(), @@ -58,7 +58,7 @@ public Query AsUpdate(IEnumerable> values) public Query AsIncrement(string column, int value = 1) { Method = "update"; - AddOrReplaceComponent("update", new IncrementClause + AddOrReplaceComponent(ClauseName.Update, new IncrementClause { Column = column, Value = value diff --git a/QueryBuilder/Query.cs b/QueryBuilder/Query.cs index 8435eca6..0ee358be 100755 --- a/QueryBuilder/Query.cs +++ b/QueryBuilder/Query.cs @@ -35,7 +35,7 @@ public Query(string table, string comment = null) : base() internal long GetOffset(string engineCode = null) { engineCode = engineCode ?? EngineScope; - var offset = this.GetOneComponent("offset", engineCode); + var offset = this.GetOneComponent(ClauseName.Offset, engineCode); return offset?.Offset ?? 0; } @@ -43,7 +43,7 @@ internal long GetOffset(string engineCode = null) internal int GetLimit(string engineCode = null) { engineCode = engineCode ?? EngineScope; - var limit = this.GetOneComponent("limit", engineCode); + var limit = this.GetOneComponent(ClauseName.Limit, engineCode); return limit?.Limit ?? 0; } @@ -104,7 +104,7 @@ public Query With(Query query) // clear the query alias query.QueryAlias = null; - return AddComponent("cte", new QueryFromClause + return AddComponent(ClauseName.Cte, new QueryFromClause { Query = query, Alias = alias, @@ -157,12 +157,12 @@ public Query With(string alias, IEnumerable columns, IEnumerable Date: Tue, 12 Dec 2023 16:52:19 +0300 Subject: [PATCH 2/2] component names as constans strings --- QueryBuilder.Tests/GeneralTests.cs | 12 +-- QueryBuilder/Base.Where.cs | 42 +++++------ QueryBuilder/BaseQuery.cs | 6 +- QueryBuilder/Compilers/Compiler.Conditions.cs | 6 +- QueryBuilder/Compilers/Compiler.cs | 74 +++++++++---------- QueryBuilder/Compilers/CteFinder.cs | 2 +- QueryBuilder/Compilers/FirebirdCompiler.cs | 6 +- QueryBuilder/Compilers/OracleCompiler.cs | 2 +- QueryBuilder/Compilers/SqlServerCompiler.cs | 8 +- .../{ClauseName.cs => ComponentName.cs} | 2 +- QueryBuilder/Join.cs | 2 +- QueryBuilder/Query.Aggregate.cs | 4 +- QueryBuilder/Query.Combine.cs | 4 +- QueryBuilder/Query.Having.cs | 40 +++++----- QueryBuilder/Query.Insert.cs | 10 +-- QueryBuilder/Query.Join.cs | 2 +- QueryBuilder/Query.Select.cs | 8 +- QueryBuilder/Query.Update.cs | 6 +- QueryBuilder/Query.cs | 26 +++---- SqlKata.Execution/QueryFactory.cs | 8 +- 20 files changed, 135 insertions(+), 135 deletions(-) rename QueryBuilder/{ClauseName.cs => ComponentName.cs} (95%) diff --git a/QueryBuilder.Tests/GeneralTests.cs b/QueryBuilder.Tests/GeneralTests.cs index 63fb3d3f..480c5737 100644 --- a/QueryBuilder.Tests/GeneralTests.cs +++ b/QueryBuilder.Tests/GeneralTests.cs @@ -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(); Assert.Single(froms); @@ -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); @@ -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(act); } @@ -286,7 +286,7 @@ public void OneLimitPerEngine() .ForSqlServer(q => q.Limit(5)) .ForSqlServer(q => q.Limit(10)); - var limits = query.GetComponents("limit", EngineCodes.SqlServer); + var limits = query.GetComponents(ComponentName.Limit, EngineCodes.SqlServer); Assert.Single(limits); Assert.Equal(10, limits.Single().Limit); } @@ -314,7 +314,7 @@ public void OneOffsetPerEngine() .ForSqlServer(q => q.Offset(5)) .ForSqlServer(q => q.Offset(10)); - var limits = query.GetComponents("offset", EngineCodes.SqlServer); + var limits = query.GetComponents(ComponentName.Offset, EngineCodes.SqlServer); Assert.Single(limits); Assert.Equal(10, limits.Single().Offset); } @@ -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()); diff --git a/QueryBuilder/Base.Where.cs b/QueryBuilder/Base.Where.cs index 8a788735..68598d4a 100644 --- a/QueryBuilder/Base.Where.cs +++ b/QueryBuilder/Base.Where.cs @@ -28,7 +28,7 @@ public Q Where(string column, string op, object value) return boolValue ? WhereTrue(column) : WhereFalse(column); } - return AddComponent(ClauseName.Where, new BasicCondition + return AddComponent(ComponentName.Where, new BasicCondition { Column = column, Operator = op, @@ -112,7 +112,7 @@ public Q Where(IEnumerable> values) public Q WhereRaw(string sql, params object[] bindings) { - return AddComponent(ClauseName.Where, new RawCondition + return AddComponent(ComponentName.Where, new RawCondition { Expression = sql, Bindings = bindings, @@ -136,12 +136,12 @@ public Q Where(Func callback) var query = callback.Invoke(NewChild()); // omit empty queries - if (!query.Clauses.Where(x => x.Component == ClauseName.Where).Any()) + if (!query.Clauses.Where(x => x.Component == ComponentName.Where).Any()) { return (Q)this; } - return AddComponent(ClauseName.Where, new NestedCondition + return AddComponent(ComponentName.Where, new NestedCondition { Query = query, IsNot = GetNot(), @@ -166,7 +166,7 @@ public Q OrWhereNot(Func callback) public Q WhereColumns(string first, string op, string second) { - return AddComponent(ClauseName.Where, new TwoColumnsCondition + return AddComponent(ComponentName.Where, new TwoColumnsCondition { First = first, Second = second, @@ -183,7 +183,7 @@ public Q OrWhereColumns(string first, string op, string second) public Q WhereNull(string column) { - return AddComponent(ClauseName.Where, new NullCondition + return AddComponent(ComponentName.Where, new NullCondition { Column = column, IsOr = GetOr(), @@ -208,7 +208,7 @@ public Q OrWhereNotNull(string column) public Q WhereTrue(string column) { - return AddComponent(ClauseName.Where, new BooleanCondition + return AddComponent(ComponentName.Where, new BooleanCondition { Column = column, Value = true, @@ -224,7 +224,7 @@ public Q OrWhereTrue(string column) public Q WhereFalse(string column) { - return AddComponent(ClauseName.Where, new BooleanCondition + return AddComponent(ComponentName.Where, new BooleanCondition { Column = column, Value = false, @@ -240,7 +240,7 @@ public Q OrWhereFalse(string column) public Q WhereLike(string column, object value, bool caseSensitive = false, string escapeCharacter = null) { - return AddComponent(ClauseName.Where, new BasicStringCondition + return AddComponent(ComponentName.Where, new BasicStringCondition { Operator = "like", Column = column, @@ -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(ClauseName.Where, new BasicStringCondition + return AddComponent(ComponentName.Where, new BasicStringCondition { Operator = "starts", Column = column, @@ -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(ClauseName.Where, new BasicStringCondition + return AddComponent(ComponentName.Where, new BasicStringCondition { Operator = "ends", Column = column, @@ -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(ClauseName.Where, new BasicStringCondition + return AddComponent(ComponentName.Where, new BasicStringCondition { Operator = "contains", Column = column, @@ -355,7 +355,7 @@ public Q OrWhereNotContains(string column, object value, bool caseSensitive = fa public Q WhereBetween(string column, T lower, T higher) { - return AddComponent(ClauseName.Where, new BetweenCondition + return AddComponent(ComponentName.Where, new BetweenCondition { Column = column, IsOr = GetOr(), @@ -387,7 +387,7 @@ public Q WhereIn(string column, IEnumerable values) { string val = values as string; - return AddComponent(ClauseName.Where, new InCondition + return AddComponent(ComponentName.Where, new InCondition { Column = column, IsOr = GetOr(), @@ -396,7 +396,7 @@ public Q WhereIn(string column, IEnumerable values) }); } - return AddComponent(ClauseName.Where, new InCondition + return AddComponent(ComponentName.Where, new InCondition { Column = column, IsOr = GetOr(), @@ -425,7 +425,7 @@ public Q OrWhereNotIn(string column, IEnumerable values) public Q WhereIn(string column, Query query) { - return AddComponent(ClauseName.Where, new InQueryCondition + return AddComponent(ComponentName.Where, new InQueryCondition { Column = column, IsOr = GetOr(), @@ -486,7 +486,7 @@ public Q Where(string column, string op, Func callback) public Q Where(string column, string op, Query query) { - return AddComponent(ClauseName.Where, new QueryCondition + return AddComponent(ComponentName.Where, new QueryCondition { Column = column, Operator = op, @@ -503,7 +503,7 @@ public Q WhereSub(Query query, object value) public Q WhereSub(Query query, string op, object value) { - return AddComponent(ClauseName.Where, new SubQueryCondition + return AddComponent(ComponentName.Where, new SubQueryCondition { Value = value, Operator = op, @@ -534,12 +534,12 @@ public Q OrWhere(string column, string op, Func callback) public Q WhereExists(Query query) { - if (!query.HasComponent(ClauseName.From)) + if (!query.HasComponent(ComponentName.From)) { throw new ArgumentException($"'{nameof(FromClause)}' cannot be empty if used inside a '{nameof(WhereExists)}' condition"); } - return AddComponent(ClauseName.Where, new ExistsCondition + return AddComponent(ComponentName.Where, new ExistsCondition { Query = query, IsNot = GetNot(), @@ -582,7 +582,7 @@ public Q OrWhereNotExists(Func callback) #region date public Q WhereDatePart(string part, string column, string op, object value) { - return AddComponent(ClauseName.Where, new BasicDateCondition + return AddComponent(ComponentName.Where, new BasicDateCondition { Operator = op, Column = column, diff --git a/QueryBuilder/BaseQuery.cs b/QueryBuilder/BaseQuery.cs index d1b91bb4..cc68ac71 100644 --- a/QueryBuilder/BaseQuery.cs +++ b/QueryBuilder/BaseQuery.cs @@ -266,7 +266,7 @@ protected bool GetNot() /// public Q From(string table) { - return AddOrReplaceComponent(ClauseName.From, new FromClause + return AddOrReplaceComponent(ComponentName.From, new FromClause { Table = table, }); @@ -282,7 +282,7 @@ public Q From(Query query, string alias = null) query.As(alias); }; - return AddOrReplaceComponent(ClauseName.From, new QueryFromClause + return AddOrReplaceComponent(ComponentName.From, new QueryFromClause { Query = query }); @@ -290,7 +290,7 @@ public Q From(Query query, string alias = null) public Q FromRaw(string sql, params object[] bindings) { - return AddOrReplaceComponent(ClauseName.From, new RawFromClause + return AddOrReplaceComponent(ComponentName.From, new RawFromClause { Expression = sql, Bindings = bindings, diff --git a/QueryBuilder/Compilers/Compiler.Conditions.cs b/QueryBuilder/Compilers/Compiler.Conditions.cs index 02580519..2b826b0d 100644 --- a/QueryBuilder/Compilers/Compiler.Conditions.cs +++ b/QueryBuilder/Compilers/Compiler.Conditions.cs @@ -170,12 +170,12 @@ protected virtual string CompileBasicDateCondition(SqlResult ctx, BasicDateCondi protected virtual string CompileNestedCondition(SqlResult ctx, NestedCondition x) where Q : BaseQuery { - if (!(x.Query.HasComponent(ClauseName.Where, EngineCode) || x.Query.HasComponent(ClauseName.Having, EngineCode))) + if (!(x.Query.HasComponent(ComponentName.Where, EngineCode) || x.Query.HasComponent(ComponentName.Having, EngineCode))) { return null; } - var clause = x.Query.HasComponent(ClauseName.Where, EngineCode) ? ClauseName.Where : ClauseName.Having; + var clause = x.Query.HasComponent(ComponentName.Where, EngineCode) ? ComponentName.Where : ComponentName.Having; var clauses = x.Query.GetComponents(clause, EngineCode); @@ -253,7 +253,7 @@ protected virtual string CompileExistsCondition(SqlResult ctx, ExistsCondition i if (OmitSelectInsideExists) { - query.ClearComponent(ClauseName.Select).SelectRaw("1"); + query.ClearComponent(ComponentName.Select).SelectRaw("1"); } var subCtx = CompileSelectQuery(query); diff --git a/QueryBuilder/Compilers/Compiler.cs b/QueryBuilder/Compilers/Compiler.cs index b8fc1028..0564fe52 100644 --- a/QueryBuilder/Compilers/Compiler.cs +++ b/QueryBuilder/Compilers/Compiler.cs @@ -79,14 +79,14 @@ protected SqlResult PrepareResult(SqlResult ctx) private Query TransformAggregateQuery(Query query) { - var clause = query.GetOneComponent(ClauseName.Aggregate, EngineCode); + var clause = query.GetOneComponent(ComponentName.Aggregate, EngineCode); if (clause.Columns.Count == 1 && !query.IsDistinct) return query; if (query.IsDistinct) { - query.ClearComponent(ClauseName.Aggregate, EngineCode); - query.ClearComponent(ClauseName.Select, EngineCode); + query.ClearComponent(ComponentName.Aggregate, EngineCode); + query.ClearComponent(ComponentName.Select, EngineCode); query.Select(clause.Columns.ToArray()); } else @@ -104,7 +104,7 @@ private Query TransformAggregateQuery(Query query) }; return new Query() - .AddComponent(ClauseName.Aggregate, outerClause) + .AddComponent(ComponentName.Aggregate, outerClause) .From(query, $"{clause.Type}Query"); } @@ -128,9 +128,9 @@ protected virtual SqlResult CompileRaw(Query query) { if (query.Method == "aggregate") { - query.ClearComponent(ClauseName.Limit) - .ClearComponent(ClauseName.Order) - .ClearComponent(ClauseName.Group); + query.ClearComponent(ComponentName.Limit) + .ClearComponent(ComponentName.Order) + .ClearComponent(ComponentName.Group); query = TransformAggregateQuery(query); } @@ -139,7 +139,7 @@ protected virtual SqlResult CompileRaw(Query query) } // handle CTEs - if (query.HasComponent(ClauseName.Cte, EngineCode)) + if (query.HasComponent(ComponentName.Cte, EngineCode)) { ctx = CompileCteQuery(ctx, query); } @@ -255,12 +255,12 @@ protected virtual SqlResult CompileDeleteQuery(Query query) Query = query }; - if (!ctx.Query.HasComponent(ClauseName.From, EngineCode)) + if (!ctx.Query.HasComponent(ComponentName.From, EngineCode)) { throw new InvalidOperationException("No table set to delete"); } - var fromClause = ctx.Query.GetOneComponent(ClauseName.From, EngineCode); + var fromClause = ctx.Query.GetOneComponent(ComponentName.From, EngineCode); string table = null; @@ -317,12 +317,12 @@ protected virtual SqlResult CompileUpdateQuery(Query query) Query = query }; - if (!ctx.Query.HasComponent(ClauseName.From, EngineCode)) + if (!ctx.Query.HasComponent(ComponentName.From, EngineCode)) { throw new InvalidOperationException("No table set to update"); } - var fromClause = ctx.Query.GetOneComponent(ClauseName.From, EngineCode); + var fromClause = ctx.Query.GetOneComponent(ComponentName.From, EngineCode); string table = null; @@ -343,7 +343,7 @@ protected virtual SqlResult CompileUpdateQuery(Query query) } // check for increment statements - var clause = ctx.Query.GetOneComponent(ClauseName.Update, EngineCode); + var clause = ctx.Query.GetOneComponent(ComponentName.Update, EngineCode); string wheres; @@ -366,7 +366,7 @@ protected virtual SqlResult CompileUpdateQuery(Query query) } - var toUpdate = ctx.Query.GetOneComponent(ClauseName.Update, EngineCode); + var toUpdate = ctx.Query.GetOneComponent(ComponentName.Update, EngineCode); var parts = new List(); for (var i = 0; i < toUpdate.Columns.Count; i++) @@ -395,10 +395,10 @@ protected virtual SqlResult CompileInsertQuery(Query query) Query = query }; - if (!ctx.Query.HasComponent(ClauseName.From, EngineCode)) + if (!ctx.Query.HasComponent(ComponentName.From, EngineCode)) throw new InvalidOperationException("No table set to insert"); - var fromClause = ctx.Query.GetOneComponent(ClauseName.From, EngineCode); + var fromClause = ctx.Query.GetOneComponent(ComponentName.From, EngineCode); if (fromClause is null) throw new InvalidOperationException("Invalid table expression"); @@ -414,7 +414,7 @@ protected virtual SqlResult CompileInsertQuery(Query query) if (table is null) throw new InvalidOperationException("Invalid table expression"); - var inserts = ctx.Query.GetComponents(ClauseName.Insert, EngineCode); + var inserts = ctx.Query.GetComponents(ComponentName.Insert, EngineCode); if (inserts[0] is InsertQueryClause insertQueryClause) return CompileInsertQueryClause(ctx, table, insertQueryClause); else @@ -566,7 +566,7 @@ protected virtual string CompileFilterConditions(SqlResult ctx, AggregatedColumn return null; } - var wheres = aggregatedColumn.Filter.GetComponents(ClauseName.Where); + var wheres = aggregatedColumn.Filter.GetComponents(ComponentName.Where); return CompileConditions(ctx, wheres); } @@ -610,9 +610,9 @@ protected virtual SqlResult OnBeforeSelect(SqlResult ctx) protected virtual string CompileColumns(SqlResult ctx) { - if (ctx.Query.HasComponent(ClauseName.Aggregate, EngineCode)) + if (ctx.Query.HasComponent(ComponentName.Aggregate, EngineCode)) { - var aggregate = ctx.Query.GetOneComponent(ClauseName.Aggregate, EngineCode); + var aggregate = ctx.Query.GetOneComponent(ComponentName.Aggregate, EngineCode); var aggregateColumns = aggregate.Columns .Select(x => CompileColumn(ctx, new Column { Name = x })) @@ -636,7 +636,7 @@ protected virtual string CompileColumns(SqlResult ctx) } var columns = ctx.Query - .GetComponents(ClauseName.Select, EngineCode) + .GetComponents(ComponentName.Select, EngineCode) .Select(x => CompileColumn(ctx, x)) .ToList(); @@ -652,14 +652,14 @@ public virtual string CompileUnion(SqlResult ctx) { // Handle UNION, EXCEPT and INTERSECT - if (!ctx.Query.GetComponents(ClauseName.Combine, EngineCode).Any()) + if (!ctx.Query.GetComponents(ComponentName.Combine, EngineCode).Any()) { return null; } var combinedQueries = new List(); - var clauses = ctx.Query.GetComponents(ClauseName.Combine, EngineCode); + var clauses = ctx.Query.GetComponents(ComponentName.Combine, EngineCode); foreach (var clause in clauses) { @@ -719,9 +719,9 @@ public virtual string CompileTableExpression(SqlResult ctx, AbstractFrom from) public virtual string CompileFrom(SqlResult ctx) { - if (ctx.Query.HasComponent(ClauseName.From, EngineCode)) + if (ctx.Query.HasComponent(ComponentName.From, EngineCode)) { - var from = ctx.Query.GetOneComponent(ClauseName.From, EngineCode); + var from = ctx.Query.GetOneComponent(ComponentName.From, EngineCode); return "FROM " + CompileTableExpression(ctx, from); } @@ -731,13 +731,13 @@ public virtual string CompileFrom(SqlResult ctx) public virtual string CompileJoins(SqlResult ctx) { - if (!ctx.Query.HasComponent(ClauseName.Join, EngineCode)) + if (!ctx.Query.HasComponent(ComponentName.Join, EngineCode)) { return null; } var joins = ctx.Query - .GetComponents(ClauseName.Join, EngineCode) + .GetComponents(ComponentName.Join, EngineCode) .Select(x => CompileJoin(ctx, x.Join)); return "\n" + string.Join("\n", joins); @@ -746,8 +746,8 @@ public virtual string CompileJoins(SqlResult ctx) public virtual string CompileJoin(SqlResult ctx, Join join, bool isNested = false) { - var from = join.GetOneComponent(ClauseName.From, EngineCode); - var conditions = join.GetComponents(ClauseName.Where, EngineCode); + var from = join.GetOneComponent(ComponentName.From, EngineCode); + var conditions = join.GetComponents(ComponentName.Where, EngineCode); var joinTable = CompileTableExpression(ctx, from); var constraints = CompileConditions(ctx, conditions); @@ -759,12 +759,12 @@ public virtual string CompileJoin(SqlResult ctx, Join join, bool isNested = fals public virtual string CompileWheres(SqlResult ctx) { - if (!ctx.Query.HasComponent(ClauseName.Where, EngineCode)) + if (!ctx.Query.HasComponent(ComponentName.Where, EngineCode)) { return null; } - var conditions = ctx.Query.GetComponents(ClauseName.Where, EngineCode); + var conditions = ctx.Query.GetComponents(ComponentName.Where, EngineCode); var sql = CompileConditions(ctx, conditions).Trim(); return string.IsNullOrEmpty(sql) ? null : $"WHERE {sql}"; @@ -772,13 +772,13 @@ public virtual string CompileWheres(SqlResult ctx) public virtual string CompileGroups(SqlResult ctx) { - if (!ctx.Query.HasComponent(ClauseName.Group, EngineCode)) + if (!ctx.Query.HasComponent(ComponentName.Group, EngineCode)) { return null; } var columns = ctx.Query - .GetComponents(ClauseName.Group, EngineCode) + .GetComponents(ComponentName.Group, EngineCode) .Select(x => CompileColumn(ctx, x)); return "GROUP BY " + string.Join(", ", columns); @@ -786,13 +786,13 @@ public virtual string CompileGroups(SqlResult ctx) public virtual string CompileOrders(SqlResult ctx) { - if (!ctx.Query.HasComponent(ClauseName.Order, EngineCode)) + if (!ctx.Query.HasComponent(ComponentName.Order, EngineCode)) { return null; } var columns = ctx.Query - .GetComponents(ClauseName.Order, EngineCode) + .GetComponents(ComponentName.Order, EngineCode) .Select(x => { @@ -812,7 +812,7 @@ public virtual string CompileOrders(SqlResult ctx) public virtual string CompileHaving(SqlResult ctx) { - if (!ctx.Query.HasComponent(ClauseName.Having, EngineCode)) + if (!ctx.Query.HasComponent(ComponentName.Having, EngineCode)) { return null; } @@ -820,7 +820,7 @@ public virtual string CompileHaving(SqlResult ctx) var sql = new List(); string boolOperator; - var having = ctx.Query.GetComponents(ClauseName.Having, EngineCode) + var having = ctx.Query.GetComponents(ComponentName.Having, EngineCode) .Cast() .ToList(); diff --git a/QueryBuilder/Compilers/CteFinder.cs b/QueryBuilder/Compilers/CteFinder.cs index c17de322..b036d075 100644 --- a/QueryBuilder/Compilers/CteFinder.cs +++ b/QueryBuilder/Compilers/CteFinder.cs @@ -32,7 +32,7 @@ public List Find() private List FindInternal(Query queryToSearch) { - var cteList = queryToSearch.GetComponents(ClauseName.Cte, engineCode); + var cteList = queryToSearch.GetComponents(ComponentName.Cte, engineCode); var resultList = new List(); diff --git a/QueryBuilder/Compilers/FirebirdCompiler.cs b/QueryBuilder/Compilers/FirebirdCompiler.cs index 0e645305..43dba203 100644 --- a/QueryBuilder/Compilers/FirebirdCompiler.cs +++ b/QueryBuilder/Compilers/FirebirdCompiler.cs @@ -17,7 +17,7 @@ protected override SqlResult CompileInsertQuery(Query query) { var ctx = base.CompileInsertQuery(query); - var inserts = ctx.Query.GetComponents(ClauseName.Insert, EngineCode); + var inserts = ctx.Query.GetComponents(ComponentName.Insert, EngineCode); if (inserts.Count > 1) { @@ -57,7 +57,7 @@ protected override string CompileColumns(SqlResult ctx) { ctx.Bindings.Insert(0, limit); - ctx.Query.ClearComponent(ClauseName.Limit); + ctx.Query.ClearComponent(ComponentName.Limit); return $"SELECT FIRST {parameterPlaceholder}" + compiled.Substring(6); } @@ -65,7 +65,7 @@ protected override string CompileColumns(SqlResult ctx) { ctx.Bindings.Insert(0, offset); - ctx.Query.ClearComponent(ClauseName.Offset); + ctx.Query.ClearComponent(ComponentName.Offset); return $"SELECT SKIP {parameterPlaceholder}" + compiled.Substring(6); } diff --git a/QueryBuilder/Compilers/OracleCompiler.cs b/QueryBuilder/Compilers/OracleCompiler.cs index c5725f67..5cd3a6e4 100644 --- a/QueryBuilder/Compilers/OracleCompiler.cs +++ b/QueryBuilder/Compilers/OracleCompiler.cs @@ -52,7 +52,7 @@ public override string CompileLimit(SqlResult ctx) var safeOrder = ""; - if (!ctx.Query.HasComponent(ClauseName.Order)) + if (!ctx.Query.HasComponent(ComponentName.Order)) { safeOrder = "ORDER BY (SELECT 0 FROM DUAL) "; } diff --git a/QueryBuilder/Compilers/SqlServerCompiler.cs b/QueryBuilder/Compilers/SqlServerCompiler.cs index 6148ba8e..3d8f457c 100644 --- a/QueryBuilder/Compilers/SqlServerCompiler.cs +++ b/QueryBuilder/Compilers/SqlServerCompiler.cs @@ -32,7 +32,7 @@ protected override SqlResult CompileSelectQuery(Query query) var offset = query.GetOffset(EngineCode); - if (!query.HasComponent(ClauseName.Select)) + if (!query.HasComponent(ComponentName.Select)) { query.Select("*"); } @@ -41,7 +41,7 @@ protected override SqlResult CompileSelectQuery(Query query) query.SelectRaw($"ROW_NUMBER() OVER ({order}) AS [row_num]", ctx.Bindings.ToArray()); - query.ClearComponent(ClauseName.Order); + query.ClearComponent(ComponentName.Order); var result = base.CompileSelectQuery(query); @@ -81,7 +81,7 @@ protected override string CompileColumns(SqlResult ctx) // top bindings should be inserted first ctx.Bindings.Insert(0, limit); - ctx.Query.ClearComponent(ClauseName.Limit); + ctx.Query.ClearComponent(ComponentName.Limit); // handle distinct if (compiled.IndexOf("SELECT DISTINCT") == 0) @@ -113,7 +113,7 @@ public override string CompileLimit(SqlResult ctx) } var safeOrder = ""; - if (!ctx.Query.HasComponent(ClauseName.Order)) + if (!ctx.Query.HasComponent(ComponentName.Order)) { safeOrder = "ORDER BY (SELECT 0) "; } diff --git a/QueryBuilder/ClauseName.cs b/QueryBuilder/ComponentName.cs similarity index 95% rename from QueryBuilder/ClauseName.cs rename to QueryBuilder/ComponentName.cs index 5d8ff57e..cfb8e9c3 100644 --- a/QueryBuilder/ClauseName.cs +++ b/QueryBuilder/ComponentName.cs @@ -1,6 +1,6 @@ namespace SqlKata { - public static class ClauseName + public static class ComponentName { public static string Select { get; set; } = "select"; public static string From { get; set; } = "from"; diff --git a/QueryBuilder/Join.cs b/QueryBuilder/Join.cs index 920f8a00..ffe71161 100644 --- a/QueryBuilder/Join.cs +++ b/QueryBuilder/Join.cs @@ -53,7 +53,7 @@ public Join AsType(string type) public Join On(string first, string second, string op = "=") { - return AddComponent(ClauseName.Where, new TwoColumnsCondition + return AddComponent(ComponentName.Where, new TwoColumnsCondition { First = first, Second = second, diff --git a/QueryBuilder/Query.Aggregate.cs b/QueryBuilder/Query.Aggregate.cs index fb348caf..8b1f12e6 100644 --- a/QueryBuilder/Query.Aggregate.cs +++ b/QueryBuilder/Query.Aggregate.cs @@ -10,8 +10,8 @@ public Query AsAggregate(string type, string[] columns = null) Method = "aggregate"; - this.ClearComponent(ClauseName.Aggregate) - .AddComponent(ClauseName.Aggregate, new AggregateClause + this.ClearComponent(ComponentName.Aggregate) + .AddComponent(ComponentName.Aggregate, new AggregateClause { Type = type, Columns = columns?.ToList() ?? new List(), diff --git a/QueryBuilder/Query.Combine.cs b/QueryBuilder/Query.Combine.cs index 0596182c..0f87c4cc 100644 --- a/QueryBuilder/Query.Combine.cs +++ b/QueryBuilder/Query.Combine.cs @@ -13,7 +13,7 @@ public Query Combine(string operation, bool all, Query query) throw new InvalidOperationException("Only select queries can be combined."); } - return AddComponent(ClauseName.Combine, new Combine + return AddComponent(ComponentName.Combine, new Combine { Query = query, Operation = operation, @@ -28,7 +28,7 @@ public Query CombineRaw(string sql, params object[] bindings) throw new InvalidOperationException("Only select queries can be combined."); } - return AddComponent(ClauseName.Combine, new RawCombine + return AddComponent(ComponentName.Combine, new RawCombine { Expression = sql, Bindings = bindings, diff --git a/QueryBuilder/Query.Having.cs b/QueryBuilder/Query.Having.cs index d5773b3d..2a00fa29 100644 --- a/QueryBuilder/Query.Having.cs +++ b/QueryBuilder/Query.Having.cs @@ -18,7 +18,7 @@ public Query Having(string column, string op, object value) return Not(op != "=").HavingNull(column); } - return AddComponent(ClauseName.Having, new BasicCondition + return AddComponent(ComponentName.Having, new BasicCondition { Column = column, Operator = op, @@ -102,7 +102,7 @@ public Query Having(IEnumerable> values) public Query HavingRaw(string sql, params object[] bindings) { - return AddComponent(ClauseName.Having, new RawCondition + return AddComponent(ComponentName.Having, new RawCondition { Expression = sql, Bindings = bindings, @@ -125,7 +125,7 @@ public Query Having(Func callback) { var query = callback.Invoke(NewChild()); - return AddComponent(ClauseName.Having, new NestedCondition + return AddComponent(ComponentName.Having, new NestedCondition { Query = query, IsNot = GetNot(), @@ -150,7 +150,7 @@ public Query OrHavingNot(Func callback) public Query HavingColumns(string first, string op, string second) { - return AddComponent(ClauseName.Having, new TwoColumnsCondition + return AddComponent(ComponentName.Having, new TwoColumnsCondition { First = first, Second = second, @@ -167,7 +167,7 @@ public Query OrHavingColumns(string first, string op, string second) public Query HavingNull(string column) { - return AddComponent(ClauseName.Having, new NullCondition + return AddComponent(ComponentName.Having, new NullCondition { Column = column, IsOr = GetOr(), @@ -192,7 +192,7 @@ public Query OrHavingNotNull(string column) public Query HavingTrue(string column) { - return AddComponent(ClauseName.Having, new BooleanCondition + return AddComponent(ComponentName.Having, new BooleanCondition { Column = column, Value = true, @@ -206,7 +206,7 @@ public Query OrHavingTrue(string column) public Query HavingFalse(string column) { - return AddComponent(ClauseName.Having, new BooleanCondition + return AddComponent(ComponentName.Having, new BooleanCondition { Column = column, Value = false, @@ -220,7 +220,7 @@ public Query OrHavingFalse(string column) public Query HavingLike(string column, object value, bool caseSensitive = false, string escapeCharacter = null) { - return AddComponent(ClauseName.Having, new BasicStringCondition + return AddComponent(ComponentName.Having, new BasicStringCondition { Operator = "like", Column = column, @@ -248,7 +248,7 @@ public Query OrHavingNotLike(string column, object value, bool caseSensitive = f } public Query HavingStarts(string column, object value, bool caseSensitive = false, string escapeCharacter = null) { - return AddComponent(ClauseName.Having, new BasicStringCondition + return AddComponent(ComponentName.Having, new BasicStringCondition { Operator = "starts", Column = column, @@ -277,7 +277,7 @@ public Query OrHavingNotStarts(string column, object value, bool caseSensitive = public Query HavingEnds(string column, object value, bool caseSensitive = false, string escapeCharacter = null) { - return AddComponent(ClauseName.Having, new BasicStringCondition + return AddComponent(ComponentName.Having, new BasicStringCondition { Operator = "ends", Column = column, @@ -306,7 +306,7 @@ public Query OrHavingNotEnds(string column, object value, bool caseSensitive = f public Query HavingContains(string column, object value, bool caseSensitive = false, string escapeCharacter = null) { - return AddComponent(ClauseName.Having, new BasicStringCondition + return AddComponent(ComponentName.Having, new BasicStringCondition { Operator = "contains", Column = column, @@ -335,7 +335,7 @@ public Query OrHavingNotContains(string column, object value, bool caseSensitive public Query HavingBetween(string column, T lower, T higher) { - return AddComponent(ClauseName.Having, new BetweenCondition + return AddComponent(ComponentName.Having, new BetweenCondition { Column = column, IsOr = GetOr(), @@ -366,7 +366,7 @@ public Query HavingIn(string column, IEnumerable values) { string val = values as string; - return AddComponent(ClauseName.Having, new InCondition + return AddComponent(ComponentName.Having, new InCondition { Column = column, IsOr = GetOr(), @@ -375,7 +375,7 @@ public Query HavingIn(string column, IEnumerable values) }); } - return AddComponent(ClauseName.Having, new InCondition + return AddComponent(ComponentName.Having, new InCondition { Column = column, IsOr = GetOr(), @@ -404,7 +404,7 @@ public Query OrHavingNotIn(string column, IEnumerable values) public Query HavingIn(string column, Query query) { - return AddComponent(ClauseName.Having, new InQueryCondition + return AddComponent(ComponentName.Having, new InQueryCondition { Column = column, IsOr = GetOr(), @@ -465,7 +465,7 @@ public Query Having(string column, string op, Func callback) public Query Having(string column, string op, Query query) { - return AddComponent(ClauseName.Having, new QueryCondition + return AddComponent(ComponentName.Having, new QueryCondition { Column = column, Operator = op, @@ -486,17 +486,17 @@ public Query OrHaving(string column, string op, Func callback) public Query HavingExists(Query query) { - if (!query.HasComponent(ClauseName.From)) + if (!query.HasComponent(ComponentName.From)) { throw new ArgumentException($"{nameof(FromClause)} cannot be empty if used inside a {nameof(HavingExists)} condition"); } // simplify the query as much as possible - query = query.Clone().ClearComponent(ClauseName.Select) + query = query.Clone().ClearComponent(ComponentName.Select) .SelectRaw("1") .Limit(1); - return AddComponent(ClauseName.Having, new ExistsCondition + return AddComponent(ComponentName.Having, new ExistsCondition { Query = query, IsNot = GetNot(), @@ -539,7 +539,7 @@ public Query OrHavingNotExists(Func callback) #region date public Query HavingDatePart(string part, string column, string op, object value) { - return AddComponent(ClauseName.Having, new BasicDateCondition + return AddComponent(ComponentName.Having, new BasicDateCondition { Operator = op, Column = column, diff --git a/QueryBuilder/Query.Insert.cs b/QueryBuilder/Query.Insert.cs index f4d53844..da45b0f0 100644 --- a/QueryBuilder/Query.Insert.cs +++ b/QueryBuilder/Query.Insert.cs @@ -31,7 +31,7 @@ public Query AsInsert(IEnumerable columns, IEnumerable values) Method = "insert"; - ClearComponent(ClauseName.Insert).AddComponent(ClauseName.Insert, new InsertClause + ClearComponent(ComponentName.Insert).AddComponent(ComponentName.Insert, new InsertClause { Columns = columnsList, Values = valuesList @@ -49,7 +49,7 @@ public Query AsInsert(IEnumerable> values, bool ret Method = "insert"; - ClearComponent(ClauseName.Insert).AddComponent(ClauseName.Insert, new InsertClause + ClearComponent(ComponentName.Insert).AddComponent(ComponentName.Insert, new InsertClause { Columns = values.Select(x => x.Key).ToList(), Values = values.Select(x => x.Value).ToList(), @@ -77,7 +77,7 @@ public Query AsInsert(IEnumerable columns, IEnumerable columns, IEnumerable columns, Query query) { Method = "insert"; - ClearComponent(ClauseName.Insert).AddComponent(ClauseName.Insert, new InsertQueryClause + ClearComponent(ComponentName.Insert).AddComponent(ComponentName.Insert, new InsertQueryClause { Columns = columns.ToList(), Query = query.Clone(), diff --git a/QueryBuilder/Query.Join.cs b/QueryBuilder/Query.Join.cs index 34101120..3d961771 100644 --- a/QueryBuilder/Query.Join.cs +++ b/QueryBuilder/Query.Join.cs @@ -9,7 +9,7 @@ private Query Join(Func callback) { var join = callback.Invoke(new Join().AsInner()); - return AddComponent(ClauseName.Join, new BaseJoin + return AddComponent(ComponentName.Join, new BaseJoin { Join = join }); diff --git a/QueryBuilder/Query.Select.cs b/QueryBuilder/Query.Select.cs index 84bd98a2..60f1c27a 100644 --- a/QueryBuilder/Query.Select.cs +++ b/QueryBuilder/Query.Select.cs @@ -24,7 +24,7 @@ public Query Select(IEnumerable columns) foreach (var column in columns) { - AddComponent(ClauseName.Select, new Column + AddComponent(ComponentName.Select, new Column { Name = column }); @@ -41,7 +41,7 @@ public Query SelectRaw(string sql, params object[] bindings) { Method = "select"; - AddComponent(ClauseName.Select, new RawColumn + AddComponent(ComponentName.Select, new RawColumn { Expression = sql, Bindings = bindings, @@ -56,7 +56,7 @@ public Query Select(Query query, string alias) query = query.Clone(); - AddComponent(ClauseName.Select, new QueryColumn + AddComponent(ComponentName.Select, new QueryColumn { Query = query.As(alias), }); @@ -73,7 +73,7 @@ public Query SelectAggregate(string aggregate, string column, Query filter = nul { Method = "select"; - AddComponent(ClauseName.Select, new AggregatedColumn + AddComponent(ComponentName.Select, new AggregatedColumn { Column = new Column { Name = column }, Aggregate = aggregate, diff --git a/QueryBuilder/Query.Update.cs b/QueryBuilder/Query.Update.cs index 398a77cd..803d5703 100644 --- a/QueryBuilder/Query.Update.cs +++ b/QueryBuilder/Query.Update.cs @@ -28,7 +28,7 @@ public Query AsUpdate(IEnumerable columns, IEnumerable values) Method = "update"; - ClearComponent(ClauseName.Update).AddComponent(ClauseName.Update, new InsertClause + ClearComponent(ComponentName.Update).AddComponent(ComponentName.Update, new InsertClause { Columns = columns.ToList(), Values = values.ToList() @@ -46,7 +46,7 @@ public Query AsUpdate(IEnumerable> values) Method = "update"; - ClearComponent(ClauseName.Update).AddComponent(ClauseName.Update, new InsertClause + ClearComponent(ComponentName.Update).AddComponent(ComponentName.Update, new InsertClause { Columns = values.Select(x => x.Key).ToList(), Values = values.Select(x => x.Value).ToList(), @@ -58,7 +58,7 @@ public Query AsUpdate(IEnumerable> values) public Query AsIncrement(string column, int value = 1) { Method = "update"; - AddOrReplaceComponent(ClauseName.Update, new IncrementClause + AddOrReplaceComponent(ComponentName.Update, new IncrementClause { Column = column, Value = value diff --git a/QueryBuilder/Query.cs b/QueryBuilder/Query.cs index 0ee358be..e57080d2 100755 --- a/QueryBuilder/Query.cs +++ b/QueryBuilder/Query.cs @@ -35,7 +35,7 @@ public Query(string table, string comment = null) : base() internal long GetOffset(string engineCode = null) { engineCode = engineCode ?? EngineScope; - var offset = this.GetOneComponent(ClauseName.Offset, engineCode); + var offset = this.GetOneComponent(ComponentName.Offset, engineCode); return offset?.Offset ?? 0; } @@ -43,7 +43,7 @@ internal long GetOffset(string engineCode = null) internal int GetLimit(string engineCode = null) { engineCode = engineCode ?? EngineScope; - var limit = this.GetOneComponent(ClauseName.Limit, engineCode); + var limit = this.GetOneComponent(ComponentName.Limit, engineCode); return limit?.Limit ?? 0; } @@ -104,7 +104,7 @@ public Query With(Query query) // clear the query alias query.QueryAlias = null; - return AddComponent(ClauseName.Cte, new QueryFromClause + return AddComponent(ComponentName.Cte, new QueryFromClause { Query = query, Alias = alias, @@ -157,12 +157,12 @@ public Query With(string alias, IEnumerable columns, IEnumerable>> GetAsync( public bool Exists(Query query, IDbTransaction transaction = null, int? timeout = null) { var clone = query.Clone() - .ClearComponent("select") + .ClearComponent(ComponentName.Select) .SelectRaw("1 as [Exists]") .Limit(1); @@ -363,7 +363,7 @@ public bool Exists(Query query, IDbTransaction transaction = null, int? timeout public async Task ExistsAsync(Query query, IDbTransaction transaction = null, int? timeout = null, CancellationToken cancellationToken = default) { var clone = query.Clone() - .ClearComponent("select") + .ClearComponent(ComponentName.Select) .SelectRaw("1 as [Exists]") .Limit(1); @@ -685,7 +685,7 @@ private static IEnumerable handleIncludes(Query query, IEnumerable resu // I will try to fetch the table name if provided and appending the Id as a convention // Here am using Humanizer package to help getting the singular form of the table - var fromTable = query.GetOneComponent("from") as FromClause; + var fromTable = query.GetOneComponent(ComponentName.From) as FromClause; if (fromTable == null) { @@ -786,7 +786,7 @@ private static async Task> handleIncludesAsync(Query query, IE // I will try to fetch the table name if provided and appending the Id as a convention // Here am using Humanizer package to help getting the singular form of the table - var fromTable = query.GetOneComponent("from") as FromClause; + var fromTable = query.GetOneComponent(ComponentName.From) as FromClause; if (fromTable == null) {