Class: Spider::Model::Storage::Db::DbSchema

Inherits:
Schema show all
Defined in:
lib/spiderfw/model/storage/db/db_schema.rb

Overview

The class describind the DB table(s) associated to a model.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeDbSchema

Returns a new instance of DbSchema.



19
20
21
22
23
24
25
26
27
28
# File 'lib/spiderfw/model/storage/db/db_schema.rb', line 19

def initialize()
    super
    @columns = {}
    @foreign_keys = {}
    @junction_tables = {}
    @sequences = {}
    @pass = {}
    @foreign_key_constraints = []
    @order = []
end

Instance Attribute Details

#columnsObject (readonly)

An Hash of column definitions for each element: => {:name => ‘COLUMN_NAME’, :attributes => {…}}



12
13
14
# File 'lib/spiderfw/model/storage/db/db_schema.rb', line 12

def columns
  @columns
end

#foreign_keysObject (readonly)

An Hash of column definitions for foreign keys: => {:primary_key_name => {:name => ‘FOREIGN_COLUMN’, :attributes => {…}}}



15
16
17
# File 'lib/spiderfw/model/storage/db/db_schema.rb', line 15

def foreign_keys
  @foreign_keys
end

#passObject (readonly)

An Hash of elements without primary columns.



17
18
19
# File 'lib/spiderfw/model/storage/db/db_schema.rb', line 17

def pass
  @pass
end

#sequencesObject (readonly)

An Hash of db names for each named sequence.



9
10
11
# File 'lib/spiderfw/model/storage/db/db_schema.rb', line 9

def sequences
  @sequences
end

Instance Method Details

#attributes(element_name) ⇒ Object

Returns column attributes for given element name.



58
59
60
61
# File 'lib/spiderfw/model/storage/db/db_schema.rb', line 58

def attributes(element_name)
    return nil if (!@columns[element_name])
    return @columns[element_name].attributes
end

#column(element_name) ⇒ Object

Returns the column name for an element.



94
95
96
# File 'lib/spiderfw/model/storage/db/db_schema.rb', line 94

def column(element_name)
    return @columns[element_name]
end

#field(element_name) ⇒ Object

Returns the db column defined for the element.



43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/spiderfw/model/storage/db/db_schema.rb', line 43

def field(element_name)
    if (@columns[element_name])
        return @columns[element_name]
    elsif (@foreign_keys[element_name])
        unless @foreign_keys[element_name].length == 1 # FIXME!!
            raise SchemaException, "No single field for element #{element_name}" 
        end
        @foreign_keys[element_name].each do |key, val|
            return val
        end
    end
    return nil
end

#foreign_key_field(element_name, key_name) ⇒ Object

Returns the defined foreign key column for given element and primary key



71
72
73
74
# File 'lib/spiderfw/model/storage/db/db_schema.rb', line 71

def foreign_key_field(element_name, key_name)
    return nil unless @foreign_keys[element_name]
    return @foreign_keys[element_name][key_name]
end

#get_schemasObject

Returns a description of all the tables used by the model. Returns a struct in the form

