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 = [: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, }.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:           select(Sequel[:CONSTRAINT_NAME].as(:name), Sequel[:COLUMN_NAME].as(:column), Sequel[:REFERENCED_TABLE_NAME].as(:table), Sequel[:REFERENCED_COLUMN_NAME].as(:key))
62:         
63:         h = {}
64:         ds.each do |row|
65:           if r = h[row[:name]]
66:             r[:columns] << m.call(row[:column])
67:             r[:key] << m.call(row[:key])
68:           else
69:             h[row[:name]] = {:name=>m.call(row[:name]), :columns=>[m.call(row[:column])], :table=>m.call(row[:table]), :key=>[m.call(row[:key])]}
70:           end
71:         end
72:         h.values
73:       end

[Source]

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

MySQL namespaces indexes per table.

[Source]

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

[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:         return @supports_timestamp_usecs if defined?(@supports_timestamp_usecs)
154:         @supports_timestamp_usecs = server_version >= 50605 && typecast_value_boolean(opts[:fractional_seconds])
155:       end

MySQL supports transaction isolation levels

[Source]

     # File lib/sequel/adapters/shared/mysql.rb, line 158
158:       def supports_transaction_isolation_levels?
159:         true
160:       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 166
166:       def tables(opts=OPTS)
167:         full_tables('BASE TABLE', opts)
168:       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 174
174:       def views(opts=OPTS)
175:         full_tables('VIEW', opts)
176:       end

[Validate]