Class: RDBr::Metadata::Relation
- Inherits:
-
Data
- Object
- Data
- RDBr::Metadata::Relation
- Defined in:
- lib/rdbr/metadata/relation.rb
Instance Attribute Summary collapse
-
#check_constraints ⇒ Object
readonly
Returns the value of attribute check_constraints.
-
#columns ⇒ Object
readonly
Returns the value of attribute columns.
-
#comment ⇒ Object
readonly
Returns the value of attribute comment.
-
#foreign_keys ⇒ Object
readonly
Returns the value of attribute foreign_keys.
-
#indexes ⇒ Object
readonly
Returns the value of attribute indexes.
-
#kind ⇒ Object
readonly
Returns the value of attribute kind.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#primary_key ⇒ Object
readonly
Returns the value of attribute primary_key.
-
#unique_constraints ⇒ Object
readonly
Returns the value of attribute unique_constraints.
Instance Method Summary collapse
- #column(name) ⇒ Object
-
#initialize(name:, kind:, columns:, comment: nil, primary_key: nil, foreign_keys: [], unique_constraints: [], check_constraints: [], indexes: []) ⇒ Relation
constructor
A new instance of Relation.
- #keyless? ⇒ Boolean
- #table? ⇒ Boolean
- #uniquely_constrained_column?(name) ⇒ Boolean
- #view? ⇒ Boolean
Constructor Details
#initialize(name:, kind:, columns:, comment: nil, primary_key: nil, foreign_keys: [], unique_constraints: [], check_constraints: [], indexes: []) ⇒ Relation
Returns a new instance of Relation.
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/rdbr/metadata/relation.rb', line 14 def initialize( name:, kind:, columns:, comment: nil, primary_key: nil, foreign_keys: [], unique_constraints: [], check_constraints: [], indexes: [] ) relation_columns = Values.collection(columns, member: Column, field: 'columns') Values.unique_names!(relation_columns, field: 'columns') validate_primary_key!(primary_key) relation_foreign_keys = (foreign_keys, ForeignKey, 'foreign keys') relation_unique_constraints = (unique_constraints, UniqueConstraint, 'unique constraints') relation_check_constraints = (check_constraints, CheckConstraint, 'check constraints') relation_indexes = (indexes, Index, 'indexes') validate_local_columns!( relation_columns, primary_key, relation_foreign_keys, relation_unique_constraints, relation_indexes ) super( name: Values.name(name, field: 'relation name'), kind: Values.deep_copy_freeze(kind.to_sym), comment: Values.optional_string(comment, field: 'relation comment'), columns: relation_columns, primary_key: primary_key, foreign_keys: relation_foreign_keys, unique_constraints: relation_unique_constraints, check_constraints: relation_check_constraints, indexes: relation_indexes ) end |
Instance Attribute Details
#check_constraints ⇒ Object (readonly)
Returns the value of attribute check_constraints
3 4 5 |
# File 'lib/rdbr/metadata/relation.rb', line 3 def check_constraints @check_constraints end |
#columns ⇒ Object (readonly)
Returns the value of attribute columns
3 4 5 |
# File 'lib/rdbr/metadata/relation.rb', line 3 def columns @columns end |
#comment ⇒ Object (readonly)
Returns the value of attribute comment
3 4 5 |
# File 'lib/rdbr/metadata/relation.rb', line 3 def comment @comment end |
#foreign_keys ⇒ Object (readonly)
Returns the value of attribute foreign_keys
3 4 5 |
# File 'lib/rdbr/metadata/relation.rb', line 3 def foreign_keys @foreign_keys end |
#indexes ⇒ Object (readonly)
Returns the value of attribute indexes
3 4 5 |
# File 'lib/rdbr/metadata/relation.rb', line 3 def indexes @indexes end |
#kind ⇒ Object (readonly)
Returns the value of attribute kind
3 4 5 |
# File 'lib/rdbr/metadata/relation.rb', line 3 def kind @kind end |
#name ⇒ Object (readonly)
Returns the value of attribute name
3 4 5 |
# File 'lib/rdbr/metadata/relation.rb', line 3 def name @name end |
#primary_key ⇒ Object (readonly)
Returns the value of attribute primary_key
3 4 5 |
# File 'lib/rdbr/metadata/relation.rb', line 3 def primary_key @primary_key end |
#unique_constraints ⇒ Object (readonly)
Returns the value of attribute unique_constraints
3 4 5 |
# File 'lib/rdbr/metadata/relation.rb', line 3 def unique_constraints @unique_constraints end |
Instance Method Details
#column(name) ⇒ Object
49 50 51 |
# File 'lib/rdbr/metadata/relation.rb', line 49 def column(name) columns.find{|column| column.name == name.to_s } end |
#keyless? ⇒ Boolean
61 62 63 |
# File 'lib/rdbr/metadata/relation.rb', line 61 def keyless? primary_key.nil? end |
#table? ⇒ Boolean
53 54 55 |
# File 'lib/rdbr/metadata/relation.rb', line 53 def table? kind == :table end |
#uniquely_constrained_column?(name) ⇒ Boolean
65 66 67 68 69 70 71 72 73 |
# File 'lib/rdbr/metadata/relation.rb', line 65 def uniquely_constrained_column?(name) column_name = name.to_s return true if primary_key&.columns == [ column_name ] return true if unique_constraints.any?{|constraint| constraint.columns == [ column_name ] } indexes.any? do |index| index.unique && !index.partial? && !index.expression? && index.columns == [ column_name ] end end |
#view? ⇒ Boolean
57 58 59 |
# File 'lib/rdbr/metadata/relation.rb', line 57 def view? [ :view, :materialized_view ].include?(kind) end |