Method: Engine2::Model#setup_schema

Defined in:
lib/engine2/model.rb

#setup_schemaObject



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/engine2/model.rb', line 44

def setup_schema
    @type_info = {}
    @dummies = []

    type_info do
        schema = @model.db_schema
        @model.primary_keys.each{|pk| (schema[pk]||={})[:primary_key] = true} if @model.primary_key

        schema.each_pair do |name, db_info|
            @info[name] = {}

            case db_info[:type]
            when :integer
                integer_field name
            when :string
                string_field name, case db_info[:db_type]
                when 'text', 'character varying'
                    100
                else
                    Integer(db_info[:column_size] || db_info[:db_type][/\((\d+)\)/, 1])
                end
            when :time
                time_field name, LOCS[:default_time_format], LOCS[:default_time_model_format]
            when :date
                date_field name, LOCS[:default_date_format], LOCS[:default_date_model_format]
            when :datetime
                datetime_field name, LOCS[:default_date_format], LOCS[:default_time_format], LOCS[:default_date_model_format], LOCS[:default_time_model_format]
            when :decimal
                size, scale = db_info[:column_size], db_info[:scale].to_i
                unless size && scale
                    db_info[:db_type] =~ /decimal\((\d+),(\d+)\)/i
                    size, scale = $1.to_i, $2.to_i
                    raise E2Error.new("Cannot parse decimal type for #{db_info}") unless size || scale
                end
                decimal_field name, size, scale
            when :blob
                blob_field name, 100000
            when :boolean
                boolean_field name
            when nil
                # ignore nil type
            else
                p db_info
                raise E2Error.new("Unknown column type: #{db_info[:type].inspect} for #{name}")
            end

            required name if !db_info[:allow_null]
            primary_key name if db_info[:primary_key]
            sequence name, "SEQ_#{@model.table_name}.nextVal" if db_info[:primary_key] && !db_info[:allow_null] && !db_info[:auto_increment] && !@model.natural_key
            default name, db_info[:ruby_default] if db_info[:ruby_default]
        end

        unique *@model.primary_keys if @model.natural_key && @model.db.adapter_scheme # uri ?

        @model.many_to_one_associations.each do |aname, assoc|
            many_to_one_field aname
            decode assoc[:keys].first
        end
    end
end