MyNixOS website logo
Description

Haskell bindings for duckdb.

Full-featured haskell bindings for the duckdb database.

duckdb-haskell

Full-featured Haskell bindings for DuckDB.

Installation

The library is available on Hackage.

Usage

  • Database.DuckDB.Internal.FFI: fully FFI bindings to the DuckDB C API
  • Database.DuckDB.Connection: connection management
  • Database.DuckDB.Query: query execution
  • Database.DuckDB.Appender: bulk data loading

Connection

  • Connect to a database:

    defaultConnectionTest :: TestTree
    defaultConnectionTest = testCase "Setup the default duckdb database" $ do
        r <- runDuckDB $ do
            (conn, db) <- defaultConnection
            close (conn, db)
        r @?= Right ()
    

Query results

  • Query from the database:

    query42Test :: TestTree
    query42Test = testCase "Query the constant (42)" $ do
        r <- runDuckDB $ withDefaultConnection $ \(_db, conn) -> do
            r <- query conn "select 42;"
            v <- valueInt32 r 0 0
            liftIO $ v @?= 42
        r @?= Right ()
    
  • Query from the database, more complex example:

    queryCreateTableTest :: TestTree
    queryCreateTableTest = testCase "Create table and query" $ do
        r <- runDuckDB $ withDefaultConnection $ \(_db, conn) -> do
            _ <- query conn "CREATE TABLE integers (i INTEGER)"
            _ <- query conn "INSERT INTO integers VALUES (1), (2), (3), (999)"
            r <- query conn "SELECT i FROM integers"
            valueInt32 r 0 0 >>= \v -> liftIO $ v @?= 1
            valueInt32 r 0 1 >>= \v -> liftIO $ v @?= 2
            valueInt32 r 0 2 >>= \v -> liftIO $ v @?= 3
            valueInt32 r 0 3 >>= \v -> liftIO $ v @?= 999
        r @?= Right ()
    
  • Query from the database, inspecting the value using Data.Vector.Storable:

    queryDataVector :: TestTree
    queryDataVector = testCase "Create table and query as efficient vectors" $ do
        r <- runDuckDB $ withDefaultConnection $ \(_db, conn) -> do
            _ <- query conn "CREATE TABLE integers (i INTEGER)"
            _ <- query conn "INSERT INTO integers VALUES (1), (2), (3), (999)"
            r <- query conn "SELECT i FROM integers"
    
            nchk <- chunkCount r
            liftIO $ nchk @?= 1
    
            chk <- chunkAt r 0
            columns <- getChunkColumnCount chk
            liftIO $ columns @?= 1
    
            rows <- getChunkSize chk
            liftIO $ rows @?= 4
    
            pointer <- getVectorData =<< getChunkVector chk 0
    
            liftIO $ do
                vec <-
                    Vec.unsafeFromForeignPtr0 <$> newForeignPtr_ pointer <*> (pure rows)
                        :: IO (Vec.Vector Int32)
                vec @?= Vec.fromList [1, 2, 3, 999]
        r @?= Right ()
    

Bulk data loading

  • Efficiently loading data using appenders:

    appenderTableTest :: TestTree
    appenderTableTest = testCase "Create table, append data and query" $ do
        r <- runDuckDB $ withDefaultConnection $ \(_db, conn) -> do
            _ <- query conn "CREATE TABLE integers (i INTEGER, j INTEGER)"
    
            withAppender conn "" "integers" $ \app ->
                forM_ [1 .. 100] $ \i -> withAppenderRow app $ do
                    appendInt32 app i
                    appendInt32 app (i + 99)
    
            r <- query conn "SELECT i, j FROM integers"
    
            liftIO $ chunkCount r >>= print
    
Metadata

Version

0.1.0.0

License

Platforms (75)

    Darwin
    FreeBSD
    Genode
    GHCJS
    Linux
    MMIXware
    NetBSD
    none
    OpenBSD
    Redox
    Solaris
    WASI
    Windows
Show all
  • aarch64-darwin
  • aarch64-genode
  • aarch64-linux
  • aarch64-netbsd
  • aarch64-none
  • 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