Class: Baza::Driver::Pg

Inherits:
BaseSqlDriver show all
Defined in:
lib/baza/driver/pg.rb

Defined Under Namespace

Classes: Column, Columns, Commands, CreateIndexSqlCreator, Database, Databases, Index, Indexes, Result, Table, Tables

Constant Summary

Constants inherited from BaseSqlDriver

BaseSqlDriver::SELECT_ARGS_ALLOWED_KEYS

Instance Attribute Summary collapse

Attributes inherited from BaseSqlDriver

#cols, #db, #indexes, #sep_col, #sep_database, #sep_index, #sep_table, #sep_val, #tables

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from BaseSqlDriver

#count, #delete, #escape, #escape_column, #escape_database, #escape_index, #escape_table, #insert, #insert_multi, #select, #single, #sql_make_where, #sqlval, #supports_multiple_databases?, #transaction

Constructor Details

#initialize(db) ⇒ Pg

Returns a new instance of Pg.



39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/baza/driver/pg.rb', line 39

def initialize(db)
  super

  @sep_database = '"'
  @sep_table = '"'
  @sep_col = '"'
  @sep_index = '"'

  if db.opts[:conn]
    @conn = db.opts.fetch(:conn)
  else
    reconnect
  end
end

Instance Attribute Details

#connObject (readonly)

Returns the value of attribute conn.



4
5
6
# File 'lib/baza/driver/pg.rb', line 4

def conn
  @conn
end

Class Method Details

.argsObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/baza/driver/pg.rb', line 20

def self.args
  [{
    label: "Host",
    name: "host"
  }, {
    label: "Port",
    name: "port"
  }, {
    label: "Username",
    name: "user"
  }, {
    label: "Password",
    name: "pass"
  }, {
    label: "Database",
    name: "db"
  }]
end

.from_object(args) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/baza/driver/pg.rb', line 6

def self.from_object(args)
  if args[:object].class.name == "PG::Connection"
    return {
      type: :success,
      args: {
        type: :pg,
        conn: args[:object]
      }
    }
  end

  nil
end

Instance Method Details

#closeObject



77
78
79
# File 'lib/baza/driver/pg.rb', line 77

def close
  @conn.close
end

#query(sql) ⇒ Object



66
67
68
# File 'lib/baza/driver/pg.rb', line 66

def query(sql)
  Baza::Driver::Pg::Result.new(self, @conn.exec(sql))
end

#query_ubuf(sql) ⇒ Object



70
71
72
73
74
75
# File 'lib/baza/driver/pg.rb', line 70

def query_ubuf(sql)
  @conn.send_query(sql)
  @conn.set_single_row_mode
  result = @conn.get_result
  Baza::Driver::Pg::Result.new(self, result, unbuffered: true)
end

#reconnectObject



54
55
56
57
58
59
60
61
62
63
64
# File 'lib/baza/driver/pg.rb', line 54

def reconnect
  require "pg" unless ::Object.const_defined?(:PG)

  args = {dbname: db.opts.fetch(:db)}
  args[:port] = db.opts.fetch(:port) if db.opts[:port]
  args[:hostaddr] = db.opts.fetch(:host) if db.opts[:host]
  args[:user] = db.opts.fetch(:user) if db.opts[:user]
  args[:password] = db.opts.fetch(:pass) if db.opts[:pass]

  @conn = PG::Connection.new(args)
end