1 (* SQL syntax and RA *)
7 type expr
= [ `Value
of Sql.Type.t
10 | `Column
of string * string option (** name, table *)
14 let expr_to_string = Show.show
<expr
>
19 | Expr
of expr
* string option (** name *)
22 type columns
= column list deriving
(Show
)
24 let collect f l
= List.flatten
(List.map f l
)
26 let get_scheme columns tables
=
27 let all = tables
>> List.map snd
>> List.flatten
in
28 let scheme name
= name
>> Tables.get_from tables
>> snd
in
29 let resolve1 = function
35 | `Column
(name
,Some t
) -> RA.Scheme.find
(scheme t
) name
36 | `Column
(name
,None
) -> RA.Scheme.find
all name
37 | _
-> RA.attr
"" Sql.Type.Text
(* some expression *)
39 let col = Option.map_default
(fun n
-> {col with RA.name
= n
}) col name
in
42 collect resolve1 columns
44 let scheme_as_params = List.map
(fun attr
-> Named attr
.RA.name
, Some attr
.RA.domain
)
46 (** replace every Column with Value of corresponding type *)
47 let rebuild tables expr
=
48 let all = tables
>> List.map snd
>> List.flatten
in
49 let scheme name
= name
>> Tables.get_from tables
>> snd
in
52 | `Value x
-> `Value x
54 let attr = RA.Scheme.find
(Option.map_default
scheme all x
) name
in
56 | `Param x
-> `Param x
57 | `Sub l
-> `Sub
(List.map
each l
)
61 (** assign types to parameters where possible *)
62 let assign_types expr
=
65 | `Value t
-> e
, Some t
67 let t = match l
>> List.map
typeof >> List.map snd
>> List.filter_valid
with
69 | h
::t -> if List.for_all
((=) h
) t then Some h
else None
72 | `Param
(n
,None
) -> `Param
(n
,t)
75 `Sub
(List.map
assign l
), t
76 | `Param
(_
,t) -> e
, t
84 | `Sub l
-> List.fold_left
loop acc l
85 | `Column _
| `Value _
-> acc
89 let get_params tables e
=
90 get_params (assign_types (rebuild tables e
))
91 (* e >> rebuild tables >> assign_types >> get_params *)
95 let e = Sub [Value Sql.Type.Text; Param (Next,None); Sub []; Param (Named "ds", Some Sql.Type.Int);] in
96 e >> get_params >> to_string >> print_endline
99 let params_of_column tables
= function
100 | All
| AllOf
_ -> []
101 | Expr
(e,_) -> get_params tables
e
103 let params_of_columns tables
= collect (params_of_column tables
)
105 let get_params_opt tables
= function
106 | Some x
-> get_params tables x
109 let get_params_l tables l
= collect (get_params tables
) l