class Sequel::MySQL::Database

Attributes

conversion_procs[R]

Hash of conversion procs for the current database

convert_invalid_date_time[R]

By default, Sequel raises an exception if in invalid date or time is used. However, if this is set to nil or :nil, the adapter treats dates like 0000-00-00 and times like 838:00:00 as nil values. If set to :string, it returns the strings as is.

convert_tinyint_to_bool[R]

Whether to convert tinyint columns to bool for the current database

Public Instance Methods

connect(server) click to toggle source

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).

    # File lib/sequel/adapters/mysql.rb
 73 def connect(server)
 74   opts = server_opts(server)
 75   conn = Mysql.init
 76   conn.options(Mysql::READ_DEFAULT_GROUP, opts[:config_default_group] || "client")
 77   conn.options(Mysql::OPT_LOCAL_INFILE, opts[:config_local_infile]) if opts.has_key?(:config_local_infile)
 78   conn.ssl_set(opts[:sslkey], opts[:sslcert], opts[:sslca], opts[:sslcapath], opts[:sslcipher]) if opts[:sslca] || opts[:sslkey]
 79   if encoding = opts[:encoding] || opts[:charset]
 80     # Set encoding before connecting so that the mysql driver knows what
 81     # encoding we want to use, but this can be overridden by READ_DEFAULT_GROUP.
 82     conn.options(Mysql::SET_CHARSET_NAME, encoding)
 83   end
 84   if read_timeout = opts[:read_timeout] and defined? Mysql::OPT_READ_TIMEOUT
 85     conn.options(Mysql::OPT_READ_TIMEOUT, read_timeout)
 86   end
 87   if connect_timeout = opts[:connect_timeout] and defined? Mysql::OPT_CONNECT_TIMEOUT
 88     conn.options(Mysql::OPT_CONNECT_TIMEOUT, connect_timeout)
 89   end
 90   conn.real_connect(
 91     opts[:host] || 'localhost',
 92     opts[:user],
 93     opts[:password],
 94     opts[:database],
 95     (opts[:port].to_i if opts[:port]),
 96     opts[:socket],
 97     Mysql::CLIENT_MULTI_RESULTS +
 98     Mysql::CLIENT_MULTI_STATEMENTS +
 99     (opts[:compress] == false ? 0 : Mysql::CLIENT_COMPRESS)
100   )
101   sqls = mysql_connection_setting_sqls
102 
103   # Set encoding a slightly different way after connecting,
104   # in case the READ_DEFAULT_GROUP overrode the provided encoding.
105   # Doesn't work across implicit reconnects, but Sequel doesn't turn on
106   # that feature.
107   sqls.unshift("SET NAMES #{literal(encoding.to_s)}") if encoding
108 
109   sqls.each{|sql| log_connection_yield(sql, conn){conn.query(sql)}}
110 
111   add_prepared_statements_cache(conn)
112   conn
113 end
convert_invalid_date_time=(v) click to toggle source

Modify the type translators for the date, time, and timestamp types depending on the value given.

    # File lib/sequel/adapters/mysql.rb
123 def convert_invalid_date_time=(v)
124   m0 = ::Sequel.method(:string_to_time)
125   @conversion_procs[11] = (v != false) ?  lambda{|val| convert_date_time(val, &m0)} : m0
126   m1 = ::Sequel.method(:string_to_date) 
127   m = (v != false) ? lambda{|val| convert_date_time(val, &m1)} : m1
128   [10, 14].each{|i| @conversion_procs[i] = m}
129   m2 = method(:to_application_timestamp)
130   m = (v != false) ? lambda{|val| convert_date_time(val, &m2)} : m2
131   [7, 12].each{|i| @conversion_procs[i] = m}
132   @convert_invalid_date_time = v
133 end
convert_tinyint_to_bool=(v) click to toggle source

Modify the type translator used for the tinyint type based on the value given.

    # File lib/sequel/adapters/mysql.rb
137 def convert_tinyint_to_bool=(v)
138   @conversion_procs[1] = TYPE_TRANSLATOR.method(v ? :boolean : :integer)
139   @convert_tinyint_to_bool = v
140 end
disconnect_connection(c) click to toggle source
    # File lib/sequel/adapters/mysql.rb
115 def disconnect_connection(c)
116   c.close
117 rescue Mysql::Error
118   nil
119 end
execute_dui(sql, opts=OPTS) click to toggle source
    # File lib/sequel/adapters/mysql.rb
142 def execute_dui(sql, opts=OPTS)
143   execute(sql, opts){|c| return affected_rows(c)}
144 end
execute_insert(sql, opts=OPTS) click to toggle source
    # File lib/sequel/adapters/mysql.rb
