Class: PGWrapper

Inherits:
Object
  • Object
show all
Defined in:
lib/railz_lite/models/wrappers/pg_wrapper.rb

Instance Method Summary collapse

Constructor Details

#initialize(db_uri) ⇒ PGWrapper

Returns a new instance of PGWrapper.



4
5
6
7
8
# File 'lib/railz_lite/models/wrappers/pg_wrapper.rb', line 4

def initialize(db_uri)
  @db = PG::Connection.new(db_uri)
  @db.type_map_for_results = PG::BasicTypeMapForResults.new(@db) # converts pgsql strings to ruby type
  @db
end

Instance Method Details

#execute(sql, *args) ⇒ Object



10
11
12
13
# File 'lib/railz_lite/models/wrappers/pg_wrapper.rb', line 10

def execute(sql, *args)
  converted_sql = convert_escaped_question_marks(sql)
  @db.exec_params(converted_sql, args).to_a
end

#execute2(sql, *args) ⇒ Object



15
16
17
18
19
# File 'lib/railz_lite/models/wrappers/pg_wrapper.rb', line 15

def execute2(sql, *args)
  converted_sql = convert_escaped_question_marks(sql)
  result = @db.exec_params(converted_sql, args) 
  [result.fields, result.to_a]
end

#insert(sql, *args) ⇒ Object



21
22
23
24
25
26
# File 'lib/railz_lite/models/wrappers/pg_wrapper.rb', line 21

def insert(sql, *args)
  sql.insert(sql.index(';'), ' RETURNING ID')
  converted_sql = convert_escaped_question_marks(sql)
  result = @db.exec_params(converted_sql, args)
  result.to_a.first['id']
end