ext/duckdb/database.c in duckdb-0.3.4.0 vs ext/duckdb/database.c in duckdb-0.5.0
- old
+ new
@@ -3,15 +3,22 @@
VALUE cDuckDBDatabase;
static void close_database(rubyDuckDB *p);
static void deallocate(void * ctx);
static VALUE allocate(VALUE klass);
+static size_t memsize(const void *p);
static VALUE duckdb_database_s_open(int argc, VALUE *argv, VALUE cDuckDBDatabase);
static VALUE duckdb_database_s_open_ext(int argc, VALUE *argv, VALUE cDuckDBDatabase);
static VALUE duckdb_database_connect(VALUE self);
static VALUE duckdb_database_close(VALUE self);
+static const rb_data_type_t database_data_type = {
+ "DuckDB/Database",
+ {NULL, deallocate, memsize,},
+ 0, 0, RUBY_TYPED_FREE_IMMEDIATELY
+};
+
static void close_database(rubyDuckDB *p) {
duckdb_close(&(p->db));
}
static void deallocate(void * ctx) {
@@ -19,15 +26,25 @@
close_database(p);
xfree(p);
}
+static size_t memsize(const void *p) {
+ return sizeof(rubyDuckDB);
+}
+
static VALUE allocate(VALUE klass) {
rubyDuckDB *ctx = xcalloc((size_t)1, sizeof(rubyDuckDB));
- return Data_Wrap_Struct(klass, NULL, deallocate, ctx);
+ return TypedData_Wrap_Struct(klass, &database_data_type, ctx);
}
+rubyDuckDB *get_struct_database(VALUE obj) {
+ rubyDuckDB *ctx;
+ TypedData_Get_Struct(obj, rubyDuckDB, &database_data_type, ctx);
+ return ctx;
+}
+
static VALUE duckdb_database_s_open(int argc, VALUE *argv, VALUE cDuckDBDatabase) {
rubyDuckDB *ctx;
VALUE obj;
char *pfile = NULL;
@@ -38,18 +55,17 @@
if (!NIL_P(file)) {
pfile = StringValuePtr(file);
}
obj = allocate(cDuckDBDatabase);
- Data_Get_Struct(obj, rubyDuckDB, ctx);
+ 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;
}
-#ifdef HAVE_DUCKDB_OPEN_EXT
static VALUE duckdb_database_s_open_ext(int argc, VALUE *argv, VALUE cDuckDBDatabase) {
rubyDuckDB *ctx;
VALUE obj;
rubyDuckDBConfig *ctx_config;
char *perror;
@@ -63,27 +79,26 @@
if (!NIL_P(file)) {
pfile = StringValuePtr(file);
}
obj = allocate(cDuckDBDatabase);
- Data_Get_Struct(obj, rubyDuckDB, ctx);
+ 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.");
}
- Data_Get_Struct(config, rubyDuckDBConfig, ctx_config);
+ 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;
}
-#endif /* HAVE_DUCKDB_OPEN_EXT */
static VALUE duckdb_database_connect(VALUE self) {
return create_connection(self);
}
@@ -93,20 +108,18 @@
*
* closes DuckDB database.
*/
static VALUE duckdb_database_close(VALUE self) {
rubyDuckDB *ctx;
- Data_Get_Struct(self, rubyDuckDB, ctx);
+ TypedData_Get_Struct(self, rubyDuckDB, &database_data_type, ctx);
close_database(ctx);
return self;
}
void init_duckdb_database(void) {
cDuckDBDatabase = rb_define_class_under(mDuckDB, "Database", rb_cObject);
rb_define_alloc_func(cDuckDBDatabase, allocate);
rb_define_singleton_method(cDuckDBDatabase, "_open", duckdb_database_s_open, -1);
-#ifdef HAVE_DUCKDB_OPEN_EXT
rb_define_singleton_method(cDuckDBDatabase, "_open_ext", duckdb_database_s_open_ext, -1);
-#endif
rb_define_private_method(cDuckDBDatabase, "_connect", duckdb_database_connect, 0);
rb_define_method(cDuckDBDatabase, "close", duckdb_database_close, 0);
}