Skip to content

Commit

Permalink
Merge pull request #829 from suketa/appender_doc
Browse files Browse the repository at this point in the history
undocument internal method and constants.
  • Loading branch information
suketa authored Nov 30, 2024
2 parents db7aac4 + b396fbf commit b0d874a
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 22 deletions.
10 changes: 8 additions & 2 deletions ext/duckdb/appender.c
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,7 @@ static VALUE appender_append_default(VALUE self) {
}
#endif

/* :nodoc: */
static VALUE appender__append_date(VALUE self, VALUE year, VALUE month, VALUE day) {
duckdb_date dt;
rubyDuckDBAppender *ctx;
Expand All @@ -311,6 +312,7 @@ static VALUE appender__append_date(VALUE self, VALUE year, VALUE month, VALUE da
return self;
}

/* :nodoc: */
static VALUE appender__append_interval(VALUE self, VALUE months, VALUE days, VALUE micros) {
duckdb_interval interval;
rubyDuckDBAppender *ctx;
Expand All @@ -324,6 +326,7 @@ static VALUE appender__append_interval(VALUE self, VALUE months, VALUE days, VAL
return self;
}

/* :nodoc: */
static VALUE appender__append_time(VALUE self, VALUE hour, VALUE min, VALUE sec, VALUE micros) {
duckdb_time time;
rubyDuckDBAppender *ctx;
Expand All @@ -337,6 +340,7 @@ static VALUE appender__append_time(VALUE self, VALUE hour, VALUE min, VALUE sec,
return self;
}

/* :nodoc: */
static VALUE appender__append_timestamp(VALUE self, VALUE year, VALUE month, VALUE day, VALUE hour, VALUE min, VALUE sec, VALUE micros) {
duckdb_timestamp timestamp;

Expand All @@ -352,6 +356,7 @@ static VALUE appender__append_timestamp(VALUE self, VALUE year, VALUE month, VAL
return self;
}

/* :nodoc: */
static VALUE appender__append_hugeint(VALUE self, VALUE lower, VALUE upper) {
duckdb_hugeint hugeint;

Expand All @@ -367,6 +372,7 @@ static VALUE appender__append_hugeint(VALUE self, VALUE lower, VALUE upper) {
return self;
}

/* :nodoc: */
static VALUE appender__append_uhugeint(VALUE self, VALUE lower, VALUE upper) {
duckdb_uhugeint uhugeint;

Expand Down Expand Up @@ -426,6 +432,8 @@ void rbduckdb_init_duckdb_appender(void) {
rb_define_method(cDuckDBAppender, "append_varchar_length", appender_append_varchar_length, 2);
rb_define_method(cDuckDBAppender, "append_blob", appender_append_blob, 1);
rb_define_method(cDuckDBAppender, "append_null", appender_append_null, 0);
rb_define_method(cDuckDBAppender, "flush", appender_flush, 0);
rb_define_method(cDuckDBAppender, "close", appender_close, 0);

#ifdef HAVE_DUCKDB_H_GE_V1_1_0
rb_define_method(cDuckDBAppender, "append_default", appender_append_default, 0);
Expand All @@ -437,6 +445,4 @@ void rbduckdb_init_duckdb_appender(void) {
rb_define_private_method(cDuckDBAppender, "_append_timestamp", appender__append_timestamp, 7);
rb_define_private_method(cDuckDBAppender, "_append_hugeint", appender__append_hugeint, 2);
rb_define_private_method(cDuckDBAppender, "_append_uhugeint", appender__append_uhugeint, 2);
rb_define_method(cDuckDBAppender, "flush", appender_flush, 0);
rb_define_method(cDuckDBAppender, "close", appender_close, 0);
}
26 changes: 6 additions & 20 deletions lib/duckdb/appender.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,16 @@ module DuckDB
# con.query('CREATE TABLE users (id INTEGER, name VARCHAR)')
# appender = con.appender('users')
# appender.append_row(1, 'Alice')
#
class Appender
include DuckDB::Converter

# :stopdoc:
RANGE_INT16 = -32_768..32_767
RANGE_INT32 = -2_147_483_648..2_147_483_647
RANGE_INT64 = -9_223_372_036_854_775_808..9_223_372_036_854_775_807
private_constant :RANGE_INT16, :RANGE_INT32, :RANGE_INT64
# :startdoc:

#
# appends huge int value.
#
# require 'duckdb'
Expand All @@ -33,13 +34,11 @@ class Appender
# .begin_row
# .append_hugeint(-170_141_183_460_469_231_731_687_303_715_884_105_727)
# .end_row
#
def append_hugeint(value)
lower, upper = integer_to_hugeint(value)
_append_hugeint(lower, upper)
end

#
# appends unsigned huge int value.
#
# require 'duckdb'
Expand All @@ -51,13 +50,11 @@ def append_hugeint(value)
# .begin_row
# .append_hugeint(340_282_366_920_938_463_463_374_607_431_768_211_455)
# .end_row
#
def append_uhugeint(value)
lower, upper = integer_to_hugeint(value)
_append_uhugeint(lower, upper)
end

#
# appends date value.
#
# require 'duckdb'
Expand All @@ -72,14 +69,12 @@ def append_uhugeint(value)
# # appender.append_date('2021-10-10')
# appender.end_row
# appender.flush
#
def append_date(value)
date = to_date(value)

_append_date(date.year, date.month, date.day)
end

#
# appends time value.
#
# require 'duckdb'
Expand All @@ -93,14 +88,12 @@ def append_date(value)
# # appender.append_time('01:01:01')
# appender.end_row
# appender.flush
#
def append_time(value)
time = _parse_time(value)

_append_time(time.hour, time.min, time.sec, time.usec)
end

#
# appends timestamp value.
#
# require 'duckdb'
Expand All @@ -115,14 +108,12 @@ def append_time(value)
# # appender.append_time('2021-08-01 01:01:01')
# appender.end_row
# appender.flush
#
def append_timestamp(value)
time = to_time(value)

_append_timestamp(time.year, time.month, time.day, time.hour, time.min, time.sec, time.nsec / 1000)
end

#
# appends interval.
# The argument must be ISO8601 duration format.
# WARNING: This method is expremental.
Expand All @@ -137,13 +128,11 @@ def append_timestamp(value)
# .append_interval('P1Y2D') # => append 1 year 2 days interval.
# .end_row
# .flush
#
def append_interval(value)
value = Interval.to_interval(value)
_append_interval(value.interval_months, value.interval_days, value.interval_micros)
end

#
# appends value.
#
# require 'duckdb'
Expand All @@ -155,7 +144,6 @@ def append_interval(value)
# appender.append(1)
# appender.append('Alice')
# appender.end_row
#
def append(value)
case value
when NilClass
Expand Down Expand Up @@ -188,7 +176,6 @@ def append(value)
end
end

#
# append a row.
#
# appender.append_row(1, 'Alice')
Expand All @@ -199,7 +186,6 @@ def append(value)
# appender.append(1)
# appender.append('Alice')
# appender.end_row
#
def append_row(*args)
begin_row
args.each do |arg|
Expand All @@ -210,11 +196,11 @@ def append_row(*args)

private

def blob?(value)
def blob?(value) # :nodoc:
value.instance_of?(DuckDB::Blob) || value.encoding == Encoding::BINARY
end

def to_date(value)
def to_date(value) # :nodoc:
case value
when Date, Time
value
Expand All @@ -227,7 +213,7 @@ def to_date(value)
end
end

def to_time(value)
def to_time(value) # :nodoc:
case value
when Time
value
Expand Down

0 comments on commit b0d874a

Please sign in to comment.