caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Oleg <oleg@okmij.org>
To: martindemello@gmail.com
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] looking for "real world" sqlite3 examples
Date: Wed, 15 Jul 2015 22:32:09 +0900	[thread overview]
Message-ID: <20150715133209.GA2016@Magus.sf-private> (raw)
In-Reply-To: <CAFrFfuEY8f1i5K7wiiRMD5A_WFokeh15y3_BMiAf2HR=vgqPiw@mail.gmail.com>


> 3. is there a maintained library for generating sql queries in a typed manner?

> But mostly, I want to look at someone else's code and get an idea of
> how this is done in ocaml; pretty much all my database code to date
> has been written in dynamically typed languages and relied on code
> generation.

I'd like to point out
        http://logic.cs.tsukuba.ac.jp/~ken/quel/

which is a library for writing query in a typed, functional style and
generate efficient SQL (without nested SELECTs). The library makes SQL
composable, however odd it may seem. One may consider the library
similar to T-LINQ, described by Cheney et al at ICFP 2013 -- only we
use pure OCaml rather than F# and generate a SQL statement that can be
submitted to any database. (We actually used PostgreSQL for testing).
The main theoretical difference is that our normalization rules are
typed and type-preserving by construction -- and extensible, to
compensate for difference among databases.

Here is a simple example

(* Should be automatically generated, but currently isn't *)
module type SCHEMA = sig   
  type 'a repr

  val oid      : <oid:int; pid:int; qty:int> repr -> int repr
  val opid     : <oid:int; pid:int; qty:int> repr -> int repr
  val qty      : <oid:int; pid:int; qty:int> repr -> int repr

  val orders   : unit -> <oid:int; pid:int; qty:int> list
end
module type SYM_SCHEMA = sig
  include SymanticsL
  include SCHEMA  with type 'a repr := 'a repr
end

(* simple query *)

module Q1'(S:SYM_SCHEMA) = struct
  open S
  let table_orders = table ("orders", orders ())

  let res =
      foreach (fun () -> table_orders) @@ fun o ->
      where (oid o =% int 2) @@ fun () ->
      yield o

  let observe = observe
end

let module M = Q1'(GenSQL) in M.observe (fun () -> M.res)
(* "SELECT x.* FROM orders AS x WHERE true AND x.oid = 2" *)


  parent reply	other threads:[~2015-07-15 13:24 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-07-05  0:03 Martin DeMello
2015-07-05 13:45 ` Erkki Seppala
2015-07-05 15:37   ` Mauricio Fernández
2015-07-15 13:32 ` Oleg [this message]
2015-07-17  7:10 ` Petter Urkedal

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=20150715133209.GA2016@Magus.sf-private \
    --to=oleg@okmij.org \
    --cc=caml-list@inria.fr \
    --cc=martindemello@gmail.com \
    /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).