Class: Timescaledb::Connection
- Inherits:
-
Object
- Object
- Timescaledb::Connection
- 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
-
#config ⇒ Object
writeonly
Sets the attribute config.
Instance Method Summary collapse
- #connected? ⇒ Boolean
-
#query(query, params = []) ⇒ Array<OpenStruct>
The SQL result.
-
#query_count(query, params = []) ⇒ Integr
The count value from SQL result.
-
#query_first(query, params = []) ⇒ OpenStruct
The first SQL result.
-
#use_connection(connection) ⇒ Object
Override the connection with a raw PG connection.
Instance Attribute Details
#config=(value) ⇒ Object (writeonly)
Sets the attribute config
12 13 14 |
# File 'lib/timescaledb/connection.rb', line 12 def config=(value) @config = value end |
Instance Method Details
#connected? ⇒ 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.
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.
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.
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.
44 45 46 |
# File 'lib/timescaledb/connection.rb', line 44 def use_connection connection @connection = connection end |