ayman-albaz / nim-duckdb

DuckDB wrapper in Nim
Apache License 2.0
20 stars 4 forks source link

Linux Build Status (Github Actions) License

nim-duckdb

This library is a DuckDB wrapper in nim. It uses nimterop to generate the C Bindings.

Supported Functions

Opening/closing a database:

import duckdb

# Open database and connection.
# Connection and database closed automatically with destructors
var dbConn = connect("mydb.db")

Opening/closing an in-memory database:

import duckdb

# Open database and connection
# Connection and database closed automatically with destructors
var dbConn = connect()

Executing commands and fetching a prepared statement

dbConn.exec("CREATE TABLE integers(i INTEGER, j INTEGER);")
dbConn.exec("INSERT INTO integers VALUES (3, 4), (5, 6), (7, NULL);")
var items: seq[seq[string]]
for item in dbConn.rows("SELECT * FROM integers WHERE i = ? or j = ?", 3, "6"):
  items.add(item)
assert items == @[@["3", "4"], @["5", "6"]]

Executing commands and fast inserting. Fast inserting is much faster than inserting in a loop.

dbConn.exec("CREATE TABLE integers(i INTEGER, j INTEGER);")
dbConn.exec("INSERT INTO integers VALUES (3, 4), (5, 6), (7, NULL);")
dbConn.fastInsert(
  "integers",
  @[
    @["11", "NULL"]
  ],
)
var items: seq[seq[string]]
for item in dbConn.rows(
  """SELECT i, j, i + j
  FROM integers"""
  ): items.add(item)

check items == @[@["3", "4", "7"], @["5", "6", "11"], @["7", "NULL", "NULL"], @["11", "NULL", "NULL"]]

Null items are represented by "NULL"

Acknowledgments

Special thanks to @Clonkk as I used his nimterop script to generate the wrapper.

Contact

I can be reached at aymanalbaz98@gmail.com