Module: Sequel::MySQL::DatabaseMethods
- Included in:
- JDBC::MySQL::DatabaseMethods, Database
- 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
- 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.
-
#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(server = nil) ⇒ 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.
16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/sequel_core/adapters/shared/mysql.rb', line 16 def alter_table_sql(table, op) quoted_table = quote_identifier(table) quoted_name = quote_identifier(op[:name]) if op[:name] case op[:op] when :rename_column "ALTER TABLE #{quoted_table} CHANGE COLUMN #{quoted_name} #{quote_identifier(op[:new_name])} #{type_literal(op)}" when :set_column_type "ALTER TABLE #{quoted_table} CHANGE COLUMN #{quoted_name} #{quoted_name} #{type_literal(op)}" when :drop_index "#{drop_index_sql(table, op)} ON #{quoted_table}" else super(table, op) end end |
#auto_increment_sql ⇒ Object
Use MySQL specific AUTO_INCREMENT text.
32 33 34 |
# File 'lib/sequel_core/adapters/shared/mysql.rb', line 32 def auto_increment_sql AUTO_INCREMENT end |
#column_references_sql(column) ⇒ Object
Handle MySQL specific syntax for column references
37 38 39 |
# File 'lib/sequel_core/adapters/shared/mysql.rb', line 37 def column_references_sql(column) "#{", FOREIGN KEY (#{quote_identifier(column[:name])})" unless column[:type] == :check}#{super(column)}" end |
#index_definition_sql(table_name, index) ⇒ Object
Handle MySQL specific index SQL syntax
42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/sequel_core/adapters/shared/mysql.rb', line 42 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_identifier(table_name)} #{literal(index[:columns])}#{using}" end |
#server_version ⇒ Object
Get version of MySQL server, used for determined capabilities.
57 58 59 60 |
# File 'lib/sequel_core/adapters/shared/mysql.rb', line 57 def server_version m = /(\d+)\.(\d+)\.(\d+)/.match(get(:version[])) @server_version ||= (m[1].to_i * 10000) + (m[2].to_i * 100) + m[3].to_i end |
#tables(server = nil) ⇒ Object
Return an array of symbols specifying table names in the current database.
63 64 65 |
# File 'lib/sequel_core/adapters/shared/mysql.rb', line 63 def tables(server=nil) self['SHOW TABLES'].server(server).map{|r| r.values.first.to_sym} end |
#use(db_name) ⇒ Object
Changes the database in use by issuing a USE statement. I would be very careful if I used this.
69 70 71 72 73 74 |
# File 'lib/sequel_core/adapters/shared/mysql.rb', line 69 def use(db_name) disconnect @opts[:database] = db_name if self << "USE #{db_name}" @schemas = nil self end |