Class: Sequel::MySQL::Database
- Includes:
- DatabaseMethods
- Defined in:
- lib/sequel_core/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::SQL_BEGIN, Sequel::MySQL::DatabaseMethods::SQL_COMMIT, Sequel::MySQL::DatabaseMethods::SQL_ROLLBACK, Sequel::MySQL::DatabaseMethods::TYPES, Sequel::MySQL::DatabaseMethods::UNIQUE, Sequel::MySQL::DatabaseMethods::UNSIGNED
Constants inherited from Database
Database::ADAPTERS, Database::SQL_BEGIN, Database::SQL_COMMIT, Database::SQL_ROLLBACK
Constants included from Schema::SQL
Schema::SQL::AUTOINCREMENT, Schema::SQL::CASCADE, Schema::SQL::COMMA_SEPARATOR, Schema::SQL::NOT_NULL, Schema::SQL::NO_ACTION, Schema::SQL::NULL, Schema::SQL::PRIMARY_KEY, Schema::SQL::RESTRICT, Schema::SQL::SET_DEFAULT, Schema::SQL::SET_NULL, Schema::SQL::TYPES, Schema::SQL::UNDERSCORE, Schema::SQL::UNIQUE, Schema::SQL::UNSIGNED
Instance Attribute Summary
Attributes inherited from Database
#loggers, #opts, #pool, #prepared_statements, #quote_identifiers
Instance Method Summary collapse
-
#connect(server) ⇒ Object
Connect to the database.
-
#dataset(opts = nil) ⇒ Object
Returns instance of Sequel::MySQL::Dataset with the given options.
-
#disconnect ⇒ Object
Closes all database connections.
-
#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.
-
#tables(server = nil) ⇒ Object
Return an array of symbols specifying table names in the current database.
-
#transaction(server = nil) ⇒ Object
Support single level transactions on MySQL.
Methods included from DatabaseMethods
#alter_table_sql, #auto_increment_sql, #column_definition_sql, #index_definition_sql, #use
Methods inherited from Database
#<<, #[], adapter_class, adapter_scheme, #add_column, #add_index, #alter_table, #call, connect, #create_or_replace_view, #create_table, #create_table!, #create_view, #drop_column, #drop_index, #drop_table, #drop_view, #execute_ddl, #execute_dui, #fetch, #from, #get, #initialize, #inspect, #log_info, #logger, #logger=, #multi_threaded?, #query, quote_identifiers=, #quote_identifiers?, #rename_column, #rename_table, #select, #serial_primary_key_options, #set_column_default, #set_column_type, single_threaded=, #single_threaded?, #synchronize, #table_exists?, #test_connection, #typecast_value, #uri
Methods included from Schema::SQL
#alter_table_sql, #alter_table_sql_list, #auto_increment_sql, #column_definition_sql, #column_list_sql, #constraint_definition_sql, #create_table_sql_list, #default_index_name, #drop_table_sql, #filter_expr, #index_definition_sql, #index_list_sql_list, #literal, #on_delete_clause, #quote_identifier, #rename_table_sql, #schema, #schema_utility_dataset, #type_literal
Constructor Details
This class inherits a constructor from Sequel::Database
Instance Method Details
#connect(server) ⇒ Object
Connect to the database. In addition to the usual database options, the following options have effect:
-
:encoding, :charset - 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.
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/sequel_core/adapters/mysql.rb', line 94 def connect(server) opts = server_opts(server) conn = Mysql.init conn.(Mysql::OPT_LOCAL_INFILE, "client") conn.real_connect( opts[:host] || 'localhost', opts[:user], opts[:password], opts[:database], opts[:port], opts[:socket], Mysql::CLIENT_MULTI_RESULTS + Mysql::CLIENT_MULTI_STATEMENTS + Mysql::CLIENT_COMPRESS ) conn.query_with_result = false if encoding = opts[:encoding] || opts[:charset] conn.query("set character_set_connection = '#{encoding}'") conn.query("set character_set_client = '#{encoding}'") conn.query("set character_set_database = '#{encoding}'") conn.query("set character_set_server = '#{encoding}'") conn.query("set character_set_results = '#{encoding}'") end conn.{attr_accessor :prepared_statements} conn.prepared_statements = {} conn.reconnect = true conn end |
#dataset(opts = nil) ⇒ Object
Returns instance of Sequel::MySQL::Dataset with the given options.
124 125 126 |
# File 'lib/sequel_core/adapters/mysql.rb', line 124 def dataset(opts = nil) MySQL::Dataset.new(self, opts) end |
#disconnect ⇒ Object
Closes all database connections.
129 130 131 |
# File 'lib/sequel_core/adapters/mysql.rb', line 129 def disconnect @pool.disconnect {|c| c.close} end |
#execute(sql, opts = {}, &block) ⇒ Object
Executes the given SQL using an available connection, yielding the connection if the block is given.
135 136 137 138 139 140 141 142 |
# File 'lib/sequel_core/adapters/mysql.rb', line 135 def execute(sql, opts={}, &block) 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.new(e.) end end |
#server_version(server = nil) ⇒ Object
Return the version of the MySQL server two which we are connecting.
145 146 147 |
# File 'lib/sequel_core/adapters/mysql.rb', line 145 def server_version(server=nil) @server_version ||= (synchronize(server){|conn| conn.server_version if conn.respond_to?(:server_version)} || super) end |
#tables(server = nil) ⇒ Object
Return an array of symbols specifying table names in the current database.
150 151 152 |
# File 'lib/sequel_core/adapters/mysql.rb', line 150 def tables(server=nil) synchronize(server){|conn| conn.list_tables.map {|t| t.to_sym}} end |
#transaction(server = nil) ⇒ Object
Support single level transactions on MySQL.
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 |
# File 'lib/sequel_core/adapters/mysql.rb', line 155 def transaction(server=nil) synchronize(server) do |conn| return yield(conn) if @transactions.include?(Thread.current) log_info(SQL_BEGIN) conn.query(SQL_BEGIN) begin @transactions << Thread.current yield(conn) rescue ::Exception => e log_info(SQL_ROLLBACK) conn.query(SQL_ROLLBACK) raise (Mysql::Error === e ? Error.new(e.) : e) unless Error::Rollback === e ensure unless e log_info(SQL_COMMIT) conn.query(SQL_COMMIT) end @transactions.delete(Thread.current) end end end |