~singpolyma/sgx-jmp

ref: f571b5e726750cf8c4523c22d77e110194c3e16a sgx-jmp/lib/postgres.rb -rw-r--r-- 489 bytes
f571b5e7Stephen Paul Weber Postgres#query_one 3 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# frozen_string_literal: true

require "delegate"

class Postgres < SimpleDelegator
	def self.connect(**kwargs)
		new(PG::EM::ConnectionPool.new(**kwargs) { |conn|
			conn.type_map_for_results = PG::BasicTypeMapForResults.new(conn)
			conn.type_map_for_queries = PG::BasicTypeMapForQueries.new(conn)
		})
	end

	def query_one(sql, *args, field_names_as: :symbol, default: nil)
		query_defer(sql, args).then do |rows|
			rows.field_names_as(field_names_as)&.first || default
		end
	end
end