-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathpostgresql.pl
191 lines (161 loc) · 6.01 KB
/
postgresql.pl
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
:- module(postgresql, [connect/6, query/3, query/4, sql/3]).
:- use_module(library(lists)).
:- use_module(library(charsio)).
:- use_module(library(sockets)).
:- use_module('messages').
:- use_module('sql_query').
:- use_module('types').
connect(User, Password, Host, Port, Database, postgresql(Stream)) :-
socket_client_open(Host:Port, Stream, [type(binary)]),
startup_message(User, Database, BytesStartup),
put_bytes(Stream, BytesStartup),
get_bytes(Stream, BytesAuth),
auth_message(password, BytesAuth),
password_message(Password, BytesPassword),
put_bytes(Stream, BytesPassword),
get_bytes(Stream, BytesOk),
auth_ok_message(BytesOk),
flush_bytes(Stream).
flush_bytes(Stream) :-
get_bytes(Stream, Bytes),
(
Bytes = [90|_] ->
true
; flush_bytes(Stream)
).
query(postgresql(Stream), Query, Result) :-
query_message(Query, BytesQuery),
put_bytes(Stream, BytesQuery),
get_bytes(Stream, BytesResponse),
try_query_response(Stream, BytesResponse, Result).
% after a query message, the following messages can be received
% - CommandComplete
% - RowDescription -> N DataRow
% - EmptyQueryResponse
% - ErrorResponse
% - NoticeResponse
% and then a ReadyForQuery message
try_query_response(Stream, BytesResponse, Result) :-
command_complete_message(BytesResponse),!,
Result = ok,
get_bytes(Stream, BytesEnd),
ready_for_query_message(BytesEnd).
try_query_response(Stream, BytesResponse, Result) :-
row_description_message(ColumnsDescription, BytesResponse),!,
% then zero or more data rows
get_bytes(Stream, BytesData),
get_data_rows(Stream, ColumnsData, BytesData),
Result = data(ColumnsDescription, ColumnsData).
% until we get a command complete message
try_query_response(Stream, BytesResponse, Result) :-
empty_query_message(BytesResponse),!,
Result = [],
get_bytes(Stream, BytesEnd),
ready_for_query_message(BytesEnd).
try_query_response(Stream, BytesResponse, Result) :-
error_message(Error, BytesResponse),!,
Result = error(Error),
get_bytes(Stream, BytesEnd),
ready_for_query_message(BytesEnd).
try_query_response(Stream, BytesResponse, Result) :-
notice_message(BytesResponse),!,
get_bytes(Stream, BytesResponse0),
try_query_response(Stream, BytesResponse0, Result).
get_data_rows(Stream, [], BytesData) :-
command_complete_message(BytesData),!,
get_bytes(Stream, BytesData0),
ready_for_query_message(BytesData0).
get_data_rows(Stream, [Column|Columns], BytesData) :-
data_row_message(Column, BytesData),!,
get_bytes(Stream, BytesData0),
get_data_rows(Stream, Columns, BytesData0).
% Extended Query. Safer
query(postgresql(Stream), Query, Params, Result) :-
length(Params, NumberParams),
parse_message(Query, NumberParams, QueryBytes),
put_bytes(Stream, QueryBytes),
flush_message(FlushBytes),
put_bytes(Stream, FlushBytes),
get_bytes(Stream, ResponseBytes),
try_parse_response(Stream, ResponseBytes, Params, Result).
try_parse_response(Stream, BytesData, Params, Result) :-
parse_complete_message(BytesData),!,
bind_message(Params, BindBytes),
put_bytes(Stream, BindBytes),
flush_message(FlushBytes),
put_bytes(Stream, FlushBytes),
get_bytes(Stream, ResponseBytes),
try_bind_response(Stream, ResponseBytes, Result).
try_parse_response(Stream, BytesResponse, _, Result) :-
error_message(Error, BytesResponse),!,
Result = error(Error),
sync_message(SyncBytes),
put_bytes(Stream, SyncBytes),
get_bytes(Stream, BytesEnd),
ready_for_query_message(BytesEnd).
try_bind_response(Stream, BytesData, Result) :-
bind_complete_message(BytesData),!,
execute_message(ExecuteBytes),
put_bytes(Stream, ExecuteBytes),
flush_message(FlushBytes),
put_bytes(Stream, FlushBytes),
get_bytes(Stream, ResponseBytes),
try_execute_response(Stream, ResponseBytes, Result).
try_bind_response(Stream, BytesResponse, _, Result) :-
error_message(Error, BytesResponse),!,
Result = error(Error),
sync_message(SyncBytes),
put_bytes(Stream, SyncBytes),
get_bytes(Stream, BytesEnd),
ready_for_query_message(BytesEnd).
try_execute_response(Stream, BytesResponse, Result) :-
ext_get_data_rows(Stream, ColumnsData, BytesResponse),
Result = data(ColumnsData).
try_execute_response(Stream, BytesResponse, Result) :-
empty_query_message(BytesResponse),!,
Result = [],
get_bytes(Stream, BytesEnd),
ready_for_query_message(BytesEnd).
try_execute_response(Stream, BytesResponse, Result) :-
error_message(Error, BytesResponse),!,
Result = error(Error),
get_bytes(Stream, BytesEnd),
ready_for_query_message(BytesEnd).
ext_get_data_rows(Stream, [], BytesData) :-
command_complete_message(BytesData),!,
sync_message(SyncBytes),
put_bytes(Stream, SyncBytes),
get_bytes(Stream, BytesData0),
ready_for_query_message(BytesData0).
ext_get_data_rows(Stream, [Column|Columns], BytesData) :-
data_row_message(Column, BytesData),!,
get_bytes(Stream, BytesData0),
ext_get_data_rows(Stream, Columns, BytesData0).
% https://www.postgresql.org/docs/current/protocol-flow.html#id-1.10.5.7.3
get_bytes(Stream, Bytes) :-
get_byte(Stream, BType),
get_byte(Stream, B3),
get_byte(Stream, B2),
get_byte(Stream, B1),
get_byte(Stream, B0),
int32(Length, [B3, B2, B1, B0]),
RemainingBytes is Length - 4,
get_bytes(Stream, RemainingBytes, Bytes0),
append([BType, B3, B2, B1, B0], Bytes0, Bytes),
!.
get_bytes(_, 0, []).
get_bytes(Stream, RemainingBytes, [B|Bytes]) :-
get_byte(Stream, B),
RemainingBytes1 is RemainingBytes - 1,
get_bytes(Stream, RemainingBytes1, Bytes).
put_bytes(_, []).
put_bytes(Stream, [Byte|Bytes]) :-
put_byte(Stream, Byte),
put_bytes(Stream, Bytes),
!.
sql(Connection, Query, Result) :-
sql_query(Query, TextQuery, Vars),
keysort(Vars, SortedVars),
maplist(pair_value, SortedVars, QueryVars),
query(Connection, TextQuery, QueryVars, Result).
pair_value(_-B, B).