Class: DuckDB::Database

Inherits:
Object
  • Object
show all
Defined in:
lib/duckdb/database.rb,
ext/duckdb/database.c

Overview

The Database class encapsulates a DuckDB database.

The usage is as follows:

require 'duckdb'

db = DuckDB::Database.open # database in memory
con = db.connect

con.query('CREATE TABLE users (id INTEGER, name VARCHAR(30))')

con.query("INSERT into users VALUES(1, 'Alice')")
con.query("INSERT into users VALUES(2, 'Bob')")
con.query("INSERT into users VALUES(3, 'Cathy')")

result = con.query('SELECT * from users')
result.each do |row|
  p row
end

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path = :memory, config: nil, &block) ⇒ Database

Opens a DuckDB database.

DuckDB::Database.new                    #=> in-memory database
DuckDB::Database.new(:memory)           #=> in-memory database
DuckDB::Database.new('test.db')         #=> file database
DuckDB::Database.new('test.db', config: config) #=> with config
DuckDB::Database.new(config: config)    #=> in-memory with config


31
32
33
34
35
36
37
38
39
# File 'lib/duckdb/database.rb', line 31

def initialize(path = :memory, config: nil, &block)
  if path.is_a?(Symbol) && path != :memory
    raise ArgumentError, "path must be a String or :memory, got #{path.inspect}"
  end

  dbpath = path == :memory ? nil : path
  _initialize(dbpath, config)
  _yield_self_and_close(&block) if block
end

Class Method Details

.open(path = :memory, *args, config: nil) ⇒ Object

Opens database.

DuckDB::Database.open                          #=> in-memory database
DuckDB::Database.open('test.db')               #=> file database
DuckDB::Database.open('test.db', config: config)
DuckDB::Database.open(config: config)          #=> in-memory with config

DuckDB::Database.open do |db|
  con = db.connect
  con.query('CREATE TABLE users (id INTEGER, name VARCHAR(30))')
end


53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/duckdb/database.rb', line 53

def open(path = :memory, *args, config: nil)
  path, config = _handle_deprecated_open_args(path, args, config)

  db = new(path, config: config)
  return db unless block_given?

  begin
    yield db
  ensure
    db.close
  end
end

Instance Method Details

#closeDuckDB::Database

closes DuckDB database.

Returns:



119
120
121
122
123
124
# File 'ext/duckdb/database.c', line 119

static VALUE duckdb_database_close(VALUE self) {
    rubyDuckDB *ctx;
    TypedData_Get_Struct(self, rubyDuckDB, &database_data_type, ctx);
    close_database(ctx);
    return self;
}

#connectObject

connects database.

The method yields block and disconnects the database if block given

db = DuckDB::Database.open

con = db.connect # => DuckDB::Connection

db.connect do |con|
  con.query('CREATE TABLE users (id INTEGER, name VARCHAR(30))')
end


103
104
105
106
107
108
109
110
111
112
# File 'lib/duckdb/database.rb', line 103

def connect
  conn = _connect
  return conn unless block_given?

  begin
    yield conn
  ensure
    conn.disconnect
  end
end