caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: "Mauricio Fernández" <mfp@acm.org>
To: Erkki Seppala <flux-caml@inside.org>, caml-list@inria.fr
Subject: Re: [Caml-list] looking for "real world" sqlite3 examples
Date: Sun, 5 Jul 2015 17:37:14 +0200	[thread overview]
Message-ID: <20150705153714.GA7839@AIKA> (raw)
In-Reply-To: <m49io9yen11.fsf@coffee.modeemi.fi>

On Sun, Jul 05, 2015 at 04:45:14PM +0300, Erkki Seppala wrote:
> Martin DeMello <martindemello@gmail.com> writes:
> 
> > 2. type conversions - in the absence of an orm, do i have to write my
> > own by hand per resultset. or is there some intermediate-level library
> > that i haven't found that would automate some of it?
> >
> > 3. is there a maintained library for generating sql queries in a typed manner?
> 
> You should look into SqlExpr:
> 
>   https://github.com/mfp/ocaml-sqlexpr/
> 
> I used it recently for a very small project, and it seemed to work
> great, even if it doesn't ensure compile-time type safety like PGOCaml.

There's a mechanism to unit test the queries automatically (ensuring that
they're coherent with the DB's schema) which can be integrated in the regular
build process, able to catch many (most) problems (syntax errors,
missing/misnamed/ambiguous columns/tables, etc.), see

  https://github.com/mfp/ocaml-sqlexpr#syntax-extension

It works by collecting all the statements that constitute the schema (marked
as "sqlinit") along with the remaining regular queries ("sql" / "sqlc"), and
then generating functions that initialize an empty DB and prepare all the
statements against it (similarly to what PGOCaml does at compile-time).

This can also serve as "early run-time checking", arguably stronger than
PGOCaml in a way (because you can verify that the queries are valid against
the DB's current schema, not against the one you had when compiling).

If you're using OUnit and all your DB-related code lies in module MyDB,
you can do something like this to unit test the SQL queries:

    open OUnit

    let test_sql f () =
      let b   = Buffer.create 13 in
      let fmt = Format.formatter_of_buffer b in
      let contents b = Format.fprintf fmt "@?"; Buffer.contents b in
        if not (f fmt) then
          assert_failure (contents b)

    let tests =
      [ "SQL tests" >:: test_sql MyDB.auto_check_db;
        ...
      ]
      
    ...
       run_test_tt_main ("All" >::: tests)

-- 
Mauricio Fernández

  reply	other threads:[~2015-07-05 15:37 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 [this message]
2015-07-15 13:32 ` Oleg
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=20150705153714.GA7839@AIKA \
    --to=mfp@acm.org \
    --cc=caml-list@inria.fr \
    --cc=flux-caml@inside.org \
    /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).