Class: Sequel::Mysql2::Database

Inherits:
Database show all
Includes:
Sequel::MySQL::DatabaseMethods, Sequel::MySQL::PreparedStatements::DatabaseMethods
Defined in:
lib/sequel/adapters/mysql2.rb

Overview

Database class for MySQL databases used with Sequel.

Constant Summary collapse

MYSQL_DATABASE_DISCONNECT_ERRORS =

Mysql::Error messages that indicate the current connection should be disconnected

/\A(Commands out of sync; you can't run this command now|Can't connect to local MySQL server through socket|MySQL server has gone away|This connection is still waiting for a result, try again once you have the result)/
DatasetClass =
self

Constants included from Sequel::MySQL::DatabaseMethods

Sequel::MySQL::DatabaseMethods::AUTO_INCREMENT, Sequel::MySQL::DatabaseMethods::CAST_TYPES, Sequel::MySQL::DatabaseMethods::COLUMN_DEFINITION_ORDER, Sequel::MySQL::DatabaseMethods::PRIMARY

Constants inherited from Database

Database::ADAPTERS, Database::AUTOINCREMENT, Database::COLUMN_DEFINITION_ORDER, Database::COMMA_SEPARATOR, Database::DEFAULT_JOIN_TABLE_COLUMN_OPTIONS, Database::MSSQL_DEFAULT_RE, Database::MYSQL_TIMESTAMP_RE, Database::NOT_NULL, Database::NULL, Database::POSTGRES_DEFAULT_RE, Database::PRIMARY_KEY, Database::SQL_BEGIN, Database::SQL_COMMIT, Database::SQL_RELEASE_SAVEPOINT, Database::SQL_ROLLBACK, Database::SQL_ROLLBACK_TO_SAVEPOINT, Database::SQL_SAVEPOINT, Database::STRING_DEFAULT_RE, Database::TEMPORARY, Database::TRANSACTION_BEGIN, Database::TRANSACTION_COMMIT, Database::TRANSACTION_ISOLATION_LEVELS, Database::TRANSACTION_ROLLBACK, Database::UNDERSCORE, Database::UNIQUE, Database::UNSIGNED

Instance Attribute Summary collapse

Attributes inherited from Database

#cache_schema, #dataset_class, #default_schema, #log_warn_duration, #loggers, #opts, #pool, #prepared_statements, #sql_log_level, #timezone, #transaction_isolation_level

Instance Method Summary collapse

Methods included from Sequel::MySQL::PreparedStatements::DatabaseMethods

#call_sproc, #execute

Methods included from Sequel::MySQL::DatabaseMethods

#cast_type_literal, #commit_prepared_transaction, #database_type, #foreign_key_list, #indexes, #rollback_prepared_transaction, #supports_create_table_if_not_exists?, #supports_prepared_transactions?, #supports_savepoints?, #supports_savepoints_in_prepared_transactions?, #supports_transaction_isolation_levels?, #tables, #use, #views

Methods inherited from Database

#<<, #[], adapter_class, adapter_scheme, #adapter_scheme, #add_column, #add_index, #add_servers, #after_commit, #after_rollback, #alter_table, #call, #cast_type_literal, connect, #create_join_table, #create_or_replace_view, #create_table, #create_table!, #create_table?, #create_view, #database_type, #dataset, #disconnect, #drop_column, #drop_index, #drop_join_table, #drop_table, #drop_table?, #drop_view, #dump_foreign_key_migration, #dump_indexes_migration, #dump_schema_cache, #dump_schema_cache?, #dump_schema_migration, #dump_table_schema, #each_server, #execute, #execute_ddl, #extend_datasets, #fetch, #foreign_key_list, #from, #from_application_timestamp, #get, #identifier_input_method, identifier_input_method, #identifier_input_method=, identifier_input_method=, #identifier_output_method, identifier_output_method, #identifier_output_method=, identifier_output_method=, #in_transaction?, #indexes, #inspect, #literal, #load_schema_cache, #load_schema_cache?, #log_exception, #log_info, #log_yield, #logger=, #prepared_statement, #query, #quote_identifiers=, quote_identifiers=, #quote_identifiers?, #remove_servers, #rename_column, #rename_table, #run, #schema, #select, #serial_primary_key_options, #servers, #set_column_default, #set_column_type, #set_prepared_statement, single_threaded=, #single_threaded?, #supports_create_table_if_not_exists?, #supports_drop_table_if_exists?, #supports_prepared_transactions?, #supports_savepoints?, #supports_savepoints_in_prepared_transactions?, #supports_transaction_isolation_levels?, #supports_transactional_ddl?, #synchronize, #table_exists?, #tables, #test_connection, #to_application_timestamp, #transaction, #typecast_value, #uri, #url, #views

