module DmIsReflective::MysqlAdapter

Public Instance Methods

indices(storage) click to toggle source
# File lib/dm-is-reflective/adapters/mysql_adapter.rb, line 9
  def indices storage
    sql = <<-SQL
      SELECT column_name, index_name, non_unique
      FROM   `information_schema`.`statistics`
      WHERE  table_schema = ? AND table_name = ?
    SQL

    select(Ext::String.compress_lines(sql),
      reflective_table_schema, storage).group_by(&:column_name).
        inject({}) do |r, (column, idxs)|
          primary = idxs.find{ |i| i.index_name == 'PRIMARY' }
          primary.index_name = :"#{storage}_pkey" if primary
          key                = !!primary
          idx_uni, idx_com = idxs.partition{ |i| i.non_unique == 0 }.map{ |i|
            if i.empty?
              nil
            elsif i.size == 1
              i.first.index_name.to_sym
            else
              i.map{ |ii| ii.index_name.to_sym }
            end
          }

          r[column.to_sym] = reflective_indices_hash(key, idx_uni, idx_com)
          r
        end
  end
storages() click to toggle source
# File lib/dm-is-reflective/adapters/mysql_adapter.rb, line 5
def storages
  select('SHOW TABLES')
end

Private Instance Methods

reflective_attributes(field, attrs={}) click to toggle source
# File lib/dm-is-reflective/adapters/mysql_adapter.rb, line 64
def reflective_attributes field, attrs={}
  attrs.merge!(field.indices) if field.indices

  attrs[:serial] = true if field.extra == 'auto_increment'

  if field.column_key == 'PRI'
    attrs[:key] = true
    attrs[:unique_index] = :"#{field.table_name}_pkey"
  end

  attrs[:allow_nil] = field.is_nullable == 'YES'
  attrs[:default]   = field.column_default           if
    field.column_default

  attrs[:length]    = field.character_maximum_length if
    field.character_maximum_length

  attrs
end
reflective_field_name(field) click to toggle source
# File lib/dm-is-reflective/adapters/mysql_adapter.rb, line 56
def reflective_field_name field
  field.column_name
end
reflective_lookup_primitive(primitive) click to toggle source
Calls superclass method
# File lib/dm-is-reflective/adapters/mysql_adapter.rb, line 84
def reflective_lookup_primitive primitive
  case primitive.upcase
  when 'YEAR'                           ; Integer
  when /\w*INT(EGER)?( SIGNED| UNSIGNED)?( ZEROFILL)?/
                                        ; Integer
  when /(DOUBLE|FLOAT|DECIMAL)( SIGNED| UNSIGNED)?( ZEROFILL)?/
                                        ; Property::Decimal
  when /\w*BLOB|\w*BINARY|ENUM|SET|CHAR/; String
  when 'TIME'                           ; Time
  when 'DATE'                           ; Date
  when 'DATETIME', 'TIMESTAMP'          ; DateTime
  when 'BOOL', 'BOOLEAN'                ; Property::Boolean
  when /\w*TEXT/                        ; Property::Text
  end || super(primitive)
end
reflective_primitive(field) click to toggle source
# File lib/dm-is-reflective/adapters/mysql_adapter.rb, line 60
def reflective_primitive field
  field.data_type
end
reflective_query_storage(storage) click to toggle source

construct needed table metadata

# File lib/dm-is-reflective/adapters/mysql_adapter.rb, line 39
  def reflective_query_storage storage
    sql = <<-SQL
      SELECT column_name, column_key, column_default, is_nullable,
             data_type, character_maximum_length, extra, table_name
      FROM      `information_schema`.`columns`
      WHERE  table_schema = ? AND table_name = ?
    SQL

    idxs = indices(storage)

    select(Ext::String.compress_lines(sql),
      reflective_table_schema, storage).map do |f|
        f.define_singleton_method(:indices){ idxs[f.column_name.to_sym] }
        f
      end
  end
reflective_table_schema() click to toggle source
# File lib/dm-is-reflective/adapters/mysql_adapter.rb, line 100
def reflective_table_schema
  # TODO: can we fix this shit in dm-mysql-adapter?
  (options[:path]     || options['path'] ||
   options[:database] || options['database']).sub('/', '')
end