public void Parse(string statement) { Context.TraceEvent(100, 0, "DMV: Begin Parse"); // break the statement into tokens List <Tokenizer.Token> tList = new List <ASStoredProcs.DMVParser.Tokenizer.Token>(); Tokenizer.Tokenizer t = new ASStoredProcs.DMVParser.Tokenizer.Tokenizer(statement); tList = t.Tokens; int ipos = 1; List <string> cols = new List <string>(); // do not proceed if the first token is not the work "SELECT" if (tList[0].Text.ToUpper() == "SELECT") { // check for a DISTINCT query if (tList.Count >= 2 && tList[1].Text.ToUpper() == "DISTINCT") { mDistinct = true; ipos++; } // collect columns while (tList[ipos].Text.ToUpper() != "FROM") { if (tList[ipos].Type != ASStoredProcs.DMVParser.Tokenizer.TokenType.Comma && tList[ipos].Text != "[" && tList[ipos].Text != "]") { cols.Add(tList[ipos].Text); } ipos++; } mCols = cols.ToArray(); ipos++; // Get the token for the schema that we are querying mFrom = tList[ipos].Text; ipos += 1; // look for the WHERE clause (if it exists) if (ipos < tList.Count && tList[ipos].Text.ToUpper() == "WHERE") { ipos++; } List <Tokenizer.Token> whereTokens = new List <ASStoredProcs.DMVParser.Tokenizer.Token>(); // where clause while (ipos < tList.Count && !(tList[ipos].Text.ToUpper() == "ORDER" && tList[ipos < tList.Count - 2?ipos + 1:ipos].Text.ToUpper() == "BY")) { if (tList[ipos].Type == ASStoredProcs.DMVParser.Tokenizer.TokenType.String) { mWhere += "'" + tList[ipos].Text + "' "; } else if (tList[ipos].Text != "[" && tList[ipos].Text != "]") { mWhere += tList[ipos].Text + " "; } whereTokens.Add(tList[ipos]); ipos++; } // build a list of where predicates to see if we can match them to the // restrictions for the rowset. int iwherePos = 0; while (iwherePos <= (whereTokens.Count - 3)) { WherePredicate w = new WherePredicate(); w.Operand1 = whereTokens[iwherePos]; w.Operator = whereTokens[iwherePos + 1]; w.Operand2 = whereTokens[iwherePos + 2]; // restriction matching can only be done with the "=" operator if (w.Operand1.Type == ASStoredProcs.DMVParser.Tokenizer.TokenType.Bracket || w.Operand2.Type == ASStoredProcs.DMVParser.Tokenizer.TokenType.Bracket || w.Operator.Text != "=") { canMatchRestrictions = false; whereList.Clear(); break; } whereList.Add(w); iwherePos += 3; if (iwherePos < whereTokens.Count) { // multiple predicates must be joined with a logical "AND" // currently nested operations with brackets are not supported if (whereTokens[iwherePos].Text.ToUpper() == "AND") { iwherePos++; } else { canMatchRestrictions = false; whereList.Clear(); break; } } } // if we have left over tokens, something has gone wrong if (iwherePos != whereTokens.Count) { whereList.Clear(); canMatchRestrictions = false; } if (canMatchRestrictions) { string xmlaRestr = DiscoverRestrictions(Context.CurrentServerID, this.FromClause); List <string> restr = extractRestrictions(xmlaRestr); mRestrictions = MatchRestrictions(restr); } ipos += 2; // Order By clause while (ipos < tList.Count) { mOrder += tList[ipos].Text + " "; ipos++; } Context.TraceEvent(100, 0, "DMV: End Parse"); } }
public void Parse(string statement) { Context.TraceEvent(100, 0, "DMV: Begin Parse"); // break the statement into tokens List<Tokenizer.Token> tList = new List<ASStoredProcs.DMVParser.Tokenizer.Token>(); Tokenizer.Tokenizer t = new ASStoredProcs.DMVParser.Tokenizer.Tokenizer(statement); tList = t.Tokens; int ipos = 1; List<string> cols = new List<string>(); // do not proceed if the first token is not the work "SELECT" if (tList[0].Text.ToUpper() == "SELECT") { // check for a DISTINCT query if (tList.Count >= 2 && tList[1].Text.ToUpper() == "DISTINCT") { mDistinct = true; ipos++; } // collect columns while (tList[ipos].Text.ToUpper() != "FROM") { if (tList[ipos].Type != ASStoredProcs.DMVParser.Tokenizer.TokenType.Comma && tList[ipos].Text != "[" && tList[ipos].Text != "]") { cols.Add(tList[ipos].Text); } ipos++; } mCols = cols.ToArray(); ipos++; // Get the token for the schema that we are querying mFrom = tList[ipos].Text; ipos += 1; // look for the WHERE clause (if it exists) if (ipos < tList.Count && tList[ipos].Text.ToUpper() == "WHERE") { ipos++; } List<Tokenizer.Token> whereTokens = new List<ASStoredProcs.DMVParser.Tokenizer.Token>(); // where clause while (ipos < tList.Count && !(tList[ipos].Text.ToUpper() == "ORDER" && tList[ipos<tList.Count-2?ipos+1:ipos].Text.ToUpper() == "BY" )) { if (tList[ipos].Type == ASStoredProcs.DMVParser.Tokenizer.TokenType.String) { mWhere += "'" + tList[ipos].Text + "' "; } else if (tList[ipos].Text != "[" && tList[ipos].Text != "]") { mWhere += tList[ipos].Text + " "; } whereTokens.Add(tList[ipos]); ipos++; } // build a list of where predicates to see if we can match them to the // restrictions for the rowset. int iwherePos = 0; while (iwherePos <= (whereTokens.Count - 3)) { WherePredicate w = new WherePredicate(); w.Operand1 = whereTokens[iwherePos]; w.Operator = whereTokens[iwherePos+1]; w.Operand2 = whereTokens[iwherePos+2]; // restriction matching can only be done with the "=" operator if ( w.Operand1.Type == ASStoredProcs.DMVParser.Tokenizer.TokenType.Bracket || w.Operand2.Type == ASStoredProcs.DMVParser.Tokenizer.TokenType.Bracket || w.Operator.Text != "=") { canMatchRestrictions = false; whereList.Clear(); break; } whereList.Add(w); iwherePos += 3; if (iwherePos < whereTokens.Count) { // multiple predicates must be joined with a logical "AND" // currently nested operations with brackets are not supported if (whereTokens[iwherePos].Text.ToUpper() == "AND") { iwherePos++; } else { canMatchRestrictions = false; whereList.Clear(); break; } } } // if we have left over tokens, something has gone wrong if (iwherePos != whereTokens.Count) { whereList.Clear(); canMatchRestrictions = false; } if (canMatchRestrictions) { string xmlaRestr = DiscoverRestrictions(Context.CurrentServerID, this.FromClause); List<string> restr = extractRestrictions(xmlaRestr); mRestrictions = MatchRestrictions(restr); } ipos += 2; // Order By clause while (ipos < tList.Count) { mOrder += tList[ipos].Text + " "; ipos++; } Context.TraceEvent(100, 0, "DMV: End Parse"); } }