wip
[sqlgg.git] / syntax.ml
blob7901b89ba9b22f2d4f2fba3c87174225560613fc
1 (* SQL syntax and RA *)
3 open Stmt.Raw
4 open Operators
6 type expr = | Value of Sql.Type.t
7 | Param of Stmt.Raw.param
8 | Sub of expr list
9 | Column of string * string option (** name, table *)
10 deriving (Show)
12 type column =
13 | All
14 | AllOf of string
15 | Expr of expr * string option (** name *)
16 deriving (Show)
18 type columns = column list deriving (Show)
20 let collect f l = List.flatten (List.map f l)
22 let get_scheme columns tables =
23 let all = tables >> List.map snd >> List.flatten in
24 let scheme name = name >> Tables.get_from tables >> snd in
25 let resolve1 = function
26 | All -> all
27 | AllOf t -> scheme t
28 | Expr (e,name) ->
29 let col =
30 begin match e with
31 | Column (name,Some t) -> RA.Scheme.find (scheme t) name
32 | Column (name,None) -> RA.Scheme.find all name
33 | _ -> RA.attr "" Sql.Type.Text
34 end in
35 let col = Option.map_default (fun n -> {col with RA.name = n}) col name in
36 [ col ]
38 collect resolve1 columns
40 let get_params e =
41 let rec loop acc e =
42 match e with
43 | Param p -> p::acc
44 | Sub l -> List.fold_left loop acc l
45 | _ -> acc
46 in loop [] e
49 let _ =
50 let e = Sub [Value Sql.Type.Text; Param (Next,None); Sub []; Param (Named "ds", Some Sql.Type.Int);] in
51 e >> get_params >> to_string >> print_endline
54 let params_of_column = function
55 | All | AllOf _ -> []
56 | Expr (e,_) -> get_params e
58 let params_of_columns = collect params_of_column
60 let get_params_opt = function
61 | Some x -> get_params x
62 | None -> []
64 let get_params_l = collect get_params