Formatting the query
Preparing for the server setup by processing the output of the query builder into database-ready SQL
Formatting the query#
In this lesson we'll focus on the
formatQuery function exported by
formatQuery is useful for displaying different representations of the query, namely the internal JSON format (
"json"), the internal format with rule identifiers stripped out (
"json_without_ids"), SQL format (
WHERE clause only), and a parameterized format useful for interacting with APIs.
Besides the format of the query representation,
formatQuery has a couple of other options that can be set.
valueProcessor is a function that will process rule values, and
quoteFieldNamesWith will quote field names with the provided quote character in SQL mode.
valueProcessor function is useful when your database engine requires special handling of certain value types. For example, the Oracle RDBMS does not have a native Boolean column type, so people typically use
VARCHAR2 with the values
false, respectively. The default
valueProcessor function will use
FALSE, so we would need to convert that to
'N'. Another value type that typically needs special handling is the date type. Most database engines have their own special syntax or functions for dates.
To demonstrate the capabilities of the
valueProcessor function, we'll wrap the
react-querybuilder and adjust the output to show what really happens to date fields on the server. Create a file called
valueProcessor.ts in the