class Sequel::Mysql2::Database

Attributes

convert_tinyint_to_bool[RW]

Whether to convert tinyint columns to bool for this 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)

:encoding

Set all the related character sets for this connection (connection, client, database, server, and results).

The options hash is also passed to mysql2, and can include mysql2 options such as :local_infile.

   # File lib/sequel/adapters/mysql2.rb
37 def connect(server)
38   opts = server_opts(server)
39   opts[:username] ||= opts.delete(:user)
40   opts[:flags] ||= 0
41   opts[:flags] |= ::Mysql2::Client::FOUND_ROWS if ::Mysql2::Client.const_defined?(:FOUND_ROWS)
42   opts[:encoding] ||= opts[:charset]
43   conn = ::Mysql2::Client.new(opts)
44   conn.query_options.merge!(:symbolize_keys=>true, :cache_rows=>false)
45     
46   if NativePreparedStatements
47     conn.instance_variable_set(:@sequel_default_query_options, conn.query_options.dup)
48   end
49 
50   sqls = mysql_connection_setting_sqls
51 
52   # Set encoding a slightly different way after connecting,
53   # in case the READ_DEFAULT_GROUP overrode the provided encoding.
54   # Doesn't work across implicit reconnects, but Sequel doesn't turn on
55   # that feature.
56   if encoding = opts[:encoding]
57     sqls.unshift("SET NAMES #{conn.escape(encoding.to_s)}")
58   end
59 
60   sqls.each{|sql| log_connection_yield(sql, conn){conn.query(sql)}}
61 
62   add_prepared_statements_cache(conn)
63   conn
64 end
execute_dui(sql, opts=OPTS) click to toggle source
   # File lib/sequel/adapters/mysql2.rb
66 def execute_dui(sql, opts=OPTS)
67   execute(sql, opts){|c| return c.affected_rows}
68 end
execute_insert(sql, opts=OPTS) click to toggle source
   # File lib/sequel/adapters/mysql2.rb
70 def execute_insert(sql, opts=OPTS)
71   execute(sql, opts){|c| return c.last_id}
72 end
freeze() click to toggle source
Calls superclass method Sequel::MySQL::DatabaseMethods#freeze
   # File lib/sequel/adapters/mysql2.rb
74 def freeze
75   server_version
76   super
77 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/mysql2.rb
80 def server_version(_server=nil)
81   @server_version ||= super()
82 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/mysql2.rb
113 def _execute(conn, sql, opts)
114   begin
115     stream = opts[:stream]
116     if NativePreparedStatements
117       if args = opts[:arguments]
118         args = args.map{|arg| bound_variable_value(arg)}
119       end
120 
121       case sql
122       when ::Mysql2::Statement
123         stmt = sql
124       when Dataset
125         sql = sql.sql
126         close_stmt = true
127         stmt = conn.prepare(sql)
128       end
129     end
130 
131     r = log_connection_yield((log_sql = opts[:log_sql]) ? sql + log_sql : sql, conn, args) do
132       if stmt
133         conn.query_options.merge!(:cache_rows=>true, :database_timezone => timezone, :application_timezone => Sequel.application_timezone, :stream=>stream, :cast_booleans=>convert_tinyint_to_bool)
134         stmt.execute(*args)
135       else
136         conn.query(sql, :database_timezone => timezone, :application_timezone => Sequel.application_timezone, :stream=>stream)
137       end
138     end
139     if opts[:type] == :select
140       if r
141         if stream
142           begin
143             r2 = yield r
144           ensure
145             # If r2 is nil, it means the block did not exit normally,
146             # so the rest of the results must be drained to prevent
147             # "commands out of sync" errors.
148             r.each{} unless r2
149           end
150         else
151           yield r
152         end
153       end
154     elsif block_given?
155       yield conn
156     end
157   rescue ::Mysql2::Error => e
158     raise_error(e)
159   ensure
160     if stmt
161       conn.query_options.replace(conn.instance_variable_get(:@sequel_default_query_options))
162       stmt.close if close_stmt
163     end
164   end
165 end
adapter_initialize() click to toggle source

Set the convert_tinyint_to_bool setting based on the default value.

    # File lib/sequel/adapters/mysql2.rb
168 def adapter_initialize
169   self.convert_tinyint_to_bool = true
170 end
bound_variable_value(arg) click to toggle source

Handle bound variable arguments that Mysql2 does not handle natively.

    # File lib/sequel/adapters/mysql2.rb
174 def bound_variable_value(arg)
175   case arg
176   when true
177     1
178   when false
179     0
180   when DateTime, Time
181     literal(arg)[1...-1]
182   else
183     arg
184   end
185 end
connection_execute_method() click to toggle source
    # File lib/sequel/adapters/mysql2.rb
188 def connection_execute_method
189   :query
190 end
database_error_classes() click to toggle source
    # File lib/sequel/adapters/mysql2.rb
192 def database_error_classes
193   [::Mysql2::Error]
194 end
database_exception_sqlstate(exception, opts) click to toggle source
    # File lib/sequel/adapters/mysql2.rb
196 def database_exception_sqlstate(exception, opts)
197   state = exception.sql_state
198   state unless state == 'HY000'
199 end
dataset_class_default() click to toggle source
    # File lib/sequel/adapters/mysql2.rb
201 def dataset_class_default
202   Dataset
203 end
disconnect_error?(e, opts) click to toggle source

If a connection object is available, try pinging it. Otherwise, if the error is a Mysql2::Error, check the SQL state and exception message for disconnects.

Calls superclass method Sequel::Database#disconnect_error?
    # File lib/sequel/adapters/mysql2.rb
208 def disconnect_error?(e, opts)
209   super ||
210     ((conn = opts[:conn]) && !conn.ping) ||
211     (e.is_a?(::Mysql2::Error) &&
212       (e.sql_state =~ /\A08/ ||
213        MYSQL_DATABASE_DISCONNECT_ERRORS.match(e.message)))
214 end
execute_prepared_statement(ps_name, opts, &block) click to toggle source

Use a native mysql2 prepared statement to implement prepared statements.

    # File lib/sequel/adapters/mysql2.rb
 88 def execute_prepared_statement(ps_name, opts, &block)
 89   ps = prepared_statement(ps_name)
 90   sql = ps.prepared_sql
 91 
 92   synchronize(opts[:server]) do |conn|
 93     stmt, ps_sql = conn.prepared_statements[ps_name]
 94     unless ps_sql == sql
 95       stmt.close if stmt
 96       stmt = log_connection_yield(conn, "Preparing #{ps_name}: #{sql}"){conn.prepare(sql)}
 97       conn.prepared_statements[ps_name] = [stmt, sql]
 98     end
 99 
100     if ps.log_sql
101       opts = Hash[opts]
102       opts = opts[:log_sql] = " (#{sql})"
103     end
104 
105     _execute(conn, stmt, opts, &block)
106   end
107 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/mysql2.rb
217 def schema_column_type(db_type)
218   convert_tinyint_to_bool && db_type =~ /\Atinyint\(1\)/ ? :boolean : super
219 end