class ROM::SQL::Schema

Specialized schema for SQL databases

@api public

Public Instance Methods

call(relation) click to toggle source

Create a new relation based on the schema definition

@param [Relation] relation The source relation

@return [Relation]

@api public

# File lib/rom/sql/schema.rb, line 143
def call(relation)
  relation.new(relation.dataset.select(*self.qualified_projection), schema: self)
end
empty() click to toggle source

Return an empty schema

@return [Schema]

@api public

# File lib/rom/sql/schema.rb, line 152
def empty
  new(EMPTY_ARRAY)
end
finalize_associations!(relations:) click to toggle source

Finalize associations

@api private

Calls superclass method
# 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) click to toggle source

Finalize all attributes by qualifying them and initializing primary key names

@api private

Calls superclass method
# File lib/rom/sql/schema.rb, line 159
def finalize_attributes!(**options)
  super do
    @attributes = map(&:qualified)
  end
end
group(&block) click to toggle source

Open Group DSL for setting GROUP BY clause in queries

@see Relation#group

@return [Mixed] Result of the block call

@api public

# File lib/rom/sql/schema.rb, line 57
def group(&block)
  GroupDSL.new(self).call(&block)
end
join(other) click to toggle source

Join with another schema

@param [Schema] other The other schema to join with

@return [Schema]

@api public

# File lib/rom/sql/schema.rb, line 123
def join(other)
  merge(other.joined)
end
joined() click to toggle source

Return a new schema with all attributes marked as joined

@return [Schema]

@api public

# File lib/rom/sql/schema.rb, line 132
def joined
  new(map(&:joined))
end
order(&block) click to toggle source

Open Order DSL for setting ORDER clause in queries

@see Relation#order

@return [Mixed] Result of the block call

@api public

# File lib/rom/sql/schema.rb, line 46
def order(&block)
  OrderDSL.new(self).call(&block)
end
project(*names, &block) click to toggle source

Project a schema

@see ROM::Schema#project @see Relation#select

@return [Schema] A new schema with projected attributes

@api public

Calls superclass method
# File lib/rom/sql/schema.rb, line 90
def project(*names, &block)
  if block
    super(*(names + ProjectionDSL.new(self).(&block)))
  else
    super
  end
end
project_fk(mapping) click to toggle source

Project schema so that it only contains renamed foreign key

@return [Schema]

@api private

# File lib/rom/sql/schema.rb, line 112
def project_fk(mapping)
  new(rename(mapping).map(&:foreign_key))
end
project_pk() click to toggle source

Project schema so that it only contains primary key

@return [Schema]

@api private

# File lib/rom/sql/schema.rb, line 103
def project_pk
  project(*primary_key_names)
end
qualified(table_alias = nil) click to toggle source

Return a new schema with attributes marked as qualified

@return [Schema]

@api public

# 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) click to toggle source

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

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

@return [Schema]

@api public

# File lib/rom/sql/schema.rb, line 78
def qualified_projection(table_alias = nil)
  new(map { |attr| attr.qualified_projection(table_alias) })
end
restriction(&block) click to toggle source

Open restriction DSL for defining query conditions using schema attributes

@see Relation#where

@return [Mixed] Result of the block call

@api public

# File lib/rom/sql/schema.rb, line 35
def restriction(&block)
  RestrictionDSL.new(self).call(&block)
end