Module: JdbcSpec::MySQL

Defined in:
lib/jdbc_adapter/jdbc_mysql.rb

Defined Under Namespace

Modules: Column

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.adapter_matcher(name) ⇒ Object



28
29
30
# File 'lib/jdbc_adapter/jdbc_mysql.rb', line 28

def self.adapter_matcher(name, *)
  name =~ /mysql/i ? self : false
end

.column_selectorObject



32
33
34
# File 'lib/jdbc_adapter/jdbc_mysql.rb', line 32

def self.column_selector
  [/mysql/i, lambda {|cfg,col| col.extend(::JdbcSpec::MySQL::Column)}]
end

.extended(adapter) ⇒ Object



36
37
38
# File 'lib/jdbc_adapter/jdbc_mysql.rb', line 36

def self.extended(adapter)
  adapter.execute("SET SQL_AUTO_IS_NULL=0")
end

Instance Method Details

#adapter_nameObject

:nodoc:



74
75
76
# File 'lib/jdbc_adapter/jdbc_mysql.rb', line 74

def adapter_name #:nodoc:
  'mysql'
end

#add_limit_offset!(sql, options) ⇒ Object

:nodoc:



227
228
229
230
231
232
233
234
235
# File 'lib/jdbc_adapter/jdbc_mysql.rb', line 227

def add_limit_offset!(sql, options) #:nodoc:
  if limit = options[:limit]
    unless offset = options[:offset]
      sql << " LIMIT #{limit}"
    else
      sql << " LIMIT #{offset}, #{limit}"
    end
  end
end

#begin_db_transactionObject

:nodoc:



103
104
105
106
107
# File 'lib/jdbc_adapter/jdbc_mysql.rb', line 103

def begin_db_transaction #:nodoc:
  @connection.begin
rescue Exception
  # Transactions aren't supported
end

#change_column(table_name, column_name, type, options = {}) ⇒ Object

:nodoc:



207
208
209
210
211
212
213
214
215
216
217
218
219
# File 'lib/jdbc_adapter/jdbc_mysql.rb', line 207

def change_column(table_name, column_name, type, options = {}) #:nodoc:
  unless options_include_default?(options)
    if column = columns(table_name).find { |c| c.name == column_name.to_s }
      options[:default] = column.default
    else
      raise "No such column: #{table_name}.#{column_name}"
    end
  end

  change_column_sql = "ALTER TABLE #{quote_table_name(table_name)} CHANGE #{quote_column_name(column_name)} #{quote_column_name(column_name)} #{type_to_sql(type, options[:limit], options[:precision], options[:scale])}"
  add_column_options!(change_column_sql, options)
  execute(change_column_sql)
end

#change_column_default(table_name, column_name, default) ⇒ Object

:nodoc:



201
202
203
204
205
# File 'lib/jdbc_adapter/jdbc_mysql.rb', line 201

def change_column_default(table_name, column_name, default) #:nodoc:
  current_type = select_one("SHOW COLUMNS FROM #{quote_table_name(table_name)} LIKE '#{column_name}'")["Type"]

  execute("ALTER TABLE #{quote_table_name(table_name)} CHANGE #{quote_column_name(column_name)} #{quote_column_name(column_name)} #{current_type} DEFAULT #{quote(default)}")
end

#charsetObject



243
244
245
# File 'lib/jdbc_adapter/jdbc_mysql.rb', line 243

def charset
  show_variable("character_set_database")
end

#collationObject



247
248
249
# File 'lib/jdbc_adapter/jdbc_mysql.rb', line 247

def collation
  show_variable("collation_database")
end

#commit_db_transactionObject

:nodoc:



109
110
111
112
113
# File 'lib/jdbc_adapter/jdbc_mysql.rb', line 109

def commit_db_transaction #:nodoc:
  @connection.commit
rescue Exception
  # Transactions aren't supported
end

#create_database(name, options = {}) ⇒ Object

:nodoc:



181
182
183
# File 'lib/jdbc_adapter/jdbc_mysql.rb', line 181

def create_database(name, options = {}) #:nodoc:
  execute "CREATE DATABASE `#{name}` DEFAULT #{character_set(options)}"
end

#create_savepointObject



125
126
127
# File 'lib/jdbc_adapter/jdbc_mysql.rb', line 125

def create_savepoint
  execute("SAVEPOINT #{current_savepoint_name}")
end

#create_table(name, options = {}) ⇒ Object

:nodoc:



193
194
195
# File 'lib/jdbc_adapter/jdbc_mysql.rb', line 193

def create_table(name, options = {}) #:nodoc:
  super(name, {:options => "ENGINE=InnoDB #{character_set(options)}"}.merge(options))
end

#current_databaseObject



189
190
191
# File 'lib/jdbc_adapter/jdbc_mysql.rb', line 189

def current_database
  select_one("SELECT DATABASE() as db")["db"]
end

#disable_referential_integrity(&block) ⇒ Object

:nodoc:



137
138
139
140
141
142
143
144
145
# File 'lib/jdbc_adapter/jdbc_mysql.rb', line 137

