Module: Simple::SQL::ConnectionAdapter

Included in:
Simple::SQL::Connection::ActiveRecordConnection, Simple::SQL::Connection::PgConnection
Defined in:
lib/simple/sql/connection_adapter.rb

Overview

This module implements an adapter between the Simple::SQL interface (i.e. ask, all, first, transaction) and a raw connection.

This module can be mixed onto objects that implement a raw_connection method, which must return a Pg::Connection.

Constant Summary collapse

Logging =
::Simple::SQL::Logging
Encoder =
::Simple::SQL::Encoder
Decoder =
::Simple::SQL::Decoder
Scope =
::Simple::SQL::Scope

Instance Method Summary collapse

Instance Method Details

#all(sql, *args, into: nil, &block) ⇒ Object

Runs a query, with optional arguments, and returns the result. If the SQL query returns rows with one column, this method returns an array of these values. Otherwise it returns an array of arrays.

Example:

  • Simple::SQL.all("SELECT id FROM users") returns an array of id values

  • Simple::SQL.all("SELECT id, email FROM users") returns an array of

    arrays `[ <id>, <email> ]`.
    

Simple::SQL.all “SELECT id, email FROM users” do |id, email|

# do something

end



39
40
41
42
43
44
45
46
# File 'lib/simple/sql/connection_adapter.rb', line 39

def all(sql, *args, into: nil, &block)
  result = exec_logged(sql, *args)
  result = enumerate(result, into: into, &block)
  if sql.is_a?(Scope) && sql.paginated?
    add_page_info(sql, result)
  end
  result
end

#ask(sql, *args, into: nil) ⇒ Object

Runs a query and returns the first result row of a query.

Examples:

  • Simple::SQL.ask "SELECT id FROM users WHERE email=$?", "foo@local" returns a number (or nil)

  • Simple::SQL.ask "SELECT id, email FROM users WHERE email=$?", "foo@local" returns an array [ <id>, <email> ] (or nil)



56
57
58
59
60
61
# File 'lib/simple/sql/connection_adapter.rb', line 56

def ask(sql, *args, into: nil)
  catch(:ok) do
    all(sql, *args, into: into) { |row| throw :ok, row }
    nil
  end
end

#exec(sql) ⇒ Object

execute one or more sql statements. This method does not allow to pass in arguments - since the pg client does not support this - but it allows to run multiple sql statements separated by “;”



19
20
21
22
23
# File 'lib/simple/sql/connection_adapter.rb', line 19

def exec(sql)
  Logging.yield_logged sql do
    raw_connection.exec sql
  end
end

#resolve_type(ftype, fmod) ⇒ Object



111
112
113
114
# File 'lib/simple/sql/connection_adapter.rb', line 111

def resolve_type(ftype, fmod)
  @resolved_types ||= {}
  @resolved_types[[ftype, fmod]] ||= raw_connection.exec("SELECT format_type($1,$2)", [ftype, fmod]).getvalue(0, 0)
end