Class: Sequel::MySQL::Database
- Includes:
- DatabaseMethods
- Defined in:
- lib/sequel/adapters/mysql.rb
Overview
Database class for MySQL databases used with Sequel.
Constant Summary
Constants included from DatabaseMethods
Sequel::MySQL::DatabaseMethods::AUTO_INCREMENT, Sequel::MySQL::DatabaseMethods::NOT_NULL, Sequel::MySQL::DatabaseMethods::NULL, Sequel::MySQL::DatabaseMethods::PRIMARY_KEY, Sequel::MySQL::DatabaseMethods::TYPES, Sequel::MySQL::DatabaseMethods::UNIQUE, Sequel::MySQL::DatabaseMethods::UNSIGNED
Constants inherited from Database
Database::ADAPTERS, Database::AUTOINCREMENT, Database::CASCADE, Database::COMMA_SEPARATOR, Database::NOT_NULL, Database::NO_ACTION, Database::NULL, Database::PRIMARY_KEY, Database::RESTRICT, Database::SET_DEFAULT, Database::SET_NULL, Database::SQL_BEGIN, Database::SQL_COMMIT, Database::SQL_ROLLBACK, Database::TYPES, Database::UNDERSCORE, Database::UNIQUE, Database::UNSIGNED
Instance Attribute Summary
Attributes inherited from Database
#default_schema, #loggers, #opts, #pool, #prepared_statements
Instance Method Summary collapse
-
#call_sproc(name, opts = {}, &block) ⇒ Object
Support stored procedures on MySQL.
-
#connect(server) ⇒ Object
Connect to the database.
-
#dataset(opts = nil) ⇒ Object
Returns instance of Sequel::MySQL::Dataset with the given options.
-
#execute(sql, opts = {}, &block) ⇒ Object
Executes the given SQL using an available connection, yielding the connection if the block is given.
-
#server_version(server = nil) ⇒ Object
Return the version of the MySQL server two which we are connecting.
-
#transaction(opts = {}) ⇒ Object
Support single level transactions on MySQL.
Methods included from DatabaseMethods
#alter_table_sql, #auto_increment_sql, #column_references_sql, #create_table_sql_list, #index_definition_sql, #tables, #use
Methods inherited from Database
#<<, #>>, #[], adapter_class, adapter_scheme, #add_column, #add_index, #alter_table, #alter_table_sql, #alter_table_sql_list, #auto_increment_sql, #call, #column_definition_sql, #column_list_sql, #column_references_sql, connect, #constraint_definition_sql, #create_or_replace_view, #create_table, #create_table!, #create_table_sql_list, #create_view, #default_index_name, #disconnect, #drop_column, #drop_index, #drop_index_sql, #drop_table, #drop_table_sql, #drop_view, #execute_ddl, #execute_dui, #execute_insert, #fetch, #filter_expr, #from, #get, #identifier_input_method, identifier_input_method, #identifier_input_method=, identifier_input_method=, #identifier_output_method, identifier_output_method, #identifier_output_method=, identifier_output_method=, #index_definition_sql, #index_list_sql_list, #initialize, #inspect, #literal, #log_info, #logger, #logger=, #multi_threaded?, #on_delete_clause, #query, #quote_identifier, #quote_identifiers=, quote_identifiers=, #quote_identifiers?, #quote_schema_table, #rename_column, #rename_table, #rename_table_sql, #schema, #schema_utility_dataset, #select, #serial_primary_key_options, #set_column_default, #set_column_type, single_threaded=, #single_threaded?, #synchronize, #table_exists?, #test_connection, #typecast_value, #upcase_identifiers=, upcase_identifiers=, #upcase_identifiers?, #uri, #url
Methods included from Sequel::Metaprogramming
Constructor Details
This class inherits a constructor from Sequel::Database
Instance Method Details
#call_sproc(name, opts = {}, &block) ⇒ Object
Support stored procedures on MySQL
48 49 50 51 |
# File 'lib/sequel/adapters/mysql.rb', line 48 def call_sproc(name, opts={}, &block) args = opts[:args] || [] execute("CALL #{name}#{args.empty? ? '()' : literal(args)}", opts.merge(:sproc=>false), &block) end |
#connect(server) ⇒ Object
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
-
: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.
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/sequel/adapters/mysql.rb', line 66 def connect(server) opts = server_opts(server) conn = Mysql.init conn.(Mysql::OPT_LOCAL_INFILE, "client") if encoding = opts[:encoding] || opts[:charset] # set charset _before_ the connect. using an option instead of "SET (NAMES|CHARACTER_SET_*)" works across reconnects conn.(Mysql::SET_CHARSET_NAME, encoding) end conn.real_connect( opts[:host] || 'localhost', opts[:user], opts[:password], opts[:database], opts[:port], opts[:socket], Mysql::CLIENT_MULTI_RESULTS + Mysql::CLIENT_MULTI_STATEMENTS + (opts[:compress] == false ? 0 : Mysql::CLIENT_COMPRESS) ) # increase timeout so mysql server doesn't disconnect us conn.query("set @@wait_timeout = #{opts[:timeout] || 2592000}") # By default, MySQL 'where id is null' selects the last inserted id conn.query("set SQL_AUTO_IS_NULL=0") unless opts[:auto_is_null] conn.query_with_result = false class << conn attr_accessor :prepared_statements end conn.prepared_statements = {} conn.reconnect = true conn end |
#dataset(opts = nil) ⇒ Object
Returns instance of Sequel::MySQL::Dataset with the given options.
102 103 104 |
# File 'lib/sequel/adapters/mysql.rb', line 102 def dataset(opts = nil) MySQL::Dataset.new(self, opts) end |
#execute(sql, opts = {}, &block) ⇒ Object
Executes the given SQL using an available connection, yielding the connection if the block is given.
108 109 110 111 112 113 114 115 116 |
# File 'lib/sequel/adapters/mysql.rb', line 108 def execute(sql, opts={}, &block) return call_sproc(sql, opts, &block) if opts[:sproc] return execute_prepared_statement(sql, opts, &block) if Symbol === sql begin synchronize(opts[:server]){|conn| _execute(conn, sql, opts, &block)} rescue Mysql::Error => e raise_error(e) end end |
#server_version(server = nil) ⇒ Object
Return the version of the MySQL server two which we are connecting.
119 120 121 |
# File 'lib/sequel/adapters/mysql.rb', line 119 def server_version(server=nil) @server_version ||= (synchronize(server){|conn| conn.server_version if conn.respond_to?(:server_version)} || super) end |
#transaction(opts = {}) ⇒ Object
Support single level transactions on MySQL.
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/sequel/adapters/mysql.rb', line 124 def transaction(opts={}) unless opts.is_a?(Hash) Deprecation.deprecate('Passing an argument other than a Hash to Database#transaction', "Use DB.transaction(:server=>#{opts.inspect})") opts = {:server=>opts} end synchronize(opts[:server]) do |conn| return yield(conn) if @transactions.include?(Thread.current) log_info(begin_transaction_sql) conn.query(begin_transaction_sql) begin @transactions << Thread.current yield(conn) rescue ::Exception => e log_info(rollback_transaction_sql) conn.query(rollback_transaction_sql) transaction_error(e, Mysql::Error) ensure unless e log_info(commit_transaction_sql) conn.query(commit_transaction_sql) end @transactions.delete(Thread.current) end end end |