Module: Utils::Postgres::Request

Defined in:
lib/bas/utils/postgres/request.rb

Overview

This module is a PostgresDB utility to make requests to a Postgres database.

Class Method Summary collapse

Class Method Details

.execute(params) ⇒ Object

Implements the request process logic to the PostgresDB table.


Params:

  • connection Connection parameters to the database: ‘host`, `port`, `dbname`, `user`, `password`.

  • query:

    • String: String with the SQL query to be executed.

    • Array: Two element array, where the first element is the SQL query (string), and the

      second one an array of elements to be interpolared in the query when using "$1, $2, ...".
      


returns HTTParty::Response



24
25
26
27
28
29
30
# File 'lib/bas/utils/postgres/request.rb', line 24

def self.execute(params)
  pg_connection = PG::Connection.new(params[:connection])

  results = execute_query(pg_connection, params[:query])

  results.map { |result| result.transform_keys(&:to_sym) }
end

.execute_query(pg_connection, query) ⇒ Object

Execute the Postgres query


pg_connection: PG::Connection object configured with the database connection. query:

  • String: String with the SQL query to be executed.

  • Array: Two element array, where the first element is the SQL query (string), and the

    second one an array of elements to be interpolared in the query when using "$1, $2, ...".
    


41
42
43
44
45
46
47
48
49
# File 'lib/bas/utils/postgres/request.rb', line 41

def self.execute_query(pg_connection, query)
  if query.is_a? String
    pg_connection.exec(query)
  else
    sentence, params = query

    pg_connection.exec_params(sentence, params)
  end
end