class Mystic::Postgres

Constants

CONNECT_FIELDS
INDEX_TYPES

Public Class Methods

new(opts={}) click to toggle source
# File lib/mystic/postgres.rb, line 31
def initialize opts={}
  return if opts.empty?
                    @pool = AccessStack.new(
                            :size => opts[:pool] || 5,
                            :timeout => opts[:timeout] || 30,
                            :expires => opts[:expires],
                            :create => lambda { create_pg opts.dup },
    :destroy => lambda { |pg| pg.close },
    :validate => lambda { |pg| pg != nil && pg.status == CONNECTION_OK }
                    )
end

Public Instance Methods

connected?() click to toggle source
# File lib/mystic/postgres.rb, line 57
def connected?
  !@pool.empty?
end
create_pg(opts) click to toggle source
# File lib/mystic/postgres.rb, line 73
def create_pg opts
  pg = PG.connect opts.subhash(*CONNECT_FIELDS)
  pg.set_notice_receiver { |r| }
  pg
end
disconnect() click to toggle source
# File lib/mystic/postgres.rb, line 49
def disconnect
                    @pool.empty!
end
escape(str) click to toggle source
# File lib/mystic/postgres.rb, line 61
def escape str
  @pool.with { |pg| pg.escape_string str }
end
execute(sql) click to toggle source
# File lib/mystic/postgres.rb, line 65
def execute sql
                    res = @pool.with { |pg| pg.exec sql }
                    v = res[0][Mystic::JSON_COL] if res.ntuples == 1 && res.nfields == 1
                    v ||= res.ntuples.times.map { |i| res[i] } unless res.nil?
                    v ||= []
                    v
end
pool_size=(v) click to toggle source
# File lib/mystic/postgres.rb, line 45
def pool_size= v
  @pool.size = v
end
reap!() click to toggle source
# File lib/mystic/postgres.rb, line 53
def reap!
    @pool.reap!
end