package mssql

  1. Overview
  2. Docs
Async SQL Server client using FreeTDS

Install

Dune Dependency

Authors

Maintainers

Sources

mssql-2.1.0.tbz
sha256=392e7ae06f0c74bf403ec63699d917c005dec5cc64152cb519df6bf0c8a6809e
sha512=3a205c7f125fc426e44c87c5536c14bf8fdf9445db85764ec8c4ce2424503d47c6a60e62137a0b66eb16ba2f59b9ddba5824878c8b0cca856d583a505f143471

Description

Mssql wraps FreeTDS in a nicer and safer interface, with support for parameterized queries, thread-based async IO, and a thread pool.

Published: 13 May 2020

README

README.md

Mssql is an Async OCaml SQL Server library, currently using FreeTDS.

Features

  • Queries run in a background thread and play nicely with Async (assuming you're pinning freetds so it releases the runtime lock)

  • Supports single connections or connection pools

  • Supports automatic conversions for common data types

  • Supports parameterized queries (although internally it's not very nice; we parse the query for $ parameters and then insert quoted params)

  • We have workarounds for FreeTDS madness, like how there's no simple way to know what date format FreeTDS was configured with

  • Intellegently handles concurrent usage for both executing queries and with transactions

  • Heavily tested

Regarding concurrent usage, the following code is safe:

let%map res1 = Mssql.execute db "SELECT * FROM table_a"
and res2 = Mssql.execute db "SELECT * FROM table_b"
in
...

Since we don't support actual concurrency, this will run one query and then the other (order is not defined).

This is also safe and will never cause a duplicate primary key error, since we prevent concurrent usage of a connection outside of a transactio if a transaction is in progress on that connection:

Mssql.execute_unit "CREATE TABLE x (id INT PRIMARY KEY)"
>>= fun () ->
let%map () =
  Mssql.with_transaction db (fun db ->
    Mssql.execute_unit db "INSERT INTO x (id) VALUES (1)"
    >>= fun ()->
    Mssql.execute_unit db ~params:Mssql.Param.[Some (Int 1)]
      "DELETE FROM x WHERE id = $1")
and Mssql.execute db "INSERT INTO x (id) VALUES (1)"

Obviously this only works if we know about the transaction, so using Mssql.begin_transaction or Mssql.execute_unit "begin" won't have this feature.

Contributions

This library is heavily optimized for our use-case, but we would love to see contributions to:

  • Support Lwt and blocking IO

  • Support parameterized queries in a better way

  • Switch to pure OCaml and not use FreeTDS

This is not an exhaustive list -- feel free to create an issue if you're considering making a new feature and want to know if we'll accept it, or just open a pull request.

Installation

You can find mssql on opam:

opam install mssql

Or you can pin this repo if you want:

opam pin add mssql https://github.com/arenadotio/ocaml-mssql.git

Usage

The tests are full of examples and you can find documentation here.

Example

Mssql.with_conn ~host ~db ~user ~password (fun db ->
  Mssql.execute_unit db
    "CREATE TABLE example (id INT NOT NULL, value INT NOT NULL)"
  >>= fun () ->
  Mssql.execute_unit db "INSERT INTO example (id, value) VALUES (1, 2)"
  >>= fun () ->
  Mssql.execute db ~params:Mssql.Param.[Some (Int 1)]
    "SELECT id FROM example WHERE id = $1"
  >>| function
  | [ row ] ->
    let id = Mssql.Row.int_exn row "id"
    and value = Mssql.Row.int_exn row "value" in
    printf "Got row with id=%d value=%d" id value
  | _ -> assert false)

Dependencies (9)

  1. dune >= "1.11"
  2. freetds >= "0.7"
  3. text >= "0.8.0"
  4. logs
  5. ocaml >= "4.06.1"
  6. iter >= "1.2"
  7. ppx_jane < "v0.15"
  8. bignum < "v0.15"
  9. async_unix < "v0.15"

Dev Dependencies (4)

  1. bisect_ppx dev & >= "2.0.0"
  2. odoc with-doc
  3. alcotest-async with-test & >= "1.0.1"
  4. alcotest with-test & >= "1.0.1"

Used by

None

Conflicts

None

OCaml

Innovation. Community. Security.