correct generated code
[sqlgg.git] / gen.ml
blobf14d85e38ab130be70cb5d321910aedd7da8c2a5
1 (* C++ code generation *)
3 open Sql
4 open Printf
5 open ExtList
6 open ExtString
7 open Operators
8 open Stmt.Raw
10 module Cpp =
11 struct
12 type value = string * string
13 type t = value list
15 let to_string x =
16 String.concat ", " (List.map (fun (n,t) -> t ^ " " ^ n) x)
18 let inline x =
19 String.concat ", " (List.map (fun (n,t) -> n) x)
21 let quote = String.replace_chars (function '\n' -> "\\\n" | c -> String.make 1 c)
22 end
24 let (inc_indent,dec_indent,make_indent) =
25 let v = ref 0 in
26 (fun () -> v := !v + 2),
27 (fun () -> v := !v - 2),
28 (fun () -> String.make !v ' ')
30 let print_indent () = print_string (make_indent ())
31 let indent s = print_indent (); print_string s
32 let indent_endline s = print_indent (); print_endline s
33 let empty_line () = print_newline ()
34 let output fmt = Printf.kprintf indent_endline fmt
35 let print fmt = Printf.kprintf print_endline fmt
36 let quote_comment_inline = String.replace_chars (function '\n' -> "\n// " | c -> String.make 1 c)
37 let comment fmt = Printf.kprintf (indent_endline & quote_comment_inline & (^) "// ") fmt
38 let open_curly () = output "{"; inc_indent ()
39 let close_curly fmt = dec_indent (); indent "}"; print fmt
40 let start_struct name =
41 output "struct %s" name;
42 open_curly ()
43 let end_struct name =
44 close_curly "; // struct %s" name;
45 empty_line ()
46 let out_public () = dec_indent(); output "public:"; inc_indent()
47 let out_private () = dec_indent(); output "private:"; inc_indent()
48 let in_namespace name f =
49 output "namespace %s" name;
50 open_curly ();
51 let result = f () in
52 close_curly " // namespace %s" name;
53 empty_line ();
54 result
56 let generate_header () =
57 output "// DO NOT EDIT MANUALLY";
58 output "";
59 output "// generated by sql2cpp";
60 output "";
61 output "#pragma once";
62 output ""
65 let ns_name table = table.Table.cpp_name
66 let item_name _ = "row"
67 let prefix_name _ = ""
70 let name_of attr index =
71 match attr.RA.name with
72 | "" -> sprintf "_%u" index
73 | s -> s
75 let set_column attr index =
76 output "Traits::set_column_%s(stmt, %u, obj.%s);"
77 (Type.to_string attr.RA.domain)
78 index
79 (name_of attr index)
81 let get_column attr index =
82 output "Traits::get_column_%s(stmt, %u, obj.%s);"
83 (Type.to_string attr.RA.domain)
84 index
85 (name_of attr (index+1))
87 let param_type_to_string t = Option.map_default Type.to_string "Any" t
89 let param_name_to_string id index =
90 match id with
91 | Next -> sprintf "_%u" index
92 | Numbered x -> sprintf "_%u" x
93 | Named s -> s
95 let make_name props default = Option.default default (Props.get props "name")
96 let default_name str index = sprintf "%s_%u" str index
98 let set_param index param =
99 let (id,t) = param in
100 output "Traits::set_param_%s(stmt, obj.%s, %u);"
101 (param_type_to_string t)
102 (param_name_to_string id index)
103 index
105 let output_scheme_binder index scheme =
106 out_private ();
107 let name = default_name "output" index in
108 output "template <class T>";
109 start_struct name;
111 output "static void of_stmt(sqlite3_stmt* stmt, T& obj)";
112 open_curly ();
113 List.iteri (fun index attr -> get_column attr index) scheme;
114 close_curly "";
116 output "static void to_stmt(sqlite3_stmt* stmt, const T& obj)";
117 open_curly ();
118 List.iteri (fun index attr -> set_column attr (index + 1)) scheme;
119 close_curly "";
120 end_struct name;
121 name
123 let output_scheme_binder index scheme =
124 match scheme with
125 | [] -> "typename Traits::no_output"
126 | _ -> output_scheme_binder index scheme
128 let params_to_values = List.mapi (fun i (n,t) -> param_name_to_string n i, param_type_to_string t)
129 let make_const_values = List.map (fun (name,t) -> name, sprintf "%s const&" t)
131 let output_value_defs vals =
132 vals >> make_const_values >> List.iter (fun (name,t) -> output "%s %s_;" t name)
134 let output_value_inits vals =
135 match vals with
136 | [] -> ()
137 | _ ->
138 output " : %s"
139 (String.concat "," (List.map (fun (name,_) -> sprintf "%s_(%s)" name name) vals))
141 let output_params_binder index params =
142 out_private ();
143 let name = default_name "params" index in
144 start_struct name;
145 let values = params_to_values params in
146 output_value_defs values;
147 empty_line ();
148 output "%s(%s)" name (Cpp.to_string (make_const_values values));
149 output_value_inits values;
150 open_curly ();
151 close_curly "";
152 empty_line ();
153 output "void set_params(sqlite3_stmt* stmt)";
154 open_curly ();
155 List.iteri set_param params;
156 close_curly "";
157 empty_line ();
158 end_struct name;
159 name
161 let output_params_binder index params =
162 match params with
163 | [] -> "typename Traits::no_params"
164 | _ -> output_params_binder index params
166 let generate_select_code index scheme params props =
167 let scheme_binder_name = output_scheme_binder index scheme in
168 let params_binder_name = output_params_binder index params in
169 out_public ();
170 output "template<class T>";
171 let values = params_to_values params in
172 let all_params = Cpp.to_string
173 (["db","sqlite3*"; "result","T&"] @ (make_const_values values))
175 let name = make_name props (default_name "select" index) in
176 let sql = Props.get props "sql" >> Option.get >> Cpp.quote in
177 output "static bool %s(%s)" name all_params;
178 open_curly ();
179 output "return Traits::do_select(db,result,_T(\"%s\"),%s(),%s(%s));"
180 sql scheme_binder_name params_binder_name (Cpp.inline (make_const_values values));
181 close_curly "";
182 empty_line ()
185 let generate_insert_code columns table index (placeholders,props) sql =
186 out_public ();
187 let name = make_name props (default_name table "insert" index) in
188 output (sprintf "static bool %s(sqlite3* db, const %s& val)" name (item_name table));
189 open_curly ();
190 output (sprintf "return Traits::do_insert<binder_%s>(db,val,_T(\"%s\"));" (item_name table) sql);
191 close_curly "";
192 output ""
196 let generate_modify_code table cols inputs index props sql =
197 (* if there is only one input column - do not require full object as a param *)
198 let (cols,inputs) = match cols with
199 | [(x,_)] -> [],(x.Sql.Col.name,x.Sql.Col.sqltype)::inputs
200 | _ -> cols,inputs
202 let params_binder_name = output_params_binder index table cols inputs in
203 out_public ();
204 let data_params = make_const_params inputs in
205 let data_params = (match cols with
206 | [] -> data_params
207 | _ -> ((sprintf "const %s&" (item_name table)),"val")::data_params)
209 let params = Cpp.Params.to_string (("sqlite3*","db") :: data_params) in
210 let name = make_name props (default_name table "modify" index) in
211 output (sprintf "static int %s(%s)" name params);
212 open_curly ();
213 output (sprintf "return Traits::do_execute(db,\"%s\",%s(%s));" sql
214 params_binder_name (Cpp.inline data_params));
215 close_curly "";
216 output ""
218 let generate_delete_code table inputs index props sql =
219 let params_binder_name = output_params_binder index table [] inputs in
220 out_public ();
221 let data_params = make_const_params inputs in
222 let params = Cpp.Params.to_string (("sqlite3*","db") :: data_params) in
223 let name = make_name props (default_name table "delete" index) in
224 output (sprintf "static int %s(%s)" name params);
225 open_curly ();
226 output (sprintf "return Traits::do_execute(db,\"%s\",%s(%s));" sql
227 params_binder_name (Cpp.Params.inline data_params));
228 close_curly "";
229 output ""
231 let generate_create_code table sql =
232 out_public ();
233 output (sprintf "static int %screate(sqlite3* db)" (prefix_name table));
234 open_curly ();
235 output (sprintf "return Traits::do_execute(db,\"%s\",typename Traits::no_params());" sql);
236 close_curly "";
237 output ""
240 let generate_code index stmt =
241 let ((scheme,params),props) = stmt in
242 begin match Props.get props "sql" with
243 | Some s -> comment "%s" s
244 | None -> ()
245 end;
246 generate_select_code index scheme params props
248 let process stmts =
250 let generate_code index stmt =
251 let (kind,table,props,sql) = stmt in
252 match kind with
253 | Stmt.Create -> generate_table_code table;
254 generate_create_code table sql
255 | Stmt.Select (outputs,exprs,inputs) ->
256 generate_select_code table inputs outputs index props sql
257 | Stmt.Modify (cols,inputs) -> generate_modify_code table cols inputs index props sql
258 | Stmt.Delete (inputs) -> generate_delete_code table inputs index props sql
261 generate_header ();
262 output "template <class Traits>";
263 start_struct "sql2cpp";
264 List.iteri generate_code stmts;
265 end_struct "sql2cpp"