Class: Sequel::MySQL::Database

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

Constant Summary collapse

AUTO_INCREMENT =
'AUTO_INCREMENT'.freeze

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, #quote_identifiers

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, #get, #initialize, #inspect, #log_info, #logger, #logger=, #multi_threaded?, #query, quote_identifiers=, #quote_identifiers?, #rename_column, #rename_table, #select, set_adapter_scheme, #set_column_default, #set_column_type, single_threaded=, #single_threaded?, #synchronize, #table_exists?, #test_connection, #typecast_value, #uri, uri_to_options

Methods included from Schema::SQL

#alter_table_sql_list, #column_list_sql, #constraint_definition_sql, #create_table_sql_list, #default_index_name, #drop_table_sql, #filter_expr, #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

#alter_table_sql(table, op) ⇒ Object



171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/sequel_core/adapters/mysql.rb', line 171

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

#auto_increment_sqlObject



104
105
106
# File 'lib/sequel_core/adapters/mysql.rb', line 104

def auto_increment_sql
  AUTO_INCREMENT
end

#column_definition_sql(column) ⇒ Object



186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# File 'lib/sequel_core/adapters/mysql.rb', line 186

def column_definition_sql(column)
  if column[:type] == :check
    return constraint_definition_sql(column)
  end
  sql = "#{literal(column[:name].to_sym)} #{TYPES[column[:type]]}"
  column[:size] ||= 255 if column[:type] == :varchar
  elements = column[:size] || column[:elements]
  sql << literal(Array(elements)) if elements
  sql << UNSIGNED if column[:unsigned]
  sql << UNIQUE if column[:unique]
  sql << NOT_NULL if column[:null] == false
  sql << NULL if column[:null] == true
  sql << " DEFAULT #{literal(column[:default])}" if column.include?(:default)
  sql << PRIMARY_KEY if column[:primary_key]
  sql << " #{auto_increment_sql}" if column[:auto_increment]
  if column[:table]
    sql << ", FOREIGN KEY (#{literal(column[:name].to_sym)}) REFERENCES #{column[:table]}"
    sql << literal(Array(column[:key])) if column[:key]
    sql << " ON DELETE #{on_delete_clause(column[:on_delete])}" if column[:on_delete]
  end
  sql
end

#connectObject



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/sequel_core/adapters/mysql.rb', line 108

def connect
  conn = Mysql.init
  conn.options(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.reconnect = true
  conn
end

#dataset(opts = nil) ⇒ Object



144
145
146
# File 'lib/sequel_core/adapters/mysql.rb', line 144

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

#disconnectObject



134
135
136
# File 'lib/sequel_core/adapters/mysql.rb', line 134

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

#execute(sql, &block) ⇒ Object



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

def execute(sql, &block)
  begin
    log_info(sql)
    @pool.hold do |conn|
      conn.query(sql)
      block[conn] if block
    end
  rescue Mysql::Error => e
    raise Error.new(e.message)
  end
end

#execute_select(sql, &block) ⇒ Object



160
161
162
163
164
165
166
167
168
169
# File 'lib/sequel_core/adapters/mysql.rb', line 160

def execute_select(sql, &block)
  execute(sql) do |c|
    r = c.use_result
    begin
      block[r]
    ensure
      r.free
    end
  end
end

#index_definition_sql(table_name, index) ⇒ Object



209
210
211
212
213
214
215
216
217
218
219
220
221
222
# File 'lib/sequel_core/adapters/mysql.rb', line 209

def index_definition_sql(table_name, index)
  index_name = index[:name] || default_index_name(table_name, index[:columns])
  unique = "UNIQUE " if index[:unique]
  case index[:type]
  when :full_text
    "CREATE FULLTEXT INDEX #{index_name} ON #{table_name} #{literal(index[:columns])}"
  when :spatial
    "CREATE SPATIAL INDEX #{index_name} ON #{table_name} #{literal(index[:columns])}"
  when nil
    "CREATE #{unique}INDEX #{index_name} ON #{table_name} #{literal(index[:columns])}"
  else
    "CREATE #{unique}INDEX #{index_name} ON #{table_name} #{literal(index[:columns])} USING #{index[:type]}"
  end
end

#serial_primary_key_optionsObject



98
99
100
# File 'lib/sequel_core/adapters/mysql.rb', line 98

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

#server_versionObject



87
88
89
90
91
92
93
94
95
96
# File 'lib/sequel_core/adapters/mysql.rb', line 87

def server_version
  @server_version ||= pool.hold do |conn|
    if conn.respond_to?(:server_version)
      pool.hold {|c| c.server_version}
    else
      m = /(\d+)\.(\d+)\.(\d+)/.match(get(:version[]))
      (m[1].to_i * 10000) + (m[2].to_i * 100) + m[3].to_i
    end
  end
end

#tablesObject



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

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

#transactionObject



224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
# File 'lib/sequel_core/adapters/mysql.rb', line 224

def transaction
  @pool.hold do |conn|
    @transactions ||= []
    if @transactions.include? Thread.current
      return yield(conn)
    end
    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.message) : e) unless Error::Rollback === e
    ensure
      unless e
        log_info(SQL_COMMIT)
        conn.query(SQL_COMMIT)
      end
      @transactions.delete(Thread.current)
    end
  end
end

#use(db_name) ⇒ Object

Changes the database in use by issuing a USE statement.



250
251
252
253
254
255
# File 'lib/sequel_core/adapters/mysql.rb', line 250

def use(db_name)
  disconnect
  @opts[:database] = db_name if self << "USE #{db_name}"
  @schemas = nil
  self
end