Coder Social home page Coder Social logo

zbroyar / pgocaml Goto Github PK

View Code? Open in Web Editor NEW

This project forked from darioteixeira/pgocaml

0.0 1.0 0.0 1.12 MB

PG'OCaml provides an interface to PostgreSQL databases for OCaml applications. It uses Camlp4 to extend the OCaml syntax, enabling one to directly embed SQL statements inside the OCaml code.

License: Other

OCaml 99.63% Shell 0.37%

pgocaml's Introduction

PG'OCaml is a set of OCaml bindings for the PostgreSQL database.

Please note that this is not the first or only PGSQL bindings for OCaml. Here are the others which you may want to consider:

  • postgresql-ocaml PostgreSQL-OCaml by Markus Mottl

  • ocamlodbc ODBC bindings by Max Guesdon which can be used to access PostgreSQL

  • ocamldbi a Perl-like DBI layer by the present author

PG'OCAML is different than the above bindings:

  • It ISN'T just a wrapper around the C libpq library. Instead it's a pure OCaml library which talks the frontend/backend protocol directly with the database.

  • It has a camlp4 layer which lets you write SQL statements directly in your code, TYPE SAFE at compile time, with TYPE INFERENCE into the SQL, and using the full PostgreSQL SQL grammar (sub-selects, PG-specific SQL, etc.). But the flip side of this is that you need to have access to the database at compile time, so the type checking magic can be done; also if you change the database schema, you will need to recompile your code to check it is still correctly typed.

  • (A minor point) - It requires PostgreSQL >= 7.4. The default interface (PGOCaml) provided is synchronous. But it also supports any asynchronous interface that implements the PGOCaml_generic.THREAD signature.

  • It doesn't work with other databases, nor will it ever work with other databases.

Usage

PG'OCaml uses environment variables (or in-code parameters, which are [ill advised] (https://hackernoon.com/how-to-use-environment-variables-keep-your-secret-keys-safe-secure-8b1a7877d69c)) to connect to your database both at compile-time and at runtime.

Variable Default Additional information
PGHOST If this starts with a / or is unspecified, PG'OCaml assumes you're specifying a Unix domain socket.
PGPORT 5432 This is also the default PostgreSQL port.
PGUSER The username of the current user, or postgres if that can't be found.
PGDATABASE falls back on PGUSER
PGPASSWORD empty string
PGPROFILING no profiling Indicates the file to write profiling information to. If it doesn't exist, don't profile
COMMENT_SRC_LOC no If set to yes, 1, or on, PG'OCaml will append a comment to each query indicating where it appears in the OCaml source code. This can be useful for logging.

Using the PPX

In addition to the camlp4 syntax extension, there is also a PPX available for more recent versions of OCaml. The PPX aims to be more or less a carbon copy of the camlp4 extension.

let () =
  let dbh = PGOCaml.connect () in
  let insert name salary =
    [%pgsql dbh "insert into employees (name, salary) VALUES ($name, $salary)"]
  in
  ignore(insert "Chris" 1_000.0);
  let get name =
    [%pgsql dbh "select salary from employees where name = $name"]
  in
  let () = [%pgsql dbh
      "execute"
      "CREATE TEMP TABLE IF NOT EXISTS employees (
        name TEXT PRIMARY KEY,
        salary FLOAT)"]
  in
  let name = "Chris" in
  let salary = get name
    |> List.hd
    |> function
        | Some(x) -> x
        | None -> raise(Failure "The database is probably broken.")
  in
  Printf.printf "%s's salary is %.02f\n" name salary;
  PGOCaml.close(dbh)

The PPX allows you to specify that queries returning results should be returned as objects, rather than tuples. This is not currently supported in the Camlp4 version (which is being deprecated).

let%lwt res =
  [%pgsql.object dbh "SELECT * FROM employees"]
in
List.iter
  (fun row ->
    Printf.printf "%s makes $%f\n" row#name row#salary)
  res

The PPX now also supports ${...} expansions.

(* where [e] is a row returned by a [pgsql.object] query *)
let%lwt incr_sal e =
  [%pgsql dbh "UPDATE employees SET salary = ${e#salary +. 1.0}"]

PG'OCaml (C) Copyright 2005-2009 Merjis Ltd, Richard W.M. Jones ([email protected]) and other authors (see CONTRIBUTORS.txt for details).

This software is distributed under the GNU LGPL with OCaml linking exception. Please see the file COPYING.LIB for full license.


For an example, please see tests

pgocaml's People

Contributors

chrismamo1 avatar darioteixeira avatar eras avatar ghuysmans avatar j0sh avatar jaapb avatar johnlepikhin avatar kit-ty-kate avatar kuy avatar mfp avatar rgrinberg avatar rwmjones avatar tizoc avatar vasilisp avatar vbmithr avatar vouillon avatar zbroyar avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.