def disable_referential_integrity(&block) #:nodoc:
  old = select_value("SELECT @@FOREIGN_KEY_CHECKS")
  begin
    update("SET FOREIGN_KEY_CHECKS = 0")
    yield
  ensure
    update("SET FOREIGN_KEY_CHECKS = #{old}")
  end
end

#drop_database(name) ⇒ Object

:nodoc:



185
186
187
# File 'lib/jdbc_adapter/jdbc_mysql.rb', line 185

def drop_database(name) #:nodoc:
  execute "DROP DATABASE IF EXISTS `#{name}`"
end

#modify_types(tp) ⇒ Object



66
67
68
69
70
71
72
# File 'lib/jdbc_adapter/jdbc_mysql.rb', line 66

def modify_types(tp)
  tp[:primary_key] = "int(11) DEFAULT NULL auto_increment PRIMARY KEY"
  tp[:decimal] = { :name => "decimal" }
  tp[:timestamp] = { :name => "datetime" }
  tp[:datetime][:limit] = nil
  tp
end

#quote(value, column = nil) ⇒ Object

QUOTING ==================================================



80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/jdbc_adapter/jdbc_mysql.rb', line 80

def quote(value, column = nil)
  return value.quoted_id if value.respond_to?(:quoted_id)

  if column && column.type == :primary_key
    value.to_s
  elsif column && String === value && column.type == :binary && column.class.respond_to?(:string_to_binary)
    s = column.class.string_to_binary(value).unpack("H*")[0]
    "x'#{s}'"
  elsif BigDecimal === value
    "'#{value.to_s("F")}'"
  else
    super
  end
end

#quoted_falseObject



99
100
101
# File 'lib/jdbc_adapter/jdbc_mysql.rb', line 99

def quoted_false
    "0"
end

#quoted_trueObject



95
96
97
# File 'lib/jdbc_adapter/jdbc_mysql.rb', line 95

def quoted_true
    "1"
end

#recreate_database(name, options = {}) ⇒ Object

:nodoc:



169
170
171
172
# File 'lib/jdbc_adapter/jdbc_mysql.rb', line 169

def recreate_database(name, options = {}) #:nodoc:
  drop_database(name)
  create_database(name, options)
end

#release_savepointObject



133
134
135
# File 'lib/jdbc_adapter/jdbc_mysql.rb', line 133

def release_savepoint
  execute("RELEASE SAVEPOINT #{current_savepoint_name}")
end

#rename_column(table_name, column_name, new_column_name) ⇒ Object

:nodoc:



221
222
223
224
225
# File 'lib/jdbc_adapter/jdbc_mysql.rb', line 221

def rename_column(table_name, column_name, new_column_name) #:nodoc:
  cols = select_one("SHOW COLUMNS FROM #{quote_table_name(table_name)} LIKE '#{column_name}'")
  current_type = cols["Type"] || cols["COLUMN_TYPE"]
  execute "ALTER TABLE #{quote_table_name(table_name)} CHANGE #{quote_table_name(column_name)} #{quote_column_name(new_column_name)} #{current_type}"
end

#rename_table(name, new_name) ⇒ Object



197
198
199
# File 'lib/jdbc_adapter/jdbc_mysql.rb', line 197

def rename_table(name, new_name)
  execute "RENAME TABLE #{quote_table_name(name)} TO #{quote_table_name(new_name)}"
end

#rollback_db_transactionObject

:nodoc:



115
116
117
118
119
# File 'lib/jdbc_adapter/jdbc_mysql.rb', line 115

def rollback_db_transaction #:nodoc:
  @connection.rollback
rescue Exception
  # Transactions aren't supported
end

#rollback_to_savepointObject



129
130
131
# File 'lib/jdbc_adapter/jdbc_mysql.rb', line 129

def rollback_to_savepoint
  execute("ROLLBACK TO SAVEPOINT #{current_savepoint_name}")
end

#show_variable(var) ⇒ Object



237
238
239
240
241
# File 'lib/jdbc_adapter/jdbc_mysql.rb', line 237

def show_variable(var)
  res = execute("show variables like '#{var}'")
  row = res.detect {|row| row["Variable_name"] == var }
  row && row["Value"]
end

#structure_dumpObject

SCHEMA STATEMENTS ========================================



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/jdbc_adapter/jdbc_mysql.rb', line 149

def structure_dump #:nodoc:
  if supports_views?
    sql = "SHOW FULL TABLES WHERE Table_type = 'BASE TABLE'"
  else
    sql = "SHOW TABLES"
  end

  select_all(sql).inject("") do |structure, table|
    table.delete('Table_type')

    hash = show_create_table(table.to_a.first.last)

    if(table = hash["Create Table"])
      structure += table + ";\n\n"
    elsif(view = hash["Create View"])
      structure += view + ";\n\n"
    end
  end
end

#supports_savepoints?Boolean

:nodoc:

Returns:

  • (Boolean)


121
122
123
# File 'lib/jdbc_adapter/jdbc_mysql.rb', line 121

def supports_savepoints? #:nodoc:
  true
end