more precise typing of CASE without selector
[sqlgg.git] / src / gen.ml
blob255b1bd8c85da04ba29fafc977ef94bf62691150
1 (* Code generation *)
3 open Printf
4 open ExtLib
5 open Prelude
6 open Stmt
8 type subst_mode = | Named | Unnamed | Oracle | PostgreSQL
10 (** defines substitution function for parameter literals *)
11 let params_mode = ref None
13 let (inc_indent,dec_indent,make_indent) =
14 let v = ref 0 in
15 (fun () -> v := !v + 2),
16 (fun () -> v := !v - 2),
17 (fun () -> String.make !v ' ')
19 let print_indent () = print_string (make_indent ())
20 let indent s = print_indent (); print_string s
21 let indent_endline s = print_indent (); print_endline s
22 let output fmt = kprintf indent_endline fmt
23 let output_l = List.iter indent_endline
24 let print fmt = kprintf print_endline fmt
25 let indented k = inc_indent (); k (); dec_indent ()
27 let name_of attr index =
28 match attr.Sql.name with
29 | "" -> sprintf "_%u" index
30 | s -> s
32 let param_name_to_string ((name,_):Sql.param_id) index =
33 match name with
34 | None -> sprintf "_%u" index
35 | Some s -> s
37 let make_name props default = Option.default default (Props.get props "name")
38 let default_name str index = sprintf "%s_%u" str index
40 let choose_name props kind index =
41 let fix = String.map begin function
42 | ('a'..'z' | 'A'..'Z' | '0'..'9' | '_' as c) -> c
43 | _ -> '_'
44 end in
45 let fix s =
46 match Props.get props "subst" with
47 | Some x -> let (_,s) = String.replace ~str:s ~sub:("%%"^x^"%%") ~by:x in fix s
48 | None -> fix s
50 let name = match kind with
51 | Create t -> sprintf "create_%s" (fix t)
52 | CreateIndex t -> sprintf "create_index_%s" (fix t)
53 | Update (Some t) -> sprintf "update_%s_%u" (fix t) index
54 | Update None -> sprintf "update_%u" index
55 | Insert (_,t) -> sprintf "insert_%s_%u" (fix t) index
56 | Delete t -> sprintf "delete_%s_%u" (fix t) index
57 | Alter t -> sprintf "alter_%s_%u" (fix t) index
58 | Drop t -> sprintf "drop_%s" (fix t)
59 | Select _ -> sprintf "select_%u" index
60 | Other -> sprintf "statement_%u" index
62 make_name props name
64 let substitute_params s params f =
65 let index = ref 0 in
66 let b = Buffer.create (String.length s) in
67 let last = List.fold_left (fun i ((_,(i1,i2)),_ as param) ->
68 let prefix = String.slice ~first:i ~last:i1 s in
69 Buffer.add_string b prefix;
70 Buffer.add_string b (f !index param);
71 incr index;
72 i2) 0 params in
73 Buffer.add_string b (String.slice ~first:last s);
74 Buffer.contents b
76 let subst_named index (id,_) = "@" ^ (param_name_to_string id index)
77 let subst_oracle index (id,_) = ":" ^ (param_name_to_string id index)
78 let subst_postgresql index _ = "$" ^ string_of_int (index + 1)
79 let subst_unnamed _ _ = "?"
81 let get_sql stmt =
82 let sql = Props.get stmt.props "sql" |> Option.get in
83 match !params_mode with
84 | None -> sql
85 | Some subst ->
86 let f = match subst with
87 | Named -> subst_named
88 | Unnamed -> subst_unnamed
89 | Oracle -> subst_oracle
90 | PostgreSQL -> subst_postgresql
92 substitute_params sql stmt.params f
94 let time_string () =
95 let module U = Unix in
96 let t = U.time () |> U.gmtime in
97 sprintf "%04u-%02u-%02uT%02u:%02uZ" (1900 + t.U.tm_year) (t.U.tm_mon+1) t.U.tm_mday t.U.tm_hour t.U.tm_min
99 module type LangTypes = sig
101 val as_api_type : Sql.Type.t -> string
102 val as_lang_type : Sql.Type.t -> string
106 module Translate(T : LangTypes) = struct
108 let param_type_to_string = T.as_api_type
109 let schema_to_values = List.mapi (fun i attr -> name_of attr i, T.as_lang_type attr.Sql.domain)
110 (* let schema_to_string = G.Values.to_string $ schema_to_values *)
111 let all_params_to_values = List.mapi (fun i (n,t) -> param_name_to_string n i, T.as_lang_type t)
112 (* rev unique rev -- to preserve ordering with respect to first occurrences *)
113 let params_to_values = List.rev $ List.unique ~cmp:(=) $ List.rev $ all_params_to_values
117 module type Generator = sig
118 type t
119 val generate : t -> string -> Stmt.t Enum.t -> unit
120 val start : unit -> t
121 val comment : t -> ('a,unit,string,unit) format4 -> 'a
122 val empty_line : t -> unit
125 module Make(S : Generator) = struct
127 let generate_header out =
128 S.comment out "DO NOT EDIT MANUALLY";
129 S.comment out "";
130 S.comment out "generated by sqlgg %s on %s" Sqlgg_config.version (time_string ());
131 S.empty_line out
133 let process name stmts =
134 let out = S.start () in
135 if !Sqlgg_config.gen_header then generate_header out;
136 S.generate out name stmts