Class: Sequel::MySQL::Database
- Includes:
- DatabaseMethods
- Defined in:
- lib/sequel/adapters/mysql.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|Lost connection to MySQL server during query)/
Constants included from 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::CASCADE, Database::COLUMN_DEFINITION_ORDER, Database::COMMA_SEPARATOR, Database::MSSQL_DEFAULT_RE, Database::MYSQL_TIMESTAMP_RE, Database::NOT_NULL, Database::NO_ACTION, Database::NULL, Database::POSTGRES_DEFAULT_RE, Database::PRIMARY_KEY, Database::RESTRICT, Database::SET_DEFAULT, Database::SET_NULL, 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
Attributes inherited from Database
#default_schema, #log_warn_duration, #loggers, #opts, #pool, #prepared_statements, #sql_log_level, #transaction_isolation_level
Instance Method Summary collapse
-
#call_sproc(name, opts = {}, &block) ⇒ Object
Support stored procedures on MySQL.
-
#connect(server) ⇒ Object
Connect to the database.
-
#dataset(opts = nil) ⇒ Object
Returns instance of Sequel::MySQL::Dataset with the given options.
-
#execute(sql, opts = {}, &block) ⇒ Object
Executes the given SQL using an available connection, yielding the connection if the block is given.
-
#server_version(server = nil) ⇒ Object
Return the version of the MySQL server two which we are connecting.
Methods included from DatabaseMethods
#cast_type_literal, #commit_prepared_transaction, #database_type, #indexes, #rollback_prepared_transaction, #supports_prepared_transactions?, #supports_savepoints?, #supports_transaction_isolation_levels?, #tables, #use
Methods inherited from Database
#<<, #[], adapter_class, adapter_scheme, #adapter_scheme, #add_column, #add_index, #add_servers, #alter_table, #call, #cast_type_literal, connect, #create_or_replace_view, #create_table, #create_table!, #create_table?, #create_view, #database_type, #disconnect, #drop_column, #drop_index, #drop_table, #drop_view, #dump_indexes_migration, #dump_schema_migration, #dump_table_schema, #each_server, #execute_ddl, #execute_dui, #execute_insert, #fetch, #from, #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=, #indexes, #initialize, #inspect, #literal, #log_info, #log_yield, #logger=, #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, single_threaded=, #single_threaded?, #supports_prepared_transactions?, #supports_savepoints?, #supports_transaction_isolation_levels?, #synchronize, #table_exists?, #tables, #test_connection, #transaction, #typecast_value, #uri, #url
Methods included from Sequel::Metaprogramming
Constructor Details
This class inherits a constructor from Sequel::Database
Instance Method Details
#call_sproc(name, opts = {}, &block) ⇒ Object
Support stored procedures on MySQL
94 95 96 97 |
# File 'lib/sequel/adapters/mysql.rb', line 94 def call_sproc(name, opts={}, &block) args = opts[:args] || [] execute("CALL #{name}#{args.empty? ? '()' : literal(args)}", opts.merge(:sproc=>false), &block) end |
#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)
-
:compress - Set to false to not compress results from the server
-
: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).
-
:read_timeout - Set the timeout in seconds for reading back results to a query.
-
: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).
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 |
# File 'lib/sequel/adapters/mysql.rb', line 120 def connect(server) opts = server_opts(server) conn = Mysql.init conn.(Mysql::READ_DEFAULT_GROUP, opts[:config_default_group] || "client") conn.(Mysql::OPT_LOCAL_INFILE, opts[:config_local_infile]) if opts.has_key?(:config_local_infile) conn.ssl_set(opts[:sslkey], opts[:sslcert], opts[:sslca], opts[:sslcapath], opts[:sslcipher]) if opts[:sslca] || opts[:sslkey] if encoding = opts[:encoding] || opts[:charset] # Set encoding before connecting so that the mysql driver knows what # encoding we want to use, but this can be overridden by READ_DEFAULT_GROUP. conn.(Mysql::SET_CHARSET_NAME, encoding) end if read_timeout = opts[:read_timeout] and defined? Mysql::OPT_READ_TIMEOUT conn.(Mysql::OPT_READ_TIMEOUT, read_timeout) end if connect_timeout = opts[:connect_timeout] and defined? Mysql::OPT_CONNECT_TIMEOUT conn.(Mysql::OPT_CONNECT_TIMEOUT, connect_timeout) end conn.real_connect( opts[:host] || 'localhost', opts[:user], opts[:password], opts[:database], opts[:port], opts[:socket], Mysql::CLIENT_MULTI_RESULTS + Mysql::CLIENT_MULTI_STATEMENTS + (opts[:compress] == false ? 0 : Mysql::CLIENT_COMPRESS) ) 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. sqls << "SET NAMES #{literal(encoding.to_s)}" if encoding # 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)}} class << conn attr_accessor :prepared_statements end conn.prepared_statements = {} conn end |
#dataset(opts = nil) ⇒ Object
Returns instance of Sequel::MySQL::Dataset with the given options.
172 173 174 |
# File 'lib/sequel/adapters/mysql.rb', line 172 def dataset(opts = nil) MySQL::Dataset.new(self, opts) end |
#execute(sql, opts = {}, &block) ⇒ Object
Executes the given SQL using an available connection, yielding the connection if the block is given.
178 179 180 181 182 183 184 185 186 |
# File 'lib/sequel/adapters/mysql.rb', line 178 def execute(sql, opts={}, &block) if opts[:sproc] call_sproc(sql, opts, &block) elsif sql.is_a?(Symbol) execute_prepared_statement(sql, opts, &block) else synchronize(opts[:server]){|conn| _execute(conn, sql, opts, &block)} end end |
#server_version(server = nil) ⇒ Object
Return the version of the MySQL server two which we are connecting.
189 190 191 |
# File 'lib/sequel/adapters/mysql.rb', line 189 def server_version(server=nil) @server_version ||= (synchronize(server){|conn| conn.server_version if conn.respond_to?(:server_version)} || super) end |