caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Andre Nathan <andre@digirati.com.br>
To: caml users <caml-list@inria.fr>
Subject: [Caml-list] [ANN?] Sequoia: MySQL query builder via syntax tree manipulation
Date: Tue, 23 Jun 2015 15:09:15 -0300	[thread overview]
Message-ID: <5589A0CB.1000705@digirati.com.br> (raw)

[-- Attachment #1: Type: text/plain, Size: 2414 bytes --]

Hello

So this was mostly an experiment for me to learn how to work with PPX
extensions. It's a hack that I'm sure will affect people's sensibilities
about what should be done with PPX, though maybe someone will find it
useful. It's far from complete so I didn't bother creating an OPAM
package yet.

  https://github.com/andrenth/sequoia

The idea is to have a safe query builder for MySQL. You map a database
using an OCaml module like so:

module MyDB = struct
  type my_table = {
     id   : int    [@sql];
     name : string [@sql];
  } [@@sql]
end [@@sql]

The @sql/@@sql attributes can take an extra string that will map to the
real MySQL column/table/database names in case they differ from their
OCaml names.

With that definition you can then build a query:

let%sql query =
  select
    t.name
  from
    MyDB.my_table as_ t

Which will build the query below via `Sequoia.Select.to_string query`:

  SELECT `t`.`name` FROM `MyDB`.`my_table` AS `t`

Variable substitution is supported using the ref syntax:

let pat = Sequoia.string "a%"
let%sql query =
  select
    t.name
  from
    MyDB.my_table as_ t
  where
    (t.name like !pat && t.id > 1)

If you reference a database, table or column that isn't declared via the
@sql/@@sql attributes or call a nonexistent MySQL function, you get a
compile-time error:

let%sql query =
  select
    (frob(t.name))
  from
    MyDB.my_table as_ t

  => This expression has type [> `Frob of [> `Column of string list ] ]
     but an expression was expected of type Sequoia.Function.t
     The second variant type does not allow tag(s) `Frob

If you try to select a column from a table that's not in a from or join
clause, you get an error too:

let%sql query = select MyDB.my_table.name

  => Table `my_table` not selected in query

There are many problems though. Off the top of my head:

* Everything must be lowercase;
* Error reporting sucks;
* You need parentheses around OCaml expressions that wouldn't be needed
  in real SQL;
* Lots of MySQL functions are not defined yet so you'll get bogus
  errors;
* Due to syntax clash you need stuff like "as_" or "in_";
* Need to find a way to make it work when the DB definitions are in
  separate files;
* Conversion to string doesn't indent the query and is really
  parenthesis-happy.
* Everybody hates MySQL.

Cheers!
Andre


[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 490 bytes --]

                 reply	other threads:[~2015-06-23 18:09 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=5589A0CB.1000705@digirati.com.br \
    --to=andre@digirati.com.br \
    --cc=caml-list@inria.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).