using System;
using System.Collections;
namespace rasp.SQL.SQLStatement {
/// <summary>
/// A collection of where clauses that can be programmatically added to and removed from.
/// </summary>
/// //TODO: Make it so that where clauses can be removed individually.
public class WhereClauses {
private ArrayList clauses = new ArrayList();
private Hashtable hash = new Hashtable();
private string Clause = "";
private bool hasChanged = false;
/// <summary>
/// Gets or sets the flag that determines if an exception is thrown when an obvious problem has been detected.
/// </summary>
public bool ThrowOnError = false;
private int _Position = 0;
/// <summary>
/// Constructs an empty collection.
/// </summary>
public WhereClauses() {
}
/// <summary>
/// Constructs and parses the given where clauses.
/// </summary>
/// <param name="clause">The where clause portion of a Select Statement.</param>
public WhereClauses(string clause) {
Clause = clause;
}
/// <summary>
/// Adds a WhereClause object to the collection.
/// </summary>
/// <param name="Name">The name of the field to appear on the left of the operator.</param>
/// <param name="RightOperand">The condition to appear on the right of the operator</param>
public void AddClause(string Name, string RightOperand) {
WhereClause temp = new WhereClause(0, 0, Name, RightOperand, "and", Operators.Equal);
AddClause(temp);
}
/// <summary>
/// Adds a WhereClause object to the collection.
/// </summary>
/// <param name="Name">The name of the field to appear on the left of the operator.</param>
/// <param name="RightOperand">The condition to appear on the right of the operator</param>
/// <param name="Operator">The operator to place between the two operands. (IE: "=" - "like" = "in")</param>
public void AddClause(string Name, string RightOperand, string Operator) {
WhereClause temp = new WhereClause(0, 0, Name, RightOperand, "and", Operator);
AddClause(temp);
}
/// <summary>
/// Adds a WhereClause to the collection.
/// </summary>
/// <param name="BeginDepth">The number of parenthesis that need to precede the clause.</param>
/// <param name="EndDepth">The number of parenthesis that need to be at the end of the clause.</param>
/// <param name="Name">The name of the field to appear on the left of the operator.</param>
/// <param name="RightOperand">The condition that appears on the right of the operator.</param>
/// <param name="Condition">The conditional statement. (IE: "and" - "or")</param>
/// <param name="Operator">The operator to place between the two operands. (IE: "=" - "like" = "in")</param>
public void AddClause(int BeginDepth, int EndDepth, string Name, string RightOperand, string Condition, string Operator) {
WhereClause temp = new WhereClause(BeginDepth, EndDepth, Name, RightOperand, Condition, Operator);
AddClause(temp);
}
/// <summary>
/// Adds a WhereClause to the colleciton.
/// </summary>
/// <param name="clause">A WhereClause to add to the collection.</param>
public void AddClause(WhereClause clause) {
if (clauses.Contains(clause)) {
clauses.Remove(clause);
}
clauses.Add(clause);
hash.Add(_Position++, clause);
hasChanged = true;
}
/// <summary>
/// Removes all of the WhereClauses from the collection.
/// </summary>
public void RemoveAll() {
clauses = new ArrayList();
}
/// <summary>
/// Gets the where clauses in such a way that it can be placed directly in a Select Statement.
/// </summary>
public string WhereClause {
get {
string clause = "";
if (clauses.Count > 0) {
clause = " where ";
bool NeedsCondition = false;
foreach(rasp.SQL.SQLStatement.WhereClause c in clauses) {
if (NeedsCondition) {
clause += c.Condition;
}
clause += TextUtilities.WriteTimes("(",c.BeginDepth);
clause += " " + c.Name + " " + c.Operator + " " + c.RightOperand + " ";
clause += TextUtilities.WriteTimes(")",c.EndDepth);
if (!NeedsCondition) { NeedsCondition = true; }
}
clause.Replace(" ", " ").TrimEnd();
}
if (!clause.ToLower().Equals(Clause) && !hasChanged && ThrowOnError) {
throw new Exception("Could not reproduce the where clause that you sent me, you sent: " + Clause + " I created: " + clause);
}
return clause.Replace(" ", " ");
}
}
private string GetValueString(object Value) {
string val = "";
if(Value.GetType() == typeof(string)) {
val = "'" + (string)Value + "'";
} else {
val = TextUtilities.GetTextValue(Value);
}
return val;
}
}
/// <summary>
/// A WhereClause represents a single expression in SQL.
/// </summary>
/// <remarks>
/// This object additionally keeps track of how many parenthesis come before and after the clause.
/// </remarks>
public class WhereClause {
/// <summary>
/// The number of parenthesis to precede the where clause.
/// </summary>
public int BeginDepth = 0;
/// <summary>
/// The number of parenthesis to follow the where clause.
/// </summary>
public int EndDepth = 0;
/// <summary>
/// The field name to appear on the left of the operator.
/// </summary>
public string Name = "";
/// <summary>
/// The condition that appears on the right of the operator.
/// </summary>
public string RightOperand = "";
/// <summary>
/// The conditional statement. (IE: "and" - "or")
/// </summary>
public string Condition = ConditionalStatements.AND;
/// <summary>
/// The operator to place between the two operands. (IE: "=" - "like" = "in")
/// </summary>
public String Operator = Operators.Equal;
/// <summary>
/// Constructs an empty where clause.
/// </summary>
public WhereClause() { }
/// <summary>
/// Constructs a single WhereClause object.
/// </summary>
/// <param name="_BeginDepth">The number of parenthesis that need to precede the clause.</param>
/// <param name="_EndDepth">The number of parenthesis that need to be at the end of the clause.</param>
/// <param name="_Name">The name of the field to appear on the left of the operator.</param>
/// <param name="_RightOperand">The condition that appears on the right of the operator.</param>
/// <param name="_Condition">The conditional statement. (IE: "and" - "or")</param>
/// <param name="_Operator">The operator to place between the two operands. (IE: "=" - "like" = "in")</param>
public WhereClause(int _BeginDepth, int _EndDepth, string _Name, string _RightOperand,
string _Condition, string _Operator) {
BeginDepth = _BeginDepth;
EndDepth = _EndDepth;
Name = _Name;
RightOperand = _RightOperand;
Condition = _Condition;
Operator = _Operator;
}
}
/// <summary>
/// A basic enumeration of values for SQL Conditional statements.
/// </summary>
/// <remarks>
/// NONE = "", AND = "and", OR = "or"
/// </remarks>
public class ConditionalStatements {
/// <summary>
/// NONE = "", AND = "and", OR = "or"
/// </summary>
public static string NONE = "", AND = "and", OR = "or";
}
/// <summary>
/// A basic enumeration of values for SQL Operators.
/// </summary>
/// <remarks>
/// Equal = "= ", Like = " like ", In = " in "
/// </remarks>
public class Operators {
/// <summary>
/// Equal = "= ", Like = " like ", In = " in "
/// </summary>
public static string Equal = "= ", Like = " like ", In = " in ";
}
}