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
=
106 let name = sprintf
"binder_%u" index
in
107 output "template <class T>";
110 output "static void of_stmt(sqlite3_stmt* stmt, T& obj)";
112 List.iteri
(fun index attr
-> get_column attr index
) scheme
;
115 output "static void to_stmt(sqlite3_stmt* stmt, const T& obj)";
117 List.iteri
(fun index attr
-> set_column attr
(index
+ 1)) scheme
;
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
=
135 (String.concat
"," (List.map
(fun (name,_
) -> sprintf
"%s_(%s)" name name) vals
))
137 let output_params_binder index params
=
139 let name = default_name "params_binder" index
in
141 let values = params_to_values params
in
142 output_value_defs values;
144 output "%s(%s)" name (Cpp.to_string (make_const_values values));
145 output_value_inits values;
149 output "void set_params(sqlite3_stmt* stmt)";
151 List.iteri
set_param params
;
157 let output_params_binder index params
=
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
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;
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));
181 let generate_insert_code columns table index (placeholders,props) sql =
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));
186 output (sprintf "return Traits::do_insert<binder_%s>(db,val,_T(\"%s\"));" (item_name table) sql);
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
198 let params_binder_name = output_params_binder index table cols inputs
in
200 let data_params = make_const_params inputs
in
201 let data_params = (match cols
with
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);
209 output (sprintf
"return Traits::do_execute(db,\"%s\",%s(%s));" sql
210 params_binder_name (Cpp.inline data_params));
214 let generate_delete_code table inputs index props
sql =
215 let params_binder_name = output_params_binder index table
[] inputs
in
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);
222 output (sprintf
"return Traits::do_execute(db,\"%s\",%s(%s));" sql
223 params_binder_name (Cpp.Params.inline data_params));
227 let generate_create_code table
sql =
229 output (sprintf
"static int %screate(sqlite3* db)" (prefix_name table
));
231 output (sprintf
"return Traits::do_execute(db,\"%s\",typename Traits::no_params());" sql);
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
242 generate_select_code index scheme
params props
246 let generate_code index stmt =
247 let (kind,table,props,sql) = stmt in
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
258 output "namespace sql2cpp";
260 List.iteri
generate_code stmts
;
261 close_curly " // namespace sql2cpp"