{table_name => :columns => {
  'column_name' => {:type => 'column_type', :attributes => {:attr => true, :attr2 => 'some_val, ...}}
}, :attributes => {
  :primary_key => 'primary_key_column', ...
}}


151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/spiderfw/model/storage/db/db_schema.rb', line 151

def get_schemas
    schemas = {}
    schemas[@table.name] = {:columns => {}, :attributes => {}, :fields_order => @order}
    @columns.each do |element, column|
        schemas[@table.name][:columns][column.name] = {:type => column.type, :attributes => column.attributes}
    end
    @foreign_keys.each_key do |element|
        @foreign_keys[element].each do |key, column|
            schemas[@table.name][:columns][column.name] = {:type => column.type, :attributes => column.attributes}
        end
    end
    schemas[@table.name][:attributes][:primary_keys] = primary_keys.map{ |k| k.name }
    schemas[@table.name][:attributes][:foreign_key_constraints] = @foreign_key_constraints
    return schemas
end

#has_fields?(element_name) ⇒ Boolean

True if element_name has a defined column or foreign key.

Returns:

  • (Boolean)


85
86
87
# File 'lib/spiderfw/model/storage/db/db_schema.rb', line 85

def has_fields?(element_name)
    return (@columns[element_name] || @foreign_keys[element_name]) ? true : false
end

#has_foreign_fields?(element_name) ⇒ Boolean

Returns:

  • (Boolean)


89
90
91
# File 'lib/spiderfw/model/storage/db/db_schema.rb', line 89

def has_foreign_fields?(element_name)
    return @foreign_keys[element_name] ? true : false
end

#primary_keysObject

Primary key column(s)



168
169
170
# File 'lib/spiderfw/model/storage/db/db_schema.rb', line 168

def primary_keys
    @table.fields.select{ |f| f.primary_key? }
end

#qualified_field(element_name, qualifier = nil) ⇒ Object

Returns the column for element_name, prefixed with the table name.

Raises:



64
65
66
67
68
# File 'lib/spiderfw/model/storage/db/db_schema.rb', line 64

def qualified_field(element_name, qualifier=nil)
    raise SchemaException, "No DB field defined in table #{@table} for element #{element_name}" unless f = field(element_name)
    qualifier ||= @table.name
    return qualifier + '.' + f.name
end

#qualified_foreign_key_field(element_name, key_name) ⇒ Object

Returns table_name + ‘.’ + #foreign_key_field



77
78
79
80
81
82
# File 'lib/spiderfw/model/storage/db/db_schema.rb', line 77

def qualified_foreign_key_field(element_name, key_name)
    f = foreign_key_field(element_name, key_name)
    return f.expression if f.is_a?(FieldExpression)
    raise "No foreign key field for #{element_name} #{key_name} in #{@table}" unless f
    return @table.name + '.' + f.name
end

#sequence(name) ⇒ Object

Returns the db name of a named sequence.



140
141
142
# File 'lib/spiderfw/model/storage/db/db_schema.rb', line 140

def sequence(name)
    @sequences[name]
end

#set_column(element_name, field) ⇒ Object

Sets the column name for an element.



99
100
101
102
103
104
# File 'lib/spiderfw/model/storage/db/db_schema.rb', line 99

def set_column(element_name, field)
    field = Field.new(@table, field[:name], field[:type], field[:attributes] || {}) if field.is_a?(Hash)
    had_column = @columns[element_name]
    @columns[element_name] = field
    @order << field unless had_column
end

#set_foreign_key(element_name, element_key, field) ⇒ Object

Sets a foreign key to the primary key of an element.



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/spiderfw/model/storage/db/db_schema.rb', line 107

def set_foreign_key(element_name, element_key, field)
    if field.is_a?(Hash)
        field[:attributes] ||= {}
        field[:attributes][:expression] ||= field[:expression]
        if field[:attributes][:expression]
            field[:name] = "#{@table}_#{element_name}_#{element_key}".upcase
            field = FieldExpression.new(@table, field[:name], field[:type], field[:attributes] || {})
        else
            field = Field.new(@table, field[:name], field[:type], field[:attributes] || {}) 
        end
    end
    @foreign_keys[element_name] ||= {}
    had_fk = @foreign_keys[element_name][element_key]
    @foreign_keys[element_name][element_key] = field
    @order << field unless had_fk
end

#set_foreign_key_constraint(name, table, keys, options = {}) ⇒ Object



124
125
126
# File 'lib/spiderfw/model/storage/db/db_schema.rb', line 124

def set_foreign_key_constraint(name, table, keys, options={})
    @foreign_key_constraints << ForeignKeyConstraint.new(name, table, keys, options)
end

#set_nil(name) ⇒ Object

Sets that given element has no associated db field.



135
136
137
# File 'lib/spiderfw/model/storage/db/db_schema.rb', line 135

def set_nil(name)
    @pass[name] = true
end

#set_sequence(name, db_name) ⇒ Object

Sets the db name for a named sequence.



130
131
132
# File 'lib/spiderfw/model/storage/db/db_schema.rb', line 130

def set_sequence(name, db_name)
    @sequences[name] = db_name
end

#tableObject

Returns the main table name.



31
32
33
# File 'lib/spiderfw/model/storage/db/db_schema.rb', line 31

def table
    return @table
end

#table=(table) ⇒ Object Also known as: set_table

Sets the main table name.



36
37
38
39
# File 'lib/spiderfw/model/storage/db/db_schema.rb', line 36

def table=(table)
    table = Table.new(table) unless table.is_a?(Table)
    @table = table
end