Class: Timescaledb::Connection

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/timescaledb/connection.rb

Overview

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.

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#config=(value) ⇒ Object (writeonly)

Sets the attribute config

Parameters:

  • value

    the value to set the attribute config to.



12
13
14
# File 'lib/timescaledb/connection.rb', line 12

def config=(value)
  @config = value
end

Instance Method Details

#connected?Boolean

Parameters:

  • True (Boolean)

    if the connection singleton was configured, otherwise returns false.

Returns:

  • (Boolean)


38
39
40
# File 'lib/timescaledb/connection.rb', line 38

def connected?
  !@config.nil?
end

#query(query, params = []) ⇒ Array<OpenStruct>

Returns The SQL result.

Parameters:

  • query (String)

    The SQL raw query.

  • params (Array) (defaults to: [])

    The SQL query parameters.

Returns:

  • (Array<OpenStruct>)

    The SQL result.



17
18
19
20
21
# File 'lib/timescaledb/connection.rb', line 17

def query(query, params = [])
  query = params.empty? ? connection.exec(query) : connection.exec_params(query, params)

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

#query_count(query, params = []) ⇒ Integr

Returns The count value from SQL result.

Parameters:

  • query (String)

    The SQL raw query.

  • params (Array) (defaults to: [])

    The SQL query parameters.

Returns:

  • (Integr)

    The count value from SQL result.



33
34
35
# File 'lib/timescaledb/connection.rb', line 33

def query_count(query, params = [])
  query_first(query, params).count.to_i
end

#query_first(query, params = []) ⇒ OpenStruct

Returns The first SQL result.

Parameters:

  • query (String)

    The SQL raw query.

  • params (Array) (defaults to: [])

    The SQL query parameters.

Returns:

  • (OpenStruct)

    The first SQL result.



26
27
28
# File 'lib/timescaledb/connection.rb', line 26

def query_first(query, params = [])
  query(query, params).first
end

#use_connection(connection) ⇒ Object

Override the connection with a raw PG connection.

Parameters:

  • connection (PG::Connection)

    The raw PG connection.



44
45
46
# File 'lib/timescaledb/connection.rb', line 44

def use_connection connection
  @connection = connection
end