Parsing SQL statements using Druid's SQL parser module

Source: Internet
Author: User
Tags git clone java reference

Druid is Ali Open source in github above the database connection pool, there is a special parsing SQL statement module Source Location:Https://github.com/alibaba/druid

Introduction to the SQL Parser module:

Https://github.com/alibaba/druid/wiki/SQL-Parser

Related APIs:

http://tool.oschina.net/apidocs/apidoc?api=druid0.26

Druid Use Description:

Druid is now published directly in the MAVEN repository and can be referenced directly. 1. A new MAVEN project named Sql_parser   2, add the following configuration in the Pom.xml file, the current use of the latest 1.0.19
< groupId >com.alibaba</groupId>    <Artifactid  >Druid</artifactid>    <version  >1.0.19</version>  </Dependency >

3. Sample Code, druid the source of the rich test code, the specific path is as follows src/test/java/com.alibaba.druid.bvt.sql.mysql.visitor/ The following App.java reference is mysqlschemastatvisitortest_delete.ajva this source file
 PackageCom.test.mvn.sql_parser;Importjava.util.List;Importcom.alibaba.druid.sql.SQLUtils;Importcom.alibaba.druid.sql.ast.SQLStatement;ImportCom.alibaba.druid.sql.dialect.mysql.visitor.MySqlSchemaStatVisitor;Importcom.alibaba.druid.util.JdbcConstants; /*** Hello world! **/ Public classApp { Public Static voidMain (string[] args) {//String sql = "Update t set name = ' x ' where ID < limit 10"; //String sql = "SELECT ID, NAME, age from USER WHERE ID =?"        Limit 2 "; //String sql = "SELECT * FROM tablename limit";String SQL= "Select User from Emp_table"; String DbType=Jdbcconstants.mysql; //formatted outputString result =sqlutils.format (SQL, DbType); SYSTEM.OUT.PRINTLN (result); //Default capitalization formatList<sqlstatement> stmtlist =sqlutils.parsestatements (SQL, DbType); //The number of independent statements resolvedSYSTEM.OUT.PRINTLN ("size is:" +stmtlist.size ());  for(inti = 0; I < stmtlist.size (); i++) {SQLStatement stmt=Stmtlist.get (i); Mysqlschemastatvisitor Visitor=NewMysqlschemastatvisitor ();             Stmt.accept (visitor); //Get table nameSystem.out.println ("Tables:" +visitor.getcurrenttable ()); //Get Action Method nameSystem.out.println ("Tables:" +visitor.gettables ()); //Get field nameSystem.out.println ("Fields:" +visitor.getcolumns ()); }     } }

 

Source code compilation:Official Installation Method:
    • git clone https://github.com/alibaba/druid.git
    • CD Druid && mvn install
    • Has fun.
but I'm not happy ... because the compile time will report a bunch of errors, and then looked at the development guide:Https://github.com/alibaba/druid/wiki/%E5%A6%82%E4%BD%95%E5%8F%82%E4%B8%8E to find a way to compile successfully, by skipping Test direct packaging
Install -dmaven.javadoc.skip=true -dmaven.test.skip=true
This will pack a snapshot version of the Druid Jar package and introduce the jar into its own test program to see what's changed. Attached: I used the introduction is to modify the setting file to join the local warehouse configuration, so that Druid compile the jar package directly into the local warehouse, and then modify the test program Pom.xml file, add the following configuration
 <  groupid  >  com.alibabagroupid  >  <  artifactid  >  druid</ Span style= "color: #800000;" >artifactid  >  <  version  >  1.0.19-snapshot</  version  >  </ dependency  >  
Then select Project-"maven-" Update project ... And then compile and run it.

Parsing SQL statements using Druid's SQL parser module

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.