1 (* SQL syntax and RA *)
8 type expr
= [ `Value
of Sql.Type.t
(** literal value *)
10 | `Func
of Sql.Type.t
option * expr list
(** return type, parameters *)
11 | `Column
of string * string option (** name, table *)
15 type expr_q
= [ `Value
of Sql.Type.t
(** literal value *)
17 | `Func
of Sql.Type.t
option * expr_q list
(** return type, parameters *)
21 let expr_to_string = Show.show
<expr
>
26 | Expr
of expr
* string option (** name *)
29 type columns
= column list deriving
(Show
)
31 let collect f l
= List.flatten
(List.map f l
)
33 let scheme_as_params = List.map
(fun attr
-> Named attr
.RA.name
, Some attr
.RA.domain
)
35 (** replace every Column with Value of corresponding type *)
36 let resolve_columns tables expr
=
37 let all = tables
>> List.map snd
>> List.flatten
in
38 let scheme name
= name
>> Tables.get_from tables
>> snd
in
41 | `Value x
-> `Value x
43 let attr = RA.Scheme.find
(Option.map_default
scheme all x
) name
in
45 | `Param x
-> `Param x
46 | `Func
(r
,l
) -> `Func
(r
,(List.map
each l
))
50 (** assign types to parameters where possible *)
51 let assign_types expr
=
52 let rec typeof e
= (* FIXME simplify *)
54 | `Value t
-> e
, Some t
56 (** Assumption: sql functions/operators have type scheme 'a -> 'a -> 'b
57 i.e. all parameters of some equal type *)
58 let (l
,t
) = l
>> List.map
typeof >> List.split
in
59 let t = match List.filter_valid
t with
61 | h
::t -> if List.for_all
((=) h
) t then Some h
else None
64 print_endline (Show.show<expr_q list>(l));
65 print_endline (Show.show<Sql.Type.t option>(t));
68 | `Param
(n
,None
) -> `Param
(n
,t)
71 let ret = if Option.is_some
ret then ret else t in
72 `Func
(ret,(List.map
assign l
)),ret
73 | `Param
(_
,t) -> e
, t
77 let show_e e
= Show.show
<expr_q
> (e
) >> print_endline
79 let resolve_types tables expr
=
81 >> resolve_columns tables
85 let infer_scheme columns tables joined_scheme
=
86 let all = tables
>> List.map snd
>> List.flatten
in
87 let scheme name
= name
>> Tables.get_from tables
>> snd
in
88 let resolve1 = function
89 | All
-> joined_scheme
94 | `Column
(name
,Some
t) -> RA.Scheme.find
(scheme t) name
95 | `Column
(name
,None
) -> RA.Scheme.find
all name
(* FIXME `all` or `joined_scheme` ? *)
96 | _
-> RA.attr "" (Option.default
Sql.Type.Text
(resolve_types tables e
>> snd
))
98 let col = Option.map_default
(fun n
-> {col with RA.name
= n
}) col name
in
101 collect resolve1 columns
107 | `Func
(_
,l
) -> List.fold_left
loop acc l
110 loop [] e
>> List.rev
112 let get_params tables e
=
113 e
>> resolve_types tables
>> fst
>> get_params
117 let e = Sub [Value Sql.Type.Text; Param (Next,None); Sub []; Param (Named "ds", Some Sql.Type.Int);] in
118 e >> get_params >> to_string >> print_endline
121 let params_of_column tables
= function
122 | All
| AllOf
_ -> []
123 | Expr
(e,_) -> get_params tables
e
125 let params_of_columns tables
= collect (params_of_column tables
)
127 let get_params_opt tables
= function
128 | Some x
-> get_params tables x
131 let get_params_l tables l
= collect (get_params tables
) l
133 (* FIXME order of columns *)
134 let do_join (tables
,params
) ((table1
,params1
),kind
) =
135 (* let (name1,scheme1) = table1 in *)
136 let tables = tables @ [table1
] in
137 let p = match kind
with
142 | `Search
e -> get_params tables e
144 tables,params
@ params1
@ p
146 let join ((t0
,p0
),joins
) =
147 let (tables,params
) = List.fold_left
do_join ([t0
],p0
) joins
in
148 let joined_scheme = tables >> List.map snd
>> List.flatten
in
149 (tables,params
,joined_scheme)