forked from ermine/sulci
-
Notifications
You must be signed in to change notification settings - Fork 0
/
markov_sql.ml
165 lines (145 loc) · 5.34 KB
/
markov_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
(* 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_words db =
T.execute db "CREATE TABLE IF NOT EXISTS words (word1 varchar(256), word2 varchar(256), counter int)" T.no_params
let create_index_word1word2 db =
T.execute db "CREATE INDEX IF NOT EXISTS word1word2 ON words (word1, word2)" T.no_params
let check_pair db ~word1 ~word2 =
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 word1;
T.set_param_Text p 1 word2;
T.finish_params p
in
T.select1 db "SELECT 1 FROM words WHERE word1=@word1 AND word2=@word2 LIMIT 1" set_params get_row
let add_pair db ~word1 ~word2 ~counter =
let set_params stmt =
let p = T.start_params stmt 3 in
T.set_param_Text p 0 word1;
T.set_param_Text p 1 word2;
T.set_param_Int p 2 counter;
T.finish_params p
in
T.execute db "INSERT INTO words (word1, word2, counter) VALUES (@word1,@word2,@counter)" set_params
let update_pair db ~word1 ~word2 =
let set_params stmt =
let p = T.start_params stmt 2 in
T.set_param_Text p 0 word1;
T.set_param_Text p 1 word2;
T.finish_params p
in
T.execute db "UPDATE words SET counter=counter+1 WHERE word1=@word1 AND word2=@word2" set_params
let get_sum db ~word1 =
let get_row stmt =
(T.get_column_Int stmt 0)
in
let set_params stmt =
let p = T.start_params stmt 1 in
T.set_param_Text p 0 word1;
T.finish_params p
in
T.select1 db "SELECT coalesce(sum(counter),0) FROM words WHERE word1=@word1 LIMIT 1" set_params get_row
let get_pair db ~word1 callback =
let invoke_callback stmt =
callback
(T.get_column_Text stmt 0)
(T.get_column_Int stmt 1)
in
let set_params stmt =
let p = T.start_params stmt 1 in
T.set_param_Text p 0 word1;
T.finish_params p
in
T.select db "SELECT word2, counter FROM words WHERE word1=@word1" set_params invoke_callback
let count db =
let get_row stmt =
(T.get_column_Int stmt 0)
in
T.select1 db "SELECT COUNT(*) FROM words" T.no_params get_row
let get_top db callback =
let invoke_callback stmt =
callback
(T.get_column_Text stmt 0)
(T.get_column_Text stmt 1)
(T.get_column_Int stmt 2)
in
T.select db "SELECT word1, word2, counter FROM words WHERE word1!='' AND word2!='' ORDER BY counter DESC LIMIT 10" T.no_params invoke_callback
module Fold = struct
let create_words db =
T.execute db "CREATE TABLE IF NOT EXISTS words (word1 varchar(256), word2 varchar(256), counter int)" T.no_params
let create_index_word1word2 db =
T.execute db "CREATE INDEX IF NOT EXISTS word1word2 ON words (word1, word2)" T.no_params
let check_pair db ~word1 ~word2 =
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 word1;
T.set_param_Text p 1 word2;
T.finish_params p
in
T.select1 db "SELECT 1 FROM words WHERE word1=@word1 AND word2=@word2 LIMIT 1" set_params get_row
let add_pair db ~word1 ~word2 ~counter =
let set_params stmt =
let p = T.start_params stmt 3 in
T.set_param_Text p 0 word1;
T.set_param_Text p 1 word2;
T.set_param_Int p 2 counter;
T.finish_params p
in
T.execute db "INSERT INTO words (word1, word2, counter) VALUES (@word1,@word2,@counter)" set_params
let update_pair db ~word1 ~word2 =
let set_params stmt =
let p = T.start_params stmt 2 in
T.set_param_Text p 0 word1;
T.set_param_Text p 1 word2;
T.finish_params p
in
T.execute db "UPDATE words SET counter=counter+1 WHERE word1=@word1 AND word2=@word2" set_params
let get_sum db ~word1 =
let get_row stmt =
(T.get_column_Int stmt 0)
in
let set_params stmt =
let p = T.start_params stmt 1 in
T.set_param_Text p 0 word1;
T.finish_params p
in
T.select1 db "SELECT coalesce(sum(counter),0) FROM words WHERE word1=@word1 LIMIT 1" set_params get_row
let get_pair db ~word1 callback acc =
let invoke_callback stmt =
callback
(T.get_column_Text stmt 0)
(T.get_column_Int stmt 1)
in
let set_params stmt =
let p = T.start_params stmt 1 in
T.set_param_Text p 0 word1;
T.finish_params p
in
let r_acc = ref acc in
T.select db "SELECT word2, counter FROM words WHERE word1=@word1" set_params (fun x -> r_acc := invoke_callback x !r_acc);
!r_acc
let count db =
let get_row stmt =
(T.get_column_Int stmt 0)
in
T.select1 db "SELECT COUNT(*) FROM words" T.no_params get_row
let get_top db callback acc =
let invoke_callback stmt =
callback
(T.get_column_Text stmt 0)
(T.get_column_Text stmt 1)
(T.get_column_Int stmt 2)
in
let r_acc = ref acc in
T.select db "SELECT word1, word2, counter FROM words WHERE word1!='' AND word2!='' ORDER BY counter DESC LIMIT 10" T.no_params (fun x -> r_acc := invoke_callback x !r_acc);
!r_acc
end (* module Fold *)
end (* module Make *)