noparse attribute
[sqlgg.git] / gen.ml
blobd52dadc3f4c69bc515811cb8bf15ca00d0453cb7
1 (* Code generation *)
3 open Printf
4 open ExtList
5 open ExtString
6 open Operators
7 open Stmt
9 type subst_mode = | Named | Unnamed
11 (** defines substitution function for parameter literals *)
12 let params_mode = ref None
14 let (inc_indent,dec_indent,make_indent) =
15 let v = ref 0 in
16 (fun () -> v := !v + 2),
17 (fun () -> v := !v - 2),
18 (fun () -> String.make !v ' ')
20 let print_indent () = print_string (make_indent ())
21 let indent s = print_indent (); print_string s
22 let indent_endline s = print_indent (); print_endline s
23 let output fmt = kprintf indent_endline fmt
24 let output_l = List.iter indent_endline
25 let print fmt = kprintf print_endline fmt
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 name = match kind with
42 | Create t -> sprintf "create_%s" t
43 | Update (Some t) -> sprintf "update_%s_%u" t index
44 | Update None -> sprintf "update_%u" index
45 | Insert (_,t) -> sprintf "insert_%s_%u" t index
46 | Delete t -> sprintf "delete_%s_%u" t index
47 | Alter t -> sprintf "alter_%s_%u" t index
48 | Drop t -> sprintf "drop_%s" t
49 | Select -> sprintf "select_%u" index
50 | Other -> sprintf "statement_%u" index
52 make_name props name
54 let substitute_params s params f =
55 let index = ref 0 in
56 let b = Buffer.create (String.length s) in
57 let last = List.fold_left (fun i ((_,(i1,i2)),_ as param) ->
58 let prefix = String.slice ~first:i ~last:i1 s in
59 Buffer.add_string b prefix;
60 Buffer.add_string b (f !index param);
61 incr index;
62 i2) 0 params in
63 Buffer.add_string b (String.slice ~first:last s);
64 Buffer.contents b
66 let subst_named index (id,_) = "@" ^ (param_name_to_string id index)
67 let subst_unnamed _ _ = "?"
69 let get_sql stmt =
70 let sql = Props.get stmt.props "sql" >> Option.get in
71 match !params_mode with
72 | None -> sql
73 | Some subst ->
74 let f = match subst with Named -> subst_named | Unnamed -> subst_unnamed in
75 substitute_params sql stmt.params f
77 let time_string () =
78 let module U = Unix in
79 let t = U.time () >> U.gmtime in
80 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
82 module type LangTypes = sig
84 val as_api_type : Sql.Type.t -> string
85 val as_lang_type : Sql.Type.t -> string
87 end
89 module Translate(T : LangTypes) = struct
91 let param_type_to_string = T.as_api_type
92 let schema_to_values = List.mapi (fun i attr -> name_of attr i, T.as_lang_type attr.RA.domain)
93 (* let schema_to_string = G.Values.to_string & schema_to_values *)
94 let all_params_to_values = List.mapi (fun i (n,t) -> param_name_to_string n i, T.as_lang_type t)
95 let params_to_values = List.unique & all_params_to_values
97 end
99 module type Generator = sig
100 type t
101 val generate : t -> string -> Stmt.t Enum.t -> unit
102 val start : unit -> t
103 val comment : t -> ('a,unit,string,unit) format4 -> 'a
104 val empty_line : t -> unit
107 module Make(S : Generator) = struct
109 let generate_header out =
110 S.comment out "DO NOT EDIT MANUALLY";
111 S.comment out "";
112 S.comment out "generated by sqlgg %s on %s" Config.version (time_string ());
113 S.comment out "visit http://ygrek.org.ua/p/sqlgg/";
114 S.empty_line out
116 let process name stmts =
117 let out = S.start () in
118 generate_header out;
119 S.generate out name stmts