SQL Parser

Background

SQL is the standard language for users to communicate with databases. The SQL parsing engine is responsible for parsing the SQL string into an abstract syntax tree for Apache ShardingSphere to understand and implement its incremental function. Currently, MySQL, PostgreSQL, SQLServer, Oracle, openGauss and SQL dialects conforming to SQL92 specifications are supported. Due to the complexity of SQL syntax, there are still a few unsupported SQLs. By using SQL parsing in the form of Java API, you can easily integrate into various systems and flexibly customize user requirements.

Parameters

Class: org.apache.shardingsphere.parser.config.SQLParserRuleConfiguration

Attributes:

nameDataTypeDescription
parseTreeCache (?)CacheOptionParse syntax tree local cache configuration
sqlStatementCache (?)CacheOptionsql statement local cache configuration

Cache option Configuration

Class:org.apache.shardingsphere.sql.parser.api.CacheOption

Attributes:

nameDataTypeDescriptionDefault Value
initialCapacityintInitial capacity of local cacheparser syntax tree local cache default value 128, SQL statement cache default value 2000
maximumSize(?)longMaximum capacity of local cacheThe default value of local cache for parsing syntax tree is 1024, and the default value of sql statement cache is 65535

Procedure

  1. Set local cache configuration.
  2. Set resolution configuration.
  3. Use the parsing engine to parse SQL.

Sample

  1. CacheOption cacheOption = new CacheOption(128, 1024L);
  2. SQLParserEngine parserEngine = new SQLParserEngine("MySQL", cacheOption);
  3. ParseASTNode parseASTNode = parserEngine.parse("SELECT t.id, t.name, t.age FROM table1 AS t ORDER BY t.id DESC;", false);
  4. SQLStatementVisitorEngine visitorEngine = new SQLStatementVisitorEngine("MySQL");
  5. MySQLStatement sqlStatement = visitorEngine.visit(parseASTNode);
  6. System.out.println(sqlStatement.toString());