Skip to content

Custom Fold Extensions

walter-weinmann edited this page May 12, 2020 · 4 revisions

1. General Description of Custom Fold extensions


Fold Operation

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 are bottom_up and top_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.

Fold Function Interface

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 method init.

  • 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 of stmnts 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 method finalize.

  • 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 either start or end showing the start and end of the fold method and
    • Pos is either last or other 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 the fold/5 methods

2. The Standard Custom Fold Extensions

sqlparse_format_flat

This function creates a flat SQL statement from a parse tree. This function ignores specified parameters.

sqlparse_format_pretty

This function creates a formatted SQL statement from a parse tree. Details of the optional parameters can be found here.