Class: Sequel::Mysql2::Database
- Inherits:
-
Database
- Object
- Database
- Sequel::Mysql2::Database
- Includes:
- Sequel::MySQL::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)/
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.
Instance Method Details
#call_sproc(name, opts = {}, &block) ⇒ Object
Support stored procedures on MySQL
54 55 56 57 |
# File 'lib/sequel/adapters/mysql2.rb', line 54 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.
-
: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.
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/sequel/adapters/mysql2.rb', line 76 def connect(server) opts = server_opts(server) conn = ::Mysql2::Client.new({ :host => opts[:host] || 'localhost', :username => opts[:user], :password => opts[:password], :database => opts[:database], :port => opts[:port], :socket => opts[:socket] }) # increase timeout so mysql server doesn't disconnect us conn.query("set @@wait_timeout = #{opts[:timeout] || 2592000}") # By default, MySQL 'where id is null' selects the last inserted id conn.query("set SQL_AUTO_IS_NULL=0") unless opts[:auto_is_null] conn end |
#dataset(opts = nil) ⇒ Object
Returns instance of Sequel::MySQL::Dataset with the given options.
97 98 99 |
# File 'lib/sequel/adapters/mysql2.rb', line 97 def dataset(opts = nil) Mysql2::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.
103 104 105 106 107 108 109 |
# File 'lib/sequel/adapters/mysql2.rb', line 103 def execute(sql, opts={}, &block) if opts[:sproc] call_sproc(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.
112 113 114 |
# File 'lib/sequel/adapters/mysql2.rb', line 112 def server_version(server=nil) @server_version ||= (synchronize(server){|conn| conn.info[:id]}) end |