public bool ContainsField(HqlField field) { for (int i = 0; i < _q.Count; ++i) { Object o = _q[i]; if (o is HqlToken) { HqlToken token = (HqlToken)o; if (token.Field != null && token.Field.ContainsField(field)) { return(true); } } else if (o is HqlCompareToken) { HqlCompareToken ct = (HqlCompareToken)o; if (ct.ContainsField(field)) { return(true); } } else { throw new Exception("Unknown object in list"); } } return(false); }
public int CompareTo(HqlKey bb, int n) { if (_o[n] == null && bb._o[n] == null) { return(0); } else if (_o[n] == null) { return(-1); } else if (bb._o[n] == null) { return(1); } HqlCompareToken.MakeAppropriateObjects(ref _o[n], ref bb._o[n]); if (_o[n] is string && bb._o[n] is string) { return(String.Compare((string)_o[n], (string)bb._o[n], StringComparison.CurrentCulture)); } if (_o[n] is Int64 && bb._o[n] is Int64) { return(((Int64)_o[n]).CompareTo((Int64)bb._o[n])); } if (_o[n] is int && bb._o[n] is int) { return(((int)_o[n]).CompareTo((int)bb._o[n])); } if (_o[n] is decimal && bb._o[n] is decimal) { return(((decimal)_o[n]).CompareTo((decimal)bb._o[n])); } return(String.Compare(_o[n].ToString(), bb._o[n].ToString(), StringComparison.CurrentCulture)); }
public bool ContainsRownum(bool OnlyWithoutTableReference) { for (int i = 0; i < _q.Count; ++i) { Object o = _q[i]; if (o is HqlToken) { HqlToken token = (HqlToken)o; if (token.Field != null && token.Field.ContainsRownum(OnlyWithoutTableReference)) { return(true); } } else if (o is HqlCompareToken) { HqlCompareToken ct = (HqlCompareToken)o; if (ct.ContainsRownum(OnlyWithoutTableReference)) { return(true); } } else { throw new Exception("Unknown object in list"); } } return(false); }
public void LoadFieldReferences(ArrayList list) { if (list == null) { return; } for (int i = 0; i < list.Count; ++i) { object o = list[i]; if (o is HqlCompareToken) { HqlCompareToken comp = (HqlCompareToken)o; LoadToken(comp.Token1); LoadToken(comp.Token2); LoadFieldReferences(comp.InValues); } else if (o is HqlToken) { LoadToken((HqlToken)o); } else if (o is HqlField) { LoadField((HqlField)o); } else { throw new Exception(String.Format("Error, what sort of other data can be found in HqlDataSource:LoadFieldReferences|{0}|", o.GetType())); } } }
private void SanityCheckHaving() { if (_having.Count == 0) { return; } if (_groupby.Count == 0) { throw new Exception("Expected GROUP-BY if HAVING is present"); } for (int i = 0; i < _having.Count; ++i) { object o = _having.EvaluationCriteria[i]; if (o is HqlToken) { continue; } else if (o is HqlCompareToken) { HqlCompareToken ct = (HqlCompareToken)o; if (!ct.ExistsIn(_select.FieldGroup)) { throw new Exception("Unable to find value from HAVING in the SELECT"); } } else { throw new Exception("Unable to find value from HAVING in the SELECT"); } } }
/// <summary> /// This will go through the list of joining records (x is left-side file) (y is right-side file) /// and run both pieces of data through the join. /// </summary> /// <param name="x"></param> /// <param name="y"></param> /// <returns></returns> private int Sort(HqlValues x, HqlValues y) { int?ret; for (int i = 0; i < _orderOfSortFields.Count; ++i) { object item = _orderOfSortFields[i]; if (!(item is HqlCompareToken)) { throw new Exception("Unknown object in SortFields"); } HqlCompareToken c = (HqlCompareToken)item; ret = CompareHqlValues(x, y, c.Token1, c.Compare); if (ret.HasValue && ret != 0) { return(ret.Value); } ret = CompareHqlValues(x, y, c.Token2, c.Compare); if (ret.HasValue && ret != 0) { return(ret.Value); } } return(0); }
private int Search(HqlValues a, HqlValues b) { object oa = null; object ob = null; int result = 0; for (int i = 0; i < _orderOfSortFields.Count; ++i) { object item = _orderOfSortFields[i]; if (!(item is HqlCompareToken)) { throw new Exception("Unknown object in SortFields"); } HqlCompareToken ctoken = (HqlCompareToken)item; if (!GetValues(b, a, ctoken, ref ob, ref oa) && !GetValues(a, b, ctoken, ref oa, ref ob)) { continue; } int ret = HqlCompareToken.CompareTo(oa, ob); if (ret != 0) { result = ret; break; } } return(result); }
/// <summary> /// Must be a SINGLE CompareToken /// </summary> /// <param name="x"></param> /// <param name="y"></param> /// <param name="item"></param> /// <param name="ox"></param> /// <param name="oy"></param> /// <returns></returns> static private bool GetValues(HqlValues x, HqlValues y, HqlCompareToken item, ref object ox, ref object oy) { object a; object b; if (!x.HasValue(item.Token1, out a)) { return(false); } if (!y.HasValue(item.Token2, out b)) { return(false); } ox = a; oy = b; return(true); }
static private int?CompareHqlValues(HqlValues x, HqlValues y, HqlToken item, HqlToken compareType) { object ox; object oy; if (!x.HasValue(item, out ox)) { return(null); } if (!y.HasValue(item, out oy)) { throw new Exception("Error! How come it doesn't have it in Y?"); } int ret = HqlCompareToken.CompareTo(ox, oy); //switch (compareType) //{ // // TODO //} return(ret); }
public void Add(HqlCompareToken c) { if (c.CompareType != HqlCompareTokenType.SINGLE) { return; } switch (c.Compare.Data) { case "=": case "like": break; default: return; } if (c.ContainsRownum(false)) { return; } _orderOfSortFields.Add(c); }
/////////////////////// // Private protected bool ParseCompare(HqlTokenProcessor processor, bool andorAllowed, ref int openParen, bool closedAllowed, HqlKeyword thisKeyword, HqlClause select) { #if DEBUG if (thisKeyword != HqlKeyword.WHERE && thisKeyword != HqlKeyword.HAVING && thisKeyword != HqlKeyword.ON) { throw new Exception("Cannot access ParseCompare with invalid keyword"); } #endif HqlToken token = processor.GetToken(); if (processor.MatchesEndOfProcessing()) { return(false); } if (token.WordType == HqlWordType.KEYWORD && token.Keyword == HqlKeyword.CLOSEDPAREN) { if (!closedAllowed) { throw new Exception("Empty Parens found"); } openParen--; return(false); } if (token.WordType == HqlWordType.KEYWORD && ( ( thisKeyword == HqlKeyword.ON && ( token.Keyword == HqlKeyword.WHERE || token.Keyword == HqlKeyword.INNER || token.Keyword == HqlKeyword.RIGHT_OUTER || token.Keyword == HqlKeyword.LEFT_OUTER || token.Keyword == HqlKeyword.FULL_OUTER || token.Keyword == HqlKeyword.WHERE || token.Keyword == HqlKeyword.GROUPBY || token.Keyword == HqlKeyword.HAVING || token.Keyword == HqlKeyword.ORDERBY || token.Keyword == HqlKeyword.WITH ) ) || ( thisKeyword == HqlKeyword.WHERE && ( token.Keyword == HqlKeyword.INNER || token.Keyword == HqlKeyword.RIGHT_OUTER || token.Keyword == HqlKeyword.LEFT_OUTER || token.Keyword == HqlKeyword.FULL_OUTER || token.Keyword == HqlKeyword.WHERE || token.Keyword == HqlKeyword.GROUPBY || token.Keyword == HqlKeyword.HAVING || token.Keyword == HqlKeyword.ORDERBY || token.Keyword == HqlKeyword.WITH ) ) || ( thisKeyword == HqlKeyword.HAVING && ( token.Keyword == HqlKeyword.ORDERBY || token.Keyword == HqlKeyword.WITH ) ) ) ) { return(false); } if (token.WordType == HqlWordType.KEYWORD && (token.Keyword == HqlKeyword.AND || token.Keyword == HqlKeyword.OR)) { if (!andorAllowed) { throw new Exception("Unexpected AND/OR"); } _q.Add(token); processor.MoveNextToken(); return(true); } if (token.WordType == HqlWordType.KEYWORD && token.Keyword == HqlKeyword.OPENPAREN) { openParen++; _q.Add(token); processor.MoveNextToken(); for (int i = 0; ; i++) { if (!ParseCompare(processor, (i > 0), ref openParen, (i > 0), thisKeyword, select)) { break; } } token = processor.GetToken(); if (token.WordType != HqlWordType.KEYWORD || token.Keyword != HqlKeyword.CLOSEDPAREN) { throw new Exception(String.Format("Unbalanced Parens in {0}", _name)); } _q.Add(token); processor.MoveNextToken(); } else { HqlToken token1 = token; if (token1.WordType == HqlWordType.UNKNOWN && !processor.CheckForTokenReference(ref token1)) { throw new Exception(String.Format("Unknown {0} reference to {1}", _name, token1.Data)); } // HACK - sort of a hack this says if I can't find this field // then add it to the select but make it not print out HqlEvaluator.VerifyFieldPresence(select, token1); // END HACK processor.MoveNextToken(); HqlToken compare = processor.GetToken(); // this could be // - a COMPARE such as |field > 0| // - an IN such as |field in ('A', 'B')| // - an IN such as |field in (select item from bob)| if (compare.WordType == HqlWordType.KEYWORD && compare.Keyword == HqlKeyword.COMPARE) { processor.MoveNextToken(); HqlToken token2 = processor.GetToken(); if (token2.WordType == HqlWordType.UNKNOWN && !processor.CheckForTokenReference(ref token2)) { throw new Exception(String.Format("Unknown WHERE reference to {0}", token2.Data)); } // HACK - sort of a hack this says if I can't find this field // then add it to the select but make it not print out HqlEvaluator.VerifyFieldPresence(select, token2); // END HACK HqlCompareToken comparetoken = new HqlCompareToken(token1, compare, token2); _q.Add(comparetoken); processor.MoveNextToken(); } else if (compare.WordType == HqlWordType.KEYWORD && (compare.Keyword == HqlKeyword.IN || compare.Keyword == HqlKeyword.NOT_IN)) { // now need to check for values or select processor.MoveNextToken(); HqlToken paren = processor.GetToken(); if (paren.WordType != HqlWordType.KEYWORD || paren.Keyword != HqlKeyword.OPENPAREN) { throw new Exception(String.Format("Expected an open paren after IN in {0}", _name)); } ArrayList compareValues = new ArrayList(); bool ExpectedValues = true; for (int count = 0; ; count++) { processor.MoveNextToken(); HqlToken val = processor.GetToken(); if (val.WordType == HqlWordType.KEYWORD && val.Keyword == HqlKeyword.CLOSEDPAREN) { break; } if (val.WordType == HqlWordType.KEYWORD && val.Keyword == HqlKeyword.COMMA) { ExpectedValues = true; continue; } if (val.WordType == HqlWordType.KEYWORD && val.Keyword == HqlKeyword.SELECT) { ExpectedValues = false; if (count != 0) { throw new Exception("You can only put a nested query in an IN statement if it is the only value present"); } HqlTokenProcessor.GetResultOfNestedQueryForInStatement(processor, compareValues); } else if ( val.WordType == HqlWordType.TEXT || val.WordType == HqlWordType.INT || val.WordType == HqlWordType.FLOAT || val.WordType == HqlWordType.LITERAL_STRING || val.WordType == HqlWordType.FIELD || val.WordType == HqlWordType.SCALAR ) { ExpectedValues = false; compareValues.Add(val); // HACK - sort of a hack this says if I can't find this field // then add it to the select but make it not print out HqlEvaluator.VerifyFieldPresence(select, val); // END HACK } else { throw new Exception("Found invalid value in IN clause"); } } if (ExpectedValues) { throw new Exception("Expected additional values in IN clause"); } if (compareValues.Count == 0) { throw new Exception("No values present in IN clause"); } HqlCompareToken comparetoken = new HqlCompareToken(token1, compare, compareValues); _q.Add(comparetoken); processor.MoveNextToken(); } else { throw new Exception(String.Format("Unknown {0} compare", _name)); } } return(true); }
static internal bool?EvaluateJoin(HqlValues values, ArrayList q, ref int counter) { if (counter > q.Count) { throw new Exception("Unexpected end of WHERE evaluation"); } bool?result = null; for (; ;) { if (counter >= q.Count) { break; } Object o = q[counter]; counter++; if (o is HqlToken) { // open paren := token.Type == HqlWordType.KEYWORD && token.Keyword == HqlKeyword.OPENPAREN // closed paren := token.Type == HqlWordType.KEYWORD && token.Keyword == HqlKeyword.CLOSEDPAREN // and_or := token.Type == HqlWordType.KEYWORD && token.Keyword == HqlKeyword.AND_OR // HqlToken token = (HqlToken)o; if (token.WordType == HqlWordType.KEYWORD && token.Keyword == HqlKeyword.OPENPAREN) { result = EvaluateJoin(values, q, ref counter); continue; } else if (token.WordType == HqlWordType.KEYWORD && token.Keyword == HqlKeyword.CLOSEDPAREN) { return(result); } else if (token.WordType == HqlWordType.KEYWORD && token.Keyword == HqlKeyword.OR) { // null null = true // null false = true // null true = true // false true = true // false false = false // true true = true bool?b1 = EvaluateJoin(values, q, ref counter); if (b1.HasValue && result.HasValue && b1 == false && result == false) { result = false; } else { result = true; } //result = MustContainValue(b1, "right OR") || MustContainValue(currentval, "left OR"); continue; } else if (token.WordType == HqlWordType.KEYWORD && token.Keyword == HqlKeyword.AND) { // null null = null // null false = false // null true = true // false true = false // false false = false // true true = true bool?b1 = EvaluateJoin(values, q, ref counter); if (!b1.HasValue) { //result = result; // Compiler yells at me so I'm going to comment out } else if (!result.HasValue) { result = b1; } else { result = b1.Value && result.Value; } //result = MustContainValue(b1, "right AND") && MustContainValue(currentval, "left AND"); continue; } else { throw new Exception("Unknown type of HqlToken in WHERE clause"); } } else if (o is HqlCompareToken) { HqlCompareToken c = (HqlCompareToken)o; result = c.EvaluateJoin(values); continue; } else { throw new Exception("Unknown type of object in WHERE evaluation."); } } return(result); }
static internal bool?Evaluate(HqlRecord record, ArrayList q, ref int counter) { if (counter > q.Count) { throw new Exception("Unexpected end of WHERE evaluation"); } bool?result = null; for (; ;) { if (counter >= q.Count) { break; } Object o = q[counter]; counter++; if (o is HqlToken) { // open paren := token.Type == HqlWordType.KEYWORD && token.Keyword == HqlKeyword.OPENPAREN // closed paren := token.Type == HqlWordType.KEYWORD && token.Keyword == HqlKeyword.CLOSEDPAREN // and_or := token.Type == HqlWordType.KEYWORD && token.Keyword == HqlKeyword.AND_OR // HqlToken token = (HqlToken)o; if (token.WordType == HqlWordType.KEYWORD && token.Keyword == HqlKeyword.OPENPAREN) { #if DEBUG if (result.HasValue) { throw new Exception("How did it get a value?"); } #endif result = Evaluate(record, q, ref counter); continue; } else if (token.WordType == HqlWordType.KEYWORD && token.Keyword == HqlKeyword.CLOSEDPAREN) { return(result); } else if (token.WordType == HqlWordType.KEYWORD && token.Keyword == HqlKeyword.OR) { bool b2 = MustContainValue(result, "left OR"); bool b1 = MustContainValue(Evaluate(record, q, ref counter), "right OR"); result = b1 || b2; continue; } else if (token.WordType == HqlWordType.KEYWORD && token.Keyword == HqlKeyword.AND) { bool b2 = MustContainValue(result, "left AND"); bool b1 = MustContainValue(Evaluate(record, q, ref counter), "right AND"); result = b1 && b2; continue; } else { throw new Exception("Unknown type of HqlToken in WHERE clause"); } } else if (o is HqlCompareToken) { HqlCompareToken c = (HqlCompareToken)o; #if DEBUG if (result.HasValue) { throw new Exception("How did it get a value?"); } #endif result = c.Evaluate(record); continue; } else { throw new Exception("Unknown type of object in WHERE evaluation."); } } return(MustContainValue(result, "whole thing")); }