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
more...

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.

[View source]

266
267
268
269
# File 'lib/sequel/extensions/pg_multirange.rb', line 266

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’).


263
264
265
# File 'lib/sequel/extensions/pg_multirange.rb', line 263

def db_type
  @db_type
end

Instance Method Details

#==(other) ⇒ Object

Don’t consider multiranges with different database types equal.

[View source]

309
310
311
312
# File 'lib/sequel/extensions/pg_multirange.rb', line 309

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)
[View source]

294
295
296
# File 'lib/sequel/extensions/pg_multirange.rb', line 294

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

#eql?(other) ⇒ Boolean

Don’t consider multiranges with different database types equal.

Returns:

  • (Boolean)
[View source]

300
301
302
303
304
305
306
# File 'lib/sequel/extensions/pg_multirange.rb', line 300

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.

[View source]

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

def op
  RangeOp.new(self)
end

#sequel_auto_param_type(ds) ⇒ Object

Allow automatic parameterization.

[View source]

341
342
343
# File 'lib/sequel/extensions/pg_multirange.rb', line 341

def sequel_auto_param_type(ds)
  "::#{db_type}"
end

#sql_literal_append(ds, sql) ⇒ Object

Append the multirange SQL to the given sql string.

[View source]

272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
# File 'lib/sequel/extensions/pg_multirange.rb', line 272

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.

[View source]

316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
# File 'lib/sequel/extensions/pg_multirange.rb', line 316

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