web page is managed elsewhere
[sqlgg.git] / src / gen.ml
blob22768c1642c4cca13ee44205ea25f89c11ad2b16
1 (* Code generation *)
3 open Printf
4 open ExtLib
5 open Prelude
6 open Stmt
8 type subst_mode = | Named | Unnamed | Oracle
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.RA.name with
29 | "" -> sprintf "_%u" index
30 | s -> s
32 let param_name_to_string ((name,_):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_unnamed _ _ = "?"
80 let get_sql stmt =
81 let sql = Props.get stmt.props "sql" >> Option.get in
82 match !params_mode with
83 | None -> sql
84 | Some subst ->
85 let f = match subst with
86 | Named -> subst_named
87 | Unnamed -> subst_unnamed
88 | Oracle -> subst_oracle
90 substitute_params sql stmt.params f
92 let time_string () =
93 let module U = Unix in
94 let t = U.time () >> U.gmtime in
95 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
97 module type LangTypes = sig
99 val as_api_type : Sql.Type.t -> string
100 val as_lang_type : Sql.Type.t -> string
104 module Translate(T : LangTypes) = struct
106 let param_type_to_string = T.as_api_type
107 let schema_to_values = List.mapi (fun i attr -> name_of attr i, T.as_lang_type attr.RA.domain)
108 (* let schema_to_string = G.Values.to_string $ schema_to_values *)
109 let all_params_to_values = List.mapi (fun i (n,t) -> param_name_to_string n i, T.as_lang_type t)
110 (* rev unique rev -- to preserve ordering with respect to first occurrences *)
111 let params_to_values = List.rev $ List.unique $ List.rev $ all_params_to_values
115 module type Generator = sig
116 type t
117 val generate : t -> string -> Stmt.t Enum.t -> unit
118 val start : unit -> t
119 val comment : t -> ('a,unit,string,unit) format4 -> 'a
120 val empty_line : t -> unit
123 module Make(S : Generator) = struct
125 let generate_header out =
126 S.comment out "DO NOT EDIT MANUALLY";
127 S.comment out "";
128 S.comment out "generated by sqlgg %s on %s" Sqlgg_config.version (time_string ());
129 S.empty_line out
131 let process name stmts =
132 let out = S.start () in
133 generate_header out;
134 S.generate out name stmts