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
SQL_BEGIN =
Sequel::Database::SQL_BEGIN
SQL_COMMIT =
Sequel::Database::SQL_COMMIT
SQL_ROLLBACK =
Sequel::Database::SQL_ROLLBACK
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.



19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/sequel_core/adapters/shared/mysql.rb', line 19

def alter_table_sql(table, op)
  type = type_literal(op[:type])
  type << '(255)' if type == 'varchar'
  case op[:op]
  when :rename_column
    "ALTER TABLE #{table} CHANGE COLUMN #{literal(op[:name])} #{literal(op[:new_name])} #{type}"
  when :set_column_type
    "ALTER TABLE #{table} CHANGE COLUMN #{literal(op[:name])} #{literal(op[:name])} #{type}"
  when :drop_index
    "DROP INDEX #{default_index_name(table, op[:columns])} ON #{table}"
  else
    super(table, op)
  end
end

#auto_increment_sqlObject

Use MySQL specific AUTO_INCREMENT text.



35
36
37
# File 'lib/sequel_core/adapters/shared/mysql.rb', line 35

def auto_increment_sql
  AUTO_INCREMENT
end

#column_definition_sql(column) ⇒ Object

Handle MySQL specific column syntax (not sure why).



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/sequel_core/adapters/shared/mysql.rb', line 40

def column_definition_sql(column)
  if column[:type] == :check
    return constraint_definition_sql(column)
  end
  sql = "#{literal(column[:name].to_sym)} #{TYPES[column[:type]]}"
  column[:size] ||= 255 if column[:type] == :varchar
  elements = column[:size] || column[:elements]
  sql << literal(Array(elements)) if elements
  sql << UNSIGNED if column[:unsigned]
  sql << UNIQUE if column[:unique]
  sql << NOT_NULL if column[:null] == false
  sql << NULL if column[:null] == true
  sql << " DEFAULT #{literal(column[:default])}" if column.include?(:default)
  sql << PRIMARY_KEY if column[:primary_key]
  sql << " #{auto_increment_sql}" if column[:auto_increment]
  if column[:table]
    sql << ", FOREIGN KEY (#{literal(column[:name].to_sym)}) REFERENCES #{column[:table]}"
    sql << literal(Array(column[:key])) if column[:key]
    sql << " ON DELETE #{on_delete_clause(column[:on_delete])}" if column[:on_delete]
  end
  sql
end

#index_definition_sql(table_name, index) ⇒ Object

Handle MySQL specific index SQL syntax



64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/sequel_core/adapters/shared/mysql.rb', line 64

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

#server_versionObject

Get version of MySQL server, used for determined capabilities.



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

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

#use(db_name) ⇒ Object

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



87
88
89
90
91
92
# File 'lib/sequel_core/adapters/shared/mysql.rb', line 87

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