Module: Simple::SQL
- Extended by:
- Forwardable, SQL
- Included in:
- SQL
- Defined in:
- lib/simple/sql.rb,
lib/simple/sql/insert.rb,
lib/simple/sql/logging.rb,
lib/simple/sql/version.rb,
lib/simple/sql/duplicate.rb,
lib/simple/sql/formatting.rb,
lib/simple/sql/reflection.rb,
lib/simple/sql/helpers/immutable.rb
Overview
The Simple::SQL module
Defined Under Namespace
Modules: Config, ConnectionAdapter, Formatting, Helpers, Logging, Reflection Classes: Connection, Duplicator, Fragment, Inserter, Result, Scope
Constant Summary collapse
- DEFAULT_CONNECTION_KEY =
– default connection —————————————————
:"Simple::SQL.default_connection"
- VERSION =
"0.5.0"
Instance Method Summary collapse
-
#connect(database_url = :auto) ⇒ Object
connects to the database specified via the url parameter.
-
#connect!(database_url = :auto) ⇒ Object
connects to the database specified via the url parameter, and sets Simple::SQL’s default connection.
-
#default_connection ⇒ Object
returns the default connection.
-
#disconnect! ⇒ Object
disconnects the current default connection.
-
#duplicate(table, ids, overrides = {}) ⇒ Object
Creates duplicates of record in a table.
- #fragment(str) ⇒ Object
-
#insert(table, records, on_conflict: nil, into: nil) ⇒ Object
-
table_name - the name of the table - records - a single hash of attributes or an array of hashes of attributes - on_conflict - uses a postgres ON CONFLICT clause to ignore insert conflicts if true.
-
Instance Method Details
#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.
34 35 36 |
# File 'lib/simple/sql.rb', line 34 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
51 52 53 54 |
# File 'lib/simple/sql.rb', line 51 def connect!(database_url = :auto) disconnect! Thread.current[DEFAULT_CONNECTION_KEY] ||= connect(database_url) end |
#default_connection ⇒ Object
returns the default connection.
43 44 45 |
# File 'lib/simple/sql.rb', line 43 def default_connection Thread.current[DEFAULT_CONNECTION_KEY] ||= connect(:auto) end |
#disconnect! ⇒ Object
disconnects the current default connection.
57 58 59 60 61 62 63 |
# File 'lib/simple/sql.rb', line 57 def disconnect! connection = Thread.current[DEFAULT_CONNECTION_KEY] return unless connection connection.disconnect! Thread.current[DEFAULT_CONNECTION_KEY] = nil end |
#duplicate(table, ids, overrides = {}) ⇒ Object
Creates duplicates of record in a table.
This method handles timestamp columns (these will be set to the current time) and primary keys (will be set to NULL.) You can pass in overrides as a third argument for specific columns.
Parameters:
-
ids: (Integer, Array<Integer>) primary key ids
-
overrides: Hash[column_names => SQL::Fragment]
28 29 30 31 32 33 |
# File 'lib/simple/sql/duplicate.rb', line 28 def duplicate(table, ids, overrides = {}) ids = Array(ids) return [] if ids.empty? Duplicator.new(table, overrides).call(ids) end |
#fragment(str) ⇒ Object
12 13 14 |
# File 'lib/simple/sql/duplicate.rb', line 12 def fragment(str) Fragment.new(str) end |
#insert(table, records, on_conflict: nil, into: nil) ⇒ Object
-
table_name - the name of the table
-
records - a single hash of attributes or an array of hashes of attributes
-
on_conflict - uses a postgres ON CONFLICT clause to ignore insert conflicts if true
12 13 14 15 16 17 18 |
# File 'lib/simple/sql/insert.rb', line 12 def insert(table, records, on_conflict: nil, into: nil) if records.is_a?(Hash) insert_many(table, [records], on_conflict, into).first else insert_many(table, records, on_conflict, into) end end |