lib/duckdb/appender.rb in duckdb-0.3.1.0 vs lib/duckdb/appender.rb in duckdb-0.3.2.0
- old
+ new
@@ -1,7 +1,8 @@
require 'date'
require 'time'
+require_relative './converter'
module DuckDB
if defined?(DuckDB::Appender)
# The DuckDB::Appender encapsulates DuckDB Appender.
#
@@ -11,10 +12,12 @@
# con.query('CREATE TABLE users (id INTEGER, name VARCHAR)')
# appender = con.appender('users')
# appender.append_row(1, 'Alice')
#
class Appender
+ include DuckDB::Converter
+
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
#
@@ -195,15 +198,11 @@
append_int64(value)
else
append_hugeint(value)
end
when String
- if defined?(DuckDB::Blob)
- blob?(value) ? append_blob(value) : append_varchar(value)
- else
- append_varchar(value)
- end
+ blob?(value) ? append_blob(value) : append_varchar(value)
when TrueClass, FalseClass
append_bool(value)
when Time
if respond_to?(:append_timestamp)
append_timestamp(value)
@@ -243,56 +242,9 @@
private
def blob?(value)
value.instance_of?(DuckDB::Blob) || value.encoding == Encoding::BINARY
- end
-
- def iso8601_interval_to_hash(value)
- digit = ''
- time = false
- hash = {}
- hash.default = 0
-
- value.each_char do |c|
- if '-0123456789.'.include?(c)
- digit += c
- elsif c == 'T'
- time = true
- digit = ''
- elsif c == 'M'
- m_interval_to_hash(hash, digit, time)
- digit = ''
- elsif c == 'S'
- s_interval_to_hash(hash, digit)
- digit = ''
- elsif 'YDH'.include?(c)
- hash[c] = digit.to_i
- digit = ''
- elsif c != 'P'
- raise ArgumentError, "The argument `#{value}` can't be parse."
- end
- end
- hash
- end
-
- def m_interval_to_hash(hash, digit, time)
- key = time ? 'TM' : 'M'
- hash[key] = digit.to_i
- end
-
- def s_interval_to_hash(hash, digit)
- sec, msec = digit.split('.')
- hash['S'] = sec.to_i
- hash['MS'] = "#{msec}000000"[0, 6].to_i
- hash['MS'] *= -1 if hash['S'].negative?
- end
-
- def hash_to__append_interval_args(hash)
- months = hash['Y'] * 12 + hash['M']
- days = hash['D']
- micros = (hash['H'] * 3600 + hash['TM'] * 60 + hash['S']) * 1_000_000 + hash['MS']
- [months, days, micros]
end
end
end
end