Class: Simple::SQL::Connection
- Inherits:
-
Object
- Object
- Simple::SQL::Connection
- Extended by:
- Forwardable
- Includes:
- ConnectionAdapter
- Defined in:
- lib/simple/sql/connection.rb,
lib/simple/sql/connection.rb,
lib/simple/sql/connection/insert.rb,
lib/simple/sql/connection/duplicate.rb,
lib/simple/sql/connection/reflection.rb
Overview
rubocop:disable Metrics/AbcSize rubocop:disable Metrics/LineLength rubocop:disable Layout/AlignHash
Direct Known Subclasses
Defined Under Namespace
Classes: ActiveRecordConnection, Duplicator, Inserter, RawConnection, Reflection
Constant Summary
Constants included from ConnectionAdapter
Simple::SQL::ConnectionAdapter::Logging, Simple::SQL::ConnectionAdapter::Scope
Class Method Summary collapse
Instance Method Summary collapse
-
#duplicate(table, ids, overrides = {}) ⇒ Object
Creates duplicates of record in a table.
-
#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.
-
- #reflection ⇒ Object
Methods included from ConnectionAdapter
#all, #ask, #costs, #each, #exec, #locked, #print, #resolve_type
Class Method Details
.create(database_url = :auto) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/simple/sql/connection.rb', line 21 def self.create(database_url = :auto) case database_url when :auto if defined?(::ActiveRecord) ActiveRecordConnection.new else RawConnection.new Simple::SQL::Config.determine_url end else RawConnection.new database_url end end |
Instance Method Details
#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]
14 15 16 17 18 19 |
# File 'lib/simple/sql/connection/duplicate.rb', line 14 def duplicate(table, ids, overrides = {}) ids = Array(ids) return [] if ids.empty? Duplicator.new(self, table, overrides).call(ids) 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
11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/simple/sql/connection/insert.rb', line 11 def insert(table, records, on_conflict: nil, into: nil) if records.is_a?(Hash) inserted_records = insert(table, [records], on_conflict: on_conflict, into: into) return inserted_records.first end return [] if records.empty? inserter = inserter(table_name: table.to_s, columns: records.first.keys, on_conflict: on_conflict, into: into) inserter.insert(records: records) end |
#reflection ⇒ Object
2 3 4 |
# File 'lib/simple/sql/connection/reflection.rb', line 2 def reflection @reflection ||= Reflection.new(self) end |