Module: Squirm::Core
- Included in:
- Squirm
- Defined in:
- lib/squirm/core.rb
Overview
The core DSL used by Squirm.
Instance Method Summary collapse
-
#connect(options = {}) ⇒ Object
Establishes a connection pool.
-
#disconnect ⇒ Object
Disconnects all pool connections and sets the pool to nil.
-
#exec(*args, &block) ⇒ Object
Executes the query and passes the result to the block you specify.
-
#pool ⇒ Squirm::Pool
Gets the connection pool.
- #procedure(*args) ⇒ Object
-
#quote_ident(*args) ⇒ String
Quotes an SQL identifier.
-
#rollback(&block) ⇒ Object
Rolls back from inside a #transaction block.
-
#transaction ⇒ Object
Performs a #use inside a transaction.
-
#use ⇒ Object
Checks out a connection and uses it for all database access inside the block.
Instance Method Details
#connect(options = {}) ⇒ Object
Establishes a connection pool.
14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/squirm/core.rb', line 14 def connect( = {}) return @pool = [:pool] if [:pool] = .dup timeout = .delete(:timeout) || 5 pool_size = .delete(:pool_size) || 1 @pool = Squirm::Pool.new(timeout) pool_size.times do conn = PGconn.open() yield conn if block_given? @pool.checkin conn end end |
#disconnect ⇒ Object
Disconnects all pool connections and sets the pool to nil.
28 29 30 31 32 |
# File 'lib/squirm/core.rb', line 28 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.
35 36 37 38 39 40 41 |
# File 'lib/squirm/core.rb', line 35 def exec(*args, &block) if current = Thread.current[:squirm_connection] current.exec(*args, &block) else use {|conn| conn.exec(*args, &block)} end end |
#pool ⇒ Squirm::Pool
Gets the connection pool.
49 50 51 |
# File 'lib/squirm/core.rb', line 49 def pool @pool if defined? @pool end |
#procedure(*args) ⇒ Object
43 44 45 |
# File 'lib/squirm/core.rb', line 43 def procedure(*args) Procedure.load(*args) end |
#quote_ident(*args) ⇒ String
Quotes an SQL identifier.
88 89 90 |
# File 'lib/squirm/core.rb', line 88 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.
69 70 71 |
# File 'lib/squirm/core.rb', line 69 def rollback(&block) block_given? ? transaction {yield; rollback} : (raise Rollback) end |
#transaction ⇒ Object
Performs a #use inside a transaction.
54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/squirm/core.rb', line 54 def transaction use do |connection| connection.transaction do |conn| begin yield conn rescue Rollback return end end end end |
#use ⇒ Object
Checks out a connection and uses it for all database access inside the block.
75 76 77 78 79 80 81 82 83 84 |
# File 'lib/squirm/core.rb', line 75 def use conn = @pool.checkout begin Thread.current[:squirm_connection] = conn yield conn.respond_to?(:raw_connection) ? conn.raw_connection : conn ensure Thread.current[:squirm_connection] = nil @pool.checkin conn end end |