Skip to content
This repository has been archived by the owner on Jun 10, 2022. It is now read-only.

diogob/pasta

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

82 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published