MyNixOS website logo
Description

Haskell bindings to the CozoDB C API.

Simple, relatively low-level bindings to the C API provided by cozo. Additionally, there are some convenience types for serializing function arguments and deserializing query results.

cozo-hs

A library with bindings and simple types to the C API of the embeddable Cozo database.

Installation

This library relies on pkg-config for libcozo_c being available on your system. You will need to have cozo_c.h and libcozo_c.so installed somewhere your linker can find it and it must be registered with pkg-config.

To do so, you can download both the header files as well as the library from the Cozo project repository or you could clone that repository and build it from source.

If there is no official way to install this library on your OS/Distro (Debian for example) then you can put the library and headers in common paths (/usr/local/lib, /usr/local/include respectively) then manually create a pkg-config file for them. Just make sure that the version you specify matches what you have installed!

Here is an example libcozo_c.pc:

Name: cozo_c
Description: C Interface with the Cozo embeddable database.
Version: 0.7.5
Cflags: -I/usr/local/include
Libs: -L/usr/local/lib -lcozo_c

After that, a simple cabal build can make sure that the C compiler is linking correctly.

After building, you may want to ensure that the runtime is linked correctly as well. Run cabal repl to try out some of the code or cabal test to run the test-suite.

Following all of this, you can include this library as a dependency in other Cabal projects the normal way.

Notes on Usage

This library makes heavy use of aeson given that the C API is defined almost entirely in terms of JSON. Therefore, some primitive data types which match the structure of returned JSON are provided as part of this library.

If you desire to use your own serialization scheme rather than the types provided, the underlying bindings are exposed in the form of functions with a ' suffix. All of these functions deal with strict ByteStrings that are marshalled from the strings produced by C.

All errors are handled in a pure Either.

A very simple program which opens an in-memory connection and runs a query is as follows:

{-#LANGUAGE OverloadedStrings#-}
import Database.Cozo
import Control.Exception (bracket, throwIO)

main :: IO ()
main = 
  bracket 
    (open "mem" "" "{}" >>= either throwIO pure)
    (fmap (const ()) . close)
    $ \c -> do
      er <- runQuery c "?[] <- [[1,2,3]]" KM.empty
      case er of
        Left e -> throwIO e
        Right (CozoResult badOkay) -> 
          case badOkay of
            Left bad -> print . cozoBadMessage $ bad
            Right okay -> print . namedRowsRows . cozoOkayNamedRows $ okay

This program will output:

[[Number 1.0,Number 2.0,Number 3.0]]
Metadata

Version

0.1.0.0

License

Platforms (77)

    Darwin
    FreeBSD
    Genode
    GHCJS
    Linux
    MMIXware
    NetBSD
    none
    OpenBSD
    Redox
    Solaris
    WASI
    Windows
Show all
  • aarch64-darwin
  • aarch64-freebsd
  • aarch64-genode
  • aarch64-linux
  • aarch64-netbsd
  • aarch64-none
  • aarch64-windows
  • aarch64_be-none
  • arm-none
  • armv5tel-linux
  • armv6l-linux
  • armv6l-netbsd
  • armv6l-none
  • armv7a-darwin
  • armv7a-linux
  • armv7a-netbsd
  • armv7l-linux
  • armv7l-netbsd
  • avr-none
  • i686-cygwin
  • i686-darwin
  • i686-freebsd
  • i686-genode
  • i686-linux
  • i686-netbsd
  • i686-none
  • i686-openbsd
  • i686-windows
  • javascript-ghcjs
  • loongarch64-linux
  • m68k-linux
  • m68k-netbsd
  • m68k-none
  • microblaze-linux
  • microblaze-none
  • microblazeel-linux
  • microblazeel-none
  • mips-linux
  • mips-none
  • mips64-linux
  • mips64-none
  • mips64el-linux
  • mipsel-linux
  • mipsel-netbsd
  • mmix-mmixware
  • msp430-none
  • or1k-none
  • powerpc-netbsd
  • powerpc-none
  • powerpc64-linux
  • powerpc64le-linux
  • powerpcle-none
  • riscv32-linux
  • riscv32-netbsd
  • riscv32-none
  • riscv64-linux
  • riscv64-netbsd
  • riscv64-none
  • rx-none
  • s390-linux
  • s390-none
  • s390x-linux
  • s390x-none
  • vc4-none
  • wasm32-wasi
  • wasm64-wasi
  • x86_64-cygwin
  • x86_64-darwin
  • x86_64-freebsd
  • x86_64-genode
  • x86_64-linux
  • x86_64-netbsd
  • x86_64-none
  • x86_64-openbsd
  • x86_64-redox
  • x86_64-solaris
  • x86_64-windows