-
Notifications
You must be signed in to change notification settings - Fork 15
Custom Fold Extensions
A fold operation can be applied to a parse tree with the following function call:
sqlparse_fold:Direction(Function/5, SQLParseTree, Options)
Types
-
Direction
- determines the processing direction, valid values arebottom_up
andtop_down
. -
Function/5
- the custom fold extension which creates the output required by the fold operation. -
SQLParseTree
- a string containing a SQL statement or a tuple containing a parse tree. -
Options
- optional parameters to control the output of the custom fold extension.
The fold function communicates with the custom fold extension (Function/5
) as follows:
custom_fold_extension:fold(Options, FunState, Context, ParseTree, FoldState)
Types
-
Options
- any optional parameters that may have been edited in the methodinit
. -
FunState
- contains information about the fold history.-
indent_lvl
shows the current indentation depth -
stmnts
contains a list showing the history of the processing depth of the parse tree. Each list element ofstmnts
is tuple of format{Statement, Clause, Rule}
, for instance{query_spec, fields, case_when_then}
.
-
-record(fstate, {
indent_lvl = 0,
stmnts = []
}).
-
Context
- the accumulated output data that can then be changed again in the methodfinalize
. -
ParseTree
- the current parse tree element to be processed. -
FoldState :: tuple()
is{Rule :: atom(), Step :: atom()}
or{Rule :: atom(), Step :: atom(), Pos :: atom()}
contains information regarding the current fold step to be processed:-
Rule
is the current grammar rule, -
Step
is eitherstart
orend
showing the start and end of the fold method and -
Pos
is eitherlast
orother
indicating the last element of a list of grammar elements.
-
Methods
Each custom fold extension has the following methods:
-
init(Options)
- to edit the optional parameters. -
fold(Options, FunState, Context, ParseTree, FoldState)
- to prepare the desired output. -
finalize()
- for finalizing the output from thefold/5
methods
This function creates a flat SQL statement from a parse tree. This function ignores specified parameters.
This function creates a formatted SQL statement from a parse tree. Details of the optional parameters can be found here.