Class Index [+]

Quicksearch

Sequel::Mysql2::Database

Database class for MySQL databases used with Sequel.

Constants

MYSQL_DATABASE_DISCONNECT_ERRORS

Mysql::Error messages that indicate the current connection should be disconnected

Attributes

convert_tinyint_to_bool[RW]

Whether to convert tinyint columns to bool for this database

Public Class Methods

new(opts={}) click to toggle source

Set the convert_tinyint_to_bool setting based on the default value.

    # File lib/sequel/adapters/mysql2.rb, line 21
21:       def initialize(opts={})
22:         super
23:         self.convert_tinyint_to_bool = Sequel::MySQL.convert_tinyint_to_bool
24:       end

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)

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

  • :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/mysql2.rb, line 44
44:       def connect(server)
45:         opts = server_opts(server)
46:         opts[:host] ||= 'localhost'
47:         opts[:username] ||= opts[:user]
48:         opts[:flags] = ::Mysql2::Client::FOUND_ROWS if ::Mysql2::Client.const_defined?(:FOUND_ROWS)
49:         conn = ::Mysql2::Client.new(opts)
50: 
51:         sqls = mysql_connection_setting_sqls
52: 
53:         # Set encoding a slightly different way after connecting,
54:         # in case the READ_DEFAULT_GROUP overrode the provided encoding.
55:         # Doesn't work across implicit reconnects, but Sequel doesn't turn on
56:         # that feature.
57:         if encoding = opts[:encoding] || opts[:charset]
58:           sqls.unshift("SET NAMES #{conn.escape(encoding.to_s)}")
59:         end
60: 
61:         sqls.each{|sql| log_yield(sql){conn.query(sql)}}
62: 
63:         add_prepared_statements_cache(conn)
64:         conn
65:       end
execute_dui(sql, opts={}) click to toggle source

Return the number of matched rows when executing a delete/update statement.

    # File lib/sequel/adapters/mysql2.rb, line 68
68:       def execute_dui(sql, opts={})
69:         execute(sql, opts){|c| return c.affected_rows}
70:       end
execute_insert(sql, opts={}) click to toggle source

Return the last inserted id when executing an insert statement.

    # File lib/sequel/adapters/mysql2.rb, line 73
73:       def execute_insert(sql, opts={})
74:         execute(sql, opts){|c| return c.last_id}
75:       end
server_version(server=nil) click to toggle source

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

    # File lib/sequel/adapters/mysql2.rb, line 78
78:       def server_version(server=nil)
79:         @server_version ||= (synchronize(server){|conn| conn.server_info[:id]} || super)
80:       end

Private Instance Methods

_execute(conn, sql, opts) 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, line 87
87:       def _execute(conn, sql, opts)
88:         begin
89:           r = log_yield((log_sql = opts[:log_sql]) ? sql + log_sql : sql){conn.query(sql, :symbolize_keys => true, :database_timezone => timezone, :application_timezone => Sequel.application_timezone)}
90:           if opts[:type] == :select
91:             yield r if r
92:           elsif block_given?
93:             yield conn
94:           end
95:         rescue ::Mysql2::Error => e
96:           raise_error(e)
97:         end
98:       end
connection_execute_method() click to toggle source

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

     # File lib/sequel/adapters/mysql2.rb, line 101
101:       def connection_execute_method
102:         :query
103:       end
database_error_classes() click to toggle source

The MySQL adapter main error class is Mysql2::Error

     # File lib/sequel/adapters/mysql2.rb, line 106
106:       def database_error_classes
107:         [::Mysql2::Error]
108:       end
database_name() click to toggle source

The database name when using the native adapter is always stored in the :database option.

     # File lib/sequel/adapters/mysql2.rb, line 123
123:       def database_name
124:         @opts[:database]
125:       end
disconnect_connection(c) click to toggle source

Closes given database connection.

     # File lib/sequel/adapters/mysql2.rb, line 128
128:       def disconnect_connection(c)
129:         c.close
130:       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.

     # File lib/sequel/adapters/mysql2.rb, line 113
113:       def disconnect_error?(e, opts)
114:         super ||
115:           ((conn = opts[:conn]) && !conn.ping) ||
116:           (e.is_a?(::Mysql2::Error) &&
117:             (e.sql_state =~ /\A08/ ||
118:              MYSQL_DATABASE_DISCONNECT_ERRORS.match(e.message)))
119:       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, line 133
133:       def schema_column_type(db_type)
134:         convert_tinyint_to_bool && db_type == 'tinyint(1)' ? :boolean : super
135:       end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.