Class: ROM::SQL::Relation

Inherits:
Relation
  • Object
show all
Includes:
ROM::SQL, 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

Constant Summary

Constants included from ROM::SQL

CheckConstraintError, ConstraintError, DatabaseError, ERROR_MAP, ForeignKeyConstraintError, MissingConfigurationError, NoAssociationError, NotNullConstraintError, Rollback, UniqueConstraintError, UnknownDBTypeError, VERSION

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Reading

#avg, #count, #distinct, #exclude, #fetch, #first, #group, #group_and_count, #having, #inner_join, #invert, #last, #left_join, #limit, #map, #max, #min, #offset, #order, #pluck, #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, #upsert

Methods included from ROM::SQL

migration

Class Method Details

.associationsObject

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.



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

def self.associations
  schema.associations
end

.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



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

def self.inherited(klass)
  super

  klass.class_eval do
    schema_dsl SQL::Schema::DSL
    schema_inferrer -> (dataset, gateway) do
      inferrer_for_db = ROM::SQL::Schema::Inferrer.get(gateway.connection.database_type.to_sym)
      inferrer_for_db.new.call(dataset, gateway)
    end

    dataset do
      table = opts[:from].first

      if db.table_exists?(table)
        pk_header = klass.primary_key_header(db, table)
        col_names = klass.schema ? klass.schema.attributes.keys : columns
        select(*col_names).order(*pk_header.qualified)
      else
        self
      end
    end

    # @!method by_pk(pk)
    #   Return a relation restricted by its primary key
    #   @param [Object] pk The primary key value
    #   @return [SQL::Relation]
    #   @api public
    view(:by_pk, attributes[:base]) do |pk|
      where(primary_key => pk)
    end
  end
end

.primary_key(value) ⇒ Object

Deprecated.

Set primary key



96
97
98
99
100
101
# File 'lib/rom/sql/relation.rb', line 96

def self.primary_key(value)
  Deprecations.announce(
    :primary_key, "use schema definition to configure primary key"
  )
  option :primary_key, reader: true, default: value
end

.primary_key_header(db, table) ⇒ 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.



79
80
81
82
83
84
85
86
87
88
89
# File 'lib/rom/sql/relation.rb', line 79

def self.primary_key_header(db, table)
  names =
    if schema
      schema.primary_key_names
    elsif db.respond_to?(:primary_key)
      Array(db.primary_key(table))
    else
      [:id]
    end
  Header.new(names, table)
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>)


132
133
134
# File 'lib/rom/sql/relation.rb', line 132

def columns
  @columns ||= dataset.columns
end

#headerHeader

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:



123
124
125
# File 'lib/rom/sql/relation.rb', line 123

def header
  @header ||= Header.new(selected_columns, table)
end

#tableSymbol

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 table name from relation’s sql statement

This value is used by ‘header` for prefixing column names

Returns:

  • (Symbol)


114
115
116
# File 'lib/rom/sql/relation.rb', line 114

def table
  @table ||= dataset.opts[:from].first
end