Module: Sequel::MySQL::DatabaseMethods
- Defined in:
- lib/sequel_core/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
- NOT_NULL =
Sequel::Schema::SQL::NOT_NULL
- NULL =
Sequel::Schema::SQL::NULL
- PRIMARY_KEY =
Sequel::Schema::SQL::PRIMARY_KEY
- TYPES =
Sequel::Schema::SQL::TYPES.merge(DateTime=>'datetime', \ TrueClass=>'tinyint', FalseClass=>'tinyint')
- UNIQUE =
Sequel::Schema::SQL::UNIQUE
- UNSIGNED =
Sequel::Schema::SQL::UNSIGNED
Instance Method Summary collapse
-
#alter_table_sql(table, op) ⇒ Object
Use MySQL specific syntax for rename column, set column type, and drop index cases.
-
#auto_increment_sql ⇒ Object
Use MySQL specific AUTO_INCREMENT text.
-
#column_references_sql(column) ⇒ Object
Handle MySQL specific syntax for column references.
-
#create_table_sql_list(name, columns, indexes = nil, options = {}) ⇒ Object
Use MySQL specific syntax for engine type and character encoding.
-
#index_definition_sql(table_name, index) ⇒ Object
Handle MySQL specific index SQL syntax.
-
#server_version ⇒ Object
Get version of MySQL server, used for determined capabilities.
-
#tables(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.
Instance Method Details
#alter_table_sql(table, op) ⇒ Object
Use MySQL specific syntax for rename column, set column type, and drop index cases.
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/sequel_core/adapters/shared/mysql.rb', line 28 def alter_table_sql(table, op) case op[:op] when :add_column if = op.delete(:table) sql = super(table, op) op[:table] = [sql, "ALTER TABLE #{quote_schema_table(table)} ADD FOREIGN KEY (#{quote_identifier(op[:name])})#{default_column_references_sql(op)}"] else super(table, op) end when :rename_column "ALTER TABLE #{quote_schema_table(table)} CHANGE COLUMN #{quote_identifier(op[:name])} #{quote_identifier(op[:new_name])} #{type_literal(op)}" when :set_column_type "ALTER TABLE #{quote_schema_table(table)} CHANGE COLUMN #{quote_identifier(op[:name])} #{quote_identifier(op[:name])} #{type_literal(op)}" when :drop_index "#{drop_index_sql(table, op)} ON #{quote_schema_table(table)}" else super(table, op) end end |
#auto_increment_sql ⇒ Object
Use MySQL specific AUTO_INCREMENT text.
50 51 52 |
# File 'lib/sequel_core/adapters/shared/mysql.rb', line 50 def auto_increment_sql AUTO_INCREMENT end |
#column_references_sql(column) ⇒ Object
Handle MySQL specific syntax for column references
55 56 57 |
# File 'lib/sequel_core/adapters/shared/mysql.rb', line 55 def column_references_sql(column) "#{", FOREIGN KEY (#{quote_identifier(column[:name])})" unless column[:type] == :check}#{super(column)}" end |
#create_table_sql_list(name, columns, indexes = nil, options = {}) ⇒ Object
Use MySQL specific syntax for engine type and character encoding
60 61 62 63 64 65 66 67 |
# File 'lib/sequel_core/adapters/shared/mysql.rb', line 60 def create_table_sql_list(name, columns, indexes = nil, = {}) [:engine] = Sequel::MySQL.default_engine unless .include?(:engine) [:charset] = Sequel::MySQL.default_charset unless .include?(:charset) [:collate] = Sequel::MySQL.default_collate unless .include?(:collate) sql = ["CREATE TABLE #{quote_schema_table(name)} (#{column_list_sql(columns)})#{" ENGINE=#{[:engine]}" if [:engine]}#{" DEFAULT CHARSET=#{[:charset]}" if [:charset]}#{" DEFAULT COLLATE=#{[:collate]}" if [:collate]}"] sql.concat(index_list_sql_list(name, indexes)) if indexes && !indexes.empty? sql end |
#index_definition_sql(table_name, index) ⇒ Object
Handle MySQL specific index SQL syntax
70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/sequel_core/adapters/shared/mysql.rb', line 70 def index_definition_sql(table_name, index) index_name = quote_identifier(index[:name] || default_index_name(table_name, index[:columns])) index_type = case index[:type] when :full_text "FULLTEXT " when :spatial "SPATIAL " else using = " USING #{index[:type]}" unless index[:type] == nil "UNIQUE " if index[:unique] end "CREATE #{index_type}INDEX #{index_name} ON #{quote_schema_table(table_name)} #{literal(index[:columns])}#{using}" end |
#server_version ⇒ Object
Get version of MySQL server, used for determined capabilities.
85 86 87 88 |
# File 'lib/sequel_core/adapters/shared/mysql.rb', line 85 def server_version m = /(\d+)\.(\d+)\.(\d+)/.match(get(SQL::Function.new(:version))) @server_version ||= (m[1].to_i * 10000) + (m[2].to_i * 100) + m[3].to_i end |
#tables(opts = {}) ⇒ Object
Return an array of symbols specifying table names in the current database.
Options:
-
:server - Set the server to use
94 95 96 97 98 99 |
# File 'lib/sequel_core/adapters/shared/mysql.rb', line 94 def tables(opts={}) ds = self['SHOW TABLES'].server(opts[:server]) ds.identifier_output_method = nil ds2 = dataset ds.map{|r| ds2.send(:output_identifier, r.values.first)} end |
#use(db_name) ⇒ Object
Changes the database in use by issuing a USE statement. I would be very careful if I used this.
103 104 105 106 107 108 |
# File 'lib/sequel_core/adapters/shared/mysql.rb', line 103 def use(db_name) disconnect @opts[:database] = db_name if self << "USE #{db_name}" @schemas = nil self end |