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
 87 def connect(server)
 88   opts = server_opts(server)
 89 
 90   if !RUBY_MYSQL_3
 91     conn = Mysql.init
 92     conn.options(Mysql::READ_DEFAULT_GROUP, opts[:config_default_group] || "client")
 93     conn.options(Mysql::OPT_LOCAL_INFILE, opts[:config_local_infile]) if opts.has_key?(:config_local_infile)
 94     if encoding = opts[:encoding] || opts[:charset]
 95       # Set encoding before connecting so that the mysql driver knows what
 96       # encoding we want to use, but this can be overridden by READ_DEFAULT_GROUP.
 97       conn.options(Mysql::SET_CHARSET_NAME, encoding)
 98     end
 99     if read_timeout = opts[:read_timeout] and defined? Mysql::OPT_READ_TIMEOUT
100       conn.options(Mysql::OPT_READ_TIMEOUT, read_timeout)
101     end
102     if connect_timeout = opts[:connect_timeout] and defined? Mysql::OPT_CONNECT_TIMEOUT
103       conn.options(Mysql::OPT_CONNECT_TIMEOUT, connect_timeout)
104     end
105   else
106     # ruby-mysql 3+ API
107     conn = Adapter.new
108     # no support for default group
109     conn.local_infile = opts[:config_local_infile] if opts.has_key?(:config_local_infile)
110     if encoding = opts[:encoding] || opts[:charset]
111       conn.charset = encoding
112     end
113     if read_timeout = opts[:read_timeout]
114       conn.read_timeout = read_timeout
115     end
116     if connect_timeout = opts[:connect_timeout]
117       conn.connect_timeout = connect_timeout
118     end
119     opts[:compress] = false
120   end
121 
122   conn.ssl_set(opts[:sslkey], opts[:sslcert], opts[:sslca], opts[:sslcapath], opts[:sslcipher]) if opts[:sslca] || opts[:sslkey]
123   conn.real_connect(
124     opts[:host] || 'localhost',
125     opts[:user],
126     opts[:password],
127     opts[:database],
128     (opts[:port].to_i if opts[:port]),
129     opts[:socket],
130     Mysql::CLIENT_MULTI_RESULTS +
131     Mysql::CLIENT_MULTI_STATEMENTS +
132     (opts[:compress] == false ? 0 : Mysql::CLIENT_COMPRESS)
133   )
134   sqls = mysql_connection_setting_sqls
135 
136   # Set encoding a slightly different way after connecting,
137   # in case the READ_DEFAULT_GROUP overrode the provided encoding.
138   # Doesn't work across implicit reconnects, but Sequel doesn't turn on
139   # that feature.
140   sqls.unshift("SET NAMES #{literal(encoding.to_s)}") if encoding
141 
142   sqls.each{|sql| log_connection_yield(sql, conn){conn.query(sql)}}
143 
144   add_prepared_statements_cache(conn)
145   conn
146 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
156 def convert_invalid_date_time=(v)
157   m0 = ::Sequel.method(:string_to_time)
158   @conversion_procs[11] = (v != false) ?  lambda{|val| convert_date_time(val, &m0)} : m0
159   m1 = ::Sequel.method(:string_to_date) 
160   m = (v != false) ? lambda{|val| convert_date_time(val, &m1)} : m1
161   [10, 14].each{|i| @conversion_procs[i] = m}
162   m2 = method(:to_application_timestamp)
163   m = (v != false) ? lambda{|val| convert_date_time(val, &m2)} : m2
164   [7, 12].each{|i| @conversion_procs[i] = m}
165   @convert_invalid_date_time = v
166 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
170 def convert_tinyint_to_bool=(v)
171   @conversion_procs[1] = v ? TYPE_TRANSLATOR_BOOLEAN : TYPE_TRANSLATOR_INTEGER
172   @convert_tinyint_to_bool = v
173 end
disconnect_connection(c) click to toggle source
    # File lib/sequel/adapters/mysql.rb
