Class: Mystic::Postgres
- Inherits:
-
Object
- Object
- Mystic::Postgres
- Defined in:
- lib/mystic/postgres.rb
Constant Summary collapse
- CONNECT_FIELDS =
[ :host, :hostaddr, :port, :dbname, :user, :password, :connect_timeout, :options, :tty, :sslmode, :krbsrvname, :gsslib ].freeze
- INDEX_TYPES =
[ :btree, :hash, :gist, :spgist, :gin ].freeze
Instance Method Summary collapse
- #connected? ⇒ Boolean
- #create_pg(opts) ⇒ Object
- #disconnect ⇒ Object
- #escape(str) ⇒ Object
- #execute(sql) ⇒ Object
-
#initialize(opts = {}) ⇒ Postgres
(also: #connect)
constructor
A new instance of Postgres.
- #pool_size=(v) ⇒ Object
- #reap! ⇒ Object
Constructor Details
#initialize(opts = {}) ⇒ Postgres Also known as: connect
Returns a new instance of Postgres.
31 32 33 34 35 36 37 38 39 40 41 |
# 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 }, :destroy => lambda { |pg| pg.close }, :validate => lambda { |pg| pg != nil && pg.status == CONNECTION_OK } ) end |
Instance Method Details
#connected? ⇒ Boolean
57 58 59 |
# File 'lib/mystic/postgres.rb', line 57 def connected? !@pool.empty? end |
#create_pg(opts) ⇒ Object
73 74 75 76 77 |
# 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 ⇒ Object
49 50 51 |
# File 'lib/mystic/postgres.rb', line 49 def disconnect @pool.empty! end |
#escape(str) ⇒ Object
61 62 63 |
# File 'lib/mystic/postgres.rb', line 61 def escape str @pool.with { |pg| pg.escape_string str } end |
#execute(sql) ⇒ Object
65 66 67 68 69 70 71 |
# 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) ⇒ Object
45 46 47 |
# File 'lib/mystic/postgres.rb', line 45 def pool_size= v @pool.size = v end |
#reap! ⇒ Object
53 54 55 |
# File 'lib/mystic/postgres.rb', line 53 def reap! @pool.reap! end |