duckdb 0.2.0

DuckDB client for D


To use this package, run the following command in your project's root directory:

Manual usage
Put the following dependency into your project's dependences section:

d-duckdb

DuckDB client for D. This client is based on DuckDB's C API.

Usage

Iterate query result via foreach

import duckdb;
import std;

void main()
{
    auto db = new Database(null);  // null for in-memory database
    auto conn = db.connect();

    writeln(duckdbVersion);

    conn.queryWithoutResult("CREATE TABLE integers (i INTEGER, j INTEGER);");
    conn.queryWithoutResult("INSERT INTO integers VALUES (3, 4), (5, 6), (7, NULL);");
    auto r = conn.query("SELECT * FROM integers;");
    foreach (int a, Nullable!int b; r)  // Specify argument types explicitly
        writeln(a, ", ",  b);

    conn.disconnect();
    db.close();
}

Prepared Statement

import duckdb;
import std;

void main()
{
    auto db = new Database(null);  // null for in-memory database
    auto conn = db.connect();

    writeln(duckdbVersion);

    conn.queryWithoutResult("CREATE TABLE integers (i SMALLINT, j BIGINT);");
    auto stmt1 = conn.prepare("INSERT INTO integers VALUES ($i,  $j)");

    foreach (i; 0..10) {
        short n1 = cast(short)i;
        long  n2 = long.max - i;
        stmt1.bindInt16(1, n1);
        stmt1.bindInt64(2, n2);
        stmt1.execute();
    }
    stmt1.destroy();

    auto stmt2 = conn.prepare("SELECT * FROM integers WHERE i BETWEEN ? AND ?;");
    stmt2.bindValues(3, 8);  // bindValues is shorter than calling each bindXXX
    foreach (short i, long j; stmt2.execute())
        writeln(i, " : ", j);
    stmt2.destroy();

    conn.disconnect();
    db.close();
}

Appender: Faster bulk insertion

import duckdb;
import std;

void main()
{
    auto db = new Database(null);  // null for in-memory database
    auto conn = db.connect();

    writeln(duckdbVersion);
    conn.queryWithoutResult("CREATE TABLE apps (i SMALLINT, str VARCHAR, ts TIMESTAMP);");
    auto appender = conn.appender("apps");
    foreach (i; 0..3)
        appender.appendRow(i, text("Name", i), Clock.currTime);
    appender.destroy();

    auto r = conn.query("SELECT * FROM apps;");
    foreach (short i, string str, SysTime ts; r)
        writeln("id: ", i, ", name: ", str, ", timestampe: ", ts);
    r.destroy();

    conn.disconnect();
    db.close();
}

DuckDB and D Types

DuckDB typeD typeDescription
BOOLEANbool
TINYINTbyte
SMALLINTshort
INTEGERint
BIGINTlong
HUGEINTstd.bigint.BigInt
UTINYINTubyte
USMALLINTushort
UINTEGERuint
UBIGINTulong
UHUGEINTstd.bigint.BigInt
FLOATfloat
DOUBLEdouble
DECIMALTODO: Phobos doesn't have BigFloat/BigDecimal, so support is limited
VARCHARstring/wstring/dstring
BLOBbyte[]
BITSTRINGstring
ENUMstring
DATEstd.datetime.Date
TIMETODO but use TIMESTAMP instead
TIMESTAMPstd.datetime.SysTime
TIMESTAMPTZstd.datetime.SysTime
TIMESTAMP_Sstd.datetime.SysTime
TIMESTAMP_MSstd.datetime.SysTime
TIMESTAMP_NSTIMESTAMP_NS is not supported because SysTime is hnsecs precision
INTERVALTODO
ARRAYT[]
LISTT[]
MAPV[K]
STRUCTstructCurrent implementation doesn't check field names
UNIONTODO
UUIDstd.uuid.UUID

Special cases

  • NULL

Use std.typecons.Nullable to accept NULL for basic types, e.g. Nullable!int. Without Nullable, client returns .init value.

  • Infinity for DATA and TIMESTAMP

Infinity/-Infinity is mapping to .init value, e.g. Date.init and SysTime.init.

TODO

  • Support TODO DuckDB types
  • Support more APIs
  • Improve API design
  • Add unittests

DuckDB official site

DuckDB C API document

Copyright (c) 2024- Masahiro Nakagawa

License

Distributed under the Boost Software License, Version 1.0.

Authors:
  • Masahiro Nakagawa
Dependencies:
none
Versions:
0.2.0 2024-Oct-10
0.1.0 2024-Oct-07
0.0.1 2024-Oct-03
~main 2024-Oct-18
Show all 4 versions
Download Stats:
  • 0 downloads today

  • 0 downloads this week

  • 1 downloads this month

  • 1 downloads total

Score:
0.4
Short URL:
duckdb.dub.pm