148 def disconnect_connection(c)
149   c.close
150 rescue Mysql::Error
151   nil
152 end
execute_dui(sql, opts=OPTS) click to toggle source
    # File lib/sequel/adapters/mysql.rb
175 def execute_dui(sql, opts=OPTS)
176   execute(sql, opts){|c| return affected_rows(c)}
177 end
execute_insert(sql, opts=OPTS) click to toggle source
    # File lib/sequel/adapters/mysql.rb
179 def execute_insert(sql, opts=OPTS)
180   execute(sql, opts){|c| return c.insert_id}
181 end
freeze() click to toggle source
Calls superclass method Sequel::MySQL::DatabaseMethods#freeze
    # File lib/sequel/adapters/mysql.rb
183 def freeze
184   server_version
185   @conversion_procs.freeze
186   super
187 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
194 def _execute(conn, sql, opts)
195   r = log_connection_yield((log_sql = opts[:log_sql]) ? sql + log_sql : sql, conn){conn.query(sql)}
196   if opts[:type] == :select
197     yield r if r
198   elsif defined?(yield)
199     yield conn
200   end
201   if conn.respond_to?(:more_results?)
202     while conn.more_results? do
203       if r
204         r.free
205         r = nil
206       end
207       begin
208         conn.next_result
209         r = conn.use_result
210       rescue Mysql::Error => e
211         raise_error(e, :disconnect=>true) if MYSQL_DATABASE_DISCONNECT_ERRORS.match(e.message)
212         break
213       end
214       yield r if opts[:type] == :select
215     end
216   end
217 rescue Mysql::Error => e
218   raise_error(e)
219 ensure
220   r.free if r
221   # Use up all results to avoid a commands out of sync message.
222   if conn.respond_to?(:more_results?)
223     while conn.more_results? do
224       begin
225         conn.next_result
226         r = conn.use_result
227       rescue Mysql::Error => e
228         raise_error(e, :disconnect=>true) if MYSQL_DATABASE_DISCONNECT_ERRORS.match(e.message)
229         break
230       end
231       r.free if r
232     end
233   end
234 end
adapter_initialize() click to toggle source
    # File lib/sequel/adapters/mysql.rb
236 def adapter_initialize
237   @conversion_procs = MYSQL_TYPES.dup
238   self.convert_tinyint_to_bool = true
239   self.convert_invalid_date_time = false
240 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
245 def affected_rows(conn)
246   s = conn.info
247   if s && s =~ /Rows matched:\s+(\d+)\s+Changed:\s+\d+\s+Warnings:\s+\d+/
248     $1.to_i
249   else
250     conn.affected_rows
251   end
252 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
255 def connection_execute_method
256   :query
257 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
262 def convert_date_time(v)
263   yield v
264 rescue InvalidValue
265   case @convert_invalid_date_time
266   when nil, :nil
267     nil
268   when :string
269     v
270   else 
271     raise
272   end
273 end
database_error_classes() click to toggle source
    # File lib/sequel/adapters/mysql.rb
275 def database_error_classes
276   [Mysql::Error]
277 end
database_exception_sqlstate(exception, opts) click to toggle source
    # File lib/sequel/adapters/mysql.rb
279 def database_exception_sqlstate(exception, opts)
280   exception.sqlstate
281 end
dataset_class_default() click to toggle source
    # File lib/sequel/adapters/mysql.rb
283 def dataset_class_default
284   Dataset
285 end
disconnect_error?(e, opts) click to toggle source
Calls superclass method Sequel::Database#disconnect_error?
    # File lib/sequel/adapters/mysql.rb
287 def disconnect_error?(e, opts)
288   super || (e.is_a?(::Mysql::Error) && MYSQL_DATABASE_DISCONNECT_ERRORS.match(e.message))
289 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
292 def schema_column_type(db_type)
293   convert_tinyint_to_bool && db_type =~ /\Atinyint\(1\)/ ? :boolean : super
294 end