Class: Sequel::MySQL::Database

Inherits:
Database show all
Defined in:
lib/sequel/adapters/mysql.rb

Constant Summary collapse

AUTO_INCREMENT =
'AUTO_INCREMENT'.freeze

Constants inherited from Database

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::PRIMARY_KEY, Schema::SQL::RESTRICT, Schema::SQL::SET_DEFAULT, Schema::SQL::SET_NULL, Schema::SQL::TYPES, Schema::SQL::UNDERSCORE, Schema::SQL::UNIQUE

Instance Attribute Summary

Attributes inherited from Database

#logger, #opts, #pool

Instance Method Summary collapse

Methods inherited from Database

#<<, #[], adapter_class, adapter_scheme, #add_column, #add_index, #alter_table, connect, #create_or_replace_view, #create_table, #create_table!, #create_view, #drop_column, #drop_index, #drop_table, #drop_view, #fetch, #from, #initialize, #multi_threaded?, #query, #rename_column, #rename_table, #select, set_adapter_scheme, #set_column_default, #set_column_type, single_threaded=, #single_threaded?, #synchronize, #table_exists?, #test_connection, #uri, uri_to_options

Methods included from Schema::SQL

#alter_table_sql_list, #column_definition_sql, #column_list_sql, #create_table_sql_list, #default_index_name, #drop_table_sql, #index_definition_sql, #index_list_sql_list, #literal, #on_delete_clause, #rename_table_sql, #schema_utility_dataset

Constructor Details

This class inherits a constructor from Sequel::Database

Instance Method Details

#alter_table_sql(table, op) ⇒ Object



146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/sequel/adapters/mysql.rb', line 146

def alter_table_sql(table, op)
  case op[:op]
  when :rename_column
    "ALTER TABLE #{table} CHANGE COLUMN #{literal(op[:name])} #{literal(op[:new_name])} #{op[:type]}"
  when :set_column_type
    "ALTER TABLE #{table} CHANGE COLUMN #{literal(op[:name])} #{literal(op[:name])} #{op[:type]}"
  when :drop_index
    "DROP INDEX #{default_index_name(table, op[:columns])} ON #{table}"
  else
    super(table, op)
  end
end

#auto_increment_sqlObject



84
85
86
# File 'lib/sequel/adapters/mysql.rb', line 84

def auto_increment_sql
  AUTO_INCREMENT
end

#connectObject



88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/sequel/adapters/mysql.rb', line 88

def connect
  conn = Mysql.real_connect(@opts[:host], @opts[:user], @opts[:password], 
  @opts[:database], @opts[:port], nil, Mysql::CLIENT_MULTI_RESULTS)
  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_results = '#{encoding}'")
  end
  conn.reconnect = true
  conn
end

#dataset(opts = nil) ⇒ Object



111
112
113
# File 'lib/sequel/adapters/mysql.rb', line 111

def dataset(opts = nil)
  MySQL::Dataset.new(self, opts)
end

#disconnectObject



101
102
103
# File 'lib/sequel/adapters/mysql.rb', line 101

def disconnect
  @pool.disconnect {|c| c.close}
end

#execute(sql) ⇒ Object



115
116
117
118
119
120
# File 'lib/sequel/adapters/mysql.rb', line 115

def execute(sql)
  @logger.info(sql) if @logger
  @pool.hold do |conn|
    conn.query(sql)
  end
end

#execute_affected(sql) ⇒ Object



138
139
140
141
142
143
144
# File 'lib/sequel/adapters/mysql.rb', line 138

def execute_affected(sql)
  @logger.info(sql) if @logger
  @pool.hold do |conn|
    conn.query(sql)
    conn.affected_rows
  end
end

#execute_insert(sql) ⇒ Object



130
131
132
133
134
135
136
# File 'lib/sequel/adapters/mysql.rb', line 130

def execute_insert(sql)
  @logger.info(sql) if @logger
  @pool.hold do |conn|
    conn.query(sql)
    conn.insert_id
  end
end

#execute_select(sql) ⇒ Object



122
123
124
125
126
127
128
# File 'lib/sequel/adapters/mysql.rb', line 122

def execute_select(sql)
  @logger.info(sql) if @logger
  @pool.hold do |conn|
    conn.query(sql)
    conn.use_result
  end
end

#serial_primary_key_optionsObject



78
79
80
# File 'lib/sequel/adapters/mysql.rb', line 78

def serial_primary_key_options
  {:primary_key => true, :type => :integer, :auto_increment => true}
end

#tablesObject



105
106
107
108
109
# File 'lib/sequel/adapters/mysql.rb', line 105

def tables
  @pool.hold do |conn|
    conn.list_tables.map {|t| t.to_sym}
  end
end

#transactionObject



159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/sequel/adapters/mysql.rb', line 159

def transaction
  @pool.hold do |conn|
    @transactions ||= []
    if @transactions.include? Thread.current
      return yield(conn)
    end
    conn.query(SQL_BEGIN)
    begin
      @transactions << Thread.current
      result = yield(conn)
      conn.query(SQL_COMMIT)
      result
    rescue => e
      conn.query(SQL_ROLLBACK)
      raise e unless Error::Rollback === e
    ensure
      @transactions.delete(Thread.current)
    end
  end
end