Class: FluidDb2::Pgsql

Inherits:
Base
  • Object
show all
Defined in:
lib/fluiddb2/pgsql.rb

Overview

Pgsql

Instance Attribute Summary

Attributes inherited from Base

#connection, #verbose

Instance Method Summary collapse

Methods inherited from Base

#initialize, #reconnect, #verbose_log

Constructor Details

This class inherits a constructor from FluidDb2::Base

Instance Method Details

#beginObject

Transaction Semantics



110
111
112
# File 'lib/fluiddb2/pgsql.rb', line 110

def begin
  @connection.exec('BEGIN', [])
end

#closeObject



24
25
26
# File 'lib/fluiddb2/pgsql.rb', line 24

def close
  @connection.close
end

#commitObject

Transaction Semantics



115
116
117
# File 'lib/fluiddb2/pgsql.rb', line 115

def commit
  @connection.exec('COMMIT', [])
end

#connectObject

Connect to Db.

Parameters:

  • uri (String)

    a location for the resource to which we will attach, eg mysql://user:[email protected]/foo



11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/fluiddb2/pgsql.rb', line 11

def connect
  uri = @uri
  host = uri.host
  dbname = uri.path.sub('/', '')

  hash = Hash['host', host, 'dbname', dbname]
  hash['port'] = uri.port unless uri.port.nil?
  hash['user'] = uri.user unless uri.user.nil?
  hash['password'] = uri.password unless uri.password.nil?

  @connection = PG.connect(hash)
end

#exec_params(sql, params = [], expected_affected_rows = nil) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/fluiddb2/pgsql.rb', line 92

def exec_params(sql, params = [], expected_affected_rows = nil)
  parts = sql.split('?')
  sql = ''
  parts.each_with_index do |p, idx|
    sql += p
    sql += "$#{idx + 1}" if idx < parts.length - 1
  end
  r = @connection.exec_params(sql, params)

  if !expected_affected_rows.nil? and r.cmd_tuples != expected_affected_rows
    fail ExpectedAffectedRowsError, "Expected affected rows, #{expected_affected_rows}, Actual affected rows, #{r.cmd_tuples}"
  end
rescue PG::Error => e
  raise DuplicateKeyError(e.message) unless e.message.index("duplicate key value violates unique constraint").nil?
  raise e
end

#execute(sql, params = [], expected_affected_rows = nil) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/fluiddb2/pgsql.rb', line 78

def execute(sql, params = [], expected_affected_rows = nil)
  sql = FluidDb2.format_to_sql(sql, params)

  verbose_log "#{self.class.name}.execute. #{sql}"
  r = @connection.exec(sql)

  if !expected_affected_rows.nil? && r.cmd_tuples != expected_affected_rows
    fail ExpectedAffectedRowsError, "Expected affected rows, #{expected_affected_rows}, Actual affected rows, #{r.cmd_tuples}"
  end
rescue PG::Error => e
  raise DuplicateKeyError, e.message unless e.message.index('duplicate key value violates unique constraint').nil?
  raise e
end

#insert(_sql, _params) ⇒ Object



124
125
126
# File 'lib/fluiddb2/pgsql.rb', line 124

def insert(_sql, _params)
  fail 'Pgsql uses SEQUENCES, so possibly easier to use 2 executes'
end

#query_for_array(sql, params = []) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/fluiddb2/pgsql.rb', line 28

def query_for_array(sql, params = [])
  sql = FluidDb2.format_to_sql(sql, params)
  results = @connection.exec(sql)

  case results.num_tuples
  when -1
    fail FluidDb2::ConnectionError
  when 0
    fail FluidDb2::NoDataFoundError
  when 1
    return FluidDb2.convert_tuple_to_hash(results.fields, results, 0)
  else
    fail FluidDb2::TooManyRowsError
  end
end

#query_for_resultset(sql, params = []) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/fluiddb2/pgsql.rb', line 60

def query_for_resultset(sql, params = [])
  sql = FluidDb2.format_to_sql(sql, params)
  results = @connection.exec(sql)

  case results.num_tuples
  when -1
    fail FluidDb2::ConnectionError
  else
    list = []
    fields = results.fields
    0.upto(results.ntuples - 1) do |nbr|
      list.push FluidDb2.convert_tuple_to_hash(fields, results, nbr)
    end

    return list
  end
end

#query_for_value(sql, params = []) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/fluiddb2/pgsql.rb', line 44

def query_for_value(sql, params = [])
  sql = FluidDb2.format_to_sql(sql, params)
  results = @connection.exec(sql)

  case results.num_tuples
  when -1
    fail FluidDb2::ConnectionError
  when 0
    fail FluidDb2::NoDataFoundError
  when 1
    return results.getvalue(0, 0)
  else
    fail FluidDb2::TooManyRowsError
  end
end

#rollbackObject

Transaction Semantics



120
121
122
# File 'lib/fluiddb2/pgsql.rb', line 120

def rollback
  @connection.exec('ROLLBACK', [])
end