private bool IsQueryStart(Token token) { return QueryStart.Contains(token.ToString()); }
private bool IsReservedWord(Token token) { return ReservedWords.Contains(token.ToString()); }
private bool IsMatch(Token[] tokens, int i, string match) { return IsMatch(tokens, i, match.Split(' ')); }
private int IsMatchWithLength(Token[] tokens, int i, string[] match) { if (match.Length > tokens.Length - i) return 0; // En typ unrollad variant för jämförelse av olika längd. switch (match.Length) { case 1: return (tokens[i] == match[0]) ? 1 : 0; case 2: return (tokens[i] == match[0] && tokens[i + 1] == match[1]) ? 2 : 0; case 3: return (tokens[i] == match[0] && tokens[i + 1] == match[1] && tokens[i + 2] == match[2]) ? 3 : 0; case 4: return (tokens[i] == match[0] && tokens[i + 1] == match[1] && tokens[i + 2] == match[2] && tokens[i + 3] == match[3]) ? 4 : 0; } throw new NotImplementedException("Har inte implementerat så lång jämförelse."); }
//private SqlSection AddJoin(Token[] tokens, ref int i, List<SqlSection> result, SqlQuery parent) //{ // throw new NotImplementedException(); //} private SqlWhere AddWhere(Token[] tokens, ref int i, SqlQuery parent) { var section = new SqlWhere(parent); section.Sections.Add(new SqlSection(section, new SqlToken(true, tokens[i]))); parent.Sections.Add(section); for (i++; i < tokens.Length; i++) { if (IsMatch(tokens, i, "ORDER", "BY")) { AddOrderBy(tokens, ref i, parent); break; } else if (IsMatch(tokens, i, "GROUP", "BY")) { AddGroupBy(tokens, ref i, parent); break; } else if (tokens[i].Type == TokenType.StartParenthesis) { if (IsMatch(tokens, i + 1, "SELECT")) { var query = new SqlQuery(section); AddTokenSection(query, tokens[i]); i++; AddSelect(tokens, ref i, query); section.Sections.Add(query); } else { AddParenthesisGroup(tokens, ref i, section); continue; } } else if (tokens[i].Type == TokenType.EndOfQuery || tokens[i].Type == TokenType.EndParenthesis) { AddTokenSection(parent, tokens[i]); break; } else if (IsQueryStart(tokens[i])) { i--; break; } AddTokenSection(section, tokens[i]); } return section; }
private bool IsMatch(Token[] tokens, int i, params string[] match) { return IsMatchWithLength(tokens, i, match) > 0; }
private void AddTokenSection(SqlSection parent, Token t) { parent.AddTokenSection(IsReservedWord(t), t); }
private void AddUpdate(Token[] tokens, ref int i, List<SqlSection> result, SqlQuery parent) { throw new NotImplementedException(); }
private SqlFrom AddFrom(Token[] tokens, ref int i, SqlQuery parent) { var section = new SqlFrom(parent); section.Sections.Add(new SqlSection(section, new SqlToken(true, tokens[i]))); parent.Sections.Add(section); for (i++; i < tokens.Length; i++) { if (IsMatch(tokens, i, "WHERE")) { AddWhere(tokens, ref i, parent); break; } else if (IsMatch(tokens, i, "ORDER", "BY")) { AddOrderBy(tokens, ref i, parent); break; } else if (IsMatch(tokens, i, "GROUP", "BY")) { AddGroupBy(tokens, ref i, parent); break; } //else if (IsMatch(tokens, i, "JOIN") || // (IsMatch(tokens, i, "LEFT") && tokens[i + 1].Type != TokenType.StartParenthesis) || // (IsMatch(tokens, i, "RIGHT") && tokens[i + 1].Type != TokenType.StartParenthesis) || // IsMatch(tokens, i, "FULL") || // IsMatch(tokens, i, "INNER") || // IsMatch(tokens, i, "OUTER")) //{ // AddJoin(tokens, ref i, result, parent); // break; //} else if (tokens[i].Type == TokenType.EndOfQuery || tokens[i].Type == TokenType.EndParenthesis) { AddTokenSection(parent, tokens[i]); break; } else if (IsQueryStart(tokens[i])) { i--; break; } AddTokenSection(section, tokens[i]); } return section; }
private SqlOrderBy AddOrderBy(Token[] tokens, ref int i, SqlQuery parent) { var section = new SqlOrderBy(parent); section.Sections.Add(new SqlSection(section, new SqlToken(true, tokens[i]))); parent.Sections.Add(section); for (i++; i < tokens.Length; i++) { if (IsMatch(tokens, i, "GROUP", "BY")) { AddOrderBy(tokens, ref i, parent); break; } else if (tokens[i].Type == TokenType.EndOfQuery || tokens[i].Type == TokenType.EndParenthesis) { AddTokenSection(parent, tokens[i]); break; } else if (IsQueryStart(tokens[i])) { i--; break; } AddTokenSection(section, tokens[i]); } return section; }
private bool IsJoin(Token[] tokens, int i) { var t1 = tokens[i]; if (t1 == "JOIN") return true; if (tokens.Length > i + 1) { if ((t1 == "OUTER" || t1 == "INNER") && tokens[i + 1] == "JOIN") return true; } if (tokens.Length <= i + 2) return false; var t2 = tokens[i + 1]; var t3 = tokens[i + 2]; return (t1 == "LEFT" && (t2 == "OUTER" || t2 == "INNER") && t3 == "JOIN") || (t1 == "RIGHT" && (t2 == "OUTER" || t2 == "INNER") && t3 == "JOIN"); }
private bool IsBlockStartWord(Token t) { if (t.Type != TokenType.Word) return false; switch (t.ToString().ToUpper()) { case "SELECT": case "FROM": case "WHERE": case "HAVING": return true; } return false; }
private void FormatWhere(Token[] tokens, ref int i, StringBuilder result) { throw new NotImplementedException(); }
private void FormatSelect(Token[] tokens, ref int i, StringBuilder result) { result.Append("SELECT"); for (; i < tokens.Length; i++) { var t = tokens[i]; if (t.Type == TokenType.EndOfQuery || (t.Type == TokenType.Word && t == "FROM")) { i--; break; } if (t.Type == TokenType.Comma) { AppendWithNewLines(result, NewLineAtSelectArgumentSeparator, t.Content.ToString()); } else { result.Append(" "); result.Append(t.Content); } } }
private void FormatFrom(Token[] tokens, ref int i, StringBuilder result) { AppendWithNewLines(result, NewLineAtFrom, "FROM"); for (; i < tokens.Length; i++) { var t = tokens[i]; if (t.Type == TokenType.EndOfQuery || (t.Type == TokenType.Word && (t == "WHERE" || t == "HAVING") || (t == "ORDER" && tokens[i + 1] == "BY") || (t == "GROUP" && tokens[i + 1] == "BY"))) { i--; break; } if (t.Type == TokenType.Comma) { AppendWithNewLines(result, NewLineAtSelectArgumentSeparator, t.Content.ToString()); } else { result.Append(" "); result.Append(t.Content); } } }