Kamirus / purescript-selda

A type-safe, high-level SQL library for PureScript
MIT License
90 stars 3 forks source link

Query with placeholders #24

Closed Kamirus closed 5 years ago

Kamirus commented 5 years ago

closes #23

AST for expressions extended with Foreign which is meant to be passed as a parameter to the function that executes a query and in the resulting SQL string it is referenced by a placeholder (backend-specific, e.g. for postgres: $1)

For now the parameters where used only in the insert operations. So as long as there was an appropriate ToSQLValue instance (from the pgclient) one could insert it without problems. But to use update one needed to serialize it to string and use Any constructor for Expr AST. Now it can use ToSQLValue instance to create Foreign and pass it into the query using litF function

litF ∷ ∀ s a. ToSQLValue a ⇒ a → Col s a

Example from test suite:

update employees                              -- UPDATE employees
  (\r → r.name .== lit "E3")                  -- WHERE name = "E3"
  (\r → r { date = litF $ date 2000 12 22 })  -- SET date = $1
                                              -- (where $1 is a placeholder)