/// <exception cref="System.SqlSyntaxErrorException" /> public static void InitRouteRule(ISchemaLoader loader) { var functions = loader.Functions; var functionManager = new MySqlFunctionManager(true); BuildFuncManager(functionManager, functions); foreach (var conf in loader.RuleConfigList) { var algorithmString = conf.Algorithm; var lexer = new MySqlLexer(algorithmString); var parser = new MySqlExprParser(lexer, functionManager, false, MySqlParser.DefaultCharset); var expression = parser.Expression(); if (lexer.Token() != MySqlToken.Eof) { throw new ConfigException("route algorithm not end with EOF: " + algorithmString); } IRuleAlgorithm algorithm; if (expression is IRuleAlgorithm) { algorithm = (IRuleAlgorithm)expression; } else { algorithm = new ExpressionAdapter(expression); } conf.RuleAlgorithm = algorithm; } }
/// <returns> /// type of /// <see cref="Tup.Cobar4Net.Parser.Ast.Expression.Primary.Wildcard" /> /// is possible. never null /// </returns> /// <exception cref="System.SqlSyntaxErrorException"> /// if identifier dose not matched /// </exception> protected internal virtual Identifier Identifier() { if (lexer.Token() == MySqlToken.None) { lexer.NextToken(); } Identifier id; switch (lexer.Token()) { case MySqlToken.OpAsterisk: { lexer.NextToken(); var wc = new Wildcard(null); wc.SetCacheEvalRst(cacheEvalRst); return(wc); } case MySqlToken.Identifier: { id = new Identifier(null, lexer.GetStringValue(), lexer.GetStringValueUppercase()); id.SetCacheEvalRst(cacheEvalRst); lexer.NextToken(); break; } default: { throw Err("expect id or * after '.'"); } } for (; lexer.Token() == MySqlToken.PuncDot;) { switch (lexer.NextToken()) { case MySqlToken.OpAsterisk: { lexer.NextToken(); var wc_1 = new Wildcard(id); wc_1.SetCacheEvalRst(cacheEvalRst); return(wc_1); } case MySqlToken.Identifier: { id = new Identifier(id, lexer.GetStringValue(), lexer.GetStringValueUppercase()); id.SetCacheEvalRst(cacheEvalRst); lexer.NextToken(); break; } default: { throw Err("expect id or * after '.'"); } } } return(id); }
public static Number String2Number(string str) { if (str == null) { return(0); } //TODO --ExprEvalUtils-String2Number try { return(Convert.ToInt32(str)); } catch (Exception ex) { Console.WriteLine("String2Number-ToInt32-{0}-{1}", str, ex); } try { return(Convert.ToInt64(str)); } catch (Exception ex) { Console.WriteLine("String2Number-ToInt64-{0}-{1}", str, ex); } try { var lexer = new MySqlLexer(str); switch (lexer.Token()) { case MySqlToken.LiteralNumPureDigit: { return(lexer.GetIntegerValue()); } case MySqlToken.LiteralNumMixDigit: { return(lexer.GetDecimalValue()); } default: { throw new ArgumentException("unrecognized number: " + str); } } } catch (SqlSyntaxErrorException e) { throw new ArgumentException("str", e); } }
/// <exception cref="System.SqlSyntaxErrorException" /> public static ISqlStatement Parse(string sql, MySqlLexer lexer, string charset) { try { ISqlStatement stmt = null; var isEof = true; var exprParser = new MySqlExprParser(lexer, charset); switch (lexer.Token()) { case MySqlToken.KwDesc: case MySqlToken.KwDescribe: { stmt = new MySqlDalParser(lexer, exprParser).Desc(); goto stmtSwitch_break; } case MySqlToken.KwSelect: case MySqlToken.PuncLeftParen: { stmt = new MySqlDmlSelectParser(lexer, exprParser).SelectUnion(); goto stmtSwitch_break; } case MySqlToken.KwDelete: { stmt = new MySqlDmlDeleteParser(lexer, exprParser).Delete(); goto stmtSwitch_break; } case MySqlToken.KwInsert: { stmt = new MySqlDmlInsertParser(lexer, exprParser).Insert(); goto stmtSwitch_break; } case MySqlToken.KwReplace: { stmt = new MySqlDmlReplaceParser(lexer, exprParser).Replace(); goto stmtSwitch_break; } case MySqlToken.KwUpdate: { stmt = new MySqlDmlUpdateParser(lexer, exprParser).Update(); goto stmtSwitch_break; } case MySqlToken.KwCall: { stmt = new MySqlDmlCallParser(lexer, exprParser).Call(); goto stmtSwitch_break; } case MySqlToken.KwSet: { stmt = new MySqlDalParser(lexer, exprParser).Set(); goto stmtSwitch_break; } case MySqlToken.KwShow: { stmt = new MySqlDalParser(lexer, exprParser).Show(); goto stmtSwitch_break; } case MySqlToken.KwAlter: case MySqlToken.KwCreate: case MySqlToken.KwDrop: case MySqlToken.KwRename: { stmt = new MySqlDdlParser(lexer, exprParser).DdlStmt(); isEof = IsEOFedDdl(stmt); goto stmtSwitch_break; } case MySqlToken.KwRelease: { stmt = new MySqlMtsParser(lexer).Release(); goto stmtSwitch_break; } case MySqlToken.Identifier: { var si = SpecialIdentifier.None; if ((si = SpecialIdentifiers[lexer.GetStringValueUppercase()]) != SpecialIdentifier.None) { switch (si) { case SpecialIdentifier.Truncate: { stmt = new MySqlDdlParser(lexer, exprParser).Truncate(); goto stmtSwitch_break; } case SpecialIdentifier.Savepoint: { stmt = new MySqlMtsParser(lexer).Savepoint(); goto stmtSwitch_break; } case SpecialIdentifier.Rollback: { stmt = new MySqlMtsParser(lexer).Rollback(); goto stmtSwitch_break; } } } goto default; } default: { throw new SqlSyntaxErrorException("sql is not a supported statement"); } } stmtSwitch_break: ; if (isEof) { while (lexer.Token() == MySqlToken.PuncSemicolon) { lexer.NextToken(); } if (lexer.Token() != MySqlToken.Eof) { throw new SqlSyntaxErrorException("Sql syntax error!"); } } return(stmt); } catch (Exception e) { throw new SqlSyntaxErrorException(BuildErrorMsg(e, lexer, sql), e); } }