duckdb-haskell: Haskell bindings for duckdb.

[ database, library, mit ] [ Propose Tags ] [ Report a vulnerability ]

Full-featured haskell bindings for the duckdb database.


[Skip to Readme]

Flags

Manual Flags

NameDescriptionDefault
ignore-native-sources

Skip building native sources (c-sources, cc-sources), for optimizing the building time of haskell-language-server during development.

Disabled

Use -f <flag> to enable a flag, or -f -<flag> to disable that flag. More info

Downloads

Maintainer's Corner

Package maintainers

For package maintainers and hackage trustees

Candidates

  • No Candidates
Versions [RSS] 0.1.0.0
Dependencies base (>=4 && <5), mtl (>=2 && <3), system-cxx-std-lib (==1.0) [details]
License MIT
Copyright (c) 2023 Tao He
Author Tao He
Maintainer sighingnow@gmail.com
Category Database
Home page https://github.com/sighingnow/duckdb-haskell
Source repo head: git clone https://github.com/sighingnow/duckdb-haskell.git
Uploaded by sighingnow at 2023-10-22T06:39:54Z
Distributions NixOS:0.1.0.0
Downloads 61 total (7 in the last 30 days)
Rating (no votes yet) [estimated by Bayesian average]
Your Rating
  • λ
  • λ
  • λ
Status Docs available [build log]
Last success reported on 2023-10-23 [all 1 reports]

Readme for duckdb-haskell-0.1.0.0

[back to package description]

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