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)



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

option :foreign_keys, default: -> { EMPTY_SET }

#indexesArray<Index> (readonly)



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

option :indexes, default: -> { EMPTY_SET }

Instance Method Details

#call(relation) ⇒ Relation

Create a new relation based on the schema definition



143
144
145
# File 'lib/rom/sql/schema.rb', line 143

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

#emptySchema

Return an empty schema



152
153
154
# File 'lib/rom/sql/schema.rb', line 152

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



168
169
170
171
172
173
174
# File 'lib/rom/sql/schema.rb', line 168

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) ⇒ 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



159
160
161
162
163
# File 'lib/rom/sql/schema.rb', line 159

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

#groupMixed

Open Group DSL for setting GROUP BY clause in queries



57
58
59
# File 'lib/rom/sql/schema.rb', line 57

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

#join(other) ⇒ Schema

Join with another schema



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

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

#joinedSchema

Return a new schema with all attributes marked as joined



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

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

#orderMixed

Open Order DSL for setting ORDER clause in queries



46
47
48
# File 'lib/rom/sql/schema.rb', line 46

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

#project(*names) ⇒ Schema

Project a schema

See Also:



90
91
92
93
94
95
96
# File 'lib/rom/sql/schema.rb', line 90

def project(*names, &)
  if block_given?
    super(*(names + ProjectionDSL.new(self).(&)))
  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



112
113
114
# File 'lib/rom/sql/schema.rb', line 112

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



103
104
105
# File 'lib/rom/sql/schema.rb', line 103

def project_pk
  project(*primary_key_names)
end

#qualified(table_alias = nil) ⇒ Schema

Return a new schema with attributes marked as qualified



66
67
68
# File 'lib/rom/sql/schema.rb', line 66

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

#qualified_projection(table_alias = nil) ⇒ Schema

Return a new schema with attributes that are aliased and marked as qualified

Intended to be used when passing attributes to ‘dataset#select`



78
79
80
# File 'lib/rom/sql/schema.rb', line 78

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

#restrictionMixed

Open restriction DSL for defining query conditions using schema attributes



35
36
37
# File 'lib/rom/sql/schema.rb', line 35

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