private void WriteHaving(ParserWriter writer, APSqlWhereClause clause, OracleCommand dbCmd) { if (clause != null && clause.Next != null) { writer.WriteLine(); writer.WriteDirect("HAVING"); APSqlWherePhrase phrase = clause.Next as APSqlWherePhrase; if (phrase.Next == null) { if (phrase is APSqlConditionAndPhrase) { WriteConditionPhrase(writer, (phrase as APSqlConditionAndPhrase).Child, APSqlConditionJoinType.AND, dbCmd); return; } else if (phrase is APSqlConditionOrPhrase) { WriteConditionPhrase(writer, (phrase as APSqlConditionOrPhrase).Child, APSqlConditionJoinType.OR, dbCmd); return; } } WriteConditionPhrase(writer, phrase, APSqlConditionJoinType.AND, dbCmd); } }
/// <summary> /// Create a 'FROM' phrase. /// </summary> /// <param name="tableDef">Table definition.</param> /// <param name="joinType">SQL 'JOIN' type.</param> /// <param name="joinOnPhrase">SQL 'JOIN' condition.</param> public APSqlFromPhrase(APTableDef tableDef, APSqlJoinType joinType, APSqlWherePhrase joinOnPhrase) { if (tableDef == null) throw new ArgumentNullException("tableDef"); _tableDef = tableDef; _joinType = joinType; _joinOnPhrase = joinOnPhrase; }
/// <summary> /// Create a 'FROM' phrase. /// </summary> /// <param name="tableDef">Table definition.</param> /// <param name="joinType">SQL 'JOIN' type.</param> /// <param name="joinOnPhrase">SQL 'JOIN' condition.</param> public APSqlFromPhrase(APTableDef tableDef, APSqlJoinType joinType, APSqlWherePhrase joinOnPhrase) { if (tableDef == null) { throw new ArgumentNullException("tableDef"); } _tableDef = tableDef; _joinType = joinType; _joinOnPhrase = joinOnPhrase; }
/// <summary> /// Create a new 'AND' condition group. /// </summary> /// <param name="phrases">The IEnumerable phrases.</param> public APSqlConditionAndPhrase(IEnumerable<APSqlWherePhrase> phrases) { IAPSqlPhrase phrase = null; foreach (APSqlWherePhrase next in phrases) { if (_child == null) phrase = _child = next; else phrase = phrase.SetNext(next); } }
/// <summary> /// Create a new 'OR' condition group. /// </summary> /// <param name="phrases">The IEnumerable phrases.</param> public APSqlConditionOrPhrase(IEnumerable <APSqlWherePhrase> phrases) { IAPSqlPhrase phrase = null; foreach (APSqlWherePhrase next in phrases) { if (_child == null) { phrase = _child = next; } else { phrase = phrase.SetNext(next); } } }
/// <summary> /// SQL 'HAVING' phrase extensions. Add new condition join 'OR' with exist condition. /// </summary> /// <param name="command">The command.</param> /// <param name="phrase">The new 'WHERE' phrase.</param> /// <returns>The command.</returns> public static APSqlSelectCommand having_or(this APSqlSelectCommand command, APSqlWherePhrase phrase) { if (command.HavingClause == null || command.HavingClause.Next == null) { command.HavingClause = new APSqlWhereClause(phrase); } else { APSqlWherePhrase exist = command.HavingClause.Next as APSqlWherePhrase; if (exist is APSqlConditionOrPhrase) { (exist as APSqlConditionOrPhrase).Child.Last.SetNext(phrase); } else { command.HavingClause = new APSqlWhereClause(APSqlConditionJoinType.OR, exist, phrase); } } return(command); }
/// <summary> /// SQL 'WHERE' phrase extensions. Add new condition join 'AND' with exist condition. /// </summary> /// <param name="command">The command.</param> /// <param name="phrase">The new 'WHERE' phrase.</param> /// <returns>The command.</returns> public static APSqlDeleteCommand where_and(this APSqlDeleteCommand command, APSqlWherePhrase phrase) { if (command.WhereClause == null || command.WhereClause.Next == null) { command.WhereClause = new APSqlWhereClause(phrase); } else { APSqlWherePhrase exist = command.WhereClause.Next as APSqlWherePhrase; if (exist is APSqlConditionAndPhrase) { (exist as APSqlConditionAndPhrase).Child.Last.SetNext(phrase); } else { command.WhereClause = new APSqlWhereClause(APSqlConditionJoinType.AND, exist, phrase); } } return(command); }
private void WriteWhere(ParserWriter writer, APSqlWhereClause clause, OracleCommand dbCmd, int?maxReturnCount = null) { bool standardWhere = clause != null && clause.Next != null; if (standardWhere || maxReturnCount != null) { writer.WriteLine(); writer.WriteDirect("WHERE"); if (standardWhere) { APSqlWherePhrase phrase = clause.Next as APSqlWherePhrase; if (phrase.Next == null) { if (phrase is APSqlConditionAndPhrase) { WriteConditionPhrase(writer, (phrase as APSqlConditionAndPhrase).Child, APSqlConditionJoinType.AND, dbCmd); return; } else if (phrase is APSqlConditionOrPhrase) { WriteConditionPhrase(writer, (phrase as APSqlConditionOrPhrase).Child, APSqlConditionJoinType.OR, dbCmd); return; } } WriteConditionPhrase(writer, phrase, APSqlConditionJoinType.AND, dbCmd); } if (maxReturnCount != null) { if (standardWhere) { writer.Write("AND"); } writer.Write("ROWNUM <= " + maxReturnCount.Value); } } }
private void WriteJoinOn(ParserWriter writer, APSqlWherePhrase phrase, OracleCommand dbCmd) { if (phrase != null) { writer.Write("ON"); if (phrase.Next == null) { if (phrase is APSqlConditionAndPhrase) { WriteConditionPhrase(writer, (phrase as APSqlConditionAndPhrase).Child, APSqlConditionJoinType.AND, dbCmd); return; } else if (phrase is APSqlConditionOrPhrase) { WriteConditionPhrase(writer, (phrase as APSqlConditionOrPhrase).Child, APSqlConditionJoinType.OR, dbCmd); return; } } WriteConditionPhrase(writer, phrase, APSqlConditionJoinType.AND, dbCmd); } }
/// <summary> /// SQL 'WHERE' phrase extensions. /// </summary> /// <param name="command">The command.</param> /// <param name="phrase">The 'WHERE' phrase.</param> /// <returns>The command.</returns> public static APSqlUpdateCommand where(this APSqlUpdateCommand command, APSqlWherePhrase phrase) { command.WhereClause = new APSqlWhereClause(phrase); return command; }
/// <summary> /// SQL 'HAVING' phrase extensions. /// </summary> /// <param name="command">The command.</param> /// <param name="phrase">The 'HAVING' phrase.</param> /// <returns>The command.</returns> public static APSqlSelectCommand having(this APSqlSelectCommand command, APSqlWherePhrase phrase) { command.HavingClause = phrase == null ? null : new APSqlWhereClause(phrase); return(command); }
/// <summary> /// SQL 'WHERE' phrase extensions. /// </summary> /// <param name="command">The command.</param> /// <param name="phrase">The 'WHERE' phrase.</param> /// <returns>The command.</returns> public static APSqlSelectCommand where (this APSqlSelectCommand command, APSqlWherePhrase phrase) { command.WhereClause = new APSqlWhereClause(phrase); return(command); }
/// <summary> /// Create a new 'OR' condition group. /// </summary> /// <param name="phrase1">'WHERE' phrase1.</param> /// <param name="phrases">'WHERE' phrases.</param> public APSqlConditionOrPhrase(APSqlWherePhrase phrase1, params APSqlWherePhrase[] phrases) { _child = phrase1; _child.SetNext(phrases); }
/// <summary> /// Create 'Full JOIN' phrase. /// </summary> /// <param name="tableDef">The table defined.</param> /// <param name="wherePhrase">Join 'ON' phrase.</param> /// <returns>A 'FROM' phrase.</returns> public static APSqlFromPhrase JoinFull(this APTableDef tableDef, APSqlWherePhrase wherePhrase) { return new APSqlFromPhrase(tableDef, APSqlJoinType.Full, wherePhrase); }
/// <summary> /// Create 'JOIN' phrase. /// </summary> /// <param name="tableDef">The table defined.</param> /// <param name="joinType">Join Type.</param> /// <param name="wherePhrase">Join 'ON' phrase.</param> /// <returns>A 'FROM' phrase.</returns> public static APSqlFromPhrase Join(this APTableDef tableDef, APSqlJoinType joinType, APSqlWherePhrase wherePhrase) { return new APSqlFromPhrase(tableDef, joinType, wherePhrase); }
/// <summary> /// Create a new 'WHERE' clause with one 'WHERE' phrase. /// </summary> /// <param name="phrase">'WHERE' phrase.</param> public APSqlWhereClause(APSqlWherePhrase phrase) { SetNext(phrase); CheckValid(); }
/// <summary> /// Create a new 'OR' condition group. /// </summary> /// <param name="phrase1">'WHERE' phrase1.</param> /// <param name="phrase2">'WHERE' phrase2.</param> public APSqlConditionOrPhrase(APSqlWherePhrase phrase1, APSqlWherePhrase phrase2) { _child = phrase1; _child.SetNext(phrase2); }
private void WriteConditionPhrase(ParserWriter writer, APSqlWherePhrase phrase, APSqlConditionJoinType join, SqlCommand dbCmd) { bool isFirst = true; writer.Write("("); while (phrase != null) { if (!isFirst) writer.Write(join.ToString()); if (phrase.IsNot) writer.Write("NOT ("); if (phrase is APSqlConditionAndPhrase) { WriteConditionPhrase(writer, (phrase as APSqlConditionAndPhrase).Child, APSqlConditionJoinType.AND, dbCmd); } else if (phrase is APSqlConditionOrPhrase) { WriteConditionPhrase(writer, (phrase as APSqlConditionOrPhrase).Child, APSqlConditionJoinType.OR, dbCmd); } else { APSqlConditionPhrase cond = phrase as APSqlConditionPhrase; // for this, if columnDef is null, mean this is a 'EXISTS ( subquery )' phrase // do not // change to check ConditionOperator. if (cond.ConditionOperator != APSqlConditionOperator.Exists && cond.ConditionOperator != APSqlConditionOperator.NotExists) WriteSelectExpression(writer, cond.Expr); if (cond.ConditionOperator == APSqlConditionOperator.Equals /*&& !cond.IsRelationDef*/ && (cond.Value == null || cond.Value == DBNull.Value)) { writer.Write("IS NULL"); } else if (cond.ConditionOperator == APSqlConditionOperator.NotEqual /*&& !cond.IsRelationDef*/ && (cond.Value == null || cond.Value == DBNull.Value)) { writer.Write("IS NOT NULL"); } else { switch (cond.ConditionOperator) { case APSqlConditionOperator.Equals: writer.Write("="); break; case APSqlConditionOperator.NotEqual: writer.Write("<>"); break; case APSqlConditionOperator.GreaterThan: writer.Write(">"); break; case APSqlConditionOperator.GreaterThanOrEqual: writer.Write(">="); break; case APSqlConditionOperator.LessThan: writer.Write("<"); break; case APSqlConditionOperator.LessThanOrEqual: writer.Write("<="); break; case APSqlConditionOperator.Between: writer.Write("BETWEEN"); break; case APSqlConditionOperator.NotBetween: writer.Write("NOT BETWEEN"); break; case APSqlConditionOperator.Like: writer.Write("LIKE"); break; case APSqlConditionOperator.NotLike: writer.Write("NOT LIKE"); break; case APSqlConditionOperator.In: writer.Write("IN"); break; case APSqlConditionOperator.NotIn: writer.Write("NOT IN"); break; case APSqlConditionOperator.Exists: writer.Write("EXISTS"); break; case APSqlConditionOperator.NotExists: writer.Write("NOT EXISTS"); break; } object value = cond.Value; if (!TryWriteValue(writer, value)) { if (value is APSqlSelectCommand) { switch (cond.SubQueryScalarRestrict) { case APSqlSubQueryScalarRestrict.All: writer.Write("ALL"); break; case APSqlSubQueryScalarRestrict.Some: writer.Write("SOME"); break; case APSqlSubQueryScalarRestrict.Any: writer.Write("ANY"); break; } writer.Write("("); int idented = writer.Idented++; writer.WriteLine(); ParseSelectInternal(value as APSqlSelectCommand, 0, dbCmd, writer); writer.Idented = idented; writer.Write(")"); } else { if (cond.ConditionOperator == APSqlConditionOperator.In || cond.ConditionOperator == APSqlConditionOperator.NotIn) { writer.Write("("); int i = 0; foreach (object val in value as Array) { if (i != 0) writer.Write(','); string paramName = writer.GetSuitableParameterName(cond.ParamName); paramName = String.Format("@{0}${1}", paramName, i); writer.Write(paramName); AddParameter(dbCmd, paramName, val, ParameterDirection.Input); i++; } writer.Write(")"); } else if (cond.ConditionOperator == APSqlConditionOperator.Between || cond.ConditionOperator == APSqlConditionOperator.NotBetween) { object begin = (value as Array).GetValue(0); object end = (value as Array).GetValue(1); if (!TryWriteValue(writer, begin)) { string paramName = writer.GetSuitableParameterName(cond.ParamName); writer.Write("@" + paramName); AddParameter(dbCmd, paramName, begin, ParameterDirection.Input); } writer.Write("AND"); if (!TryWriteValue(writer, end)) { string paramName = writer.GetSuitableParameterName(cond.ParamName); writer.Write("@" + paramName); AddParameter(dbCmd, paramName, end, ParameterDirection.Input); } } else { string paramName = writer.GetSuitableParameterName(cond.ParamName); writer.Write("@" + paramName); AddParameter(dbCmd, paramName, value, ParameterDirection.Input); } } } } } if (phrase.IsNot) writer.Write(")"); isFirst = false; phrase = phrase.Next as APSqlWherePhrase; } writer.Write(")"); }
/// <summary> /// Create a new 'AND' condition group. /// </summary> /// <param name="phrase1">'WHERE' phrase1.</param> /// <param name="phrase2">'WHERE' phrase2.</param> public APSqlConditionAndPhrase(APSqlWherePhrase phrase1, APSqlWherePhrase phrase2) { _child = phrase1; _child.SetNext(phrase2); }
private void WriteConditionPhrase(ParserWriter writer, APSqlWherePhrase phrase, APSqlConditionJoinType join, OracleCommand dbCmd) { bool isFirst = true; writer.Write("("); while (phrase != null) { if (!isFirst) { writer.Write(join.ToString()); } if (phrase.IsNot) { writer.Write("NOT ("); } if (phrase is APSqlConditionAndPhrase) { WriteConditionPhrase(writer, (phrase as APSqlConditionAndPhrase).Child, APSqlConditionJoinType.AND, dbCmd); } else if (phrase is APSqlConditionOrPhrase) { WriteConditionPhrase(writer, (phrase as APSqlConditionOrPhrase).Child, APSqlConditionJoinType.OR, dbCmd); } else { APSqlConditionPhrase cond = phrase as APSqlConditionPhrase; // for this, if columnDef is null, mean this is a 'EXISTS ( subquery )' phrase // do not // change to check ConditionOperator. if (cond.ConditionOperator != APSqlConditionOperator.Exists && cond.ConditionOperator != APSqlConditionOperator.NotExists) { WriteSelectExpression(writer, cond.Expr); } if (cond.ConditionOperator == APSqlConditionOperator.Equals /*&& !cond.IsRelationDef*/ && (cond.Value == null || cond.Value == DBNull.Value)) { writer.Write("IS NULL"); } else if (cond.ConditionOperator == APSqlConditionOperator.NotEqual /*&& !cond.IsRelationDef*/ && (cond.Value == null || cond.Value == DBNull.Value)) { writer.Write("IS NOT NULL"); } else { switch (cond.ConditionOperator) { case APSqlConditionOperator.Equals: writer.Write("="); break; case APSqlConditionOperator.NotEqual: writer.Write("<>"); break; case APSqlConditionOperator.GreaterThan: writer.Write(">"); break; case APSqlConditionOperator.GreaterThanOrEqual: writer.Write(">="); break; case APSqlConditionOperator.LessThan: writer.Write("<"); break; case APSqlConditionOperator.LessThanOrEqual: writer.Write("<="); break; case APSqlConditionOperator.Between: writer.Write("BETWEEN"); break; case APSqlConditionOperator.NotBetween: writer.Write("NOT BETWEEN"); break; case APSqlConditionOperator.Like: writer.Write("LIKE"); break; case APSqlConditionOperator.NotLike: writer.Write("NOT LIKE"); break; case APSqlConditionOperator.In: writer.Write("IN"); break; case APSqlConditionOperator.NotIn: writer.Write("NOT IN"); break; case APSqlConditionOperator.Exists: writer.Write("EXISTS"); break; case APSqlConditionOperator.NotExists: writer.Write("NOT EXISTS"); break; } object value = cond.Value; if (!TryWriteValue(writer, value)) { if (value is APSqlSelectCommand) { switch (cond.SubQueryScalarRestrict) { case APSqlSubQueryScalarRestrict.All: writer.Write("ALL"); break; case APSqlSubQueryScalarRestrict.Some: writer.Write("SOME"); break; case APSqlSubQueryScalarRestrict.Any: writer.Write("ANY"); break; } writer.Write("("); int idented = writer.Idented++; writer.WriteLine(); ParseSelectInternal(value as APSqlSelectCommand, dbCmd, writer); writer.Idented = idented; writer.Write(")"); } else { if (cond.ConditionOperator == APSqlConditionOperator.In || cond.ConditionOperator == APSqlConditionOperator.NotIn) { writer.Write("("); int i = 0; foreach (object val in value as Array) { if (i != 0) { writer.Write(','); } string paramName = writer.GetSuitableParameterName(cond.ParamName); paramName = String.Format("v_{0}${1}", paramName, i); writer.Write(":" + paramName); AddParameter(dbCmd, paramName, val, ParameterDirection.Input); i++; } writer.Write(")"); } else if (cond.ConditionOperator == APSqlConditionOperator.Between || cond.ConditionOperator == APSqlConditionOperator.NotBetween) { object begin = (value as Array).GetValue(0); object end = (value as Array).GetValue(1); if (!TryWriteValue(writer, begin)) { string paramName = writer.GetSuitableParameterName(cond.ParamName); writer.Write(":" + paramName); AddParameter(dbCmd, paramName, begin, ParameterDirection.Input); } writer.Write("AND"); if (!TryWriteValue(writer, end)) { string paramName = writer.GetSuitableParameterName(cond.ParamName); writer.Write(":" + paramName); AddParameter(dbCmd, paramName, end, ParameterDirection.Input); } } else { string paramName = writer.GetSuitableParameterName(cond.ParamName); writer.Write(":" + paramName); AddParameter(dbCmd, paramName, value, ParameterDirection.Input); } } } } } if (phrase.IsNot) { writer.Write(")"); } isFirst = false; phrase = phrase.Next as APSqlWherePhrase; } writer.Write(")"); }
/// <summary> /// Build a APQuery. /// </summary> /// <param name="additionCondition">Addition condition.</param> /// <param name="additionOrders">Addition orders</param> /// <param name="fuzzySearchString">Fuzzy search string.</param> /// <returns>The APQuery.</returns> public virtual APSqlSelectCommand BuildQuery(APSqlWherePhrase additionCondition = null, IEnumerable<APSqlOrderPhrase> additionOrders = null, string fuzzySearchString = null) { List<APSqlSelectPhrase> select = _source.GetPrimeSelectPhrases(); List<APSqlFromPhrase> from = _source.GetPrimeFormPhrases(); List<APSqlWherePhrase> where = _source.GetPrimeWherePhrases(); // Base build. foreach (APRptReferDef refer in _def.Refers) { APRptColumn column = _source.AllColumns[refer.ColumnId]; column.AddToQuerySelectPhrases(select); column.AddToQueryFromPhrases(from); column.AddToQueryWherePhrases(where); } var query = APQuery .select(select) .from(from) .primary(_source.GetPrimaryExpr()); // Order build. List<APSqlOrderPhrase> orderby = new List<APSqlOrderPhrase>(); if (_def.Orders.Count > 0) { foreach (APRptOrderDef order in _def.Orders) { orderby.Add(_source.AllColumns[order.ColumnId].GetQueryOrderByPhrase(order.According)); } } if (additionOrders != null) { foreach (APSqlOrderPhrase order in additionOrders) { orderby.Add(order); } } query.order_by(orderby); // Filter build. if (_def.Condition.Filters.Count > 0) where.Add(new APRptConditionBuilder(_def.Condition, _source.AllColumns).BuildCondition()); // Additions condition. if (additionCondition != null) where.Add(additionCondition); // Fuzzy search condition. APSqlWherePhrase fuzzyWhere; if (!String.IsNullOrEmpty(fuzzySearchString) && (fuzzyWhere = _source.GetFuzzySearchPhrase(fuzzySearchString)) != null) where.Add(fuzzyWhere); query.where(where); return query; }
private void WriteJoinOn(ParserWriter writer, APSqlWherePhrase phrase, SqlCommand dbCmd) { if (phrase != null) { writer.Write("ON"); if (phrase.Next == null) { if (phrase is APSqlConditionAndPhrase) { WriteConditionPhrase(writer, (phrase as APSqlConditionAndPhrase).Child, APSqlConditionJoinType.AND, dbCmd); return; } else if (phrase is APSqlConditionOrPhrase) { WriteConditionPhrase(writer, (phrase as APSqlConditionOrPhrase).Child, APSqlConditionJoinType.OR, dbCmd); return; } } WriteConditionPhrase(writer, phrase, APSqlConditionJoinType.AND, dbCmd); } }
/// <summary> /// Create a new 'AND' condition group. /// </summary> /// <param name="phrase1">'WHERE' phrase1.</param> /// <param name="phrases">'WHERE' phrases.</param> public APSqlConditionAndPhrase(APSqlWherePhrase phrase1, params APSqlWherePhrase[] phrases) { _child = phrase1; _child.SetNext(phrases); }
/// <summary> /// SQL 'WHERE' phrase extensions. Add new condition join 'OR' with exist condition. /// </summary> /// <param name="command">The command.</param> /// <param name="phrase">The new 'WHERE' phrase.</param> /// <returns>The command.</returns> public static APSqlUpdateCommand where_or(this APSqlUpdateCommand command, APSqlWherePhrase phrase) { if (command.WhereClause == null || command.WhereClause.Next == null) { command.WhereClause = new APSqlWhereClause(phrase); } else { APSqlWherePhrase exist = command.WhereClause.Next as APSqlWherePhrase; if (exist is APSqlConditionOrPhrase) (exist as APSqlConditionOrPhrase).Child.Last.SetNext(phrase); else command.WhereClause = new APSqlWhereClause(APSqlConditionJoinType.OR, exist, phrase); } return command; }
/// <summary> /// Create 'Full JOIN' phrase. /// </summary> /// <param name="tableDef">The table defined.</param> /// <param name="wherePhrase">Join 'ON' phrase.</param> /// <returns>A 'FROM' phrase.</returns> public static APSqlFromPhrase JoinFull(this APTableDef tableDef, APSqlWherePhrase wherePhrase) { return(new APSqlFromPhrase(tableDef, APSqlJoinType.Full, wherePhrase)); }