private static MiniSqlParserParser CreateParser(string inputStr , DBMSType dbmsType , bool forSqlAccessor) { var input = new AntlrInputStream(inputStr); var lexer = new MiniSqlParserLexer(input); var tokens = new CommonTokenStream(lexer); var parser = new MiniSqlParserParser(tokens); var astListener = new MakeASTListener(tokens, dbmsType, forSqlAccessor); var errorListener = new CumulativeErrorListener(); var lexerErrorListener = new CumulativeLexerErrorListener(); MiniSqlParserAST.SetDbmsType(lexer, parser, dbmsType); // 文法で曖昧な箇所は動的にしか発見できないらしい //parser.AddErrorListener(new DiagnosticErrorListener()); //parser.Interpreter.PredictionMode = PredictionMode.LlExactAmbigDetection; lexer.RemoveErrorListeners(); lexer.AddErrorListener(lexerErrorListener); parser.AddParseListener(astListener); parser.RemoveErrorListeners(); parser.AddErrorListener(errorListener); return(parser); }
public SqlBuilders(string sqlsStr , SqlBuilder.DbmsType dbmsType = SqlBuilder.DbmsType.Unknown , bool forSqlAccessor = true) : this(MiniSqlParserAST.CreateStmts(sqlsStr , SqlBuilder.ConvertDbmsType(dbmsType) , forSqlAccessor)) { }
private Stmt GetStmt() { if (_stmt == null) { _stmt = MiniSqlParserAST.CreateStmt(_sqlStr, _dbmsType, _forSqlAccessor); } return(_stmt); }
public SetPlaceHoldersWrapper(TVisitor visitor, Dictionary <string, string> placeHolders) { _visitor = visitor; _placeHolders = new Dictionary <string, Node>(); foreach (var placeHolder in placeHolders) { var value = MiniSqlParserAST.CreatePlaceHolderNode(placeHolder.Value); _placeHolders.Add(placeHolder.Key, value); } }
public InsertResultVisitor(int index , string exprStr , Identifier aliasName , DBMSType dbmsType = DBMSType.Unknown , bool forSqlAccessor = false) { _index = index; var expr = MiniSqlParserAST.CreateExpr(exprStr, dbmsType, forSqlAccessor); _result = new ResultExpr(expr, true, aliasName); }
public SqlExpr(string value) { if (value.Trim().ToUpper() == "DEFAULT") { _value = new Default(); } else { _value = MiniSqlParserAST.CreateExpr(value); } }
public AddWherePredicateVisitor(string predicateStr , DBMSType dbmsType = DBMSType.Unknown , bool forSqlAccessor = false) { if (string.IsNullOrEmpty(predicateStr)) { _predicate = null; } else { _predicate = MiniSqlParserAST.CreatePredicate(predicateStr, dbmsType, forSqlAccessor); } }
ConvertPlaceHolders(Dictionary <string, string> placeHolders) { var ret = new Dictionary <string, Node>(); if (placeHolders == null) { return(ret); } foreach (var placeHolder in placeHolders) { var value = MiniSqlParserAST.CreatePlaceHolderNode(placeHolder.Value); ret.Add(placeHolder.Key, value); } return(ret); }
public AddOrderByVisitor(IEnumerable <Tuple <string, bool> > orderByItems , DBMSType dbmsType = DBMSType.Unknown , bool forSqlAccessor = false) { // KeyValuePair(Of String, Boolean) = (OrderBy項目文字列, ASC) _orderBy = new OrderBy(); foreach (var orderByItem in orderByItems) { var expr = MiniSqlParserAST.CreateExpr(orderByItem.Item1, dbmsType, forSqlAccessor); _orderBy.Add(new OrderingTerm(expr, null , orderByItem.Item2 ? OrderSpec.Desc : OrderSpec.None , NullOrder.None)); } _dmbsType = dbmsType; }
public static Stmts CreateStmts(string sqls , DBMSType dbmsType = DBMSType.Unknown , bool forSqlAccessor = false) { var parser = MiniSqlParserAST.CreateParser(sqls, dbmsType, forSqlAccessor); var astListener = (MakeASTListener)parser.ParseListeners[0]; var errorListener = (CumulativeErrorListener)parser.ErrorListeners[0]; // SQL文を解析する var context = parser.stmts_root(); if (errorListener.HasSyntaxError) { throw errorListener.ThrowException(); } else if (astListener.HasSqlAccessorSyntaxError) { throw astListener.ThrowSqlAccessorException(); } return((Stmts)astListener.GetAST()); }
public static Node CreatePlaceHolderNode(string placeHolderValue , DBMSType dbmsType = DBMSType.Unknown , bool forSqlAccessor = false) { var parser = MiniSqlParserAST.CreateParser(placeHolderValue, dbmsType, forSqlAccessor); var astListener = (MakeASTListener)parser.ParseListeners[0]; var errorListener = (CumulativeErrorListener)parser.ErrorListeners[0]; // SQL文を解析する var context = parser.placeholder_root(); if (errorListener.HasSyntaxError) { throw errorListener.ThrowException(); } else if (astListener.HasSqlAccessorSyntaxError) { throw astListener.ThrowSqlAccessorException(); } return((Node)astListener.GetAST()); }
/// <summary> /// プレースホルダに値を適用する /// </summary> /// <param name="placeHolders"></param> /// <remarks></remarks> public void Place(Dictionary <string, string> placeHolders) { if (_placedHolders == null) { _placedHolders = new Dictionary <string, INode>(); } // プレースホルダノード単体の場合はReplacePlaceHoldersで置き換えられない if (this.IsPlaceHolderOnly) { var placeHolderName = ((PlaceHolderPredicate)_predicate).LabelName; if (placeHolders.ContainsKey(placeHolderName)) { try { _predicate = MiniSqlParserAST.CreatePredicate(placeHolders[placeHolderName]); // 適用したプレースホルダを記録する if (!_placedHolders.ContainsKey(placeHolderName)) { _placedHolders.Add(placeHolderName, _predicate); } return; } catch (SqlSyntaxErrorsException ex) {; throw new CannotBuildASTException("Type of placeholder value is mismatched", ex); } } } // プレースホルダに値を適用する var visitor = new ReplacePlaceHolders(placeHolders); _predicate.Accept(visitor); // 適用したプレースホルダを記録する foreach (var placedHolder in visitor.PlacedHolders) { if (!_placedHolders.ContainsKey(placedHolder.Key)) { _placedHolders.Add(placedHolder.Key, placedHolder.Value); } } }
public SqlPredicate(string predicate) { _predicate = MiniSqlParserAST.CreatePredicate(predicate); }