-
Notifications
You must be signed in to change notification settings - Fork 78
/
statement.cr
44 lines (41 loc) · 1.24 KB
/
statement.cr
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
class PG::Statement < ::DB::Statement
def initialize(connection, command : String)
super(connection, command)
end
protected def conn
connection.as(Connection).connection
end
protected def perform_query(args : Enumerable) : ResultSet
params = args.map { |arg| PQ::Param.encode(arg) }
conn = self.conn
conn.send_parse_message(command)
conn.send_bind_message params
conn.send_describe_portal_message
conn.send_execute_message
conn.send_sync_message
conn.expect_frame PQ::Frame::ParseComplete
conn.expect_frame PQ::Frame::BindComplete
frame = conn.read
case frame
when PQ::Frame::RowDescription
fields = frame.fields
when PQ::Frame::NoData
fields = nil
else
raise "expected RowDescription or NoData, got #{frame}"
end
ResultSet.new(self, fields)
rescue e : IO::Error
raise DB::ConnectionLost.new(connection, cause: e)
end
protected def perform_exec(args : Enumerable) : ::DB::ExecResult
result = perform_query(args)
result.each { }
::DB::ExecResult.new(
rows_affected: result.rows_affected,
last_insert_id: 0_i64 # postgres doesn't support this
)
rescue e : IO::Error
raise DB::ConnectionLost.new(connection, cause: e)
end
end