Sha256: d31c7ea849987d23a640fa497c448ef351b4c2be265e27a557d4e36b4d4e2b7a

Contents?: true

Size: 1.62 KB

Versions: 2

Compression:

Stored size: 1.62 KB

Contents

require 'singleton'

module Timescaledb
  # Minimal connection setup for Timescaledb directly with the PG.
  # The concept is use a singleton component that can query
  # independently of the ActiveRecord::Base connections.
  # This is useful for the extension and hypertable metadata.
  # It can also #use_connection from active record if needed.
  class Connection
    include Singleton

    attr_writer :config

    # @param [String] query The SQL raw query.
    # @param [Array] params The SQL query parameters.
    # @return [Array<OpenStruct>] The SQL result.
    def query(query, params = [])
      query = params.empty? ? connection.exec(query) : connection.exec_params(query, params)

      query.map(&OpenStruct.method(:new))
    end

    # @param [String] query The SQL raw query.
    # @param [Array] params The SQL query parameters.
    # @return [OpenStruct] The first SQL result.
    def query_first(query, params = [])
      query(query, params).first
    end

    # @param [String] query The SQL raw query.
    # @param [Array] params The SQL query parameters.
    # @return [Integr] The count value from SQL result.
    def query_count(query, params = [])
      query_first(query, params).count.to_i
    end

    # @param [Boolean] True if the connection singleton was configured, otherwise returns false.
    def connected?
      !@config.nil?
    end

    # Override the connection with a raw PG connection.
    # @param [PG::Connection] connection The raw PG connection.
    def use_connection connection
      @connection = connection
    end

    private

    def connection
      @connection ||= PG.connect(@config)
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
timescaledb-0.3.0 lib/timescaledb/connection.rb
timescaledb-0.2.9 lib/timescaledb/connection.rb