0
+require 'active_record/connection_adapters/abstract_adapter'
0
+require 'active_record/connection_adapters/dbslayer_connection'
0
+require 'active_record/connection_adapters/mysql_adapter'
0
+ # Establishes a connection to the database that's used by all Active Record objects.
0
+ def self.dbslayer_connection(config) # :nodoc:
0
+ config = config.symbolize_keys
0
+ connection = ConnectionAdapters::DbslayerConnection.new(host, port)
0
+ ConnectionAdapters::DbslayerAdapter.new(connection, logger, [host, port], config)
0
+ module ConnectionAdapters
0
+ class DbslayerColumn < MysqlColumn #:nodoc:
0
+ # def extract_default(default)
0
+ # if type == :binary || type == :text
0
+ # raise ArgumentError, "#{type} columns cannot have a default value: #{default.inspect}"
0
+ # elsif missing_default_forged_as_empty_string?(default)
0
+ def simplified_type(field_type)
0
+ return :boolean if DbslayerAdapter.emulate_booleans && field_type.downcase.index("tinyint(1)")
0
+ return :string if field_type =~ /enum/i
0
+ # MySQL misreports NOT NULL column default when none is given.
0
+ # We can't detect this for columns which may have a legitimate ''
0
+ # default (string) but we can for others (integer, datetime, boolean,
0
+ # Test whether the column has default '', is not null, and is not
0
+ # a type allowing default ''.
0
+ # def missing_default_forged_as_empty_string?(default)
0
+ # type != :string && !null && default == ''
0
+ # The DbslayerAdapter is an adapter to use Rails with the DBSlayer
0
+ # * <tt>:host</tt> -- Defaults to localhost
0
+ # * <tt>:port</tt> -- Defaults to 3306
0
+ # Like the MySQL adapter: by default, the MysqlAdapter will consider all columns of type tinyint(1)
0
+ # as boolean. If you wish to disable this emulation (which was the default
0
+ # behavior in versions 0.13.1 and earlier) you can add the following line
0
+ # to your environment.rb file:
0
+ # ActiveRecord::ConnectionAdapters::DbslayerAdapter.emulate_booleans = false
0
+ class DbslayerAdapter < MysqlAdapter
0
+ def initialize(connection, logger, connection_options, config)
0
+ super(connection, logger, connection_options, config)
0
+ ActiveRecord::Base.allow_concurrency = true
0
+ def adapter_name #:nodoc:
0
+ # def supports_migrations? #:nodoc:
0
+ # def native_database_types #:nodoc:
0
+ # :primary_key => "int(11) DEFAULT NULL auto_increment PRIMARY KEY",
0
+ # :string => { :name => "varchar", :limit => 255 },
0
+ # :text => { :name => "text" },
0
+ # :integer => { :name => "int", :limit => 11 },
0
+ # :float => { :name => "float" },
0
+ # :decimal => { :name => "decimal" },
0
+ # :datetime => { :name => "datetime" },
0
+ # :timestamp => { :name => "datetime" },
0
+ # :time => { :name => "time" },
0
+ # :date => { :name => "date" },
0
+ # :binary => { :name => "blob" },
0
+ # :boolean => { :name => "tinyint", :limit => 1 }
0
+ # # QUOTING ==================================================
0
+ # def quote(value, column = nil)
0
+ # if value.kind_of?(String) && column && column.type == :binary && column.class.respond_to?(:string_to_binary)
0
+ # s = column.class.string_to_binary(value).unpack("H*")[0]
0
+ # elsif value.kind_of?(BigDecimal)
0
+ # "'#{value.to_s("F")}'"
0
+ # def quote_column_name(name) #:nodoc:
0
+ # def quote_table_name(name) #:nodoc:
0
+ # quote_column_name(name).gsub('.', '`.`')
0
+ # def quote_string(string) #:nodoc:
0
+ # @connection.quote(string)
0
+ # REFERENTIAL INTEGRITY ====================================
0
+ def disable_referential_integrity(&block) #:nodoc:
0
+ #FIXME: I CAN'T LET YOU DO THIS
0
+ # old = select_value("SELECT @@FOREIGN_KEY_CHECKS")
0
+ # update("SET FOREIGN_KEY_CHECKS = 0")
0
+ # update("SET FOREIGN_KEY_CHECKS = #{old}")
0
+ # CONNECTION MANAGEMENT ====================================
0
+ stats = @connection.mysql_stats
0
+ !stats.nil? && !stats.empty?
0
+ # DO NOTHING, we connect on the request
0
+ # DO NOTHING, we connect on the request
0
+ # DATABASE STATEMENTS ======================================
0
+ def select_rows(sql, name = nil)
0
+ result = execute(sql, name)
0
+ def execute(sql, name = nil) #:nodoc:
0
+ @connection.execute(sql)
0
+ # def insert_sql(sql, name = nil, pk = nil, id_value = nil, sequence_name = nil) #:nodoc:
0
+ # id_value || @connection.insert_id
0
+ # def update_sql(sql, name = nil) #:nodoc:
0
+ # @connection.affected_rows
0
+ def begin_db_transaction #:nodoc:
0
+ # FIXME: raise exception?
0
+ # Transactions aren't supported
0
+ def commit_db_transaction #:nodoc:
0
+ # FIXME: raise exception?
0
+ # Transactions aren't supported
0
+ def rollback_db_transaction #:nodoc:
0
+ # FIXME: raise exception?
0
+ # Transactions aren't supported
0
+ # SCHEMA STATEMENTS ========================================
0
+ # def structure_dump #:nodoc:
0
+ # sql = "SHOW FULL TABLES WHERE Table_type = 'BASE TABLE'"
0
+ # select_all(sql).inject("") do |structure, table|
0
+ # table.delete('Table_type')
0
+ # structure += select_one("SHOW CREATE TABLE #{quote_table_name(table.to_a.first.last)}")["Create Table"] + ";\n\n"
0
+ # def recreate_database(name) #:nodoc:
0
+ # create_database(name)
0
+ # Create a new MySQL database with optional :charset and :collation.
0
+ # Charset defaults to utf8.
0
+ # create_database 'charset_test', :charset => 'latin1', :collation => 'latin1_bin'
0
+ # create_database 'matt_development'
0
+ # create_database 'matt_development', :charset => :big5
0
+ # def create_database(name, options = {})
0
+ # if options[:collation]
0
+ # execute "CREATE DATABASE `#{name}` DEFAULT CHARACTER SET `#{options[:charset] || 'utf8'}` COLLATE `#{options[:collation]}`"
0
+ # execute "CREATE DATABASE `#{name}` DEFAULT CHARACTER SET `#{options[:charset] || 'utf8'}`"
0
+ # def drop_database(name) #:nodoc:
0
+ # execute "DROP DATABASE IF EXISTS `#{name}`"
0
+ # def current_database
0
+ # select_value 'SELECT DATABASE() as db'
0
+ # Returns the database character set.
0
+ # @charset = show_variable 'character_set_database'
0
+ # # Returns the database collation strategy.
0
+ # @collation = show_variable 'collation_database'
0
+ def tables(name = nil) #:nodoc:
0
+ execute("SHOW TABLES", name).rows.each { |row| tables << row[0] }
0
+ # def drop_table(table_name, options = {})
0
+ # super(table_name, options)
0
+ def indexes(table_name, name = nil)#:nodoc:
0
+ execute("SHOW KEYS FROM #{quote_table_name(table_name)}", name).rows.each do |row|
0
+ if current_index != row[2]
0
+ next if row[2] == "PRIMARY" # skip the primary key
0
+ current_index = row[2]
0
+ indexes << IndexDefinition.new(row[0], row[2], row[1] == "0", [])
0
+ indexes.last.columns << row[4]
0
+ def columns(table_name, name = nil)#:nodoc:
0
+ sql = "SHOW FIELDS FROM #{quote_table_name(table_name)}"
0
+ execute(sql, name).rows.each { |row| columns << DbslayerColumn.new(row[0], row[4], row[1], row[2] == "YES") }
0
+ # def create_table(table_name, options = {}) #:nodoc:
0
+ # super(table_name, options.reverse_merge(:options => "ENGINE=InnoDB"))
0
+ # def rename_table(table_name, new_name)
0
+ # execute "RENAME TABLE #{quote_table_name(table_name)} TO #{quote_table_name(new_name)}"
0
+ # def change_column_default(table_name, column_name, default) #:nodoc:
0
+ # current_type = select_one("SHOW COLUMNS FROM #{quote_table_name(table_name)} LIKE '#{column_name}'")["Type"]
0
+ # execute("ALTER TABLE #{quote_table_name(table_name)} CHANGE #{quote_column_name(column_name)} #{quote_column_name(column_name)} #{current_type} DEFAULT #{quote(default)}")
0
+ # def change_column(table_name, column_name, type, options = {}) #:nodoc:
0
+ # unless options_include_default?(options)
0
+ # if column = columns(table_name).find { |c| c.name == column_name.to_s }
0
+ # options[:default] = column.default
0
+ # raise "No such column: #{table_name}.#{column_name}"
0
+ # 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])}"
0
+ # add_column_options!(change_column_sql, options)
0
+ # execute(change_column_sql)
0
+ # def rename_column(table_name, column_name, new_column_name) #:nodoc:
0
+ # current_type = select_one("SHOW COLUMNS FROM #{quote_table_name(table_name)} LIKE '#{column_name}'")["Type"]
0
+ # execute "ALTER TABLE #{quote_table_name(table_name)} CHANGE #{quote_column_name(column_name)} #{quote_column_name(new_column_name)} #{current_type}"
0
+ # SHOW VARIABLES LIKE 'name'
0
+ def show_variable(name)
0
+ variables = select_all("SHOW VARIABLES LIKE '#{name}'")
0
+ variables.first['Value'] unless variables.empty?
0
+ # Returns a table's primary key and belonging sequence.
0
+ def pk_and_sequence_for(table) #:nodoc:
0
+ execute("describe #{quote_table_name(table)}").each_hash do |h|
0
+ keys << h["Field"]if h["Key"] == "PRI"
0
+ keys.length == 1 ? [keys.first, nil] : nil
0
+ # By default, MySQL 'where id is null' selects the last inserted id.
0
+ # Turn this off. http://dev.rubyonrails.org/ticket/6778
0
+ ##FIXME !!! execute("SET SQL_AUTO_IS_NULL=0")
0
+ def select(sql, name = nil)
0
+ result = execute(sql, name)
0
+ rows = result.hash_rows
0
+ # Executes the update statement and returns the number of rows affected.
0
+ def update_sql(sql, name = nil)
0
+ execute(sql, name).rows[0][0]
0
+ @version ||= @connection.server_info.scan(/^(\d+)\.(\d+)\.(\d+)/).flatten.map { |v| v.to_i }