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

Included Modules

Sequel::Database::SplitAlterTable

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
MYSQL_TIMESTAMP_RE = /\ACURRENT_(?:DATE|TIMESTAMP)?\z/
DATABASE_ERROR_REGEXPS = { /Duplicate entry .+ for key/ => UniqueConstraintViolation, /foreign key constraint fails/ => ForeignKeyConstraintViolation, /cannot be null/ => NotNullConstraintViolation, /Deadlock found when trying to get lock; try restarting transaction/ => SerializationFailure, }.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 46
46:       def cast_type_literal(type)
47:         CAST_TYPES[type] || super
48:       end

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

[Source]

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

MySQL uses the :mysql database type

[Source]

    # File lib/sequel/adapters/shared/mysql.rb, line 57
57:       def database_type
58:         :mysql
59:       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 64
64:       def foreign_key_list(table, opts={})
65:         m = output_identifier_meth
66:         im = input_identifier_meth
67:         ds = metadata_dataset.
68:           from(:INFORMATION_SCHEMA__KEY_COLUMN_USAGE).
69:           where(:TABLE_NAME=>im.call(table), :TABLE_SCHEMA=>Sequel.function(:DATABASE)).
70:           exclude(:CONSTRAINT_NAME=>'PRIMARY').
71:           exclude(:REFERENCED_TABLE_NAME=>nil).
72:           select(:CONSTRAINT_NAME___name, :COLUMN_NAME___column, :REFERENCED_TABLE_NAME___table, :REFERENCED_COLUMN_NAME___key)
73:         
74:         h = {}
75:         ds.each do |row|
76:           if r = h[row[:name]]
77:             r[:columns] << m.call(row[:column])
78:             r[:key] << m.call(row[:key])
79:           else
80:             h[row[:name]] = {:name=>m.call(row[:name]), :columns=>[m.call(row[:column])], :table=>m.call(row[:table]), :key=>[m.call(row[:key])]}
81:           end
82:         end
83:         h.values
84:       end

MySQL namespaces indexes per table.

[Source]

    # File lib/sequel/adapters/shared/mysql.rb, line 87
87:       def global_index_namespace?
88:         false
89:       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 96
 96:       def indexes(table, opts={})
 97:         indexes = {}
 98:         remove_indexes = []
 99:         m = output_identifier_meth
100:         im = input_identifier_meth
101:         metadata_dataset.with_sql("SHOW INDEX FROM ?", SQL::Identifier.new(im.call(table))).each do |r|
102:           name = r[:Key_name]
103:           next if name == PRIMARY
104:           name = m.call(name)
105:           remove_indexes << name if r[:Sub_part] && ! opts[:partial]
106:           i = indexes[name] ||= {:columns=>[], :unique=>r[:Non_unique] != 1}
107:           i[:columns] << m.call(r[:Column_name])
108:         end
109:         indexes.reject{|k,v| remove_indexes.include?(k)}
110:       end

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

[Source]

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

Get version of MySQL server, used for determined capabilities.

[Source]

     # File lib/sequel/adapters/shared/mysql.rb, line 119
119:       def server_version
120:         @server_version ||= begin
121:           m = /(\d+)\.(\d+)\.(\d+)/.match(get(SQL::Function.new(:version)))
122:           (m[1].to_i * 10000) + (m[2].to_i * 100) + m[3].to_i
123:         end
124:       end

MySQL supports CREATE TABLE IF NOT EXISTS syntax.

[Source]

     # File lib/sequel/adapters/shared/mysql.rb, line 127
127:       def supports_create_table_if_not_exists?
128:         true
129:       end

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

[Source]

     # File lib/sequel/adapters/shared/mysql.rb, line 132
132:       def supports_prepared_transactions?
133:         server_version >= 50000
134:       end

MySQL supports savepoints

[Source]

     # File lib/sequel/adapters/shared/mysql.rb, line 137
137:       def supports_savepoints?
138:         server_version >= 50000
139:       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 143
143:       def supports_savepoints_in_prepared_transactions?
144:         super && (server_version <= 50512 || server_version >= 50523)
145:       end

MySQL supports transaction isolation levels

[Source]

     # File lib/sequel/adapters/shared/mysql.rb, line 148
148:       def supports_transaction_isolation_levels?
149:         true
150:       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 156
156:       def tables(opts={})
157:         full_tables('BASE TABLE', opts)
158:       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 162
162:       def use(db_name)
163:         disconnect
164:         @opts[:database] = db_name if self << "USE #{db_name}"
165:         @schemas = {}
166:         self
167:       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 173
173:       def views(opts={})
174:         full_tables('VIEW', opts)
175:       end

[Validate]