Class: Pod4::PgInterface

Inherits:
Interface show all
Includes:
SQLHelper
Defined in:
lib/pod4/pg_interface.rb

Overview

Pod4 Interface for requests on a SQL table via pg, the PostgresQL adapter.

If your DB table is one-one with your model, you shouldn’t need to override anything. Example:

class CustomerInterface < SwingShift::PgInterface
  set_schema :public    # optional
  set_table  :customer
  set_id_fld :id
end

Constant Summary

Constants inherited from Interface

Interface::ACTIONS

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from SQLHelper

#quoted_table

Methods included from Metaxing

#define_class_method, #metaclass

Constructor Details

#initialize(connectHash, testClient = nil) ⇒ PgInterface

Initialise the interface by passing it a Pg connection hash. For testing ONLY you can also pass an object which pretends to be a Pg client, in which case the hash is pretty much ignored.



80
81
82
83
84
85
86
87
88
89
# File 'lib/pod4/pg_interface.rb', line 80

def initialize(connectHash, testClient=nil)
  raise(ArgumentError, 'invalid connection hash') unless connectHash.kind_of?(Hash)

  @connect_hash = connectHash.dup
  @test_client  = testClient 
  @client       = nil

rescue => e
  handle_error(e)
end

Instance Attribute Details

#id_fldObject (readonly)

Returns the value of attribute id_fld.



29
30
31
# File 'lib/pod4/pg_interface.rb', line 29

def id_fld
  @id_fld
end

Class Method Details

.id_fldObject

Raises:



67
68
69
# File 'lib/pod4/pg_interface.rb', line 67

def id_fld
  raise Pod4Error, "You need to use set_id_fld to set the ID column"
end

.schemaObject



45
# File 'lib/pod4/pg_interface.rb', line 45

def schema; nil; end

.set_id_fld(idFld) ⇒ Object

Set the name of the column that holds the unique id for the table.



63
64
65
# File 'lib/pod4/pg_interface.rb', line 63

def set_id_fld(idFld)
  define_class_method(:id_fld) {idFld.to_s.to_sym}
end

.set_schema(schema) ⇒ Object

Set the name of the schema. This is optional.



41
42
43
# File 'lib/pod4/pg_interface.rb', line 41

def set_schema(schema) 
  define_class_method(:schema) {schema.to_s.to_sym}
end

.set_table(table) ⇒ Object

Set the name of the database table



51
52
53
# File 'lib/pod4/pg_interface.rb', line 51

def set_table(table)
  define_class_method(:table) {table.to_s.to_sym}
end

.tableObject

Raises:



55
56
57
# File 'lib/pod4/pg_interface.rb', line 55

def table
  raise Pod4Error, "You need to use set_table to set the table name"
end

Instance Method Details

#create(record) ⇒ Object

Record is a hash of field: value

By a happy coincidence, insert returns the unique ID for the record, which is just what we want to do, too.



117
118
119
120
121
122
123
124
125
126
127
# File 'lib/pod4/pg_interface.rb', line 117

def create(record)
  raise(ArgumentError, "Bad type for record parameter") \
    unless record.kind_of?(Hash) || record.kind_of?(Octothorpe)

  sql, vals = sql_insert(record) 
  x = selectp(sql, *vals)
  x.first[id_fld]

rescue => e
  handle_error(e)
end

#delete(id) ⇒ Object

ID is whatever you set in the interface using set_id_fld



173
174
175
176
177
178
179
180
181
182
183
# File 'lib/pod4/pg_interface.rb', line 173

def delete(id)
  read_or_die(id)

  sql, vals = sql_delete(id_fld => id)
  executep(sql, *vals)

  self

rescue => e
  handle_error(e)
end

#execute(sql) ⇒ Object

Run SQL code on the server; return true or false for success or failure



271
272
273
274
275
276
277
278
279
280
281
# File 'lib/pod4/pg_interface.rb', line 271

def execute(sql)
  raise(ArgumentError, "Bad SQL parameter") unless sql.kind_of?(String)

  ensure_connection

  Pod4.logger.debug(__FILE__){ "execute: #{sql}" }
  @client.exec(sql)

rescue => e
  handle_error(e)
end

#executep(sql, *vals) ⇒ Object

