Class: ROM::SQL::Schema
- Inherits:
-
ROM::Schema
- Object
- ROM::Schema
- ROM::SQL::Schema
- 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
-
#foreign_keys ⇒ Array<ForeignKey>
readonly
Array with foreign keys.
-
#indexes ⇒ Array<Index>
readonly
Array with schema indexes.
Instance Method Summary collapse
-
#call(relation) ⇒ Relation
Create a new relation based on the schema definition.
-
#empty ⇒ Schema
Return an empty schema.
-
#finalize_attributes!(**options) ⇒ Object
private
Finalize all attributes by qualifying them and initializing primary key names.
-
#group(&block) ⇒ Mixed
Open Group DSL for setting GROUP BY clause in queries.
-
#join(other) ⇒ Schema
Join with another schema.
-
#joined ⇒ Schema
Return a new schema with all attributes marked as joined.
-
#order(&block) ⇒ Mixed
Open Order DSL for setting ORDER clause in queries.
-
#project(*names, &block) ⇒ Schema
Project a schema.
-
#project_fk(mapping) ⇒ Schema
private
Project schema so that it only contains renamed foreign key.
-
#project_pk ⇒ Schema
private
Project schema so that it only contains primary key.
-
#qualified(table_alias = nil) ⇒ Schema
Return a new schema with attributes marked as qualified.
-
#qualified_projection(table_alias = nil) ⇒ Schema
Return a new schema with attributes that are aliased and marked as qualified.
-
#restriction(&block) ⇒ Mixed
Open restriction DSL for defining query conditions using schema attributes.
Instance Attribute Details
#foreign_keys ⇒ Array<ForeignKey> (readonly)
Returns Array with foreign keys.
25 |
# File 'lib/rom/sql/schema.rb', line 25 option :foreign_keys, default: -> { EMPTY_SET } |
#indexes ⇒ Array<Index> (readonly)
Returns Array with schema indexes.
21 |
# File 'lib/rom/sql/schema.rb', line 21 option :indexes, default: -> { EMPTY_SET } |
Instance Method Details
#call(relation) ⇒ Relation
Create a new relation based on the schema definition
142 143 144 |
# File 'lib/rom/sql/schema.rb', line 142 def call(relation) relation.new(relation.dataset.select(*qualified_projection), schema: self) end |
#empty ⇒ Schema
Return an empty schema
151 152 153 |
# File 'lib/rom/sql/schema.rb', line 151 def empty new(EMPTY_ARRAY) 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
158 159 160 161 162 |
# File 'lib/rom/sql/schema.rb', line 158 def finalize_attributes!(**) super do @attributes = map(&:qualified) end end |
#group(&block) ⇒ Mixed
Open Group DSL for setting GROUP BY clause in queries
56 57 58 |
# File 'lib/rom/sql/schema.rb', line 56 def group(&block) GroupDSL.new(self).call(&block) end |
#join(other) ⇒ Schema
Join with another schema
122 123 124 |
# File 'lib/rom/sql/schema.rb', line 122 def join(other) merge(other.joined) end |
#joined ⇒ Schema
Return a new schema with all attributes marked as joined
131 132 133 |
# File 'lib/rom/sql/schema.rb', line 131 def joined new(map(&:joined)) end |
#order(&block) ⇒ Mixed
Open Order DSL for setting ORDER clause in queries
45 46 47 |
# File 'lib/rom/sql/schema.rb', line 45 def order(&block) OrderDSL.new(self).call(&block) end |
#project(*names, &block) ⇒ Schema
Project a schema
89 90 91 92 93 94 95 |
# File 'lib/rom/sql/schema.rb', line 89 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
111 112 113 |
# File 'lib/rom/sql/schema.rb', line 111 def project_fk(mapping) new(rename(mapping).map(&:foreign_key)) end |
#project_pk ⇒ 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 primary key
102 103 104 |
# File 'lib/rom/sql/schema.rb', line 102 def project_pk project(*primary_key_names) end |
#qualified(table_alias = nil) ⇒ Schema
Return a new schema with attributes marked as qualified
65 66 67 |
# File 'lib/rom/sql/schema.rb', line 65 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
77 78 79 |
# File 'lib/rom/sql/schema.rb', line 77 def qualified_projection(table_alias = nil) new(map { |attr| attr.qualified_projection(table_alias) }) end |
#restriction(&block) ⇒ Mixed
Open restriction DSL for defining query conditions using schema attributes
34 35 36 |
# File 'lib/rom/sql/schema.rb', line 34 def restriction(&block) RestrictionDSL.new(self).call(&block) end |