Sha256: ceef4772e01a7039b782dc061a2eeb3d02b9bf98a8042696bb260de5c32bcb88

Contents?: true

Size: 861 Bytes

Versions: 2

Compression:

Stored size: 861 Bytes

Contents

module Adhoq
  class Executor
    class << self
      def select(query)
        with_sandbox do
          current_connection.exec_query(query)
        end
      end

      def explain(query)
        with_sandbox do
          current_connection.explain(query)
        end
      end

      def current_connection
        ActiveRecord::Base.connection
      end

      def with_sandbox
        result = nil
        ActiveRecord::Base.transaction do
          result = yield
          raise ActiveRecord::Rollback
        end
        result
      end
    end

    def initialize(query)
      @query = query
    end

    def execute
      wrap_result(self.class.select(@query))
    end

    def explain
      self.class.explain(@query)
    end

    private

    def wrap_result(ar_result)
      Adhoq::Result.new(ar_result.columns, ar_result.rows)
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
adhoq-0.0.5 lib/adhoq/executor.rb
adhoq-0.0.4 lib/adhoq/executor.rb