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

Methods

Included Modules

UnmodifiedIdentifiers::DatabaseMethods Sequel::Database::SplitAlterTable

Constants

CAST_TYPES = {String=>:CHAR, Integer=>:SIGNED, Time=>:DATETIME, DateTime=>:DATETIME, Numeric=>:DECIMAL, BigDecimal=>:DECIMAL, File=>:BINARY}.freeze
COLUMN_DEFINITION_ORDER = [:generated, :collate, :null, :default, :unique, :primary_key, :auto_increment, :references].freeze
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, /CONSTRAINT .+ failed for/ => CheckConstraintViolation, /\AStatement aborted because lock\(s\) could not be acquired immediately and NOWAIT is set\./ => DatabaseLockTimeout, }.freeze

Attributes

default_charset  [RW]  Set the default charset used for CREATE TABLE. You can pass the :charset option to create_table to override this setting.
default_collate  [RW]  Set the default collation used for CREATE TABLE. You can pass the :collate option to create_table to override this setting.
default_engine  [RW]  Set the default engine used for CREATE TABLE. You can pass the :engine option to create_table to override this setting.

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 38
38:       def cast_type_literal(type)
39:         CAST_TYPES[type] || super
40:       end

[Source]

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

[Source]

    # File lib/sequel/adapters/shared/mysql.rb, line 46
46:       def database_type
47:         :mysql
48:       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 53
53:       def foreign_key_list(table, opts=OPTS)
54:         m = output_identifier_meth
55:         im = input_identifier_meth
56:         ds = metadata_dataset.
57:           from(Sequel[:INFORMATION_SCHEMA][:KEY_COLUMN_USAGE]).
58:           where(:TABLE_NAME=>im.call(table), :TABLE_SCHEMA=>Sequel.function(:DATABASE)).
59:           exclude(:CONSTRAINT_NAME=>'PRIMARY').
60:           exclude(:REFERENCED_TABLE_NAME=>nil).
61:           order(:CONSTRAINT_NAME, :POSITION_IN_UNIQUE_CONSTRAINT).
62:           select(Sequel[:CONSTRAINT_NAME].as(:name), Sequel[:COLUMN_NAME].as(:column), Sequel[:REFERENCED_TABLE_NAME].as(:table), Sequel[:REFERENCED_COLUMN_NAME].as(:key))
63:         
64:         h = {}
65:         ds.each do |row|
66:           if r = h[row[:name]]
67:             r[:columns] << m.call(row[:column])
68:             r[:key] << m.call(row[:key])
69:           else
70:             h[row[:name]] = {:name=>m.call(row[:name]), :columns=>[m.call(row[:column])], :table=>m.call(row[:table]), :key=>[m.call(row[:key])]}
71:           end
72:         end
73:         h.values
74:       end

[Source]

    # File lib/sequel/adapters/shared/mysql.rb, line 76
76:       def freeze
77:         server_version
78:         mariadb?
79:         supports_timestamp_usecs?
80:         super
81:       end

MySQL namespaces indexes per table.

[Source]

    # File lib/sequel/adapters/shared/mysql.rb, line 84
84:       def global_index_namespace?
85:         false
86:       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 93
 93:       def indexes(table, opts=OPTS)
 94:         indexes = {}
 95:         remove_indexes = []
 96:         m = output_identifier_meth
 97:         schema, table = schema_and_table(table)
 98: 
 99:         table = Sequel::SQL::Identifier.new(table)
100:         sql = "SHOW INDEX FROM #{literal(table)}"
101:         if schema
102:           schema = Sequel::SQL::Identifier.new(schema)
103:           sql += " FROM #{literal(schema)}"
104:         end
105: 
106:         metadata_dataset.with_sql(sql).each do |r|
107:           name = r[:Key_name]
108:           next if name == 'PRIMARY'
109:           name = m.call(name)
110:           remove_indexes << name if r[:Sub_part] && ! opts[:partial]
111:           i = indexes[name] ||= {:columns=>[], :unique=>r[:Non_unique] != 1}
112:           i[:columns] << m.call(r[:Column_name])
113:         end
114:         indexes.reject{|k,v| remove_indexes.include?(k)}
115:       end

Whether the database is MariaDB and not MySQL

[Source]

     # File lib/sequel/adapters/shared/mysql.rb, line 122
122:       def mariadb?
123:         return @is_mariadb if defined?(@is_mariadb)
124:         @is_mariadb = !(fetch('SELECT version()').single_value! !~ /mariadb/i)
125:       end

[Source]

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

Get version of MySQL server, used for determined capabilities.

[Source]

     # File lib/sequel/adapters/shared/mysql.rb, line 128
128:       def server_version
129:         @server_version ||= begin
130:           m = /(\d+)\.(\d+)\.(\d+)/.match(fetch('SELECT version()').single_value!)
131:           (m[1].to_i * 10000) + (m[2].to_i * 100) + m[3].to_i
132:         end
133:       end

MySQL supports CREATE TABLE IF NOT EXISTS syntax.

[Source]

     # File lib/sequel/adapters/shared/mysql.rb, line 136
136:       def supports_create_table_if_not_exists?
137:         true
138:       end

Generated columns are supported in MariaDB 5.2.0+ and MySQL 5.7.6+.

[Source]

     # File lib/sequel/adapters/shared/mysql.rb, line 141
141:       def supports_generated_columns?
142:         server_version >= (mariadb? ? 50200 : 50706)
143:       end

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

[Source]

     # File lib/sequel/adapters/shared/mysql.rb, line 146
146:       def supports_prepared_transactions?
147:         server_version >= 50000
148:       end

MySQL 5+ supports savepoints

[Source]

     # File lib/sequel/adapters/shared/mysql.rb, line 151
151:       def supports_savepoints?
152:         server_version >= 50000
153:       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 157
157:       def supports_savepoints_in_prepared_transactions?
158:         super && (server_version <= 50512 || server_version >= 50523)
159:       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 165
165:       def supports_timestamp_usecs?
166:         return @supports_timestamp_usecs if defined?(@supports_timestamp_usecs)
167:         @supports_timestamp_usecs = server_version >= 50605 && typecast_value_boolean(opts[:fractional_seconds])
168:       end

MySQL supports transaction isolation levels

[Source]

     # File lib/sequel/adapters/shared/mysql.rb, line 171
171:       def supports_transaction_isolation_levels?
172:         true
173:       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 179
179:       def tables(opts=OPTS)
180:         full_tables('BASE TABLE', opts)
181:       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 187
187:       def views(opts=OPTS)
188:         full_tables('VIEW', opts)
189:       end

[Validate]