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, ForeignKey, Index, Indexes, Result, Table, Tables

Constant Summary

Constants inherited from BaseSqlDriver

BaseSqlDriver::SELECT_ARGS_ALLOWED_KEYS, BaseSqlDriver::SEPARATOR_COLUMN, BaseSqlDriver::SEPARATOR_DATABASE, BaseSqlDriver::SEPARATOR_INDEX, BaseSqlDriver::SEPARATOR_TABLE, BaseSqlDriver::SEPARATOR_VALUE

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_column, #escape_database, escape_database, #escape_index, escape_index, #escape_table, escape_table, #foreign_key_support?, #insert, #insert_multi, #select, #single, #sql_make_where, sqlval, #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)
  elsif db.opts[:db]
    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



89
90
91
# File 'lib/baza/driver/pg.rb', line 89

def close
  @conn.close
end

#connected?Boolean

Returns:

  • (Boolean)


54
55
56
# File 'lib/baza/driver/pg.rb', line 54

def connected?
  @conn ? true : false
end

#escape(string) ⇒ Object



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

def escape(string)
  if @conn
    @conn.escape_string(string.to_s)
  else
    PG::Connection.escape_string(string.to_s)
  end
end

#query(sql) ⇒ Object



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

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

#query_ubuf(sql) ⇒ Object



82
83
84
85
86
87
# File 'lib/baza/driver/pg.rb', line 82

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



66
67
68
69
70
71
72
73
74
75
76
# File 'lib/baza/driver/pg.rb', line 66

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