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

:nodoc:



63
64
65
66
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
# File 'ext/duckdb/database.c', line 63

static VALUE duckdb_database_s_open(int argc, VALUE *argv, VALUE cDuckDBDatabase) {
    rubyDuckDB *ctx;
    VALUE obj;
    duckdb_config config;
    char *perror = NULL;

    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);

    config = create_config_with_ruby_api();

    if (duckdb_open_ext(pfile, &(ctx->db), config, &perror) == DuckDBError) {
        VALUE error_msg = rb_str_new_cstr(perror ? perror : "Unknown error");
        if (perror) {
            duckdb_free(perror);
        }
        duckdb_destroy_config(&config);
        rb_raise(eDuckDBError, "failed to open database: %s", StringValueCStr(error_msg));
    }

    duckdb_destroy_config(&config);
    return obj;
}

._open_ext(*args) ⇒ Object

:nodoc:



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'ext/duckdb/database.c', line 97

static VALUE duckdb_database_s_open_ext(int argc, VALUE *argv, VALUE cDuckDBDatabase) {
    rubyDuckDB *ctx;
    VALUE obj;
    rubyDuckDBConfig *ctx_config;
    duckdb_config config_to_use;
    char *perror = NULL;
    int need_destroy_config = 0;

    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);
        /* Set duckdb_api to "ruby" for the provided config */
        if (duckdb_set_config(ctx_config->config, "duckdb_api", "ruby") == DuckDBError) {
            rb_raise(eDuckDBError, "failed to set duckdb_api config");
        }
        config_to_use = ctx_config->config;
    } else {
        config_to_use = create_config_with_ruby_api();
        need_destroy_config = 1;
    }

    if (duckdb_open_ext(pfile, &(ctx->db), config_to_use, &perror) == DuckDBError) {
        VALUE error_msg = rb_str_new_cstr(perror ? perror : "Unknown error");
        if (perror) {
            duckdb_free(perror);
        }
        if (need_destroy_config) {
            duckdb_destroy_config(&config_to_use);
        }
        rb_raise(eDuckDBError, "failed to open database: %s", StringValueCStr(error_msg));
    }

    if (need_destroy_config) {
        duckdb_destroy_config(&config_to_use);
    }

    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:



162
163
164
165
166
167
# File 'ext/duckdb/database.c', line 162

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