-
Notifications
You must be signed in to change notification settings - Fork 18
/
sql.ml
508 lines (422 loc) · 17.4 KB
/
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
(** *)
open Printf
open ExtLib
open Prelude
module Type =
struct
type kind =
| Unit of [`Interval]
| Int
| Text
| Blob
| Float
| Bool
| Datetime
| Decimal
| Any (* FIXME - Top and Bottom ? *)
[@@deriving eq, show{with_path=false}]
(* TODO NULL is currently typed as Any? which actually is a misnormer *)
type nullability =
| Nullable (** can be NULL *)
| Strict (** cannot be NULL *)
| Depends (** unknown, to be determined *)
[@@deriving eq, show{with_path=false}]
type t = { t : kind; nullability : nullability; }[@@deriving eq, show{with_path=false}]
let nullability nullability = fun t -> { t; nullability }
let strict = nullability Strict
let depends = nullability Depends
let nullable = nullability Nullable
let make_nullable { t; nullability=_ } = nullable t
let (=) : t -> t -> bool = equal
let show { t; nullability; } = show_kind t ^ (match nullability with Nullable -> "?" | Depends -> "??" | Strict -> "")
let _ = pp
let pp pf t = Format.pp_print_string pf (show t)
let type_name t = show_kind t.t
let is_any { t; nullability = _ } = equal_kind t Any
let is_unit = function { t = Unit _; _ } -> true | _ -> false
(** @return (subtype, supertype) *)
let order_kind x y =
if equal_kind x y then
`Equal
else
match x,y with
| Any, t | t, Any -> `Order (t,t)
| Int, Float | Float, Int -> `Order (Int,Float)
(* arbitrary decision : allow int<->decimal but require explicit cast for floats *)
| Decimal, Int | Int, Decimal -> `Order (Int,Decimal)
| Text, Blob | Blob, Text -> `Order (Text,Blob)
| Int, Datetime | Datetime, Int -> `Order (Int,Datetime)
| Text, Datetime | Datetime, Text -> `Order (Datetime,Text)
| _ -> `No
let order_nullability x y =
match x,y with
| Depends, Depends -> `Equal Depends
| Nullable, Nullable -> `Equal Nullable
| Strict, Strict -> `Equal Strict
| Depends, n
| n, Depends -> `Equal n (* Order ? *)
| Strict, Nullable -> `Strict_Nullable
| Nullable, Strict -> `Nullable_Strict
let common_nullability = List.fold_left (fun acc t ->
match acc, t.nullability with
| _, Nullable
| Nullable, _ -> Nullable
| _, Strict
| Strict, _ -> Strict
| Depends, Depends -> Depends
) Depends
let common_nullability l = match common_nullability l with Depends -> Strict | n -> n
let undepend t nullability = if equal_nullability t.nullability Depends then { t with nullability } else t
let common_type_ order x y =
match order_nullability x.nullability y.nullability, order_kind x.t y.t with
| _, `No -> None
| `Equal nullability, `Order pair -> Some {t = order pair; nullability}
| `Equal nullability, `Equal -> Some { x with nullability }
| (`Nullable_Strict|`Strict_Nullable), `Equal -> Some (nullable x.t) (* FIXME need nullability order? *)
| (`Nullable_Strict|`Strict_Nullable), `Order pair -> Some (nullable @@ order pair)
let common_type_l_ order = function
| [] -> None
| t::ts -> List.fold_left (fun acc t -> match acc with None -> None | Some prev -> common_type_ order prev t) (Some t) ts
let subtype = common_type_ fst
let supertype = common_type_ snd
let common_subtype = common_type_l_ fst
let common_supertype = common_type_l_ snd
let common_type = subtype
let has_common_type x y = Option.is_some @@ subtype x y
type tyvar = Typ of t | Var of int
let string_of_tyvar = function Typ t -> show t | Var i -> sprintf "'%c" (Char.chr @@ Char.code 'a' + i)
type func =
| Group of t (* _ -> t *)
| Agg (* 'a -> 'a *)
| Multi of tyvar * tyvar (* 'a -> ... -> 'a -> 'b *)
| Ret of kind (* _ -> t *) (* TODO eliminate *)
| F of tyvar * tyvar list
let monomorphic ret args = F (Typ ret, List.map (fun t -> Typ t) args)
let fixed ret args = monomorphic (depends ret) (List.map depends args)
let identity = F (Var 0, [Var 0])
let pp_func pp =
let open Format in
function
| Agg -> fprintf pp "|'a| -> 'a"
| Group ret -> fprintf pp "|_| -> %s" (show ret)
| Ret ret -> fprintf pp "_ -> %s" (show_kind ret)
| F (ret, args) -> fprintf pp "%s -> %s" (String.concat " -> " @@ List.map string_of_tyvar args) (string_of_tyvar ret)
| Multi (ret, each_arg) -> fprintf pp "{ %s }+ -> %s" (string_of_tyvar each_arg) (string_of_tyvar ret)
let string_of_func = Format.asprintf "%a" pp_func
let is_grouping = function
| Group _ | Agg -> true
| Ret _ | F _ | Multi _ -> false
end
module Constraint =
struct
type conflict_algo = | Ignore | Replace | Abort | Fail | Rollback
[@@deriving show{with_path=false}, ord]
type t = | PrimaryKey | NotNull | Null | Unique | Autoincrement | OnConflict of conflict_algo
[@@deriving show{with_path=false}, ord]
end
module Constraints = struct
include Set.Make(Constraint)
let show s = [%derive.show: Constraint.t list] (elements s)
let pp fmt s = Format.fprintf fmt "%s" (show s)
end
type attr = {name : string; domain : Type.t; extra : Constraints.t; }
[@@deriving show {with_path=false}]
let make_attribute name kind extra =
if Constraints.mem Null extra && Constraints.mem NotNull extra then fail "Column %s can be either NULL or NOT NULL, but not both" name;
let domain = Type.{ t = Option.default Int kind; nullability = if Constraints.mem NotNull extra then Strict else Nullable } in
{name;domain;extra}
let unnamed_attribute domain = {name="";domain;extra=Constraints.empty}
module Schema =
struct
type t = attr list
[@@deriving show]
exception Error of t * string
let raise_error t fmt = Printf.ksprintf (fun s -> raise (Error (t,s))) fmt
(** FIXME attribute case sensitivity? *)
let by_name name = function attr -> attr.name = name
let find_by_name t name = List.find_all (by_name name) t
let find t name =
match find_by_name t name with
| [x] -> x
| [] -> raise (Error (t,"missing attribute : " ^ name))
| _ -> raise (Error (t,"duplicate attribute : " ^ name))
let mem_by_name t a =
match find_by_name t a.name with
| [_] -> true
| [] -> false
| _ -> raise (Error (t,"duplicate attribute : " ^ a.name))
let sub_by_name l del = List.filter (fun x -> not (mem_by_name del x)) l
let make_unique = List.unique ~cmp:(fun a1 a2 -> a1.name = a2.name && a1.name <> "")
let is_unique t = List.length (make_unique t) = List.length t
let check_unique t = is_unique t || raise (Error (t,"duplicate attributes"))
let project names t = List.map (find t) names
let change_inplace t before after =
ignore (find t before);
List.map (fun attr ->
match by_name before attr with
| true -> after
| false -> attr ) t
let exists t name =
match (find t name : attr) with
| _ -> true
| exception _ -> false
let rename t oldname newname =
if not (exists t oldname) then raise @@ Error (t, "no such column : " ^ oldname);
if exists t newname then raise @@ Error (t, "column already exists : " ^ newname);
List.map (fun attr -> if attr.name = oldname then { attr with name = newname } else attr) t
let to_string v = v |> List.map (fun attr -> sprintf "%s %s" (Type.show attr.domain) attr.name) |>
String.concat ", " |> sprintf "[%s]"
let names t = t |> List.map (fun attr -> attr.name) |> String.concat "," |> sprintf "[%s]"
module Join = struct
type 'a condition = On of 'a | Default | Natural | Using of string list [@@deriving show]
type typ = Left | Right | Full | Inner [@@deriving show]
let cross t1 t2 = t1 @ t2
(* TODO check that attribute types match (ignoring nullability)? *)
let natural t1 t2 =
let (common,t1only) = List.partition (fun a -> mem_by_name t2 a) t1 in
if 0 = List.length common then raise (Error (t1,"no common attributes for natural join of " ^ (names t1) ^ " and " ^ (names t2)));
common @ t1only @ sub_by_name t2 common
let using l t1 t2 =
let common = List.map (find t1) l in
List.iter (fun a -> let (_:attr) = find t2 a.name in ()) common;
common @ sub_by_name t1 common @ sub_by_name t2 common
let join typ cond a b =
let nullable = List.map (fun a -> { a with domain = Type.make_nullable a.domain }) in
let action = match cond with Default | On _ -> cross | Natural -> natural | Using l -> using l in
match typ with
| Inner -> action a b
| Left -> action a (nullable b)
| Right -> action (nullable a) b
| Full -> action (nullable a) (nullable b)
end
let cross_all l = List.fold_left Join.cross [] l
let compound t1 t2 =
if List.length t1 <> List.length t2 then raise (Error (t1, (to_string t1) ^ " differs in size to " ^ (to_string t2)));
let show_name i a =
match a.name with
| "" -> sprintf "column %d (of %d)" (i+1) (List.length t1)
| s -> s
in
List.combine t1 t2
|> List.mapi begin fun i (a1,a2) ->
match Type.supertype a1.domain a2.domain with
| Some t -> { a1 with domain=t }
| None -> raise (Error (t1, sprintf "Attributes do not match : %s of type %s and %s of type %s"
(show_name i a1) (Type.show a1.domain)
(show_name i a2) (Type.show a2.domain)))
end
let add t col pos =
match find_by_name t col.name with
| [] ->
begin
match pos with
| `First -> col::t
| `Default -> t @ [col]
| `After name ->
try
let (i,_) = List.findi (fun _ attr -> by_name name attr) t in
let (l1,l2) = List.split_nth (i+1) t in
l1 @ (col :: l2)
with
Not_found -> raise (Error (t,"Can't insert column " ^ col.name ^ " after non-existing column " ^ name))
end
| _ -> raise (Error (t,"Already has column " ^ col.name))
let drop t col =
ignore (find t col);
List.remove_if (by_name col) t
let change t oldcol col pos =
match pos with
| `Default -> change_inplace t oldcol col
| `First | `After _ -> add (drop t oldcol) col pos
let to_string = show
let print x = prerr_endline (to_string x)
end
type table_name = { db : string option; tn : string } [@@deriving show]
let show_table_name { db; tn } = match db with Some db -> sprintf "%s.%s" db tn | None -> tn
let make_table_name ?db tn = { db; tn }
type schema = Schema.t [@@deriving show]
type table = table_name * schema [@@deriving show]
let print_table out (name,schema) =
IO.write_line out (show_table_name name);
schema |> List.iter begin fun {name;domain;extra} ->
IO.printf out "%10s %s %s\n" (Type.show domain) name (Constraints.show extra)
end;
IO.write_line out ""
(** optional name and start/end position in string *)
type param_id = { label : string option; pos : int * int; } [@@deriving show]
type param = { id : param_id; typ : Type.t; } [@@deriving show]
let new_param id typ = { id; typ; }
type params = param list [@@deriving show]
type ctor =
| Simple of param_id * var list option
| Verbatim of string * string
and var =
| Single of param
| SingleIn of param
| ChoiceIn of { param: param_id; kind : [`In | `NotIn]; vars: var list }
| Choice of param_id * ctor list
| TupleList of param_id * schema
[@@deriving show]
type vars = var list [@@deriving show]
type alter_pos = [ `After of string | `Default | `First ]
type alter_action = [
| `Add of attr * alter_pos
| `RenameTable of table_name
| `RenameColumn of string * string
| `RenameIndex of string * string
| `Drop of string
| `Change of string * attr * alter_pos
| `None ]
type select_result = (schema * param list)
type direction = [ `Fixed | `Param of param_id ] [@@deriving show]
type int_or_param = [`Const of int | `Limit of param]
type limit_t = [ `Limit | `Offset ]
type col_name = {
cname : string; (** column name *)
tname : table_name option;
}
and limit = param list * bool
and nested = source * (source * Schema.Join.typ * join_condition) list
and source = [ `Select of select_full | `Table of table_name | `Nested of nested ] * table_name option (* alias *)
and join_condition = expr Schema.Join.condition
and select = {
columns : column list;
from : nested option;
where : expr option;
group : expr list;
having : expr option;
}
and select_full = {
select : select * select list;
order : order;
limit : limit option;
}
and order = (expr * direction option) list
and 'expr choices = (param_id * 'expr option) list
and expr =
| Value of Type.t (** literal value *)
| Param of param
| Inparam of param
| Choices of param_id * expr choices
| InChoice of param_id * [`In | `NotIn] * expr
| Fun of Type.func * expr list (** parameters *)
| SelectExpr of select_full * [ `AsValue | `Exists ]
| Column of col_name
| Inserted of string (** inserted value *)
and column =
| All
| AllOf of table_name
| Expr of expr * string option (** name *)
[@@deriving show {with_path=false}]
type columns = column list [@@deriving show]
let expr_to_string = show_expr
type assignments = (col_name * expr) list
type insert_action =
{
target : table_name;
action : [ `Set of assignments option
| `Values of (string list option * [ `Expr of expr | `Default ] list list option) (* column names * list of value tuples *)
| `Param of (string list option * param_id)
| `Select of (string list option * select_full) ];
on_duplicate : assignments option;
}
type stmt =
| Create of table_name * [ `Schema of schema | `Select of select_full ]
| Drop of table_name
| Alter of table_name * alter_action list
| Rename of (table_name * table_name) list
| CreateIndex of string * table_name * string list (* index name, table name, columns *)
| Insert of insert_action
| Delete of table_name * expr option
| DeleteMulti of table_name list * nested * expr option
| Set of string * expr
| Update of table_name * assignments * expr option * order * param list (* where, order, limit *)
| UpdateMulti of source list * assignments * expr option
| Select of select_full
| CreateRoutine of string * Type.kind option * (string * Type.kind * expr option) list
(*
open Schema
let test = [{name="a";domain=Type.Int}; {name="b";domain=Type.Int}; {name="c";domain=Type.Text};];;
let () = print test
let () = print (project ["b";"c";"b"] test)
let () = print (project ["b";"d"] test)
let () = print (rename test "a" "new_a")
*)
module Function : sig
val lookup : string -> int -> Type.func
val add : int -> Type.func -> string -> unit
val exclude : int -> string -> unit
val monomorphic : Type.t -> Type.t list -> string -> unit
val multi : ret:Type.tyvar -> Type.tyvar -> string -> unit
val multi_polymorphic : string -> unit
val sponge : Type.func
end = struct
let h = Hashtbl.create 10
let add_ narg typ name =
let name = String.lowercase_ascii name in
if Hashtbl.mem h (name,narg) then
let func = match narg with None -> sprintf "%S" name | Some n -> sprintf "%S of %d arguments" name n in
fail "Function %s already registered" func
else
Hashtbl.add h (name,narg) typ
let exclude narg name = add_ (Some narg) None name
let add_multi typ name = add_ None (Some typ) name
let add narg typ name = add_ (Some narg) (Some typ) name
let sponge = let open Type in let any = depends Any in Multi (Typ any, Typ any)
let lookup name narg =
let name = String.lowercase_ascii name in
match Hashtbl.find h (name,Some narg) with
| None ->
eprintfn "W: wrong number of arguments for known function %S, treating as untyped" name;
sponge
| Some t -> t
| exception _ ->
match Hashtbl.find h (name,None) with
| None -> assert false
| Some t -> t
| exception _ ->
eprintfn "W: unknown function %S of %d arguments, treating as untyped" name narg;
sponge
let monomorphic ret args name = add (List.length args) Type.(monomorphic ret args) name
let multi_polymorphic name = add_multi Type.(Multi (Var 0, Var 0)) name
let multi ~ret args name = add_multi Type.(Multi (ret, args)) name
end
let () =
let open Type in
let open Function in
let (||>) x f = List.iter f x in
let int = strict Int in
let float = strict Float in
let text = strict Text in
let datetime = strict Datetime in
"count" |> add 0 (Group int); (* count( * ) - asterisk is treated as no parameters in parser *)
"count" |> add 1 (Group int);
"avg" |> add 1 (Group float);
["max";"min";"sum"] ||> add 1 Agg; (* TODO nullable, think no rows *)
["max";"min"] ||> multi_polymorphic; (* sqlite3 *)
["lower";"upper";"unhex";"md5";"sha";"sha1";"sha2"] ||> monomorphic text [text];
"hex" |> monomorphic text [int];
"length" |> monomorphic int [text];
["random"] ||> monomorphic int [];
"floor" |> monomorphic int [float];
"nullif" |> add 2 (F (Var 0 (* TODO nullable *), [Var 0; Var 0]));
"ifnull" |> add 2 (F (Var 0, [Var 1; Var 0]));
["least";"greatest";"coalesce"] ||> multi_polymorphic;
"strftime" |> exclude 1; (* requires at least 2 arguments *)
["concat";"concat_ws";"strftime"] ||> multi ~ret:(Typ text) (Typ text);
"date" |> monomorphic datetime [datetime];
"time" |> monomorphic text [datetime];
"julianday" |> multi ~ret:(Typ float) (Typ text);
"from_unixtime" |> monomorphic datetime [int];
"from_unixtime" |> monomorphic text [int;text];
["pow"; "power"] ||> monomorphic float [float;int];
"unix_timestamp" |> monomorphic int [];
"unix_timestamp" |> monomorphic int [datetime];
["timestampdiff";"timestampadd"] ||> monomorphic int [strict @@ Unit `Interval;datetime;datetime];
"any_value" |> add 1 (F (Var 0,[Var 0])); (* 'a -> 'a but not aggregate *)
"substring" |> monomorphic text [text; int];
"substring" |> monomorphic text [text; int; int];
"substring_index" |> monomorphic text [text; text; int];
"last_insert_id" |> monomorphic int [];
"last_insert_id" |> monomorphic int [int];
()