Module: Sequel::MySQL::DatabaseMethods
- Included in:
- DataObjects::MySQL::DatabaseMethods, JDBC::MySQL::DatabaseMethods, Database, Sequel::Mysql2::Database, Swift::MySQL::DatabaseMethods
- Defined in:
- lib/sequel/adapters/shared/mysql.rb
Overview
Methods shared by Database instances that connect to MySQL, currently supported by the native and JDBC adapters.
Constant Summary collapse
- AUTO_INCREMENT =
'AUTO_INCREMENT'.freeze
- PRIMARY =
'PRIMARY'.freeze
- MYSQL_TIMESTAMP_RE =
/\ACURRENT_(?:DATE|TIMESTAMP)?\z/- CAST_TYPES =
.freeze # SEQUEL5
{String=>:CHAR, Integer=>:SIGNED, Time=>:DATETIME, DateTime=>:DATETIME, Numeric=>:DECIMAL, BigDecimal=>:DECIMAL, File=>:BINARY}
- COLUMN_DEFINITION_ORDER =
.freeze # SEQUEL5
[:collate, :null, :default, :unique, :primary_key, :auto_increment, :references]
Instance Method Summary collapse
-
#cast_type_literal(type) ⇒ Object
MySQL’s cast rules are restrictive in that you can’t just cast to any possible database type.
-
#commit_prepared_transaction(transaction_id, opts = OPTS) ⇒ Object
Commit an existing prepared transaction with the given transaction identifier string.
-
#database_type ⇒ Object
MySQL uses the :mysql database type.
-
#foreign_key_list(table, opts = OPTS) ⇒ Object
Use the Information Schema’s KEY_COLUMN_USAGE table to get basic information on foreign key columns, but include the constraint name.
- #freeze ⇒ Object
-
#global_index_namespace? ⇒ Boolean
MySQL namespaces indexes per table.
-
#indexes(table, opts = OPTS) ⇒ Object
Use SHOW INDEX FROM to get the index information for the table.
-
#rollback_prepared_transaction(transaction_id, opts = OPTS) ⇒ Object
Rollback an existing prepared transaction with the given transaction identifier string.
-
#server_version ⇒ Object
Get version of MySQL server, used for determined capabilities.
-
#supports_create_table_if_not_exists? ⇒ Boolean
MySQL supports CREATE TABLE IF NOT EXISTS syntax.
-
#supports_prepared_transactions? ⇒ Boolean
MySQL 5+ supports prepared transactions (two-phase commit) using XA.
-
#supports_savepoints? ⇒ Boolean
MySQL 5+ supports savepoints.
-
#supports_savepoints_in_prepared_transactions? ⇒ Boolean
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.
-
#supports_timestamp_usecs? ⇒ Boolean
Support fractional timestamps on MySQL 5.6.5+ if the :fractional_seconds Database option is used.
-
#supports_transaction_isolation_levels? ⇒ Boolean
MySQL supports transaction isolation levels.
-
#tables(opts = OPTS) ⇒ Object
Return an array of symbols specifying table names in the current database.
-
#use(db_name) ⇒ Object
Changes the database in use by issuing a USE statement.
-
#views(opts = OPTS) ⇒ Object
Return an array of symbols specifying view names in the current database.
Instance Method Details
#cast_type_literal(type) ⇒ Object
MySQL’s cast rules are restrictive in that you can’t just cast to any possible database type.
56 57 58 |
# File 'lib/sequel/adapters/shared/mysql.rb', line 56 def cast_type_literal(type) CAST_TYPES[type] || super end |
#commit_prepared_transaction(transaction_id, opts = OPTS) ⇒ Object
Commit an existing prepared transaction with the given transaction identifier string.
62 63 64 |
# File 'lib/sequel/adapters/shared/mysql.rb', line 62 def commit_prepared_transaction(transaction_id, opts=OPTS) run("XA COMMIT #{literal(transaction_id)}", opts) end |
#database_type ⇒ Object
MySQL uses the :mysql database type
67 68 69 |
# File 'lib/sequel/adapters/shared/mysql.rb', line 67 def database_type :mysql end |
#foreign_key_list(table, opts = OPTS) ⇒ Object
Use the Information Schema’s KEY_COLUMN_USAGE table to get basic information on foreign key columns, but include the constraint name.
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/sequel/adapters/shared/mysql.rb', line 74 def foreign_key_list(table, opts=OPTS) m = output_identifier_meth im = input_identifier_meth ds = . from(Sequel[:INFORMATION_SCHEMA][:KEY_COLUMN_USAGE]). where(:TABLE_NAME=>im.call(table), :TABLE_SCHEMA=>Sequel.function(:DATABASE)). exclude(:CONSTRAINT_NAME=>'PRIMARY'). exclude(:REFERENCED_TABLE_NAME=>nil). select(Sequel[:CONSTRAINT_NAME].as(:name), Sequel[:COLUMN_NAME].as(:column), Sequel[:REFERENCED_TABLE_NAME].as(:table), Sequel[:REFERENCED_COLUMN_NAME].as(:key)) h = {} ds.each do |row| if r = h[row[:name]] r[:columns] << m.call(row[:column]) r[:key] << m.call(row[:key]) else h[row[:name]] = {:name=>m.call(row[:name]), :columns=>[m.call(row[:column])], :table=>m.call(row[:table]), :key=>[m.call(row[:key])]} end end h.values end |
#freeze ⇒ Object
96 97 98 99 100 |
# File 'lib/sequel/adapters/shared/mysql.rb', line 96 def freeze server_version super end |
#global_index_namespace? ⇒ Boolean
MySQL namespaces indexes per table.
103 104 105 |
# File 'lib/sequel/adapters/shared/mysql.rb', line 103 def global_index_namespace? false end |
#indexes(table, opts = OPTS) ⇒ Object
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.
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/sequel/adapters/shared/mysql.rb', line 112 def indexes(table, opts=OPTS) indexes = {} remove_indexes = [] m = output_identifier_meth im = input_identifier_meth schema, table = schema_and_table(table) table = Sequel::SQL::Identifier.new(table) sql = "SHOW INDEX FROM #{literal(table)}" if schema schema = Sequel::SQL::Identifier.new(schema) sql += " FROM #{literal(schema)}" end .with_sql(sql).each do |r| name = r[:Key_name] next if name == 'PRIMARY' name = m.call(name) remove_indexes << name if r[:Sub_part] && ! opts[:partial] i = indexes[name] ||= {:columns=>[], :unique=>r[:Non_unique] != 1} i[:columns] << m.call(r[:Column_name]) end indexes.reject{|k,v| remove_indexes.include?(k)} end |
#rollback_prepared_transaction(transaction_id, opts = OPTS) ⇒ Object
Rollback an existing prepared transaction with the given transaction identifier string.
139 140 141 |
# File 'lib/sequel/adapters/shared/mysql.rb', line 139 def rollback_prepared_transaction(transaction_id, opts=OPTS) run("XA ROLLBACK #{literal(transaction_id)}", opts) end |
#server_version ⇒ Object
Get version of MySQL server, used for determined capabilities.
144 145 146 147 148 149 |
# File 'lib/sequel/adapters/shared/mysql.rb', line 144 def server_version @server_version ||= begin m = /(\d+)\.(\d+)\.(\d+)/.match(get(SQL::Function.new(:version))) (m[1].to_i * 10000) + (m[2].to_i * 100) + m[3].to_i end end |
#supports_create_table_if_not_exists? ⇒ Boolean
MySQL supports CREATE TABLE IF NOT EXISTS syntax.
152 153 154 |
# File 'lib/sequel/adapters/shared/mysql.rb', line 152 def supports_create_table_if_not_exists? true end |
#supports_prepared_transactions? ⇒ Boolean
MySQL 5+ supports prepared transactions (two-phase commit) using XA
157 158 159 |
# File 'lib/sequel/adapters/shared/mysql.rb', line 157 def supports_prepared_transactions? server_version >= 50000 end |
#supports_savepoints? ⇒ Boolean
MySQL 5+ supports savepoints
162 163 164 |
# File 'lib/sequel/adapters/shared/mysql.rb', line 162 def supports_savepoints? server_version >= 50000 end |
#supports_savepoints_in_prepared_transactions? ⇒ Boolean
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
168 169 170 |
# File 'lib/sequel/adapters/shared/mysql.rb', line 168 def supports_savepoints_in_prepared_transactions? super && (server_version <= 50512 || server_version >= 50523) end |
#supports_timestamp_usecs? ⇒ Boolean
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.
176 177 178 179 |
# File 'lib/sequel/adapters/shared/mysql.rb', line 176 def return if defined?() = server_version >= 50605 && typecast_value_boolean(opts[:fractional_seconds]) end |
#supports_transaction_isolation_levels? ⇒ Boolean
MySQL supports transaction isolation levels
182 183 184 |
# File 'lib/sequel/adapters/shared/mysql.rb', line 182 def supports_transaction_isolation_levels? true end |
#tables(opts = OPTS) ⇒ Object
Return an array of symbols specifying table names in the current database.
Options:
- :server
-
Set the server to use
190 191 192 |
# File 'lib/sequel/adapters/shared/mysql.rb', line 190 def tables(opts=OPTS) full_tables('BASE TABLE', opts) end |
#use(db_name) ⇒ Object
Changes the database in use by issuing a USE statement. I would be very careful if I used this.
196 197 198 199 200 201 202 |
# File 'lib/sequel/adapters/shared/mysql.rb', line 196 def use(db_name) Sequel::Deprecation.deprecate("Database#use", "Create a new Sequel::Database instance instead of using Database#use") disconnect @opts[:database] = db_name if self << "USE #{db_name}" @schemas = {} self end |
#views(opts = OPTS) ⇒ Object
Return an array of symbols specifying view names in the current database.
Options:
- :server
-
Set the server to use
208 209 210 |
# File 'lib/sequel/adapters/shared/mysql.rb', line 208 def views(opts=OPTS) full_tables('VIEW', opts) end |