Methods included from Sequel::Metaprogramming

#meta_def

Constructor Details

#initialize(opts = {}) ⇒ Database

Set the convert_tinyint_to_bool setting based on the default value.



21
22
23
24
# File 'lib/sequel/adapters/mysql2.rb', line 21

def initialize(opts={})
  super
  self.convert_tinyint_to_bool = Sequel::MySQL.convert_tinyint_to_bool
end

Instance Attribute Details

#convert_tinyint_to_boolObject

Whether to convert tinyint columns to bool for this database



18
19
20
# File 'lib/sequel/adapters/mysql2.rb', line 18

def convert_tinyint_to_bool
  @convert_tinyint_to_bool
end

Instance Method Details

#connect(server) ⇒ Object

Connect to the database. In addition to the usual database options, the following options have effect:

  • :auto_is_null - Set to true to use MySQL default behavior of having a filter for an autoincrement column equals NULL to return the last inserted row.

  • :charset - Same as :encoding (:encoding takes precendence)

  • :config_default_group - The default group to read from the in the MySQL config file.

  • :config_local_infile - If provided, sets the Mysql::OPT_LOCAL_INFILE option on the connection with the given value.

  • :connect_timeout - Set the timeout in seconds before a connection attempt is abandoned.

  • :encoding - Set all the related character sets for this connection (connection, client, database, server, and results).

  • :socket - Use a unix socket file instead of connecting via TCP/IP.

  • :timeout - Set the timeout in seconds before the server will disconnect this connection (a.k.a. @@wait_timeout).



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/sequel/adapters/mysql2.rb', line 44

def connect(server)
  opts = server_opts(server)
  opts[:host] ||= 'localhost'
  opts[:username] ||= opts[:user]
  opts[:flags] = ::Mysql2::Client::FOUND_ROWS if ::Mysql2::Client.const_defined?(:FOUND_ROWS)
  conn = ::Mysql2::Client.new(opts)

  sqls = []
  # Set encoding a slightly different way after connecting,
  # in case the READ_DEFAULT_GROUP overrode the provided encoding.
  # Doesn't work across implicit reconnects, but Sequel doesn't turn on
  # that feature.
  if encoding = opts[:encoding] || opts[:charset]
    sqls << "SET NAMES #{conn.escape(encoding.to_s)}"
  end

  # Increase timeout so mysql server doesn't disconnect us.
  # Value used by default is maximum allowed value on Windows.
  sqls << "SET @@wait_timeout = #{opts[:timeout] || 2147483}"

  # By default, MySQL 'where id is null' selects the last inserted id
  sqls << "SET SQL_AUTO_IS_NULL=0" unless opts[:auto_is_null]

  sqls.each{|sql| log_yield(sql){conn.query(sql)}}

  add_prepared_statements_cache(conn)
  conn
end

#execute_dui(sql, opts = {}) ⇒ Object

Return the number of matched rows when executing a delete/update statement.



74
75
76
# File 'lib/sequel/adapters/mysql2.rb', line 74

def execute_dui(sql, opts={})
  execute(sql, opts){|c| return c.affected_rows}
end

#execute_insert(sql, opts = {}) ⇒ Object

Return the last inserted id when executing an insert statement.



79
80
81
# File 'lib/sequel/adapters/mysql2.rb', line 79

def execute_insert(sql, opts={})
  execute(sql, opts){|c| return c.last_id}
end

#server_version(server = nil) ⇒ Object

Return the version of the MySQL server two which we are connecting.



84
85
86
# File 'lib/sequel/adapters/mysql2.rb', line 84

def server_version(server=nil)
  @server_version ||= (synchronize(server){|conn| conn.server_info[:id]} || super)
end