JSqlParser Series II code structure (original)

Source: Internet
Author: User

JSqlParser Series II code structure (original)
JSqlPaser abstracts all SQL statements into Statement, which indicates an operation on the database. Package net. sf. jsqlparser. statement;/*** An operation on the db (SELECT, UPDATE ecc .) */public interface Statement {void accept (StatementVisitor statementVisitor);} common Statement includes Select, Create, Drop, Insert, and Delete. They are used as Statement implementation classes, all implement the accept method. This is a typical application of the Visitor mode and runs through every corner of the SQL statement parsing by JSqlParser. Here you only need to know the corresponding StatementVisitor of Statement. To customize SQL statements, you only need to implement the StatementVisitor interface. Take the query statement (Select) as an example. The method of the Select class is as follows: we can see that the Select object has two important members: SelectBody, List <WithItem>, here, WithItem corresponds to the with keyword of the SQL statement, which is rare. It can be seen that the focus of commonly used Select statements is SelectBody. SelectBody is an interface defined as follows: package net. sf. jsqlparser. statement. select; public interface SelectBody {void accept (SelectVisitor selectVisitor);} is another Visitor, but it is replaced with SelectVisitor. That is, the visitor to the Select statement. If you want to customize the Select statement parsing, You can implement this interface. The Select statement is further subdivided and can be roughly expressed as follows: select SelectItem from FromItem where Expression where SelectItem indicates the content to be selected: /*** Anything between "SELECT" and "FROM" <BR> * (that is, any column or expression etc to be retrieved with the query) */public interface SelectItem {void accept (SelectItemVisitor selectItemVisitor);} FromItem indicates the data source (table or embedded selection statement) package net. sf. jsqlparser. statement. select; import net. sf. jsqlparser. express Ion. alias;/*** An item in a "SELECT [...] FROM item1 "statement. (for example a table or a * sub-select) */public interface FromItem {void accept (FromItemVisitor fromItemVisitor); Alias getAlias (); void setAlias (Alias alias ); response getitor (); void setitor (Response parameters);} You Can See That SeletItemVisitor is used for SelectItem resolution, and FromItem is used for parsing. The FromItemVisitor mode is the same. Expression is a little more complex. We will introduce it separately below. In expression SQL parsing, condition Parsing is the most complex. JSqlParser abstracts the conditional expressions between where and order by with Exception. Package net. sf. jsqlparser. expression; public interface Expression {void accept (ExpressionVisitor expressionVisitor);} Haha, I saw another ExpressionVisitor. Do you think the idea of JSqlParser is quite concise. Expressions are further subdivided into multiple types. common expressions include: 1. conditional expressions such as AndExpression (and), OrExpression (or) 2. relational expressions such as objective STO (= ), minorThan (<), GreaterThan (> ),...... 3. arithmetic expressions such as Addition (+), Subtraction (-), Multiplication (*), Division (/),...... 4. Column expressions such as Column 5, case expressions such as CaseExpression 6, value expressions such as StringValue, DateValue, LongValue, DoubleValue ,...... 7. Function expressions such as Function 8 and parameter expressions such as JdbcParameter and JdbcNameParameter ,...... If you want to customize the ExpressionVisitor, the corresponding processing should be given for the above expressions. As mentioned above, the commonly used Visitor includes StatementVisitor, SelectVisitor, ExpressionVisitor, SelectItemVisitor, FromItemVisitor, etc. when parsing a specific SQL statement, you need to customize the relevant Visitor.

Related Article

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.