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 47
47:       def cast_type_literal(type)
48:         CAST_TYPES[type] || super
49:       end

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

[Source]

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

MySQL uses the :mysql database type

[Source]

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

MySQL namespaces indexes per table.

[Source]

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

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

[Source]

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

Get version of MySQL server, used for determined capabilities.

[Source]

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

MySQL supports CREATE TABLE IF NOT EXISTS syntax.

[Source]

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

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

[Source]

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

MySQL 5+ supports savepoints

[Source]

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

Support fractional timestamps on MySQL 5.6.5+ if the :fractional_seconds Database option is used. Technically, MySQL 5.6.4+ supports them, but automatic initialization of datetime values wasn‘t supported to 5.6.5+, and this is related to that.

[Source]

     # File lib/sequel/adapters/shared/mysql.rb, line 152
152:       def supports_timestamp_usecs?
153:         @supports_timestamp_usecs ||= server_version >= 50605 && typecast_value_boolean(opts[:fractional_seconds])
154:       end

MySQL supports transaction isolation levels

[Source]

     # File lib/sequel/adapters/shared/mysql.rb, line 157
157:       def supports_transaction_isolation_levels?
158:         true
159:       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 165
165:       def tables(opts=OPTS)
166:         full_tables('BASE TABLE', opts)
167:       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 171
171:       def use(db_name)
172:         disconnect
173:         @opts[:database] = db_name if self << "USE #{db_name}"
174:         @schemas = {}
175:         self
176:       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 182
182:       def views(opts=OPTS)
183:         full_tables('VIEW', opts)
184:       end

[Validate]