Class: DuckDB::Interval

Inherits:
Object
  • Object
show all
Defined in:
lib/duckdb/interval.rb

Overview

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(interval_months: 0, interval_days: 0, interval_micros: 0) ⇒ Interval

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>


150
151
152
153
154
# 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

Instance Attribute Details

#interval_daysObject (readonly)

Returns the value of attribute interval_days.



142
143
144
# File 'lib/duckdb/interval.rb', line 142

def interval_days
  @interval_days
end

#interval_microsObject (readonly)

Returns the value of attribute interval_micros.



142
143
144
# File 'lib/duckdb/interval.rb', line 142

def interval_micros
  @interval_micros
end

#interval_monthsObject (readonly)

Returns the value of attribute interval_months.



142
143
144
# File 'lib/duckdb/interval.rb', line 142

def interval_months
  @interval_months
end

Class Method Details

.iso8601_parse(value) ⇒ Object

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>

Raises:

  • (ArgumentError)


51
52
53
54
55
56
57
58
59
# 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

.mk_interval(year: 0, month: 0, day: 0, hour: 0, min: 0, sec: 0, usec: 0) ⇒ Object

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>


65
66
67
68
69
70
71
# 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

.to_interval(value) ⇒ Object

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>


85
86
87
88
89
90
91
92
93
94
# 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

Instance Method Details

#==(other) ⇒ Object



156
157
158
159
160
161
# 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

#eql?(other) ⇒ Boolean



163
164
165
# File 'lib/duckdb/interval.rb', line 163

def eql?(other)
  self == other
end