Class: RDBI::Driver::PostgreSQL::Statement

Inherits:
Statement
  • Object
show all
Extended by:
MethLab
Defined in:
lib/rdbi/driver/postgresql.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(query, dbh) ⇒ Statement

Returns a new instance of Statement.



250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
# File 'lib/rdbi/driver/postgresql.rb', line 250

def initialize( query, dbh )
  super( query, dbh )
  @stmt_name = Time.now.to_f.to_s

  ep = Epoxy.new( query )
  @index_map = ep.indexed_binds
  query = ep.quote(Hash[@index_map.compact.zip([])]) do |x|
    case x
    when Integer
      "$#{x+1}" 
    when Symbol
      num = @index_map.index(x)
      "$#{num+1}"
    end
  end

  @pg_result = dbh.pg_conn.prepare(
    @stmt_name,
    query
  )

  # @input_type_map initialized in superclass
  @output_type_map = RDBI::Type.create_type_hash( RDBI::Type::Out )
  @output_type_map[ :bigint ] = RDBI::Type.filterlist( RDBI::Type::Filters::STR_TO_INT )

  prep_finalizer { @pg_result.clear }
end

Instance Attribute Details

#pg_resultObject

Returns the value of attribute pg_result.



247
248
249
# File 'lib/rdbi/driver/postgresql.rb', line 247

def pg_result
  @pg_result
end

Instance Method Details

#new_execution(*binds) ⇒ Object

Returns an Array of things used to fill out the parameters to RDBI::Result.new



293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
# File 'lib/rdbi/driver/postgresql.rb', line 293

def new_execution( *binds )
  # FIXME move to RDBI::Util or something.
  hashes, binds = binds.partition { |x| x.kind_of?(Hash) }
  hash = hashes.inject({}, :merge)
  hash.each do |key, value|
    if index = @index_map.index(key)
      binds.insert(index, value)
    end
  end

  pg_result = @dbh.pg_conn.exec_prepared( @stmt_name, binds )

  columns = [] 
  column_query = (0...pg_result.num_fields).map do |x|
    "format_type(#{ pg_result.ftype(x) }, #{ pg_result.fmod(x) }) as col#{x}"
  end.join(", ")

  unless column_query.empty?
    @dbh.pg_conn.exec("select #{column_query}")[0].values.each_with_index do |type, i|
      c = RDBI::Column.new
      c.name = pg_result.fname( i ).to_sym
      c.type = type
      if c.type.start_with? 'timestamp'
        c.ruby_type = 'timestamp'.to_sym
      else
        c.ruby_type = c.type.to_sym
      end
      columns << c
    end
  end

  this_schema = RDBI::Schema.new
  this_schema.columns = columns

  [ Cursor.new(pg_result, this_schema), this_schema, @output_type_map ]
end

#new_modification(*binds) ⇒ Object



278
279
280
281
282
283
284
285
286
287
288
289
290
# File 'lib/rdbi/driver/postgresql.rb', line 278

def new_modification(*binds)
  # FIXME move to RDBI::Util or something.
  hashes, binds = binds.partition { |x| x.kind_of?(Hash) }
  hash = hashes.inject({}) { |x, y| x.merge(y) }
  hash.keys.each do |key| 
    if index = @index_map.index(key)
      binds.insert(index, hash[key])
    end
  end

  pg_result = @dbh.pg_conn.exec_prepared( @stmt_name, binds )
  return pg_result.cmd_tuples
end