146 def execute_insert(sql, opts=OPTS)
147   execute(sql, opts){|c| return c.insert_id}
148 end
freeze() click to toggle source
Calls superclass method Sequel::MySQL::DatabaseMethods#freeze
    # File lib/sequel/adapters/mysql.rb
150 def freeze
151   server_version
152   @conversion_procs.freeze
153   super
154 end
server_version(server=nil) click to toggle source

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

    # File lib/sequel/adapters/mysql.rb
157 def server_version(server=nil)
158   @server_version ||= (synchronize(server){|conn| conn.server_version if conn.respond_to?(:server_version)} || super)
159 end

Private Instance Methods

_execute(conn, sql, opts) { |r| ... } click to toggle source

Execute the given SQL on the given connection. If the :type option is :select, yield the result of the query, otherwise yield the connection if a block is given.

    # File lib/sequel/adapters/mysql.rb
166 def _execute(conn, sql, opts)
167   begin
168     r = log_connection_yield((log_sql = opts[:log_sql]) ? sql + log_sql : sql, conn){conn.query(sql)}
169     if opts[:type] == :select
170       yield r if r
171     elsif block_given?
172       yield conn
173     end
174     if conn.respond_to?(:more_results?)
175       while conn.more_results? do
176         if r
177           r.free
178           r = nil
179         end
180         begin
181           conn.next_result
182           r = conn.use_result
183         rescue Mysql::Error => e
184           raise_error(e, :disconnect=>true) if MYSQL_DATABASE_DISCONNECT_ERRORS.match(e.message)
185           break
186         end
187         yield r if opts[:type] == :select
188       end
189     end
190   rescue Mysql::Error => e
191     raise_error(e)
192   ensure
193     r.free if r
194     # Use up all results to avoid a commands out of sync message.
195     if conn.respond_to?(:more_results?)
196       while conn.more_results? do
197         begin
198           conn.next_result
199           r = conn.use_result
200         rescue Mysql::Error => e
201           raise_error(e, :disconnect=>true) if MYSQL_DATABASE_DISCONNECT_ERRORS.match(e.message)
202           break
203         end
204         r.free if r
205       end
206     end
207   end
208 end
adapter_initialize() click to toggle source
    # File lib/sequel/adapters/mysql.rb
210 def adapter_initialize
211   @conversion_procs = MYSQL_TYPES.dup
212   self.convert_tinyint_to_bool = true
213   self.convert_invalid_date_time = false
214 end
affected_rows(conn) click to toggle source

Try to get an accurate number of rows matched using the query info. Fall back to affected_rows if there was no match, but that may be inaccurate.

    # File lib/sequel/adapters/mysql.rb
219 def affected_rows(conn)
220   s = conn.info
221   if s && s =~ /Rows matched:\s+(\d+)\s+Changed:\s+\d+\s+Warnings:\s+\d+/
222     $1.to_i
223   else
224     conn.affected_rows
225   end
226 end
connection_execute_method() click to toggle source

MySQL connections use the query method to execute SQL without a result

    # File lib/sequel/adapters/mysql.rb
229 def connection_execute_method
230   :query
231 end
convert_date_time(v) { |v| ... } click to toggle source

If convert_invalid_date_time is nil, :nil, or :string and the conversion raises an InvalidValue exception, return v if :string and nil otherwise.

    # File lib/sequel/adapters/mysql.rb
236 def convert_date_time(v)
237   begin
238     yield v
239   rescue InvalidValue
240     case @convert_invalid_date_time
241     when nil, :nil
242       nil
243     when :string
244       v
245     else 
246       raise
247     end
248   end
249 end
database_error_classes() click to toggle source
    # File lib/sequel/adapters/mysql.rb
251 def database_error_classes
252   [Mysql::Error]
253 end
database_exception_sqlstate(exception, opts) click to toggle source
    # File lib/sequel/adapters/mysql.rb
255 def database_exception_sqlstate(exception, opts)
256   exception.sqlstate
257 end
dataset_class_default() click to toggle source
    # File lib/sequel/adapters/mysql.rb
259 def dataset_class_default
260   Dataset
261 end
disconnect_error?(e, opts) click to toggle source
Calls superclass method Sequel::Database#disconnect_error?
    # File lib/sequel/adapters/mysql.rb
263 def disconnect_error?(e, opts)
264   super || (e.is_a?(::Mysql::Error) && MYSQL_DATABASE_DISCONNECT_ERRORS.match(e.message))
265 end
schema_column_type(db_type) click to toggle source

Convert tinyint(1) type to boolean if convert_tinyint_to_bool is true

    # File lib/sequel/adapters/mysql.rb
268 def schema_column_type(db_type)
269   convert_tinyint_to_bool && db_type =~ /\Atinyint\(1\)/ ? :boolean : super
270 end