Run SQL code on the server as per execute() but with parameter insertion.

Placeholders in the SQL string should all be %s as per sql_helper methods. Values should be as returned by sql_helper methods.



290
291
292
293
294
295
296
297
298
299
300
# File 'lib/pod4/pg_interface.rb', line 290

def executep(sql, *vals)
  raise(ArgumentError, "Bad SQL parameter") unless sql.kind_of?(String)

  ensure_connection

  Pod4.logger.debug(__FILE__){ "parameterised execute: #{sql}" }
  @client.exec_params( *parse_for_params(sql, vals) )

rescue => e
  handle_error(e)
end

#list(selection = nil) ⇒ Object



99
100
101
102
103
104
105
106
107
108
# File 'lib/pod4/pg_interface.rb', line 99

def list(selection=nil)
  raise(ArgumentError, 'selection parameter is not a hash') \
    unless selection.nil? || selection.respond_to?(:keys)

  sql, vals = sql_select(nil, selection)
  selectp(sql, *vals) {|r| Octothorpe.new(r) }

rescue => e
  handle_error(e)
end

#read(id) ⇒ Object

ID corresponds to whatever you set in set_id_fld



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/pod4/pg_interface.rb', line 133

def read(id)
  raise(ArgumentError, "ID parameter is nil") if id.nil?

  sql, vals = sql_select(nil, id_fld => id) 
  rows = selectp(sql, *vals)
  Octothorpe.new(rows.first)

rescue => e
  # Select has already wrapped the error in a Pod4Error, but in this case we want to catch
  # something
  raise CantContinue, "That doesn't look like an ID" \
    if e.cause.class == PG::InvalidTextRepresentation

  handle_error(e)
end

#schemaObject



92
# File 'lib/pod4/pg_interface.rb', line 92

def schema; self.class.schema; end

#select(sql) ⇒ Object

Run SQL code on the server. Return the results.

Will return an array of records, or you can use it in block mode, like this:

select("select * from customer") do |r|
  # r is a single record
end

The returned results will be an array of hashes (or if you passed a block, of whatever you returned from the block).



198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
# File 'lib/pod4/pg_interface.rb', line 198

def select(sql)
  raise(ArgumentError, "Bad SQL parameter") unless sql.kind_of?(String)

  ensure_connection

  Pod4.logger.debug(__FILE__){ "select: #{sql}" }

  rows = []
  @client.exec(sql) do |query|
    oids = make_oid_hash(query)

    query.each do |r| 
      row = cast_row_fudge(r, oids)

      if block_given? 
        rows << yield(row)
      else
        rows << row
      end

    end
  end

  @client.cancel 

  rows

rescue => e
  handle_error(e)
end

#selectp(sql, *vals) ⇒ Object

Run SQL code on the server as per select() but with parameter insertion.

Placeholders in the SQL string should all be %s as per sql_helper methods. Values should be as returned by sql_helper methods.



236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
# File 'lib/pod4/pg_interface.rb', line 236

def selectp(sql, *vals)
  raise(ArgumentError, "Bad SQL parameter") unless sql.kind_of?(String)

  ensure_connection

  Pod4.logger.debug(__FILE__){ "select: #{sql} #{vals.inspect}" }

  rows = []
  @client.exec_params( *parse_for_params(sql, vals) ) do |query|
    oids = make_oid_hash(query)

    query.each do |r| 
      row = cast_row_fudge(r, oids)

      if block_given? 
        rows << yield(row)
      else
        rows << row
      end

    end
  end

  @client.cancel 

  rows

rescue => e
  handle_error(e)
end

#tableObject



93
# File 'lib/pod4/pg_interface.rb', line 93

def table;  self.class.table;  end

#update(id, record) ⇒ Object

ID is whatever you set in the interface using set_id_fld record should be a Hash or Octothorpe.



154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/pod4/pg_interface.rb', line 154

def update(id, record)
  raise(ArgumentError, "Bad type for record parameter") \
    unless record.kind_of?(Hash) || record.kind_of?(Octothorpe)

  read_or_die(id)

  sql, vals = sql_update(record, id_fld => id)
  executep(sql, *vals)

  self

rescue => e
  handle_error(e)
end