Class Sequel::MySQL::Database
In: lib/sequel/adapters/mysql.rb
Parent: Sequel::Database

Database class for MySQL databases used with Sequel.

Methods

Included Modules

Sequel::MySQL::DatabaseMethods

Constants

MYSQL_DATABASE_DISCONNECT_ERRORS = /\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)/   Mysql::Error messages that indicate the current connection should be disconnected

Public Instance methods

Support stored procedures on MySQL

[Source]

    # File lib/sequel/adapters/mysql.rb, line 69
69:       def call_sproc(name, opts={}, &block)
70:         args = opts[:args] || [] 
71:         execute("CALL #{name}#{args.empty? ? '()' : literal(args)}", opts.merge(:sproc=>false), &block)
72:       end

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.

[Source]

     # File lib/sequel/adapters/mysql.rb, line 91
 91:       def connect(server)
 92:         opts = server_opts(server)
 93:         conn = Mysql.init
 94:         conn.options(Mysql::READ_DEFAULT_GROUP, opts[:config_default_group] || "client")
 95:         conn.options(Mysql::OPT_LOCAL_INFILE, opts[:config_local_infile]) if opts.has_key?(:config_local_infile)
 96:         conn.ssl_set(opts[:sslkey], opts[:sslcert], opts[:sslca], opts[:sslcapath], opts[:sslcipher]) if opts[:sslca] || opts[:sslkey]
 97:         if encoding = opts[:encoding] || opts[:charset]
 98:           # Set encoding before connecting so that the mysql driver knows what
 99:           # encoding we want to use, but this can be overridden by READ_DEFAULT_GROUP.
100:           conn.options(Mysql::SET_CHARSET_NAME, encoding)
101:         end
102:         conn.real_connect(
103:           opts[:host] || 'localhost',
104:           opts[:user],
105:           opts[:password],
106:           opts[:database],
107:           opts[:port],
108:           opts[:socket],
109:           Mysql::CLIENT_MULTI_RESULTS +
110:           Mysql::CLIENT_MULTI_STATEMENTS +
111:           (opts[:compress] == false ? 0 : Mysql::CLIENT_COMPRESS)
112:         )
113:         sqls = []
114:         # Set encoding a slightly different way after connecting,
115:         # in case the READ_DEFAULT_GROUP overrode the provided encoding.
116:         # Doesn't work across implicit reconnects, but Sequel doesn't turn on
117:         # that feature.
118:         sqls << "SET NAMES #{literal(encoding.to_s)}" if encoding
119: 
120:         # Increase timeout so mysql server doesn't disconnect us
121:         # Value used by default is maximum allowed value on Windows.
122:         sqls << "SET @@wait_timeout = #{opts[:timeout] || 2147483}"
123: 
124:         # By default, MySQL 'where id is null' selects the last inserted id
125:         sqls << "SET SQL_AUTO_IS_NULL=0" unless opts[:auto_is_null]
126: 
127:         sqls.each{|sql| log_yield(sql){conn.query(sql)}}
128: 
129:         class << conn
130:           attr_accessor :prepared_statements
131:         end
132:         conn.prepared_statements = {}
133:         conn
134:       end

Returns instance of Sequel::MySQL::Dataset with the given options.

[Source]

     # File lib/sequel/adapters/mysql.rb, line 137
137:       def dataset(opts = nil)
138:         MySQL::Dataset.new(self, opts)
139:       end

Executes the given SQL using an available connection, yielding the connection if the block is given.

[Source]

     # File lib/sequel/adapters/mysql.rb, line 143
143:       def execute(sql, opts={}, &block)
144:         if opts[:sproc]
145:           call_sproc(sql, opts, &block)
146:         elsif sql.is_a?(Symbol)
147:           execute_prepared_statement(sql, opts, &block)
148:         else
149:           synchronize(opts[:server]){|conn| _execute(conn, sql, opts, &block)}
150:         end
151:       end

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

[Source]

     # File lib/sequel/adapters/mysql.rb, line 154
154:       def server_version(server=nil)
155:         @server_version ||= (synchronize(server){|conn| conn.server_version if conn.respond_to?(:server_version)} || super)
156:       end

[Validate]