bind params too
[sqlgg.git] / gen.ml
blob613d0e1f855448fdccb04cfa415ba4396c5e88ce
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 let name = sprintf "binder_%u" index in
107 output "template <class T>";
108 start_struct name;
110 output "static void of_stmt(sqlite3_stmt* stmt, T& obj)";
111 open_curly ();
112 List.iteri (fun index attr -> get_column attr index) scheme;
113 close_curly "";
115 output "static void to_stmt(sqlite3_stmt* stmt, const T& obj)";
116 open_curly ();
117 List.iteri (fun index attr -> set_column attr (index + 1)) scheme;
118 close_curly "";
119 end_struct name;
120 name
122 let output_scheme_binder i s = in_namespace "detail" (fun () -> output_scheme_binder i s)
124 let params_to_values = List.mapi (fun i (n,t) -> param_name_to_string n i, param_type_to_string t)
125 let make_const_values = List.map (fun (name,t) -> name, sprintf "%s const&" t)
127 let output_value_defs vals =
128 vals >> make_const_values >> List.iter (fun (name,t) -> output "%s %s_;" t name)
130 let output_value_inits vals =
131 match vals with
132 | [] -> ()
133 | _ ->
134 output " : %s"
135 (String.concat "," (List.map (fun (name,_) -> sprintf "%s_(%s)" name name) vals))
137 let output_params_binder index params =
138 out_private ();
139 let name = default_name "params_binder" index in
140 start_struct name;
141 let values = params_to_values params in
142 output_value_defs values;
143 empty_line ();
144 output "%s(%s)" name (Cpp.to_string (make_const_values values));
145 output_value_inits values;
146 open_curly ();
147 close_curly "";
148 empty_line ();
149 output "void set_params(sqlite3_stmt* stmt)";
150 open_curly ();
151 List.iteri set_param params;
152 close_curly "";
153 empty_line ();
154 end_struct name;
155 name
157 let output_params_binder index params =
158 match params with
159 | [] -> "typename Traits::no_params"
160 | _ -> output_params_binder index params
162 let generate_select_code index scheme params props =
163 let scheme_binder_name = output_scheme_binder index scheme in
164 let params_binder_name = output_params_binder index params in
165 out_public ();
166 output "template<class T>";
167 let values = params_to_values params in
168 let all_params = Cpp.to_string
169 (["sqlite3*","db"; "T&","result"] @ (make_const_values values))
171 let name = make_name props (default_name "select" index) in
172 let sql = Props.get props "sql" >> Option.get >> Cpp.quote in
173 output "static bool %s(%s)" name all_params;
174 open_curly ();
175 output "return Traits::do_select(db,result,_T(\"%s\"),%s(),%s(%s));"
176 sql scheme_binder_name params_binder_name (Cpp.inline (make_const_values values));
177 close_curly "";
178 empty_line ()
181 let generate_insert_code columns table index (placeholders,props) sql =
182 out_public ();
183 let name = make_name props (default_name table "insert" index) in
184 output (sprintf "static bool %s(sqlite3* db, const %s& val)" name (item_name table));
185 open_curly ();
186 output (sprintf "return Traits::do_insert<binder_%s>(db,val,_T(\"%s\"));" (item_name table) sql);
187 close_curly "";
188 output ""
192 let generate_modify_code table cols inputs index props sql =
193 (* if there is only one input column - do not require full object as a param *)
194 let (cols,inputs) = match cols with
195 | [(x,_)] -> [],(x.Sql.Col.name,x.Sql.Col.sqltype)::inputs
196 | _ -> cols,inputs
198 let params_binder_name = output_params_binder index table cols inputs in
199 out_public ();
200 let data_params = make_const_params inputs in
201 let data_params = (match cols with
202 | [] -> data_params
203 | _ -> ((sprintf "const %s&" (item_name table)),"val")::data_params)
205 let params = Cpp.Params.to_string (("sqlite3*","db") :: data_params) in
206 let name = make_name props (default_name table "modify" index) in
207 output (sprintf "static int %s(%s)" name params);
208 open_curly ();
209 output (sprintf "return Traits::do_execute(db,\"%s\",%s(%s));" sql
210 params_binder_name (Cpp.inline data_params));
211 close_curly "";
212 output ""
214 let generate_delete_code table inputs index props sql =
215 let params_binder_name = output_params_binder index table [] inputs in
216 out_public ();
217 let data_params = make_const_params inputs in
218 let params = Cpp.Params.to_string (("sqlite3*","db") :: data_params) in
219 let name = make_name props (default_name table "delete" index) in
220 output (sprintf "static int %s(%s)" name params);
221 open_curly ();
222 output (sprintf "return Traits::do_execute(db,\"%s\",%s(%s));" sql
223 params_binder_name (Cpp.Params.inline data_params));
224 close_curly "";
225 output ""
227 let generate_create_code table sql =
228 out_public ();
229 output (sprintf "static int %screate(sqlite3* db)" (prefix_name table));
230 open_curly ();
231 output (sprintf "return Traits::do_execute(db,\"%s\",typename Traits::no_params());" sql);
232 close_curly "";
233 output ""
236 let generate_code index stmt =
237 let ((scheme,params),props) = stmt in
238 begin match Props.get props "sql" with
239 | Some s -> comment "%s" s
240 | None -> ()
241 end;
242 generate_select_code index scheme params props
244 let process stmts =
246 let generate_code index stmt =
247 let (kind,table,props,sql) = stmt in
248 match kind with
249 | Stmt.Create -> generate_table_code table;
250 generate_create_code table sql
251 | Stmt.Select (outputs,exprs,inputs) ->
252 generate_select_code table inputs outputs index props sql
253 | Stmt.Modify (cols,inputs) -> generate_modify_code table cols inputs index props sql
254 | Stmt.Delete (inputs) -> generate_delete_code table inputs index props sql
257 generate_header ();
258 output "namespace sql2cpp";
259 open_curly ();
260 List.iteri generate_code stmts;
261 close_curly " // namespace sql2cpp"