forked from ermine/sulci
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtalkers_sql.ml
215 lines (193 loc) · 7.67 KB
/
talkers_sql.ml
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
(* DO NOT EDIT MANUALLY *)
(* *)
(* generated by sqlgg 0.2.3-49-g30df037 on 2010-01-14T17:45Z *)
module Make (T : Sqlgg_traits.M) = struct
let create_talkers db =
T.execute db "CREATE TABLE IF NOT EXISTS talkers (jid varchar, nick varchar, room varchar, words int, me int, sentences int)" T.no_params
let create_index_talkers_idx db =
T.execute db "CREATE INDEX IF NOT EXISTS talkers_idx ON talkers (jid, room)" T.no_params
let create_index_words_idx db =
T.execute db "CREATE INDEX IF NOT EXISTS words_idx ON talkers (words)" T.no_params
let test_nick db ~nick ~room =
let get_row stmt =
(T.get_column_Int stmt 0)
in
let set_params stmt =
let p = T.start_params stmt 2 in
T.set_param_Text p 0 nick;
T.set_param_Text p 1 room;
T.finish_params p
in
T.select1 db "SELECT 1 FROM talkers WHERE nick=@nick AND room=@room LIMIT 1" set_params get_row
let test_jid db ~jid ~room =
let get_row stmt =
(T.get_column_Int stmt 0)
in
let set_params stmt =
let p = T.start_params stmt 2 in
T.set_param_Text p 0 jid;
T.set_param_Text p 1 room;
T.finish_params p
in
T.select1 db "SELECT 1 FROM talkers WHERE jid=@jid AND room=@room limit 1" set_params get_row
let update_by_nick db ~words ~sentences ~me ~nick ~room =
let set_params stmt =
let p = T.start_params stmt 5 in
T.set_param_Int p 0 words;
T.set_param_Int p 1 sentences;
T.set_param_Int p 2 me;
T.set_param_Text p 3 nick;
T.set_param_Text p 4 room;
T.finish_params p
in
T.execute db "UPDATE talkers SET words=words+@words, sentences=sentences+@sentences, me=me+@me WHERE nick=@nick AND room=@room" set_params
let update_by_jid db ~words ~sentences ~me ~jid ~room =
let set_params stmt =
let p = T.start_params stmt 5 in
T.set_param_Int p 0 words;
T.set_param_Int p 1 sentences;
T.set_param_Int p 2 me;
T.set_param_Text p 3 jid;
T.set_param_Text p 4 room;
T.finish_params p
in
T.execute db "UPDATE talkers SET words=words+@words, sentences=sentences+@sentences, me=me+@me WHERE jid=@jid AND room=@room" set_params
let insert_new db ~jid ~nick ~room ~words ~me ~sentences =
let set_params stmt =
let p = T.start_params stmt 6 in
T.set_param_Text p 0 jid;
T.set_param_Text p 1 nick;
T.set_param_Text p 2 room;
T.set_param_Int p 3 words;
T.set_param_Int p 4 me;
T.set_param_Int p 5 sentences;
T.finish_params p
in
T.execute db "INSERT INTO talkers (jid, nick, room, words, me, sentences) VALUES (@jid,@nick,@room,@words,@me,@sentences)" set_params
let select_talkers_by_nick db ~room ~nick callback =
let invoke_callback stmt =
callback
(T.get_column_Text stmt 0)
(T.get_column_Int stmt 1)
(T.get_column_Int stmt 2)
(T.get_column_Int stmt 3)
in
let set_params stmt =
let p = T.start_params stmt 2 in
T.set_param_Text p 0 room;
T.set_param_Text p 1 nick;
T.finish_params p
in
T.select db "SELECT nick, words, me, sentences FROM talkers WHERE room=@room AND nick LIKE @nick ORDER BY words DESC, sentences ASC" set_params invoke_callback
let select_talkers_limit db ~room callback =
let invoke_callback stmt =
callback
(T.get_column_Text stmt 0)
(T.get_column_Int stmt 1)
(T.get_column_Int stmt 2)
(T.get_column_Int stmt 3)
in
let set_params stmt =
let p = T.start_params stmt 1 in
T.set_param_Text p 0 room;
T.finish_params p
in
T.select db "SELECT nick, words, me, sentences FROM talkers WHERE room=@room ORDER BY words DESC, sentences ASC LIMIT 10" set_params invoke_callback
module Fold = struct
let create_talkers db =
T.execute db "CREATE TABLE IF NOT EXISTS talkers (jid varchar, nick varchar, room varchar, words int, me int, sentences int)" T.no_params
let create_index_talkers_idx db =
T.execute db "CREATE INDEX IF NOT EXISTS talkers_idx ON talkers (jid, room)" T.no_params
let create_index_words_idx db =
T.execute db "CREATE INDEX IF NOT EXISTS words_idx ON talkers (words)" T.no_params
let test_nick db ~nick ~room =
let get_row stmt =
(T.get_column_Int stmt 0)
in
let set_params stmt =
let p = T.start_params stmt 2 in
T.set_param_Text p 0 nick;
T.set_param_Text p 1 room;
T.finish_params p
in
T.select1 db "SELECT 1 FROM talkers WHERE nick=@nick AND room=@room LIMIT 1" set_params get_row
let test_jid db ~jid ~room =
let get_row stmt =
(T.get_column_Int stmt 0)
in
let set_params stmt =
let p = T.start_params stmt 2 in
T.set_param_Text p 0 jid;
T.set_param_Text p 1 room;
T.finish_params p
in
T.select1 db "SELECT 1 FROM talkers WHERE jid=@jid AND room=@room limit 1" set_params get_row
let update_by_nick db ~words ~sentences ~me ~nick ~room =
let set_params stmt =
let p = T.start_params stmt 5 in
T.set_param_Int p 0 words;
T.set_param_Int p 1 sentences;
T.set_param_Int p 2 me;
T.set_param_Text p 3 nick;
T.set_param_Text p 4 room;
T.finish_params p
in
T.execute db "UPDATE talkers SET words=words+@words, sentences=sentences+@sentences, me=me+@me WHERE nick=@nick AND room=@room" set_params
let update_by_jid db ~words ~sentences ~me ~jid ~room =
let set_params stmt =
let p = T.start_params stmt 5 in
T.set_param_Int p 0 words;
T.set_param_Int p 1 sentences;
T.set_param_Int p 2 me;
T.set_param_Text p 3 jid;
T.set_param_Text p 4 room;
T.finish_params p
in
T.execute db "UPDATE talkers SET words=words+@words, sentences=sentences+@sentences, me=me+@me WHERE jid=@jid AND room=@room" set_params
let insert_new db ~jid ~nick ~room ~words ~me ~sentences =
let set_params stmt =
let p = T.start_params stmt 6 in
T.set_param_Text p 0 jid;
T.set_param_Text p 1 nick;
T.set_param_Text p 2 room;
T.set_param_Int p 3 words;
T.set_param_Int p 4 me;
T.set_param_Int p 5 sentences;
T.finish_params p
in
T.execute db "INSERT INTO talkers (jid, nick, room, words, me, sentences) VALUES (@jid,@nick,@room,@words,@me,@sentences)" set_params
let select_talkers_by_nick db ~room ~nick callback acc =
let invoke_callback stmt =
callback
(T.get_column_Text stmt 0)
(T.get_column_Int stmt 1)
(T.get_column_Int stmt 2)
(T.get_column_Int stmt 3)
in
let set_params stmt =
let p = T.start_params stmt 2 in
T.set_param_Text p 0 room;
T.set_param_Text p 1 nick;
T.finish_params p
in
let r_acc = ref acc in
T.select db "SELECT nick, words, me, sentences FROM talkers WHERE room=@room AND nick LIKE @nick ORDER BY words DESC, sentences ASC" set_params (fun x -> r_acc := invoke_callback x !r_acc);
!r_acc
let select_talkers_limit db ~room callback acc =
let invoke_callback stmt =
callback
(T.get_column_Text stmt 0)
(T.get_column_Int stmt 1)
(T.get_column_Int stmt 2)
(T.get_column_Int stmt 3)
in
let set_params stmt =
let p = T.start_params stmt 1 in
T.set_param_Text p 0 room;
T.finish_params p
in
let r_acc = ref acc in
T.select db "SELECT nick, words, me, sentences FROM talkers WHERE room=@room ORDER BY words DESC, sentences ASC LIMIT 10" set_params (fun x -> r_acc := invoke_callback x !r_acc);
!r_acc
end (* module Fold *)
end (* module Make *)