Module: DataMapper::Reflection::MysqlAdapter

Defined in:
lib/dm-reflection/adapters/mysql.rb

Instance Method Summary collapse

Instance Method Details

#get_properties(table) ⇒ Hash

TODO:

Consider returning actual DataMapper::Properties from this. It would probably require passing in a Model Object.

Get the column specifications for a specific table

Parameters:

  • table (String)

    the name of the table to get column specifications for

Returns:

  • (Hash)

    the column specs are returned in a hash keyed by ‘:name`, `:field`, `:type`, `:required`, `:default`, `:key`



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/dm-reflection/adapters/mysql.rb', line 87

def get_properties(table)
  # TODO: use SHOW INDEXES to find out unique and non-unique indexes
  join_table = false
  columns = select("SHOW COLUMNS FROM #{table} IN #{options[:path][1..-1]};")

  if columns.length == 2 && columns[0].field.downcase[-3,3] == "_id" && columns[1].field.downcase[-3,3] == "_id"
    left_table_name,right_table_name = join_table_name(table)
    join_table = true
  end
  
  columns.map do |column|
    type           = get_type(column.type)
    auto_increment = column.extra == 'auto_increment'

    if type == Integer && auto_increment
      type = DataMapper::Types::Serial
    end

    field_name = column.field.downcase

    attribute = {
      :name     => field_name,
      :type     => type,
      :required => column.null == 'NO',
      :default  => column.default,
      :key      => column.key == 'PRI',
    }
    
    # TODO: use the naming convention to compare the name vs the column name
    unless attribute[:name] == column.field
      attribute[:field] = column.field
    end
    
    if join_table
      attribute[:type] = :many_to_many
      attribute.delete(:default)
      attribute.delete(:key)
      attribute.delete(:required)
      attribute[:relationship] = {
        # M:M requires we wire things a bit differently and remove the join model
        :many_to_many => true,
        :parent_name => left_table_name.pluralize,
        :parent => ActiveSupport::Inflector.classify(left_table_name), 
        :child_name => right_table_name.pluralize,
        :child => ActiveSupport::Inflector.classify(right_table_name), 
        # When we can detect more from the database we can optimize this
        :cardinality => Infinity, 
        :bidirectional => true }      
        return [attribute]      
    elsif type == Integer && field_name[-3,3] == "_id"
      attribute.delete(:default)
      attribute.delete(:key)
      attribute.delete(:required)
      fixed_field_name = field_name[0..-4]
      unless table == ActiveSupport::Inflector.tableize(fixed_field_name)
        attribute[:type] = :belongs_to
        attribute[:name] = ActiveSupport::Inflector.singularize(fixed_field_name)
        attribute[:model] = ActiveSupport::Inflector.classify(attribute[:name])
        attribute[:other_side] = { 
          :name => ActiveSupport::Inflector.pluralize(table),
          :model => ActiveSupport::Inflector.camelize(table),
          # When we can detect more from the database we can optimize this
          :cardinality => Infinity }
      end
    end
    attribute
  end
end

#get_storage_namesString Array

Get the list of table names

Returns:

  • (String Array)

    the names of the tables in the database.



56
57
58
59
# File 'lib/dm-reflection/adapters/mysql.rb', line 56

def get_storage_names
  # This gets all the non view tables, but has to strip column 0 out of the two column response.
  select("SHOW FULL TABLES FROM #{options[:path][1..-1]} WHERE Table_type = 'BASE TABLE'").map { |item| item.first }
end

#get_type(db_type) ⇒ Type

TODO:

This should be verified to identify all mysql primitive types and that they map to the correct DataMapper/Ruby types.

Convert the database type into a DataMapper type

Parameters:

  • db_type (String)

    type specified by the database

Returns:

  • (Type)

    a DataMapper or Ruby type object.



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
# File 'lib/dm-reflection/adapters/mysql.rb', line 14

def get_type(db_type)
  # TODO: return a Hash with the :type, :min, :max and other
  # options rather than just the type

  db_type.match(/\A(\w+)/)
  {
    'tinyint'     =>  Integer    ,
    'smallint'    =>  Integer    ,
    'mediumint'   =>  Integer    ,
    'int'         =>  Integer    ,
    'bigint'      =>  Integer    ,
    'integer'     =>  Integer    ,
    'varchar'     =>  String     ,
    'char'        =>  String     ,
    'enum'        =>  String     ,
    'decimal'     =>  BigDecimal ,
    'double'      =>  Float      ,
    'float'       =>  Float      ,
    'datetime'    =>  DateTime   ,
    'timestamp'   =>  DateTime   ,
    'date'        =>  Date       ,
    'boolean'     =>  Types::Boolean,
    'tinyblob'    =>  Types::Text,
    'blob'        =>  Types::Text,
    'mediumblob'  =>  Types::Text,
    'longblob'    =>  Types::Text,
    'tinytext'    =>  Types::Text,
    'text'        =>  Types::Text,
    'mediumtext'  =>  Types::Text,
    'longtext'    =>  Types::Text,
  }[$1] || raise("unknown type: #{db_type}")
end

#join_table_name(name, name_list = nil) ⇒ String

This method breaks the join table into the two other table names

Parameters:

  • Name (String)

    join table name

Returns:

  • (String, String)

    The two other table names joined.



67
68
69
70
71
72
73
74
75
76
# File 'lib/dm-reflection/adapters/mysql.rb', line 67

def join_table_name(name, name_list=nil)
  name_list = get_storage_names.sort if name_list.nil?
  left = name_list[name_list.index(name)-1]
  right = name[left.length+1..-1]
  if name_list.include?(right)
    return left,right
  else
    return nil,nil
  end
end

#separatorObject



47
48
49
# File 'lib/dm-reflection/adapters/mysql.rb', line 47

def separator
  '--'
end