Class: ROM::SQL::Schema

Inherits:
ROM::Schema
  • Object
show all
Defined in:
lib/rom/sql/schema.rb,
lib/rom/sql/schema/dsl.rb,
lib/rom/sql/schema/inferrer.rb,
lib/rom/sql/schema/index_dsl.rb,
lib/rom/sql/schema/type_builder.rb,
lib/rom/sql/schema/attributes_inferrer.rb

Overview

Specialized schema for SQL databases

Defined Under Namespace

Classes: AttributesInferrer, DSL, IndexDSL, Inferrer, TypeBuilder

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#foreign_keysArray<ForeignKey> (readonly)

Returns Array with foreign keys.

Returns:



24
# File 'lib/rom/sql/schema.rb', line 24

option :foreign_keys, default: -> { EMPTY_SET }

#indexesArray<Index> (readonly)

Returns Array with schema indexes.

Returns:

  • (Array<Index>)

    Array with schema indexes



20
# File 'lib/rom/sql/schema.rb', line 20

option :indexes, default: -> { EMPTY_SET }

Instance Method Details

#call(relation) ⇒ Relation

Create a new relation based on the schema definition

Parameters:

  • relation (Relation)

    The source relation

Returns:



129
130
131
# File 'lib/rom/sql/schema.rb', line 129

def call(relation)
  relation.new(relation.dataset.select(*self), schema: self)
end

#emptySchema

Return an empty schema

Returns:



138
139
140
# File 'lib/rom/sql/schema.rb', line 138

def empty
  new(EMPTY_ARRAY)
end

#finalize_associations!(relations:) ⇒ 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.

Finalize associations



154
155
156
157
158
159
160
# File 'lib/rom/sql/schema.rb', line 154

def finalize_associations!(relations:)
  super do
    associations.map do |definition|
      SQL::Associations.const_get(definition.type).new(definition, relations)
    end
  end
end

#finalize_attributes!(options = EMPTY_HASH) ⇒ 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.

Finalize all attributes by qualifying them and initializing primary key names



145
146
147
148
149
# File 'lib/rom/sql/schema.rb', line 145

def finalize_attributes!(options = EMPTY_HASH)
  super do
    @attributes = map(&:qualified)
  end
end

#group(&block) ⇒ Mixed

Open Group DSL for setting GROUP BY clause in queries

Returns:

  • (Mixed)

    Result of the block call

See Also:



55
56
57
# File 'lib/rom/sql/schema.rb', line 55

def group(&block)
  GroupDSL.new(self).call(&block)
end

#join(other) ⇒ Schema

Join with another schema

Parameters:

  • other (Schema)

    The other schema to join with

Returns:



109
110
111
# File 'lib/rom/sql/schema.rb', line 109

def join(other)
  merge(other.joined)
end

#joinedSchema

Return a new schema with all attributes marked as joined

Returns:



118
119
120
# File 'lib/rom/sql/schema.rb', line 118

def joined
  new(map(&:joined))
end

#order(&block) ⇒ Mixed

Open Order DSL for setting ORDER clause in queries

Returns:

  • (Mixed)

    Result of the block call

See Also:



44
45
46
# File 'lib/rom/sql/schema.rb', line 44

def order(&block)
  OrderDSL.new(self).call(&block)
end

#project(*names, &block) ⇒ Schema

Project a schema

Returns:

  • (Schema)

    A new schema with projected attributes

See Also:



76
77
78
79
80
81
82
# File 'lib/rom/sql/schema.rb', line 76

def project(*names, &block)
  if block
    super(*(names + ProjectionDSL.new(self).(&block)))
  else
    super
  end
end

#project_fk(mapping) ⇒ Schema

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.

Project schema so that it only contains renamed foreign key

Returns:



98
99
100
# File 'lib/rom/sql/schema.rb', line 98

def project_fk(mapping)
  new(rename(mapping).map(&:foreign_key))
end

#project_pkSchema

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.

Project schema so that it only contains primary key

Returns:



89
90
91
# File 'lib/rom/sql/schema.rb', line 89

def project_pk
  project(*primary_key_names)
end

#qualified(table_alias = nil) ⇒ Schema

Return a new schema with attributes marked as qualified

Returns:



64
65
66
# File 'lib/rom/sql/schema.rb', line 64

def qualified(table_alias = nil)
  new(map { |attr| attr.qualified(table_alias) })
end

#restriction(&block) ⇒ Mixed

Open restriction DSL for defining query conditions using schema attributes

Returns:

  • (Mixed)

    Result of the block call

See Also:



33
34
35
# File 'lib/rom/sql/schema.rb', line 33

def restriction(&block)
  RestrictionDSL.new(self).call(&block)
end