Class: Sequel::Postgres::PGMultiRange

Inherits:
Array show all
Includes:
SQL::AliasMethods
Defined in:
lib/sequel/extensions/pg_range_ops.rb,
lib/sequel/extensions/pg_multirange.rb

Overview

:nocov:

Defined Under Namespace

Modules: DatabaseMethods Classes: Creator, Parser

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from SQL::AliasMethods

#as

Methods inherited from Array

#case, #pg_array, #pg_json, #pg_jsonb, #pg_row, #sql_expr, #sql_negate, #sql_or, #sql_string_join, #sql_value_list, #~

Constructor Details

#initialize(ranges, db_type) ⇒ PGMultiRange

Set the array of ranges to delegate to, and the database type.


276
277
278
279
# File 'lib/sequel/extensions/pg_multirange.rb', line 276

def initialize(ranges, db_type)
  super(ranges)
  @db_type = db_type.to_s
end

Instance Attribute Details

#db_typeObject

The type of this multirange (e.g. 'int4multirange').


273
274
275
# File 'lib/sequel/extensions/pg_multirange.rb', line 273

def db_type
  @db_type
end

Instance Method Details

#==(other) ⇒ Object

Don't consider multiranges with different database types equal.


319
320
321
322
# File 'lib/sequel/extensions/pg_multirange.rb', line 319

def ==(other)
  return false if PGMultiRange === other && other.db_type != db_type
  super
end

#cover?(value) ⇒ Boolean Also known as: ===

Return whether the value is inside any of the ranges in the multirange.

Returns:

  • (Boolean)

304
305
306
# File 'lib/sequel/extensions/pg_multirange.rb', line 304

def cover?(value)
  any?{|range| range.cover?(value)}
end

#eql?(other) ⇒ Boolean

Don't consider multiranges with different database types equal.

Returns:

  • (Boolean)

310
311
312
313
314
315
316
# File 'lib/sequel/extensions/pg_multirange.rb', line 310

def eql?(other)
  if PGMultiRange === other
    return false unless other.db_type == db_type
    other = other.__getobj__
  end
  __getobj__.eql?(other)
end

#opObject

Wrap the PGRange instance in an RangeOp, allowing you to easily use the PostgreSQL range functions and operators with literal ranges.


153
154
155
# File 'lib/sequel/extensions/pg_range_ops.rb', line 153

def op
  RangeOp.new(self)
end

#sql_literal_append(ds, sql) ⇒ Object

Append the multirange SQL to the given sql string.


282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
# File 'lib/sequel/extensions/pg_multirange.rb', line 282

def sql_literal_append(ds, sql)
  sql << db_type << '('
  joiner = nil
  conversion_meth = nil
  each do |range|
    if joiner
      sql << joiner
    else
      joiner = ', '
    end

    unless range.is_a?(PGRange)
      conversion_meth ||= :"typecast_value_#{db_type.sub('multi', '')}"
      range = ds.db.send(conversion_meth, range)
    end

    ds.literal_append(sql, range)
  end
  sql << ')'
end

#unquoted_literal(ds) ⇒ Object

Return a string containing the unescaped version of the multirange. Separated out for use by the bound argument code.


326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
# File 'lib/sequel/extensions/pg_multirange.rb', line 326

def unquoted_literal(ds)
  val = String.new
  val << "{"

  joiner = nil
  conversion_meth = nil
  each do |range|
    if joiner
      val << joiner
    else
      joiner = ', '
    end

    unless range.is_a?(PGRange)
      conversion_meth ||= :"typecast_value_#{db_type.sub('multi', '')}"
      range = ds.db.send(conversion_meth, range)
    end

    val << range.unquoted_literal(ds)
  end
   
  val << "}"
end