This video is available to students only

Formatting the query

Preparing for the server setup by processing the output of the query builder into database-ready SQL

Formatting the query#

The formatQuery function#

In this lesson we'll focus on the formatQuery function exported by react-querybuilder. 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 ("sql"; the 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.

The valueProcessor function#

The 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 'Y' and 'N' representing true and false, respectively. The default valueProcessor function will use TRUE and FALSE, so we would need to convert that to 'Y' and '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 defaultValueProcessor from 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 client/src/ folder.

No discussions yet