Antlr4初学

背景 项目需要实现前端任意字段匹配查询,故需要配合表达式来生产sql,其实就是定义一种DSL,让前后端相互了解这个语意,调研后决定使用Antlr,相关介绍就不过分多说,直接看效果。语法参考Odata filter ,后期不满足可以直接修改g4文件 Odata filter示例 Country_Region_Code eq 'ES' or Country_Region_Code eq 'US' Country_Region_Code eq 'ES' and Payment_Terms_Code eq '14 DAYS' Entry_No ge 610 Entry_No lt 610 VAT_Bus_Posting_Group ne 'EXPORT' Odata.g4 grammar OData; /* * Parser Rules */ program: expression; expression: LP expression RP # Parenthesis | K_STARTSWITH LP column=column_name ',' value=TEXT RP # StartsWith | K_ENDSWITH LP column=column_name ',' value=TEXT RP # EndsWith | K_CONTAINS LP column=column_name ',' value=TEXT RP # Contains | column=column_name K_IN LP value=decimal_array RP # InDecimal | column=column_name K_IN LP value=string_array RP # InText | column=column_name compare=( Equal | NotEqual | GreaterThan | GreaterThanOrEqual | LessThan | LessThanOrEqual) value=decimal # CompareDecimal | column=column_name compare=( Equal | NotEqual | GreaterThan | GreaterThanOrEqual | LessThan | LessThanOrEqual) value=TEXT # CompareText | expression logic = (K_AND | K_OR) expression # Logic ; column_name : COLUMN_NAME | '[' column_name ']' ; string_array : TEXT (',' TEXT)* ; decimal_array : NUMBER (',' NUMBER)* ; text: TEXT; decimal : NUMBER ; /* * Lexer Rules */ K_IN: I N; K_AND: A N D; K_OR: O R; K_STARTSWITH: S T A R T S W I T H; K_ENDSWITH: E N D S W I T H; K_CONTAINS: C O N T A I N S; LP : '('; RP : ')'; Equal: E Q; NotEqual: N E; GreaterThan: G T; GreaterThanOrEqual: G E; LessThan: L T; LessThanOrEqual: L E; COLUMN_NAME : VALID_ID_START VALID_ID_CHAR* ; TEXT :'"' ....

January 8, 2023

记一次多数据源切换失效问题

事故现场 ### Error querying database. Cause: java.sql.SQLSyntaxErrorException: ORA-00933: SQL command not properly ended ### The error may exist in class path resource [mappers/scm/NCMapper.xml] ### The error may involve defaultParameterMap ### The error occurred while setting parameters ### SQL: select name,code from bd_customer where name like '%%' LIMIT ? ### Cause: java.sql.SQLSyntaxErrorException: ORA-00933: SQL command not properly ended ; bad SQL grammar []; nested exception is java.sql.SQLSyntaxErrorException: ORA-00933: SQL command not properly ended at org....

December 11, 2022