class DuckDB::Interval
Interval
class represents DuckDB’s interval type value.
The usage is as follows:
require 'duckdb' interval = DuckDB::Interval.new(interval_months: 14, interval_days: 3, interval_micros: 14706123456) # or # interval = DuckDB::Interval.mk_interval(year: 1, month: 2, day: 3, hour: 4, min: 5, sec: 6, usec: 123456) # or # interval = DuckDB::Interval.iso8601_parse('P1Y2M3DT4H5M6.123456S') db = DuckDB::Database.open # database in memory con = db.connect con.execute('CREATE TABLE users (value INTERVAL)') require 'duckdb' db = DuckDB::Database.open con = db.connect con.query('CREATE TABLE intervals (interval_value INTERVAL)') appender = con.appender('intervals') appender .append_interval(interval) .end_row .flush
Attributes
Public Class Methods
Source
# File lib/duckdb/interval.rb, line 51 def iso8601_parse(value) m = ISO8601_REGEXP.match(value) raise ArgumentError, "The argument `#{value}` can't be parse." if m.nil? year, month, day, hour, min, sec, usec = matched_to_i(m) mk_interval(year: year, month: month, day: day, hour: hour, min: min, sec: sec, usec: usec) end
parses the ISO8601 format string and return the Interval
object.
DuckDB::Interval.iso8601_parse('P1Y2M3DT4H5M6.123456S') => #<DuckDB::Interval:0x00007f9b9c0b3b60 @interval_months=14, @interval_days=3, @interval_micros=14706123456>
Source
# File lib/duckdb/interval.rb, line 65 def mk_interval(year: 0, month: 0, day: 0, hour: 0, min: 0, sec: 0, usec: 0) Interval.new( interval_months: (year * 12) + month, interval_days: day, interval_micros: (((hour * 3600) + (min * 60) + sec) * 1_000_000) + usec ) end
creates the Interval
object.
DuckDB::Interval.mk_interval(year: 1, month: 2, day: 3, hour: 4, min: 5, sec: 6, usec: 123456) => #<DuckDB::Interval:0x00007f9b9c0b3b60 @interval_months=14, @interval_days=3, @interval_micros=14706123456>
Source
# File lib/duckdb/interval.rb, line 150 def initialize(interval_months: 0, interval_days: 0, interval_micros: 0) @interval_months = interval_months @interval_days = interval_days @interval_micros = interval_micros end
creates the Interval
object. The arguments are the number of months, days, and microseconds. The default value is 0.
DuckDB::Interval.new(interval_months: 1, interval_days: 2, interval_micros: 3) => #<DuckDB::Interval:0x00007f9b9c0b3b60 @interval_months=1, @interval_days=2, @interval_micros=3>
Source
# File lib/duckdb/interval.rb, line 85 def to_interval(value) case value when String iso8601_parse(value) when Interval value else raise ArgumentError, "The argument `#{value}` can't be parse." end end
Convert the value to the Interval
object. The value can be String or Interval
object. If the value is String, it is parsed as ISO8601 format. If the value is Interval
object, it is returned as is. Otherwise, ArgumentError is raised.
DuckDB::Interval.to_interval('P1Y2M3DT4H5M6.123456S') => #<DuckDB::Interval:0x00007f9b9c0b3b60 @interval_months=14, @interval_days=3, @interval_micros=14706123456> interval = DuckDB::Interval.to_interval('P1Y2M3DT4H5M6.123456S') DuckDB::Interval.to_interval(interval) => #<DuckDB::Interval:0x00007f9b9c0b3b60 @interval_months=14, @interval_days=3, @interval_micros=14706123456>
Public Instance Methods
Source
# File lib/duckdb/interval.rb, line 156 def ==(other) other.is_a?(Interval) && @interval_months == other.interval_months && @interval_days == other.interval_days && @interval_micros == other.interval_micros end