Class: DuckDB::Database
- Inherits:
-
Object
- Object
- DuckDB::Database
- 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
-
.open(path = :memory, *args, config: nil) ⇒ Object
Opens database.
Instance Method Summary collapse
- #close ⇒ Object
-
#connect ⇒ Object
connects database.
-
#initialize(path = :memory, config: nil, &block) ⇒ Database
constructor
Opens a DuckDB database.
Constructor Details
#initialize(path = :memory, config: nil, &block) ⇒ Database
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
#close ⇒ Object
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 |
# File 'ext/duckdb/database.c', line 175
static VALUE database_close(VALUE self) {
rubyDuckDB *ctx;
TypedData_Get_Struct(self, rubyDuckDB, &database_data_type, ctx);
/*
* A closed wrapper must not be handed to the next get_or_create, so drop it
* from the cache's set first. The next call then opens a fresh wrapper.
*/
if (!NIL_P(ctx->cache_wrappers)) {
rb_funcall(ctx->cache_wrappers, rb_intern("delete"), 1, self);
ctx->cache_wrappers = Qnil;
ctx->cached_path = Qnil;
}
rb_thread_call_without_gvl(close_database_without_gvl, ctx, NULL, NULL);
return self;
}
|
#connect ⇒ Object
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 |