Module: Sequel::MySQL::DatabaseMethods

Included in:
DataObjects::MySQL::DatabaseMethods, JDBC::MySQL::DatabaseMethods, Database
Defined in:
lib/sequel/adapters/shared/mysql.rb

Overview

Methods shared by Database instances that connect to MySQL, currently supported by the native and JDBC adapters.

Constant Summary collapse

AUTO_INCREMENT =
'AUTO_INCREMENT'.freeze
CAST_TYPES =
{String=>:CHAR, Integer=>:SIGNED, Time=>:DATETIME, DateTime=>:DATETIME, Numeric=>:DECIMAL, BigDecimal=>:DECIMAL, File=>:BINARY}
PRIMARY =
'PRIMARY'.freeze

Instance Method Summary collapse

Instance Method Details

#cast_type_literal(type) ⇒ Object

MySQL’s cast rules are restrictive in that you can’t just cast to any possible database type.



17
18
19
# File 'lib/sequel/adapters/shared/mysql.rb', line 17

def cast_type_literal(type)
  CAST_TYPES[type] || super
end

#database_typeObject

MySQL uses the :mysql database type



22
23
24
# File 'lib/sequel/adapters/shared/mysql.rb', line 22

def database_type
  :mysql
end

#indexes(table) ⇒ Object

Return a hash containing index information. Hash keys are index name symbols. Values are subhashes with two keys, :columns and :unique. The value of :columns is an array of symbols of column names. The value of :unique is true or false depending on if the index is unique.

Does not include the primary key index or indexes on partial keys.



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/sequel/adapters/shared/mysql.rb', line 32

def indexes(table)
  indexes = {}
  remove_indexes = []
  m = output_identifier_meth
  im = input_identifier_meth
  .with_sql("SHOW INDEX FROM ?", SQL::Identifier.new(im.call(table))).each do |r|
    name = r[:Key_name]
    next if name == PRIMARY
    name = m.call(name)
    remove_indexes << name if r[:Sub_part]
    i = indexes[name] ||= {:columns=>[], :unique=>r[:Non_unique] != 1}
    i[:columns] << m.call(r[:Column_name])
  end
  indexes.reject{|k,v| remove_indexes.include?(k)}
end

#server_versionObject

Get version of MySQL server, used for determined capabilities.



49
50
51
52
# File 'lib/sequel/adapters/shared/mysql.rb', line 49

def server_version
  m = /(\d+)\.(\d+)\.(\d+)/.match(get(SQL::Function.new(:version)))
  @server_version ||= (m[1].to_i * 10000) + (m[2].to_i * 100) + m[3].to_i
end

#supports_savepoints?Boolean

MySQL supports savepoints

Returns:

  • (Boolean)


64
65
66
# File 'lib/sequel/adapters/shared/mysql.rb', line 64

def supports_savepoints?
  true
end

#tables(opts = {}) ⇒ Object

Return an array of symbols specifying table names in the current database.

Options:

  • :server - Set the server to use



58
59
60
61
# File 'lib/sequel/adapters/shared/mysql.rb', line 58

def tables(opts={})
  m = output_identifier_meth
  .with_sql('SHOW TABLES').server(opts[:server]).map{|r| m.call(r.values.first)}
end

#use(db_name) ⇒ Object

Changes the database in use by issuing a USE statement. I would be very careful if I used this.



70
71
72
73
74
75
# File 'lib/sequel/adapters/shared/mysql.rb', line 70

def use(db_name)
  disconnect
  @opts[:database] = db_name if self << "USE #{db_name}"
  @schemas = {}
  self
end