Projects/Wortel

From foldr
Jump to: navigation, search

Information icon.svg This project has been superseeded by purescript-postgresql-client, which uses a superior alternative (PQdescribePrepared) to the method described below.

Wortel
Summary SQL-aware type checker
Type Programming language
Language PureScript
Repository GitHub

Wortel is a programming language that type-checks embedded SQL queries at compile time. Wortel is based on System F.

Schemas

Wortel defers the actual type-checking of queries to the DBMS, unlike quotation-based approaches[1]. This allows for a simpler implementation, easier support for multiple vendors, and future compatibility with new SQL extensions. It does however require that you have a DBMS running, available for the type checker to connect to.

To tell the type checker how to connect to the DBMS, you must give it a mapping from schema names to connection strings. Schema names can then be used in Wortel code to decide which schema to use for a particular query. Database connections are tagged with a schema as well. This makes sure you don't run a query against the wrong database.

Type-checking SQL queries

When the type checker encounters a SQL query, it connects to the DBMS and issues the following statement:

SELECT t.*
FROM (<original query>) AS t
LIMIT 0

For query parameters, the type checker fills in NULL :: <type>. Now the type checker knows the result type of the query.

Open issues

  • It is undesirable to have NULL be an inhabitant of every type. However, SQL queries may return NULL at any time.
  • It may be desirable to allow the programmer to define custom mappings between their types and SQL types.

References

  1. Wadler, P. "Everything Old is New Again: Quoted Domain Specific Languages." https://www.youtube.com/watch?v=DlBwJ4rvz5c