| Module | Sequel::MySQL::DatabaseMethods |
| In: |
lib/sequel/adapters/shared/mysql.rb
|
| 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 |
| 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. |
# 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
Use the Information Schema‘s KEY_COLUMN_USAGE table to get basic information on foreign key columns, but include the constraint name.
# 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
# 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
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.
# 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
# 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.
# 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 doesn‘t support savepoints inside prepared transactions in from 5.5.12 to 5.5.23, see bugs.mysql.com/bug.php?id=64374
# 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.
# 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
Return an array of symbols specifying table names in the current database.
Options:
| :server : | Set the server to use |
# File lib/sequel/adapters/shared/mysql.rb, line 179
179: def tables(opts=OPTS)
180: full_tables('BASE TABLE', opts)
181: end