Module Sequel::MySQL::DatabaseMethods
In: lib/sequel/adapters/shared/mysql.rb

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

Methods

Constants

AUTO_INCREMENT = 'AUTO_INCREMENT'.freeze
CAST_TYPES = {String=>:CHAR, Integer=>:SIGNED, Time=>:DATETIME, DateTime=>:DATETIME, Numeric=>:DECIMAL, BigDecimal=>:DECIMAL, File=>:BINARY}
COLUMN_DEFINITION_ORDER = [:collate, :null, :default, :unique, :primary_key, :auto_increment, :references]
PRIMARY = 'PRIMARY'.freeze

Public Instance methods

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

[Source]

    # File lib/sequel/adapters/shared/mysql.rb, line 39
39:       def cast_type_literal(type)
40:         CAST_TYPES[type] || super
41:       end

Commit an existing prepared transaction with the given transaction identifier string.

[Source]

    # File lib/sequel/adapters/shared/mysql.rb, line 45
45:       def commit_prepared_transaction(transaction_id)
46:         run("XA COMMIT #{literal(transaction_id)}")
47:       end

MySQL uses the :mysql database type

[Source]

    # File lib/sequel/adapters/shared/mysql.rb, line 50
50:       def database_type
51:         :mysql
52:       end

Use the Information Schema‘s KEY_COLUMN_USAGE table to get basic information on foreign key columns, but include the constraint name.

[Source]

    # File lib/sequel/adapters/shared/mysql.rb, line 57
57:       def foreign_key_list(table, opts={})
58:         m = output_identifier_meth
59:         im = input_identifier_meth
60:         ds = metadata_dataset.
61:           from(:INFORMATION_SCHEMA__KEY_COLUMN_USAGE).
62:           where(:TABLE_NAME=>im.call(table)).
63:           exclude(:CONSTRAINT_NAME=>'PRIMARY').
64:           exclude(:REFERENCED_TABLE_NAME=>nil).
65:           select(:CONSTRAINT_NAME___name, :COLUMN_NAME___column, :REFERENCED_TABLE_NAME___table, :REFERENCED_COLUMN_NAME___key)
66:         
67:         h = {}
68:         ds.each do |row|
69:           if r = h[row[:name]]
70:             r[:columns] << m.call(row[:column])
71:             r[:key] << m.call(row[:key])
72:           else
73:             h[row[:name]] = {:name=>m.call(row[:name]), :columns=>[m.call(row[:column])], :table=>m.call(row[:table]), :key=>[m.call(row[:key])]}
74:           end
75:         end
76:         h.values
77:       end

MySQL namespaces indexes per table.

[Source]

    # File lib/sequel/adapters/shared/mysql.rb, line 80
80:       def global_index_namespace?
81:         false
82:       end

Use SHOW INDEX FROM to get the index information for the table.

By default partial indexes are not included, you can use the option :partial to override this.

[Source]

     # File lib/sequel/adapters/shared/mysql.rb, line 89
 89:       def indexes(table, opts={})
 90:         indexes = {}
 91:         remove_indexes = []
 92:         m = output_identifier_meth
 93:         im = input_identifier_meth
 94:         metadata_dataset.with_sql("SHOW INDEX FROM ?", SQL::Identifier.new(im.call(table))).each do |r|
 95:           name = r[:Key_name]
 96:           next if name == PRIMARY
 97:           name = m.call(name)
 98:           remove_indexes << name if r[:Sub_part] && ! opts[:partial]
 99:           i = indexes[name] ||= {:columns=>[], :unique=>r[:Non_unique] != 1}
100:           i[:columns] << m.call(r[:Column_name])
101:         end
102:         indexes.reject{|k,v| remove_indexes.include?(k)}
103:       end

Rollback an existing prepared transaction with the given transaction identifier string.

[Source]

     # File lib/sequel/adapters/shared/mysql.rb, line 107
107:       def rollback_prepared_transaction(transaction_id)
108:         run("XA ROLLBACK #{literal(transaction_id)}")
109:       end

Get version of MySQL server, used for determined capabilities.

[Source]

     # File lib/sequel/adapters/shared/mysql.rb, line 112
112:       def server_version
113:         m = /(\d+)\.(\d+)\.(\d+)/.match(get(SQL::Function.new(:version)))
114:         @server_version ||= (m[1].to_i * 10000) + (m[2].to_i * 100) + m[3].to_i
115:       end

MySQL supports CREATE TABLE IF NOT EXISTS syntax.

[Source]

     # File lib/sequel/adapters/shared/mysql.rb, line 118
118:       def supports_create_table_if_not_exists?
119:         true
120:       end

MySQL supports prepared transactions (two-phase commit) using XA

[Source]

     # File lib/sequel/adapters/shared/mysql.rb, line 123
123:       def supports_prepared_transactions?
124:         server_version >= 50000
125:       end

MySQL supports savepoints

[Source]

     # File lib/sequel/adapters/shared/mysql.rb, line 128
128:       def supports_savepoints?
129:         server_version >= 50000
130:       end

MySQL doesn‘t support savepoints inside prepared transactions in from 5.5.12 to 5.5.23, see bugs.mysql.com/bug.php?id=64374

[Source]

     # File lib/sequel/adapters/shared/mysql.rb, line 134
134:       def supports_savepoints_in_prepared_transactions?
135:         super && (server_version <= 50512 || server_version >= 50523)
136:       end

MySQL supports transaction isolation levels

[Source]

     # File lib/sequel/adapters/shared/mysql.rb, line 139
139:       def supports_transaction_isolation_levels?
140:         true
141:       end

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

Options:

  • :server - Set the server to use

[Source]

     # File lib/sequel/adapters/shared/mysql.rb, line 147
147:       def tables(opts={})
148:         full_tables('BASE TABLE', opts)
149:       end

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

[Source]

     # File lib/sequel/adapters/shared/mysql.rb, line 153
153:       def use(db_name)
154:         disconnect
155:         @opts[:database] = db_name if self << "USE #{db_name}"
156:         @schemas = {}
157:         self
158:       end

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

Options:

  • :server - Set the server to use

[Source]

     # File lib/sequel/adapters/shared/mysql.rb, line 164
164:       def views(opts={})
165:         full_tables('VIEW', opts)
166:       end

[Validate]