Sha256: d5010a6311af372703e87feba54809a1fd0999ab7ca32180a87a6790bd81ce46

Contents?: true

Size: 1.66 KB

Versions: 21

Compression:

Stored size: 1.66 KB

Contents

module RailsDb
  module Adapters

    class BaseAdapter
      extend ::RailsDb::Connection

      MULTI_STATEMENT_HELP_TEXT = "EXPERIMENTAL: You can import only file with SQL statements separated by ';'. Each new statement must start from new line."

      def self.execute(sql)
        t0 = Time.now
        connection.execute(sql)
        Time.now - t0
      end

      def self.exec_query(sql)
        t0 = Time.now
        results = connection.exec_query(sql)
        execution_time = Time.now - t0
        [results, execution_time]
      end

      def self.select(sql)
        BaseAdapter.exec_query(sql)
      end

      def self.explain(sql)
        BaseAdapter.exec_query(sql)
      end

      def self.adapter_name
        'base'
      end

      def self.mime
        'text/x-sql'
      end

      def self.truncate(table_name)
        execute("TRUNCATE TABLE #{table_name};")
      end

      def self.delete(table_name, pk_name, pk_id)
        case pk_id
          when Fixnum, Bignum then
            execute("DELETE FROM #{table_name} WHERE #{pk_name} = #{pk_id};")
          else
            execute("DELETE FROM #{table_name} WHERE #{pk_name} = '#{pk_id}';")
        end
      end

      def self.count(table_name)
        select("SELECT COUNT(*) FROM #{table_name}")[0].rows.flatten.last.to_i
      end

      def self.primary_key(table_name)
        connection.primary_key(table_name)
      end

      def self.indexes(table_name)
        connection.indexes(table_name)
      end

      private

      def self.multiple_execute(sql, divider = ";\n")
        sql.split(divider).each do |statement|
          connection.execute(statement)
        end
      end

    end

  end
end

Version data entries

21 entries across 21 versions & 2 rubygems

Version Path
rails_db-2.3.1 lib/rails_db/adapters/base_adapter.rb
rails_db-2.3.0 lib/rails_db/adapters/base_adapter.rb
rails_db-2.2.1 lib/rails_db/adapters/base_adapter.rb
rails_db-2.2.0 lib/rails_db/adapters/base_adapter.rb
rails_db-2.1.1 lib/rails_db/adapters/base_adapter.rb
rails_db-2.1.0 lib/rails_db/adapters/base_adapter.rb
rails_db-2.0.6 lib/rails_db/adapters/base_adapter.rb
rails_db-2.0.5 lib/rails_db/adapters/base_adapter.rb
rails_db-2.0.4 lib/rails_db/adapters/base_adapter.rb
xplore-0.0.1.alpha lib/rails_db/adapters/base_adapter.rb
rails_db-2.0.3 lib/rails_db/adapters/base_adapter.rb
rails_db-2.0.2 lib/rails_db/adapters/base_adapter.rb
rails_db-2.0.1 lib/rails_db/adapters/base_adapter.rb
rails_db-2.0.0 lib/rails_db/adapters/base_adapter.rb
rails_db-1.6.0 lib/rails_db/adapters/base_adapter.rb
rails_db-1.5.0 lib/rails_db/adapters/base_adapter.rb
rails_db-1.4.1 lib/rails_db/adapters/base_adapter.rb
rails_db-1.4.0 lib/rails_db/adapters/base_adapter.rb
rails_db-1.3.4 lib/rails_db/adapters/base_adapter.rb
rails_db-1.3.3 lib/rails_db/adapters/base_adapter.rb