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,
lib/simple/sql/connection_manager.rb

Overview

The ConnectionManager manages a pool of ActiveRecord::Base classes.

ActiveRecord assigns a connection_pool to a class. If you want to connect to multiple detabases you must inherit from ActiveRecord::Base. This is what we do dynamically in this ConnectionManager.

Note that connections to the same database are always shared within a single ConnectionPool.

Defined Under Namespace

Modules: Config, ConnectionManager, Formatting, GemHelper, Helpers, Logging, MonkeyPatches, TablePrint Classes: Connection, Fragment, Result

Constant Summary collapse

VERSION =
GemHelper.version "simple-sql"

Instance Method Summary collapse

Instance Method Details

#configurationObject

deprecated



63
64
65
# File 'lib/simple/sql.rb', line 63

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.



50
51
52
# File 'lib/simple/sql.rb', line 50

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



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

def connect!(database_url = :auto)
  disconnect!
  @default_connection = connect(database_url)
end

#default_connectionObject

returns the default connection.



70
71
72
# File 'lib/simple/sql.rb', line 70

def default_connection
  @default_connection ||= connect(:auto)
end

#disconnect!Object

disconnects the current default connection.



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

def disconnect!
  ::Simple::SQL::ConnectionManager.disconnect_all!
  @default_connection = nil
end

#escape_string(s) ⇒ Object



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

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

#table_print(records, io: STDOUT, width: :auto) ⇒ Object



39
40
41
42
# File 'lib/simple/sql.rb', line 39

def table_print(records, io: STDOUT, width: :auto)
  ::Simple::SQL::TablePrint.table_print(records, width: width, io: io)
  records
end

#with_connection(database_url = :auto) ⇒ Object



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

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

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