Module: Simple::SQL

Extended by:
Forwardable, SQL
Included in:
SQL
Defined in:
lib/simple/sql.rb,
lib/simple/sql/logging.rb,
lib/simple/sql/version.rb

Overview

The Simple::SQL module

Defined Under Namespace

Modules: Config, Connection, Decoder, Encoder, Logging

Constant Summary collapse

VERSION =
"0.2.4"

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#loggerObject

Returns the value of attribute logger.



15
16
17
# File 'lib/simple/sql.rb', line 15

def logger
  @logger
end

Instance Method Details

#all(sql, *args, &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



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

def all(sql, *args, &block)
  result  = exec_logged(sql, *args)
  decoder = Decoder.new(result)

  enumerate(result, decoder, block)
end

#ask(sql, *args) ⇒ 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.rb', line 56

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

#connect!(database_url = :auto) ⇒ Object

connects to the database specified via the url parameter. If called without argument it tries to determine a DATABASE_URL from either the environment setting (DATABASE_URL) or from a config/database.yml file, taking into account the RAILS_ENV and RACK_ENV settings.



148
149
150
151
152
153
154
155
156
157
# File 'lib/simple/sql.rb', line 148

def connect!(database_url = :auto)
  database_url = Config.determine_url if database_url == :auto

  logger.info "Connecting to #{database_url}"
  config = Config.parse_url(database_url)

  require "pg"
  connection = Connection.new(PG::Connection.new(config))
  self.connector = lambda { connection }
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 “;”



21
22
23
24
25
# File 'lib/simple/sql.rb', line 21

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

#record(sql, *args, into: Hash) ⇒ Object

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



83
84
85
86
87
88
# File 'lib/simple/sql.rb', line 83

def record(sql, *args, into: Hash)
  catch(:ok) do
    records(sql, *args, into: into) { |row| throw :ok, row }
    nil
  end
end

#records(sql, *args, into: Hash, &block) ⇒ Object

Runs a query, with optional arguments, and returns the result as an array of Hashes.

Example:

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

    hashes { id: id, email: email }
    

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

# do something

end



75
76
77
78
79
80
# File 'lib/simple/sql.rb', line 75

def records(sql, *args, into: Hash, &block)
  result  = exec_logged(sql, *args)
  decoder = Decoder.new(result, :record, into: into)

  enumerate(result, decoder, block)
end