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/reflection.rb
Overview
The Simple::SQL module
Defined Under Namespace
Modules: Config, Connection, ConnectionAdapter, Decoder, Encoder, Logging, Reflection, SimpleTransactions Classes: Duplicator, Fragment, Inserter, Scope
Constant Summary collapse
- VERSION =
"0.4.3"
Instance Method Summary collapse
-
#configuration(database_url = :auto) ⇒ Object
returns a configuration hash, either from the passed in database URL, from a DATABASE_URL environment value, or from the config/database.yml file.
-
#connect!(database_url = :auto) ⇒ Object
connects to the database specified via the url parameter.
-
#disconnect! ⇒ Object
disconnects the current connection.
-
#duplicate(table, ids, overrides = {}) ⇒ Object
Creates duplicates of record in a table.
- #fragment(str) ⇒ Object
-
#insert(table, records, on_conflict: 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
#configuration(database_url = :auto) ⇒ Object
returns a configuration hash, either from the passed in database URL, from a DATABASE_URL environment value, or from the config/database.yml file.
71 72 73 74 |
# File 'lib/simple/sql.rb', line 71 def configuration(database_url = :auto) database_url = Config.determine_url if database_url == :auto Config.parse_url(database_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.
80 81 82 83 84 85 86 87 88 89 |
# File 'lib/simple/sql.rb', line 80 def connect!(database_url = :auto) database_url = Config.determine_url if database_url == :auto Logging.info "Connecting to #{database_url}" config = Config.parse_url(database_url) require "pg" connection = Connection.pg_connection(PG::Connection.new(config)) self.connector = lambda { connection } end |
#disconnect! ⇒ Object
disconnects the current connection.
92 93 94 |
# File 'lib/simple/sql.rb', line 92 def disconnect! self.connector = 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]
24 25 26 27 28 29 |
# File 'lib/simple/sql/duplicate.rb', line 24 def duplicate(table, ids, overrides = {}) ids = Array(ids) return [] if ids.empty? Duplicator.new(table, overrides).call(ids) end |
#fragment(str) ⇒ Object
8 9 10 |
# File 'lib/simple/sql/duplicate.rb', line 8 def fragment(str) Fragment.new(str) end |
#insert(table, records, on_conflict: 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
10 11 12 13 14 15 16 |
# File 'lib/simple/sql/insert.rb', line 10 def insert(table, records, on_conflict: nil) if records.is_a?(Hash) insert_many(table, [records], on_conflict).first else insert_many(table, records, on_conflict) end end |