Module: Simple::SQL::ConnectionAdapter
- 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
- Scope =
::Simple::SQL::Scope
Instance Method Summary collapse
-
#all(sql, *args, into: nil, &block) ⇒ Object
Runs a query, with optional arguments, and returns the result.
-
#ask(sql, *args, into: nil) ⇒ Object
Runs a query and returns the first result row of a query.
-
#exec(sql) ⇒ Object
execute one or more sql statements.
- #resolve_type(ftype, fmod) ⇒ Object
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 ofarrays `[ <id>, <email> ]`.
Simple::SQL.all “SELECT id, email FROM users” do |id, email|
# do something
end
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/simple/sql/connection_adapter.rb', line 36 def all(sql, *args, into: nil, &block) pg_result = exec_logged(sql, *args) # enumerate the rows in pg_result. This returns either an Array of Hashes # (if into is set), or an array of row arrays or of singular values. # # Even if into is set to something different than a Hash, we'll convert # each row into a Hash initially, and only later convert it to the final # target type (via RowConverter.convert). This is to allow to fill in # more entries later on. records = enumerate(pg_result, into: into) # optimization: If we wouldn't clear here the GC would do this later. pg_result.clear unless pg_result.autoclear? # [TODO] - resolve associations. Note that this is only possible if the type # is not an Array (i.e. into is nil) if sql.is_a?(Scope) && sql.paginated? records.send(:set_pagination_info, sql) end records.each(&block) if block records 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 (ornil) -
Simple::SQL.ask "SELECT id, email FROM users WHERE email=$?", "foo@local"returns an array[ <id>, <email> ](ornil)
70 71 72 73 74 75 |
# File 'lib/simple/sql/connection_adapter.rb', line 70 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 “;”
16 17 18 19 20 |
# File 'lib/simple/sql/connection_adapter.rb', line 16 def exec(sql) Logging.yield_logged sql do raw_connection.exec sql end end |
#resolve_type(ftype, fmod) ⇒ Object
114 115 116 117 |
# File 'lib/simple/sql/connection_adapter.rb', line 114 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 |