Giter Site home page Giter Site logo

xclerc / ocaml-mssql Goto Github PK

View Code? Open in Web Editor NEW

This project forked from arenadotio/ocaml-mssql

0.0 1.0 0.0 119 KB

A high-level wrapper OCaml SQL Server client library using FreeTDS

License: Apache License 2.0

Makefile 1.49% OCaml 66.82% Shell 0.10% C 31.59%

ocaml-mssql's Introduction

CircleCI Coverage Status

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

This is not on opam yet, but you can pin the dev repo:

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

We also recommend pinning freetds to our version which releases the global runtime lock during IO until this pull request is merged:

opam pin add freetds https://github.com/arenadotio/ocaml-freetds.git#release-lock-during-io

Usage

See the .mli files for more info (we'll upload generated docs at some point).

The tests are full of examples.

Single connection

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)

Connection pool

Mssql.Pool.with_pool ~host ~db ~user ~password ~max_connections:10 (fun p ->
  Mssql.Pool.with_conn p (fun db ->
    (* same as above *)))

ocaml-mssql's People

Contributors

brendanlong 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.