1. Overview

A parser and type checker for SQL written in Haskell. Mainly targets the PostgreSQL dialect of SQL and PL/pgSQL syntax only. BSD licensed.

This is the documentation for version 0.6.x. Documentation for other versions is available here: http://jakewheat.github.io/hssqlppp/.

If you just need a SQL parser, this is an updated project focused on parsing SQL only: http://jakewheat.github.io/simple-sql-parser/latest/.

3. Purpose

Possible uses of this library include:

  • type-checker can help with SQL development and catch bugs which aren’t statically detected by Postgres

  • front end for a SQL DBMS

  • generating SQL code programmatically/ generating SQL from some other source

  • transforming SQL code, possibly using quasiquoting, e.g. writing PL/pgSQL with less boilerplate, or optimisation rewrites

  • viewing the catalog produced by some SQL code

With some extra work:

  • generating documentation for SQL code

  • support running one dialect of SQL on a different DBMS using automatic translation - portable SQL

  • typesafe database access from Haskell

  • lint or semantic analysis for SQL or PL/pgSQL

4. Status

Pre-alpha in the traditional sense (not yet feature complete). Support for parsing SQL solid on a large subset of SQL. Supports queries/DML and DDL. Here is an extract from the parsing tests which gives some examples of what can currently be parsed:

The type checker is currently still being fixed after some reworking of parsing and ast types. It is pretty solid on a wide range of queries.

Extract from the type checking tests:

Currently limited support for quasiquoting, here are the extracts from the tests:

The pretty printer should support everything the parser supports (i.e. it pretty prints code which both the parser and Postgres understand).

Catalog information is limited, supports the type checking. Some information is collected on scalar types, domains, composite types, casts, tables, views and functions.

5. Installation

This project is currently developed on GHC 7.10.3. It has also been tested recently with 7.8.4.

To install use

cabal install hssqlppp

To work with the development version, install stack https://github.com/commercialhaskell/stack/blob/master/doc/install_and_upgrade.md. Then:

git clone https://github.com/JakeWheat/hssqlppp.git

cd hssqlppp

make test

Or you can do something like this without stack:

git clone https://github.com/JakeWheat/hssqlppp.git

cd hssqlppp/hssqlppp

cabal install --only-dependencies --enable-tests
cabal build
cabal test
build/dist/Tests/Tests --hide-successes