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,
lib/simple/sql/fragment.rb,
lib/simple/sql/formatting.rb,
lib/simple/sql/helpers/immutable.rb

Overview

The Simple::SQL module

Defined Under Namespace

Modules: Config, Formatting, Helpers, Logging Classes: Connection, Fragment, Result

Constant Summary collapse

DEFAULT_CONNECTION_KEY =

– default connection —————————————————

:"Simple::SQL.default_connection"
VERSION =
"0.5.16"

Instance Method Summary collapse

Instance Method Details

#configurationObject

deprecated



56
57
58
# File 'lib/simple/sql.rb', line 56

def configuration
  Config.parse_url(Config.determine_url)
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.

Returns the connection object.



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

def connect(database_url = :auto)
  Connection.create(database_url)
end

#connect!(database_url = :auto) ⇒ Object

connects to the database specified via the url parameter, and sets Simple::SQL’s default connection.

see connect, default_connection



73
74
75
76
# File 'lib/simple/sql.rb', line 73

def connect!(database_url = :auto)
  disconnect!
  Thread.current[DEFAULT_CONNECTION_KEY] ||= connect(database_url)
end

#default_connectionObject

returns the default connection.



65
66
67
# File 'lib/simple/sql.rb', line 65

def default_connection
  Thread.current[DEFAULT_CONNECTION_KEY] ||= connect(:auto)
end

#disconnect!Object

disconnects the current default connection.



79
80
81
82
83
84
85
# File 'lib/simple/sql.rb', line 79

def disconnect!
  connection = Thread.current[DEFAULT_CONNECTION_KEY]
  return unless connection

  connection.disconnect!
  Thread.current[DEFAULT_CONNECTION_KEY] = nil
end

#escape_string(s) ⇒ Object



29
30
31
32
33
34
35
# File 'lib/simple/sql.rb', line 29

def escape_string(s)
  expect! s => [Symbol, String, nil]

  return "NULL" unless s

  "'#{PG::Connection.escape_string(s)}'"
end

#fragment(str) ⇒ Object



7
8
9
# File 'lib/simple/sql/fragment.rb', line 7

def fragment(str)
  Fragment.new(str)
end

#with_connection(database_url = :auto) ⇒ Object



47
48
49
50
51
52
53
# File 'lib/simple/sql.rb', line 47

def with_connection(database_url = :auto)
  connection = connect(database_url)

  yield(connection) if connection
ensure
  connection&.disconnect!
end