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

#add_limit_offset!(sql, options) ⇒ Object

:nodoc:



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

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:



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

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

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

:nodoc:



187
188
189
190
191
192
193
194
195
196
197
198
199
# File 'lib/jdbc_adapter/jdbc_mysql.rb', line 187

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:



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

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



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

def charset
  show_variable("character_set_database")
end

#collationObject



227
228
229
# File 'lib/jdbc_adapter/jdbc_mysql.rb', line 227

def collation
  show_variable("collation_database")
end

#commit_db_transactionObject

:nodoc:



105
106
107
108
109
# File 'lib/jdbc_adapter/jdbc_mysql.rb', line 105

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

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

:nodoc:



161
162
163
# File 'lib/jdbc_adapter/jdbc_mysql.rb', line 161

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

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

:nodoc:



173
174
175
# File 'lib/jdbc_adapter/jdbc_mysql.rb', line 173

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

#current_databaseObject



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

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

#disable_referential_integrity(&block) ⇒ Object

:nodoc:



117
118
119
120
121
122
123
124
125
# File 'lib/jdbc_adapter/jdbc_mysql.rb', line 117

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:



165
166
167
# File 'lib/jdbc_adapter/jdbc_mysql.rb', line 165

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 ==================================================



76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/jdbc_adapter/jdbc_mysql.rb', line 76

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



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

def quoted_false
    "0"
end

#quoted_trueObject



91
92
93
# File 'lib/jdbc_adapter/jdbc_mysql.rb', line 91

def quoted_true
    "1"
end

#recreate_database(name) ⇒ Object

:nodoc:



149
150
151
152
# File 'lib/jdbc_adapter/jdbc_mysql.rb', line 149

def recreate_database(name) #:nodoc:
  drop_database(name)
  create_database(name)
end

#rename_column(table_name, column_name, new_column_name) ⇒ Object

:nodoc:



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

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



177
178
179
# File 'lib/jdbc_adapter/jdbc_mysql.rb', line 177

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

#rollback_db_transactionObject

:nodoc:



111
112
113
114
115
# File 'lib/jdbc_adapter/jdbc_mysql.rb', line 111

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

#show_variable(var) ⇒ Object



217
218
219
220
221
# File 'lib/jdbc_adapter/jdbc_mysql.rb', line 217

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 ========================================



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/jdbc_adapter/jdbc_mysql.rb', line 129

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 = select_one("SHOW CREATE TABLE #{quote_table_name(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