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_selectorObject



21
22
23
# File 'lib/jdbc_adapter/jdbc_mysql.rb', line 21

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

.column_selectorObject



17
18
19
# File 'lib/jdbc_adapter/jdbc_mysql.rb', line 17

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

.extended(adapter) ⇒ Object



25
26
27
# File 'lib/jdbc_adapter/jdbc_mysql.rb', line 25

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

Instance Method Details

#add_limit_offset!(sql, options) ⇒ Object

:nodoc:



151
152
153
154
155
156
157
158
159
# File 'lib/jdbc_adapter/jdbc_mysql.rb', line 151

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

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

:nodoc:



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

def change_column(table_name, column_name, type, options = {}) #:nodoc:
  unless options.include?(:default) && !(options[:null] == false && options[:default].nil?)
    options[:default] = select_one("SHOW COLUMNS FROM #{table_name} LIKE '#{column_name}'")["Default"]
  end
  
  change_column_sql = "ALTER TABLE #{table_name} CHANGE #{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:



130
131
132
133
134
# File 'lib/jdbc_adapter/jdbc_mysql.rb', line 130

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

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

#create_database(name) ⇒ Object

:nodoc:



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

def create_database(name) #:nodoc:
  execute "CREATE DATABASE `#{name}`"
end

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

:nodoc:



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

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

#current_databaseObject



118
119
120
# File 'lib/jdbc_adapter/jdbc_mysql.rb', line 118

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

#drop_database(name) ⇒ Object

:nodoc:



114
115
116
# File 'lib/jdbc_adapter/jdbc_mysql.rb', line 114

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

#modify_types(tp) ⇒ Object



55
56
57
58
59
60
61
# File 'lib/jdbc_adapter/jdbc_mysql.rb', line 55

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



65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/jdbc_adapter/jdbc_mysql.rb', line 65

def quote(value, column = nil)
  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

#quote_column_name(name) ⇒ Object

:nodoc:



78
79
80
# File 'lib/jdbc_adapter/jdbc_mysql.rb', line 78

def quote_column_name(name) #:nodoc:
    "`#{name}`"
end

#quoted_falseObject



86
87
88
# File 'lib/jdbc_adapter/jdbc_mysql.rb', line 86

def quoted_false
    "0"
end

#quoted_trueObject



82
83
84
# File 'lib/jdbc_adapter/jdbc_mysql.rb', line 82

def quoted_true
    "1"
end

#recreate_database(name) ⇒ Object

:nodoc:



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

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

#rename_column(table_name, column_name, new_column_name) ⇒ Object

:nodoc:



146
147
148
149
# File 'lib/jdbc_adapter/jdbc_mysql.rb', line 146

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

#rename_table(name, new_name) ⇒ Object



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

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

#structure_dumpObject

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



92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/jdbc_adapter/jdbc_mysql.rb', line 92

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')
    structure += select_one("SHOW CREATE TABLE #{table.to_a.first.last}")["Create Table"] + ";\n\n"
  end
end