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

Class Method Details

._open(*args) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'ext/duckdb/database.c', line 46

static VALUE duckdb_database_s_open(int argc, VALUE *argv, VALUE cDuckDBDatabase) {
    rubyDuckDB *ctx;
    VALUE obj;

    char *pfile = NULL;
    VALUE file = Qnil;

    rb_scan_args(argc, argv, "01", &file);

    if (!NIL_P(file)) {
        pfile = StringValuePtr(file);
    }

    obj = allocate(cDuckDBDatabase);
    TypedData_Get_Struct(obj, rubyDuckDB, &database_data_type, ctx);
    if (duckdb_open(pfile, &(ctx->db)) == DuckDBError) {
        rb_raise(eDuckDBError, "Failed to open database"); /* FIXME */
    }
    return obj;
}

._open_ext(*args) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'ext/duckdb/database.c', line 67

static VALUE duckdb_database_s_open_ext(int argc, VALUE *argv, VALUE cDuckDBDatabase) {
    rubyDuckDB *ctx;
    VALUE obj;
    rubyDuckDBConfig *ctx_config;
    char *perror;

    char *pfile = NULL;
    VALUE file = Qnil;
    VALUE config = Qnil;

    rb_scan_args(argc, argv, "02", &file, &config);

    if (!NIL_P(file)) {
        pfile = StringValuePtr(file);
    }

    obj = allocate(cDuckDBDatabase);
    TypedData_Get_Struct(obj, rubyDuckDB, &database_data_type, ctx);
    if (!NIL_P(config)) {
        if (!rb_obj_is_kind_of(config, cDuckDBConfig)) {
            rb_raise(rb_eTypeError, "The second argument must be DuckDB::Config object.");
        }
        ctx_config = get_struct_config(config);
        if (duckdb_open_ext(pfile, &(ctx->db), ctx_config->config, &perror) == DuckDBError) {
            rb_raise(eDuckDBError, "Failed to open database %s", perror);
        }
    } else {
        if (duckdb_open(pfile, &(ctx->db)) == DuckDBError) {
            rb_raise(eDuckDBError, "Failed to open database"); /* FIXME */
        }
    }
    return obj;
}

.open(dbpath = nil, config = nil) ⇒ Object

Opens database. The first argument is DuckDB database file path to open. If there is no argument, the method opens DuckDB database in memory. The method yields block if block is given.

DuckDB::Database.open('duckdb_database.db') #=> DuckDB::Database

DuckDB::Database.open #=> opens DuckDB::Database in memory.

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


41
42
43
44
45
46
47
48
49
50
# File 'lib/duckdb/database.rb', line 41

def open(dbpath = nil, config = nil)
  db = _db_open(dbpath, config)
  return db unless block_given?

  begin
    yield db
  ensure
    db.close
  end
end

Instance Method Details

#closeDuckDB::Database

closes DuckDB database.

Returns:



111
112
113
114
115
116
# File 'ext/duckdb/database.c', line 111

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


74
75
76
77
78
79
80
81
82
83
# File 'lib/duckdb/database.rb', line 74

def connect
  conn = _connect
  return conn unless block_given?

  begin
    yield conn
  ensure
    conn.disconnect
  end
end