Module: Squirm::Core

Included in:
Squirm, Executor
Defined in:
lib/squirm/core.rb

Overview

The core DSL used by Squirm.

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#poolObject (readonly)

Returns the value of attribute pool.



9
10
11
# File 'lib/squirm/core.rb', line 9

def pool
  @pool
end

Instance Method Details

#connect(options = {}) ⇒ Object

Establishes a connection pool.

Parameters:

  • options (Hash) (defaults to: {})

    The connection options

Options Hash (options):

  • :pool (String)

    Use the given pool rather than Squirm’s.

  • :timeout (Fixnum)

    The pool timeout.

  • :pool_size (Fixnum)

    The pool size.



16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/squirm/core.rb', line 16

def connect(options = {})
  return @pool = options[:pool] if options[:pool]
  options   = options.dup
  timeout   = options.delete(:timeout) || 5
  pool_size = options.delete(:pool_size) || 1
  @pool     = Squirm::Pool.new(timeout)
  pool_size.times do
    conn = PGconn.open(options)
    yield conn if block_given?
    @pool.checkin conn
  end
end

#disconnectObject

Disconnects all pool connections and sets the pool to nil.



30
31
32
33
34
# File 'lib/squirm/core.rb', line 30

def disconnect
  return unless pool
  pool.map(&:close)
  @pool = nil
end

#exec(*args, &block) ⇒ Object

Executes the query and passes the result to the block you specify.



37
38
39
40
41
42
43
# File 'lib/squirm/core.rb', line 37

def exec(*args, &block)
  if current = Thread.current[:squirm_connection]
    current.exec(*args, &block)
  else
    use {|conn| conn.exec(*args, &block)}
  end
end

#procedure(*args) ⇒ Object



45
46
47
# File 'lib/squirm/core.rb', line 45

def procedure(*args)
  Procedure.load(*args)
end

#quote_ident(*args) ⇒ String

Quotes an SQL identifier.

Returns:

  • (String)

    The identifier.



85
86
87
# File 'lib/squirm/core.rb', line 85

def quote_ident(*args)
  PGconn.quote_ident(*args.map(&:to_s))
end

#rollback(&block) ⇒ Object

Rolls back from inside a #transaction block. When called with block, performs a transaction inside a block and rolls back when it gets to the end. This can be useful in tests.



65
66
67
# File 'lib/squirm/core.rb', line 65

def rollback(&block)
  block_given? ? transaction {yield; rollback} : (raise Rollback)
end

#transactionObject

Performs a #use inside a transaction.



50
51
52
53
54
55
56
57
58
59
60
# File 'lib/squirm/core.rb', line 50

def transaction
  use do |connection|
    connection.transaction do |conn|
      begin
        yield conn
      rescue Rollback
        return
      end
    end
  end
end

#use(conn = nil) ⇒ Object

Uses a connection for all database access inside the given block. If no connection is given, then one will be checked out from the pool for use inside the block, and then checked back in when the method returns.



72
73
74
75
76
77
78
79
80
81
# File 'lib/squirm/core.rb', line 72

def use(conn = nil)
  conn_given = !!conn
  conn = conn_given ? conn : @pool.checkout
  begin
    yield Thread.current[:squirm_connection] = conn
  ensure
    Thread.current[:squirm_connection] = nil
    @pool.checkin conn unless conn_given
  end
end