diogob / pasta

PostgreSQL AST Assembler

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

PASTA Haskell CI

PASTA stands for PostgreSQL Abstract Syntax Tree Assembler. It provies a set of functions and lenses that will build queries, based on an internal AST representation. It also provides a toSQL function the generated AST into Text.

In other words, it allows you to:

  • Write SQL queries in Haskell without resorting to string/text concatenation.
  • Catch several SQL syntax errors as type-cheker errors.

It won't help you with:

  • Encoding or decoding values, connecting to the database or executing the queries, for this you can rely on database connection libraries such as:

  • Creating higher level abstractions over database objects, for this check:

A simple select can be generated as:

import Pasta

toSQL (selectFrom "some_table")

Note that toSQL currently is implemented using showt (a function from TextShow module).

A INSERT statement with conflict resolution (aka UPSERT).

import Pasta

toSQL $
    insert "public.foo" ("bar" :| ["qux"]) ("2" :| ["3"])
    & onConflict .~ doUpdate "foo_pkey" ["bar" .= ("qux" :: Text)]

The above toSQL will result in:

INSERT INTO "public"."foo" ("bar") VALUES ('qux') ON CONFLICT ON CONSTRAINT "pkey" DO UPDATE SET "bar" = 'qux' WHERE true

You can use the // operator to build fully qualified identifiers as in:

toSQL
  ( select 
  & columns .~ ("*" :| []) 
  & relations .~ ["table1"] 
  & conditions .~ (("table1"//"c") `In` selectFrom "sub")
  )

Wich results in:

SELECT * FROM "table1" "table1" WHERE "table1"."c" IN (SELECT * FROM "sub" "sub" WHERE true)

Note that :| (an operator from semigroups module) is re-exported from PASTA for convenience. The NonEmpty type is used in several PASTA functions.

About

PostgreSQL AST Assembler

License:BSD 3-Clause "New" or "Revised" License


Languages

Language:Haskell 100.0%