FastPrepRender SQL with a Query Builder

Render SQL with a Query Builder

Pylon logoPylon● MediumFULLTIMEPHONE SCREEN
Learn

Problem statement

Process a finite sequence of query-builder operations. TABLE name begins a fresh query. Supported rows are:

  • ["SELECT", field1, ...]
  • ["WHERE", left, operator, right] (starts the condition list)
  • ["COND", connector, left, operator, right] (appends AND or OR)
  • ["ALIAS", alias] (aliases the current table)
  • ["JOIN", table, column] (appends JOIN table USING (column))
  • ["RENDER"] emits the current SQL.

Tokens are already safe SQL fragments. Use one space between clauses. A query without SELECT fields renders SELECT *.

Function

renderQueries(operations: String[][]) → String[]

Examples

Example 1

operations = [["TABLE","users"],["SELECT","id","name"],["WHERE","active","=","true"],["RENDER"]]return = ["SELECT id, name FROM users WHERE active = true"]

The builder renders select fields and one condition in canonical clause order.

Example 2

operations = [["TABLE","orders"],["ALIAS","o"],["JOIN","customers","customer_id"],["WHERE","status","=","paid"],["RENDER"]]return = ["SELECT * FROM orders AS o JOIN customers USING (customer_id) WHERE status = paid"]

The builder places aliases, joins, and the initial condition in canonical clause order.

Constraints

  • 1 <= operations.length <= 10^4.
  • Every RENDER follows TABLE; WHERE appears at most once per query; COND follows WHERE.
  • Connectors are AND or OR.
See Pylon hiring insights
public String[] renderQueries(String[][] operations) {
  // write your code here
}
operations[["TABLE","users"],["SELECT","id","name"],["WHERE","active","=","true"],["RENDER"]]
expected["SELECT id, name FROM users WHERE active = true"]
Checking account…