Module: Operations

Included in:
QAT::DB
Defined in:
lib/qat/db/operations.rb

Overview

Database operations helper methods

Defined Under Namespace

Classes: ConnectionError

Instance Method Summary collapse

Instance Method Details

#connect(*args, &block) ⇒ Sequel::Database

Establishes a connection

Returns:



14
15
16
17
# File 'lib/qat/db/operations.rb', line 14

def connect(*args, &block)
  @connection = Sequel.connect(*args, &block)
  connection
end

#connectionSequel::Database

Returns the current connection

Returns:



8
9
10
# File 'lib/qat/db/operations.rb', line 8

def connection
  @connection
end

#execute(sql, opts = nil) ⇒ NilClass

Executes a SQL statement in the open connection

Parameters:

  • sql (String)

    SQL statement to execute

  • opts (Hash) (defaults to: nil)

    optional parameters

Returns:

  • (NilClass)

See Also:

  • Sequel::Database#run


24
25
26
27
28
# File 'lib/qat/db/operations.rb', line 24

def execute(sql, opts=nil)
  args = [sql]
  args << opts if opts
  connection.run(*args)
end

#query(sql) ⇒ Array

Executes a SQL query in the open connection and returns an array of results

Parameters:

  • sql (String)

    SQL query to execute

Returns:

  • (Array)


33
34
35
36
37
38
# File 'lib/qat/db/operations.rb', line 33

def query(sql)
  connection = Sequel.connect(self.args)
  connection[sql].map { |row| row }
  connection
  connection.close
end