1 (* C++ code generation *)
12 type value = string * string
16 String.concat
", " (List.map
(fun (n
,t
) -> t ^
" " ^ n
) 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
)
24 let (inc_indent
,dec_indent
,make_indent
) =
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
;
44 close_curly "; // struct %s" name
;
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
;
52 close_curly " // namespace %s" name
;
56 let generate_header () =
57 output "// DO NOT EDIT MANUALLY";
59 output "// generated by sql2cpp";
61 output "#pragma once";
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
75 let set_column attr index
=
76 output "Traits::set_column_%s(stmt, %u, obj.%s);"
77 (Type.to_string attr
.RA.domain
)
81 let get_column attr index
=
82 output "Traits::get_column_%s(stmt, %u, obj.%s);"
83 (Type.to_string attr
.RA.domain
)
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
=
91 | Next
-> sprintf
"_%u" index
92 | Numbered x
-> sprintf
"_%u" x
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
=
100 output "Traits::set_param_%s(stmt, obj.%s, %u);"
101 (param_type_to_string t
)
102 (param_name_to_string id index
)
105 let output_scheme_binder index scheme
=
107 let name = default_name "output" index
in
108 output "template <class T>";
111 output "static void of_stmt(sqlite3_stmt* stmt, T& obj)";
113 List.iteri
(fun index attr
-> get_column attr index
) scheme
;
116 output "static void to_stmt(sqlite3_stmt* stmt, const T& obj)";
118 List.iteri
(fun index attr
-> set_column attr
(index
+ 1)) scheme
;
123 let output_scheme_binder index scheme
=
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
=
139 (String.concat
"," (List.map
(fun (name,_
) -> sprintf
"%s_(%s)" name name) vals
))
141 let output_params_binder index params
=
143 let name = default_name "params" index
in
145 let values = params_to_values params
in
146 output_value_defs values;
148 output "%s(%s)" name (Cpp.to_string (make_const_values values));
149 output_value_inits values;
153 output "void set_params(sqlite3_stmt* stmt)";
155 List.iteri
set_param params
;
161 let output_params_binder index params
=
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
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;
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));
185 let generate_insert_code columns table index (placeholders,props) sql =
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));
190 output (sprintf "return Traits::do_insert<binder_%s>(db,val,_T(\"%s\"));" (item_name table) sql);
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
202 let params_binder_name = output_params_binder index table cols inputs
in
204 let data_params = make_const_params inputs
in
205 let data_params = (match cols
with
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);
213 output (sprintf
"return Traits::do_execute(db,\"%s\",%s(%s));" sql
214 params_binder_name (Cpp.inline data_params));
218 let generate_delete_code table inputs index props
sql =
219 let params_binder_name = output_params_binder index table
[] inputs
in
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);
226 output (sprintf
"return Traits::do_execute(db,\"%s\",%s(%s));" sql
227 params_binder_name (Cpp.Params.inline data_params));
231 let generate_create_code table
sql =
233 output (sprintf
"static int %screate(sqlite3* db)" (prefix_name table
));
235 output (sprintf
"return Traits::do_execute(db,\"%s\",typename Traits::no_params());" sql);
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
246 generate_select_code index scheme
params props
250 let generate_code index stmt =
251 let (kind,table,props,sql) = stmt in
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
262 output "template <class Traits>";
263 start_struct "sql2cpp";
264 List.iteri
generate_code stmts
;