Class: Simple::SQL::Connection

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
ConnectionAdapter
Defined in:
lib/simple/sql/connection.rb,
lib/simple/sql/connection.rb,
lib/simple/sql/connection/scope.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

ActiveRecordConnection, RawConnection

Defined Under Namespace

Classes: ActiveRecordConnection, Duplicator, Inserter, RawConnection, Reflection, Scope

Constant Summary

Constants included from ConnectionAdapter

Simple::SQL::ConnectionAdapter::Logging

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ConnectionAdapter

#all, #ask, #costs, #each, #exec, #locked, #print, #resolve_type

Class Method Details

.create(database_url = :auto) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/simple/sql/connection.rb', line 22

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

#reflectionObject



6
7
8
# File 'lib/simple/sql/connection/reflection.rb', line 6

def reflection
  @reflection ||= Reflection.new(self)
end

#reset_reflectionObject



2
3
4
# File 'lib/simple/sql/connection/reflection.rb', line 2

def reset_reflection
  @reflection = nil
end

#scope(sql, args = []) ⇒ Object

Build a scope object

This call supports a few variants:

Simple::SQL.scope("SELECT * FROM mytable")
Simple::SQL.scope(table: "mytable", select: "*")

The second option also allows one to pass in more options, like the following:

Simple::SQL.scope(table: "mytable", select: "*", where: { id: 1, foo: "bar" }, order_by: "id desc")


21
22
23
# File 'lib/simple/sql/connection/scope.rb', line 21

def scope(sql, args = [])
  ::Simple::SQL::Connection::Scope.new sql, args, connection: self
end