Module: Simple::SQL

Extended by:
SQL, Transactions
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, Decoder, Encoder, Logging, Transactions

Constant Summary collapse

VERSION =
"0.2.1"

Constants included from Transactions

Transactions::SELF

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Transactions

nesting_level, nesting_level=, transaction

Instance Attribute Details

#loggerObject

Returns the value of attribute logger.



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

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



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

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)



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

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.



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

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 = 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 “;”



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

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.



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

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



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

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

  enumerate(result, decoder, block)
end