Coder Social home page Coder Social logo

cloudquery / go-duckdb Goto Github PK

View Code? Open in Web Editor NEW

This project forked from marcboeker/go-duckdb

0.0 1.0 0.0 216.55 MB

go-duckdb provides a database/sql driver for the DuckDB database engine.

License: MIT License

C++ 99.00% C 0.54% Go 0.44% Makefile 0.01%

go-duckdb's Introduction

Go SQL driver for DuckDB

The DuckDB driver conforms to the built-in database/sql interface.

Tests status

Installation

go get github.com/marcboeker/go-duckdb

go-duckdb uses CGO to make calls to DuckDB. You must build your binaries with CGO_ENABLED=1.

Usage

go-duckdb hooks into the database/sql interface provided by the Go stdlib. To open a connection, simply specify the driver type as duckdb:

db, err := sql.Open("duckdb", "")

This creates an in-memory instance of DuckDB. If you would like to store the data on the filesystem, you need to specify the path where to store the database:

db, err := sql.Open("duckdb", "/path/to/foo.db")

If you want to set specific config options for DuckDB, you can add them as query style parameters in the form of name=value to the DSN, like:

db, err := sql.Open("duckdb", "/path/to/foo.db?access_mode=read_only&threads=4")

Alternatively, you can also use sql.OpenDB when you want to perform some initialization before the connection is created and returned from the connection pool on call to db.Conn. Here's an example that installs and loads the JSON extension for each connection:

connector, err := duckdb.NewConnector("/path/to/foo.db?access_mode=read_only&threads=4", func(execer driver.Execer) error {
  bootQueries := []string{
    "INSTALL 'json'",
    "LOAD 'json'",
  }

  for _, qry := range bootQueries {
    _, err = execer.Exec(qry, nil)
    if err != nil {
      return err
    }
  }
  return nil
})
if err != nil {
  return nil, err
}

db := sql.OpenDB(connector)
db.SetMaxOpenConns(poolsize)
...

Please refer to the database/sql GoDoc for further usage instructions.

DuckDB Appender API

If you want to use the DuckDB Appender API, you can obtain a new Appender by supplying a DuckDB connection to NewAppenderFromConn().

connector, err := NewConnector("test.db", nil)
if err != {
  ...
}
conn, err := connector.Connect(context.Background())
if err != {
  ...
}
defer conn.Close()

// Retrieve appender from connection.
appender, err := NewAppenderFromConn(conn, "", "test")
if err != {
  ...
}
defer appender.Close()

err = appender.AppendRow(...)
if err != {
  ...
}

// Optional, if you want to access the appended rows immediately.
err = appender.Flush()
if err != {
  ...
}

Linking DuckDB

By default, go-duckdb statically links DuckDB into your binary. Statically linking DuckDB adds around 30 MB to your binary size. On Linux (Intel) and macOS (Intel and ARM), go-duckdb bundles pre-compiled static libraries for fast builds. On other platforms, it falls back to compiling DuckDB from source, which takes around 10 minutes. You can force go-duckdb to build DuckDB from source by passing -tags=duckdb_from_source to go build.

Alternatively, you can dynamically link DuckDB by passing -tags=duckdb_use_lib to go build. You must have a copy of libduckdb available on your system (.so on Linux or .dylib on macOS), which you can download from the DuckDB releases page. For example:

# On Linux
CGO_ENABLED=1 CGO_LDFLAGS="-L/path/to/libs" go build -tags=duckdb_use_lib main.go
LD_LIBRARY_PATH=/path/to/libs ./main

# On macOS
CGO_ENABLED=1 CGO_LDFLAGS="-L/path/to/libs" go build -tags=duckdb_use_lib main.go
DYLD_LIBRARY_PATH=/path/to/libs ./main

go-duckdb's People

Contributors

adityahegde avatar ajzo90 avatar andyyu2004 avatar begelundmuller avatar chhetripradeep avatar deka108 avatar elefeint avatar erezrokah avatar flarco avatar florianuekermann avatar goober avatar hermanschaaf avatar junhwong avatar marcboeker avatar mesge avatar panta avatar pborzenkov avatar pjain1 avatar rakeshsharma14317 avatar segfault88 avatar wolfeidau avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.