Module: Sequel::MySQL::DatabaseMethods

Included in:
JDBC::MySQL::DatabaseMethods, Database
Defined in:
lib/sequel_core/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
NOT_NULL =
Sequel::Schema::SQL::NOT_NULL
NULL =
Sequel::Schema::SQL::NULL
PRIMARY_KEY =
Sequel::Schema::SQL::PRIMARY_KEY
TYPES =
Sequel::Schema::SQL::TYPES
UNIQUE =
Sequel::Schema::SQL::UNIQUE
UNSIGNED =
Sequel::Schema::SQL::UNSIGNED

Instance Method Summary collapse

Instance Method Details

#alter_table_sql(table, op) ⇒ Object

Use MySQL specific syntax for rename column, set column type, and drop index cases.



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/sequel_core/adapters/shared/mysql.rb', line 22

def alter_table_sql(table, op)
  case op[:op]
  when :add_column
    if related = op.delete(:table)
      sql = super(table, op)
      op[:table] = related
      [sql, "ALTER TABLE #{quote_schema_table(table)} ADD FOREIGN KEY (#{quote_identifier(op[:name])})#{default_column_references_sql(op)}"]
    else
      super(table, op)
    end
  when :rename_column
    "ALTER TABLE #{quote_schema_table(table)} CHANGE COLUMN #{quote_identifier(op[:name])} #{quote_identifier(op[:new_name])} #{type_literal(op)}"
  when :set_column_type
    "ALTER TABLE #{quote_schema_table(table)} CHANGE COLUMN #{quote_identifier(op[:name])} #{quote_identifier(op[:name])} #{type_literal(op)}"
  when :drop_index
    "#{drop_index_sql(table, op)} ON #{quote_schema_table(table)}"
  else
    super(table, op)
  end
end

#auto_increment_sqlObject

Use MySQL specific AUTO_INCREMENT text.



44
45
46
# File 'lib/sequel_core/adapters/shared/mysql.rb', line 44

def auto_increment_sql
  AUTO_INCREMENT
end

#column_references_sql(column) ⇒ Object

Handle MySQL specific syntax for column references



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

def column_references_sql(column)
  "#{", FOREIGN KEY (#{quote_identifier(column[:name])})" unless column[:type] == :check}#{super(column)}"
end

#index_definition_sql(table_name, index) ⇒ Object

Handle MySQL specific index SQL syntax



54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/sequel_core/adapters/shared/mysql.rb', line 54

def index_definition_sql(table_name, index)
  index_name = quote_identifier(index[:name] || default_index_name(table_name, index[:columns]))
  index_type = case index[:type]
  when :full_text
    "FULLTEXT "
  when :spatial
    "SPATIAL "
  else
    using = " USING #{index[:type]}" unless index[:type] == nil
    "UNIQUE " if index[:unique]
  end
  "CREATE #{index_type}INDEX #{index_name} ON #{quote_schema_table(table_name)} #{literal(index[:columns])}#{using}"
end

#server_versionObject

Get version of MySQL server, used for determined capabilities.



69
70
71
72
# File 'lib/sequel_core/adapters/shared/mysql.rb', line 69

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

#tables(server = nil) ⇒ Object

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



75
76
77
# File 'lib/sequel_core/adapters/shared/mysql.rb', line 75

def tables(server=nil)
  self['SHOW TABLES'].server(server).map{|r| r.values.first.to_sym}
end

#use(db_name) ⇒ Object

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



81
82
83
84
85
86
# File 'lib/sequel_core/adapters/shared/mysql.rb', line 81

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