Class: ROM::SQL::Relation

Inherits:
Relation
  • Object
show all
Includes:
Reading, Writing
Defined in:
lib/rom/sql/relation.rb,
lib/rom/sql/relation/reading.rb,
lib/rom/sql/relation/writing.rb

Overview

Sequel-specific relation extensions

Defined Under Namespace

Modules: Reading, Writing

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Reading

#avg, #count, #distinct, #exclude, #first, #group, #group_and_count, #inner_join, #invert, #last, #left_join, #limit, #map, #max, #min, #offset, #order, #prefix, #project, #qualified, #qualified_columns, #rename, #reverse, #select, #select_append, #select_group, #sum, #union, #unique?, #where

Methods included from Writing

#delete, #insert, #multi_insert, #update

Constructor Details

#initialize(dataset, registry = {}) ⇒ Relation

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of Relation.



72
73
74
75
# File 'lib/rom/sql/relation.rb', line 72

def initialize(dataset, registry = {})
  super
  @table = dataset.opts[:from].first
end

Instance Attribute Details

#headerHeader (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Return a header for this relation

Returns:



29
30
31
# File 'lib/rom/sql/relation.rb', line 29

def header
  @header
end

#tableObject (readonly)

Name of the table used in FROM clause



34
35
36
# File 'lib/rom/sql/relation.rb', line 34

def table
  @table
end

Class Method Details

.inherited(klass) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Set default dataset for a relation sub-class



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/rom/sql/relation.rb', line 39

def self.inherited(klass)
  super

  klass.class_eval do
    dataset do
      table = opts[:from].first

      if db.table_exists?(table)
        # quick fix for dbs w/o primary_key inference
        #
        # TODO: add a way of setting a pk explicitly on a relation
        pk =
          if db.respond_to?(:primary_key)
            Array(db.primary_key(table))
          else
            [:id]
          end.map { |name| :"#{table}__#{name}" }

          select(*columns).order(*pk)
      else
        self
      end
    end
  end
end

.primary_key(value) ⇒ Object



65
66
67
# File 'lib/rom/sql/relation.rb', line 65

def self.primary_key(value)
  option :primary_key, reader: true, default: value
end

Instance Method Details

#columnsArray<Symbol>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Return raw column names

Returns:

  • (Array<Symbol>)


91
92
93
# File 'lib/rom/sql/relation.rb', line 91

def columns
  dataset.columns
end