|
| 1 | +#include "ruby-duckdb.h" |
| 2 | + |
| 3 | +static VALUE cDuckDBTableFunction; |
| 4 | + |
| 5 | +static void deallocate(void *ctx); |
| 6 | +static VALUE allocate(VALUE klass); |
| 7 | +static size_t memsize(const void *p); |
| 8 | +static VALUE duckdb_table_function_initialize(VALUE self); |
| 9 | +static VALUE rbduckdb_table_function_set_name(VALUE self, VALUE name); |
| 10 | +static VALUE rbduckdb_table_function_add_parameter(VALUE self, VALUE logical_type); |
| 11 | +static VALUE rbduckdb_table_function_add_named_parameter(VALUE self, VALUE name, VALUE logical_type); |
| 12 | + |
| 13 | +static const rb_data_type_t table_function_data_type = { |
| 14 | + "DuckDB/TableFunction", |
| 15 | + {NULL, deallocate, memsize,}, |
| 16 | + 0, 0, RUBY_TYPED_FREE_IMMEDIATELY |
| 17 | +}; |
| 18 | + |
| 19 | +static void deallocate(void *ctx) { |
| 20 | + rubyDuckDBTableFunction *p = (rubyDuckDBTableFunction *)ctx; |
| 21 | + |
| 22 | + if (p->table_function) { |
| 23 | + duckdb_destroy_table_function(&(p->table_function)); |
| 24 | + p->table_function = NULL; |
| 25 | + } |
| 26 | + xfree(p); |
| 27 | +} |
| 28 | + |
| 29 | +static VALUE allocate(VALUE klass) { |
| 30 | + rubyDuckDBTableFunction *ctx = xcalloc((size_t)1, sizeof(rubyDuckDBTableFunction)); |
| 31 | + return TypedData_Wrap_Struct(klass, &table_function_data_type, ctx); |
| 32 | +} |
| 33 | + |
| 34 | +static size_t memsize(const void *p) { |
| 35 | + return sizeof(rubyDuckDBTableFunction); |
| 36 | +} |
| 37 | + |
| 38 | +/* |
| 39 | + * call-seq: |
| 40 | + * DuckDB::TableFunction.new -> DuckDB::TableFunction |
| 41 | + * |
| 42 | + * Creates a new table function. |
| 43 | + * |
| 44 | + * tf = DuckDB::TableFunction.new |
| 45 | + * tf.name = "my_function" |
| 46 | + * # ... configure tf ... |
| 47 | + */ |
| 48 | +static VALUE duckdb_table_function_initialize(VALUE self) { |
| 49 | + rubyDuckDBTableFunction *ctx; |
| 50 | + |
| 51 | + TypedData_Get_Struct(self, rubyDuckDBTableFunction, &table_function_data_type, ctx); |
| 52 | + |
| 53 | + ctx->table_function = duckdb_create_table_function(); |
| 54 | + if (!ctx->table_function) { |
| 55 | + rb_raise(eDuckDBError, "Failed to create table function"); |
| 56 | + } |
| 57 | + |
| 58 | + return self; |
| 59 | +} |
| 60 | + |
| 61 | +/* |
| 62 | + * call-seq: |
| 63 | + * table_function.name = name -> name |
| 64 | + * |
| 65 | + * Sets the name of the table function. |
| 66 | + * |
| 67 | + * tf.name = "my_function" |
| 68 | + */ |
| 69 | +static VALUE rbduckdb_table_function_set_name(VALUE self, VALUE name) { |
| 70 | + rubyDuckDBTableFunction *ctx; |
| 71 | + const char *func_name; |
| 72 | + |
| 73 | + TypedData_Get_Struct(self, rubyDuckDBTableFunction, &table_function_data_type, ctx); |
| 74 | + |
| 75 | + if (!ctx->table_function) { |
| 76 | + rb_raise(eDuckDBError, "Table function is destroyed"); |
| 77 | + } |
| 78 | + |
| 79 | + func_name = StringValueCStr(name); |
| 80 | + duckdb_table_function_set_name(ctx->table_function, func_name); |
| 81 | + |
| 82 | + return name; |
| 83 | +} |
| 84 | + |
| 85 | +/* |
| 86 | + * call-seq: |
| 87 | + * table_function.add_parameter(logical_type) -> self |
| 88 | + * |
| 89 | + * Adds a positional parameter to the table function. |
| 90 | + * |
| 91 | + * tf.add_parameter(DuckDB::LogicalType::BIGINT) |
| 92 | + * tf.add_parameter(DuckDB::LogicalType::VARCHAR) |
| 93 | + */ |
| 94 | +static VALUE rbduckdb_table_function_add_parameter(VALUE self, VALUE logical_type) { |
| 95 | + rubyDuckDBTableFunction *ctx; |
| 96 | + rubyDuckDBLogicalType *ctx_logical_type; |
| 97 | + |
| 98 | + TypedData_Get_Struct(self, rubyDuckDBTableFunction, &table_function_data_type, ctx); |
| 99 | + |
| 100 | + if (!ctx->table_function) { |
| 101 | + rb_raise(eDuckDBError, "Table function is destroyed"); |
| 102 | + } |
| 103 | + |
| 104 | + ctx_logical_type = get_struct_logical_type(logical_type); |
| 105 | + duckdb_table_function_add_parameter(ctx->table_function, ctx_logical_type->logical_type); |
| 106 | + |
| 107 | + return self; |
| 108 | +} |
| 109 | + |
| 110 | +/* |
| 111 | + * call-seq: |
| 112 | + * table_function.add_named_parameter(name, logical_type) -> self |
| 113 | + * |
| 114 | + * Adds a named parameter to the table function. |
| 115 | + * |
| 116 | + * tf.add_named_parameter("limit", DuckDB::LogicalType::BIGINT) |
| 117 | + */ |
| 118 | +static VALUE rbduckdb_table_function_add_named_parameter(VALUE self, VALUE name, VALUE logical_type) { |
| 119 | + rubyDuckDBTableFunction *ctx; |
| 120 | + rubyDuckDBLogicalType *ctx_logical_type; |
| 121 | + const char *param_name; |
| 122 | + |
| 123 | + TypedData_Get_Struct(self, rubyDuckDBTableFunction, &table_function_data_type, ctx); |
| 124 | + |
| 125 | + if (!ctx->table_function) { |
| 126 | + rb_raise(eDuckDBError, "Table function is destroyed"); |
| 127 | + } |
| 128 | + |
| 129 | + param_name = StringValueCStr(name); |
| 130 | + ctx_logical_type = get_struct_logical_type(logical_type); |
| 131 | + duckdb_table_function_add_named_parameter(ctx->table_function, param_name, ctx_logical_type->logical_type); |
| 132 | + |
| 133 | + return self; |
| 134 | +} |
| 135 | + |
| 136 | +rubyDuckDBTableFunction *get_struct_table_function(VALUE self) { |
| 137 | + rubyDuckDBTableFunction *ctx; |
| 138 | + TypedData_Get_Struct(self, rubyDuckDBTableFunction, &table_function_data_type, ctx); |
| 139 | + return ctx; |
| 140 | +} |
| 141 | + |
| 142 | +void rbduckdb_init_duckdb_table_function(void) { |
| 143 | +#if 0 |
| 144 | + VALUE mDuckDB = rb_define_module("DuckDB"); |
| 145 | +#endif |
| 146 | + cDuckDBTableFunction = rb_define_class_under(mDuckDB, "TableFunction", rb_cObject); |
| 147 | + rb_define_alloc_func(cDuckDBTableFunction, allocate); |
| 148 | + |
| 149 | + rb_define_method(cDuckDBTableFunction, "initialize", duckdb_table_function_initialize, 0); |
| 150 | + rb_define_method(cDuckDBTableFunction, "set_name", rbduckdb_table_function_set_name, 1); |
| 151 | + rb_define_method(cDuckDBTableFunction, "name=", rbduckdb_table_function_set_name, 1); |
| 152 | + rb_define_method(cDuckDBTableFunction, "add_parameter", rbduckdb_table_function_add_parameter, 1); |
| 153 | + rb_define_method(cDuckDBTableFunction, "add_named_parameter", rbduckdb_table_function_add_named_parameter, 2); |
| 154 | +} |
0 commit comments