Jsqlparser series of two code structure (original)
Blog Park hundred Flavor Chalet Original, reproduced please indicate the source.
The previous article briefly describes how to set up the Jsqlparser project, and this chapter introduces the code structure of the Jsqlparser project in general.
I. Directory structure
Jsqlparser directory structure is relatively simple, mainly have expressions, parsers, statements processing several directories. The following diagram shows a specific SQL statement and an expression:
Next, briefly describe some of the abstract concepts in Jsqlparser.
II. SQL statements (statement)
Jsqlpaser all SQL statements as statement,statement represents an operation on the database.
Package net.sf.jsqlparser.statement; /** */Publicinterface Statement {void Accept ( Statementvisitor statementvisitor);}
Common statement are:select,create,Drop,Insert,Delete , etc., which are implemented as statement classes, All implement the Accept method. This is a typical application of the visitor pattern, running through the Jsqlparser parsing the SQL statements in every corner. Here you just need to know statement corresponds to Statementvisitor. If you want to customize the SQL statement, you only need to implement the Statementvisitor interface.
In the case of a query statement (select), here is a method for the Select class:
As you can see, the Select object has two important members:selectbody, List<withitem>, where Withitem corresponds to the WITH keyword of the SQL statement and is rare. The focus of common SELECT statements is visible in Selectbody. Selectbody is an interface that is defined as follows:
Package Net.sf.jsqlparser.statement.select; Public Interface selectbody { void Accept (Selectvisitor selectvisitor);}
Again a visitor, but here replaced by Selectvisitor. That is, the visitor for the SELECT statement. If you want to customize the parse SELECT statement, you can implement the interface.
The SELECT statement is further subdivided and can be broadly expressed as follows:
Select SelectItem from Fromitem where Expression
Where SelectItem represents the content to select:
/***/Publicinterface selectitem {void Accept ( Selectitemvisitor selectitemvisitor);}
Fromitem represents the data source (table or embedded selection statement)
package Net.sf.jsqlparser.statement.select; import Net.sf.jsqlparser.expression.Alias; /** * an item in a "SELECT [...] From Item1 "statement. (For example a table or a * sub-select) */ public interface Fromitem { accept (Fromitemvisitor fromitemvisitor); Alias Getalias (); void Setalias (alias alias); Pivot Getpivot (); void Setpivot (pivot pivot);}
You can see that SelectItem parsing will be used when the Seletitemvisitor,fromitem parsing will use the Fromitemvisitor, the pattern is the same.
Expression is slightly more complex and is described separately below.
Iii. Expressions (expression)
During SQL parsing, the parsing of conditions is the most complex. Jsqlparser abstract The conditional expression between where and order by exception.
Package net.sf.jsqlparser.expression; Public Interface Expression { void Accept (ExpressionVisitor expressionvisitor);}
Haha, also see a expressionvisitor, is not think Jsqlparser idea is quite concise.
Expression is further subdivided into a variety of more common:
1. Conditional expressions
such as: Andexpression (and), orexpression (or)
2. Relationship Expression
such as: equalsto (=), Minorthan (<), GreaterThan (>), ...
3. Arithmetic expressions
such as: addition (+), subtraction (-), multiplication (*), Division (/),......
4. Column expression
such as: Column
5. Case expression
such as: caseexpression
6. Value Expression
such as: Stringvalue,datevalue,longvalue,doublevalue,......
7. Function expression
such as: Function
8. Parameter expression
such as: Jdbcparameter,jdbcnameparameter,......
If you want to customize the ExpressionVisitor, for the above different expressions, you should give corresponding processing.
Iv. Visitors (Visitor)
As mentioned above, commonly used Visitor have statementvisitor,selectvisitor,Expressionvisitor,selectitemvisitor,fromitemvisitor, etc. , it is necessary to customize the relevant visitor when parsing a specific piece of SQL statement.
Jsqlparser series of two code structure (original)