Module: ArJdbc::MySQL
- Included in:
- ActiveRecord::ConnectionAdapters::MysqlAdapter
- Defined in:
- lib/arjdbc/mysql/adapter.rb
Defined Under Namespace
Modules: Column
Class Method Summary collapse
Instance Method Summary collapse
-
#adapter_name ⇒ Object
:nodoc:.
- #add_column(table_name, column_name, type, options = {}) ⇒ Object
- #add_column_position!(sql, options) ⇒ Object
-
#add_limit_offset!(sql, options) ⇒ Object
:nodoc:.
- #arel2_visitors ⇒ Object
-
#begin_db_transaction ⇒ Object
:nodoc:.
- #case_sensitive_equality_operator ⇒ Object
- #case_sensitive_modifier(node) ⇒ Object
-
#change_column(table_name, column_name, type, options = {}) ⇒ Object
:nodoc:.
-
#change_column_default(table_name, column_name, default) ⇒ Object
:nodoc:.
- #change_column_null(table_name, column_name, null, default = nil) ⇒ Object
- #charset ⇒ Object
- #collation ⇒ Object
-
#commit_db_transaction ⇒ Object
:nodoc:.
- #configure_connection ⇒ Object
-
#create_database(name, options = {}) ⇒ Object
:nodoc:.
- #create_savepoint ⇒ Object
-
#create_table(name, options = {}) ⇒ Object
:nodoc:.
- #current_database ⇒ Object
-
#disable_referential_integrity(&block) ⇒ Object
:nodoc:.
-
#drop_database(name) ⇒ Object
:nodoc:.
-
#jdbc_columns(table_name, name = nil) ⇒ Object
:nodoc:.
- #limited_update_conditions(where_sql, quoted_table_name, quoted_primary_key) ⇒ Object
- #modify_types(tp) ⇒ Object
-
#quote(value, column = nil) ⇒ Object
QUOTING ==================================================.
- #quoted_false ⇒ Object
- #quoted_true ⇒ Object
-
#recreate_database(name, options = {}) ⇒ Object
:nodoc:.
- #release_savepoint ⇒ Object
-
#rename_column(table_name, column_name, new_column_name) ⇒ Object
:nodoc:.
- #rename_table(name, new_name) ⇒ Object
-
#rollback_db_transaction ⇒ Object
:nodoc:.
- #rollback_to_savepoint ⇒ Object
- #show_variable(var) ⇒ Object
-
#structure_dump ⇒ Object
SCHEMA STATEMENTS ========================================.
-
#supports_savepoints? ⇒ Boolean
:nodoc:.
- #type_to_sql(type, limit = nil, precision = nil, scale = nil) ⇒ Object
Class Method Details
.column_selector ⇒ Object
5 6 7 |
# File 'lib/arjdbc/mysql/adapter.rb', line 5 def self.column_selector [/mysql/i, lambda {|cfg,col| col.extend(::ArJdbc::MySQL::Column)}] end |
.extended(adapter) ⇒ Object
9 10 11 |
# File 'lib/arjdbc/mysql/adapter.rb', line 9 def self.extended(adapter) adapter.configure_connection end |
.jdbc_connection_class ⇒ Object
17 18 19 |
# File 'lib/arjdbc/mysql/adapter.rb', line 17 def self.jdbc_connection_class ::ActiveRecord::ConnectionAdapters::MySQLJdbcConnection end |
Instance Method Details
#adapter_name ⇒ Object
:nodoc:
97 98 99 |
# File 'lib/arjdbc/mysql/adapter.rb', line 97 def adapter_name #:nodoc: 'MySQL' end |
#add_column(table_name, column_name, type, options = {}) ⇒ Object
244 245 246 247 248 249 |
# File 'lib/arjdbc/mysql/adapter.rb', line 244 def add_column(table_name, column_name, type, = {}) add_column_sql = "ALTER TABLE #{quote_table_name(table_name)} ADD #{quote_column_name(column_name)} #{type_to_sql(type, [:limit], [:precision], [:scale])}" (add_column_sql, ) add_column_position!(add_column_sql, ) execute(add_column_sql) end |
#add_column_position!(sql, options) ⇒ Object
336 337 338 339 340 341 342 |
# File 'lib/arjdbc/mysql/adapter.rb', line 336 def add_column_position!(sql, ) if [:first] sql << " FIRST" elsif [:after] sql << " AFTER #{quote_column_name([:after])}" end end |
#add_limit_offset!(sql, options) ⇒ Object
:nodoc:
297 298 299 300 301 302 303 304 305 306 307 |
# File 'lib/arjdbc/mysql/adapter.rb', line 297 def add_limit_offset!(sql, ) #:nodoc: limit, offset = [:limit], [:offset] if limit && offset sql << " LIMIT #{offset.to_i}, #{sanitize_limit(limit)}" elsif limit sql << " LIMIT #{sanitize_limit(limit)}" elsif offset sql << " OFFSET #{offset.to_i}" end sql end |
#arel2_visitors ⇒ Object
101 102 103 |
# File 'lib/arjdbc/mysql/adapter.rb', line 101 def arel2_visitors {'jdbcmysql' => ::Arel::Visitors::MySQL} end |
#begin_db_transaction ⇒ Object
:nodoc:
142 143 144 145 146 |
# File 'lib/arjdbc/mysql/adapter.rb', line 142 def begin_db_transaction #:nodoc: @connection.begin rescue Exception # Transactions aren't supported end |
#case_sensitive_equality_operator ⇒ Object
105 106 107 |
# File 'lib/arjdbc/mysql/adapter.rb', line 105 def case_sensitive_equality_operator "= BINARY" end |
#case_sensitive_modifier(node) ⇒ Object
109 110 111 |
# File 'lib/arjdbc/mysql/adapter.rb', line 109 def case_sensitive_modifier(node) Arel::Nodes::Bin.new(node) end |
#change_column(table_name, column_name, type, options = {}) ⇒ Object
:nodoc:
266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 |
# File 'lib/arjdbc/mysql/adapter.rb', line 266 def change_column(table_name, column_name, type, = {}) #:nodoc: column = column_for(table_name, column_name) unless () [:default] = column.default end unless .has_key?(:null) [:null] = column.null 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, [:limit], [:precision], [:scale])}" (change_column_sql, ) add_column_position!(change_column_sql, ) execute(change_column_sql) end |
#change_column_default(table_name, column_name, default) ⇒ Object
:nodoc:
251 252 253 254 |
# File 'lib/arjdbc/mysql/adapter.rb', line 251 def change_column_default(table_name, column_name, default) #:nodoc: column = column_for(table_name, column_name) change_column table_name, column_name, column.sql_type, :default => default end |
#change_column_null(table_name, column_name, null, default = nil) ⇒ Object
256 257 258 259 260 261 262 263 264 |
# File 'lib/arjdbc/mysql/adapter.rb', line 256 def change_column_null(table_name, column_name, null, default = nil) column = column_for(table_name, column_name) unless null || default.nil? execute("UPDATE #{quote_table_name(table_name)} SET #{quote_column_name(column_name)}=#{quote(default)} WHERE #{quote_column_name(column_name)} IS NULL") end change_column table_name, column_name, column.sql_type, :null => null end |
#charset ⇒ Object
315 316 317 |
# File 'lib/arjdbc/mysql/adapter.rb', line 315 def charset show_variable("character_set_database") end |
#collation ⇒ Object
319 320 321 |
# File 'lib/arjdbc/mysql/adapter.rb', line 319 def collation show_variable("collation_database") end |
#commit_db_transaction ⇒ Object
:nodoc:
148 149 150 151 152 |
# File 'lib/arjdbc/mysql/adapter.rb', line 148 def commit_db_transaction #:nodoc: @connection.commit rescue Exception # Transactions aren't supported end |
#configure_connection ⇒ Object
13 14 15 |
# File 'lib/arjdbc/mysql/adapter.rb', line 13 def configure_connection execute("SET SQL_AUTO_IS_NULL=0") end |
#create_database(name, options = {}) ⇒ Object
:nodoc:
220 221 222 223 224 225 226 |
# File 'lib/arjdbc/mysql/adapter.rb', line 220 def create_database(name, = {}) #:nodoc: if [:collation] execute "CREATE DATABASE `#{name}` DEFAULT CHARACTER SET `#{[:charset] || 'utf8'}` COLLATE `#{[:collation]}`" else execute "CREATE DATABASE `#{name}` DEFAULT CHARACTER SET `#{[:charset] || 'utf8'}`" end end |
#create_savepoint ⇒ Object
164 165 166 |
# File 'lib/arjdbc/mysql/adapter.rb', line 164 def create_savepoint execute("SAVEPOINT #{current_savepoint_name}") end |
#create_table(name, options = {}) ⇒ Object
:nodoc:
236 237 238 |
# File 'lib/arjdbc/mysql/adapter.rb', line 236 def create_table(name, = {}) #:nodoc: super(name, {:options => "ENGINE=InnoDB"}.merge()) end |
#current_database ⇒ Object
232 233 234 |
# File 'lib/arjdbc/mysql/adapter.rb', line 232 def current_database select_one("SELECT DATABASE() as db")["db"] end |
#disable_referential_integrity(&block) ⇒ Object
:nodoc:
176 177 178 179 180 181 182 183 184 |
# File 'lib/arjdbc/mysql/adapter.rb', line 176 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:
228 229 230 |
# File 'lib/arjdbc/mysql/adapter.rb', line 228 def drop_database(name) #:nodoc: execute "DROP DATABASE IF EXISTS `#{name}`" end |
#jdbc_columns(table_name, name = nil) ⇒ Object
:nodoc:
208 209 210 211 212 213 |
# File 'lib/arjdbc/mysql/adapter.rb', line 208 def jdbc_columns(table_name, name = nil)#:nodoc: sql = "SHOW FIELDS FROM #{quote_table_name(table_name)}" execute(sql, :skip_logging).map do |field| ::ActiveRecord::ConnectionAdapters::MysqlColumn.new(field["Field"], field["Default"], field["Type"], field["Null"] == "YES") end end |
#limited_update_conditions(where_sql, quoted_table_name, quoted_primary_key) ⇒ Object
113 114 115 |
# File 'lib/arjdbc/mysql/adapter.rb', line 113 def limited_update_conditions(where_sql, quoted_table_name, quoted_primary_key) where_sql end |
#modify_types(tp) ⇒ Object
88 89 90 91 92 93 94 95 |
# File 'lib/arjdbc/mysql/adapter.rb', line 88 def modify_types(tp) tp[:primary_key] = "int(11) DEFAULT NULL auto_increment PRIMARY KEY" tp[:integer] = { :name => 'int', :limit => 4 } tp[:decimal] = { :name => "decimal" } tp[:timestamp] = { :name => "datetime" } tp[:datetime][:limit] = nil tp end |
#quote(value, column = nil) ⇒ Object
QUOTING ==================================================
119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/arjdbc/mysql/adapter.rb', line 119 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_false ⇒ Object
138 139 140 |
# File 'lib/arjdbc/mysql/adapter.rb', line 138 def quoted_false "0" end |
#quoted_true ⇒ Object
134 135 136 |
# File 'lib/arjdbc/mysql/adapter.rb', line 134 def quoted_true "1" end |
#recreate_database(name, options = {}) ⇒ Object
:nodoc:
215 216 217 218 |
# File 'lib/arjdbc/mysql/adapter.rb', line 215 def recreate_database(name, = {}) #:nodoc: drop_database(name) create_database(name, ) end |
#release_savepoint ⇒ Object
172 173 174 |
# File 'lib/arjdbc/mysql/adapter.rb', line 172 def release_savepoint execute("RELEASE SAVEPOINT #{current_savepoint_name}") end |
#rename_column(table_name, column_name, new_column_name) ⇒ Object
:nodoc:
283 284 285 286 287 288 289 290 291 292 293 294 295 |
# File 'lib/arjdbc/mysql/adapter.rb', line 283 def rename_column(table_name, column_name, new_column_name) #:nodoc: = {} if column = columns(table_name).find { |c| c.name == column_name.to_s } [:default] = column.default [:null] = column.null else raise ActiveRecord::ActiveRecordError, "No such column: #{table_name}.#{column_name}" end current_type = select_one("SHOW COLUMNS FROM #{quote_table_name(table_name)} LIKE '#{column_name}'")["Type"] rename_column_sql = "ALTER TABLE #{quote_table_name(table_name)} CHANGE #{quote_column_name(column_name)} #{quote_column_name(new_column_name)} #{current_type}" (rename_column_sql, ) execute(rename_column_sql) end |
#rename_table(name, new_name) ⇒ Object
240 241 242 |
# File 'lib/arjdbc/mysql/adapter.rb', line 240 def rename_table(name, new_name) execute "RENAME TABLE #{quote_table_name(name)} TO #{quote_table_name(new_name)}" end |
#rollback_db_transaction ⇒ Object
:nodoc:
154 155 156 157 158 |
# File 'lib/arjdbc/mysql/adapter.rb', line 154 def rollback_db_transaction #:nodoc: @connection.rollback rescue Exception # Transactions aren't supported end |
#rollback_to_savepoint ⇒ Object
168 169 170 |
# File 'lib/arjdbc/mysql/adapter.rb', line 168 def rollback_to_savepoint execute("ROLLBACK TO SAVEPOINT #{current_savepoint_name}") end |
#show_variable(var) ⇒ Object
309 310 311 312 313 |
# File 'lib/arjdbc/mysql/adapter.rb', line 309 def show_variable(var) res = execute("show variables like '#{var}'") row = res.detect {|row| row["Variable_name"] == var } row && row["Value"] end |
#structure_dump ⇒ Object
SCHEMA STATEMENTS ========================================
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 |
# File 'lib/arjdbc/mysql/adapter.rb', line 188 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:
160 161 162 |
# File 'lib/arjdbc/mysql/adapter.rb', line 160 def supports_savepoints? #:nodoc: true end |
#type_to_sql(type, limit = nil, precision = nil, scale = nil) ⇒ Object
323 324 325 326 327 328 329 330 331 332 333 334 |
# File 'lib/arjdbc/mysql/adapter.rb', line 323 def type_to_sql(type, limit = nil, precision = nil, scale = nil) return super unless type.to_s == 'integer' case limit when 1; 'tinyint' when 2; 'smallint' when 3; 'mediumint' when nil, 4, 11; 'int(11)' # compatibility with MySQL default when 5..8; 'bigint' else raise(ActiveRecordError, "No integer type has byte size #{limit}") end end |