1 (* C++ code generation *)
12 module Values
= struct
15 String.concat
", " (List.map
(fun (n
,t
) -> t ^
" " ^ n
) x
)
18 String.concat
", " (List.map
(fun (n
,t
) -> n
) x
)
22 let quote = String.replace_chars
(function '
\n'
-> "\\n\\\n" | '
\r'
-> "" | '
"' -> "\\\"" | c -> String.make 1 c)
23 let quote s = "\"" ^ quote s ^ "\""
25 let quote_comment_inline = String.replace_chars (function '\n' -> "\n// " | c -> String.make 1 c)
26 let comment fmt = Printf.kprintf (indent_endline & quote_comment_inline & (^) "// ") fmt
27 let open_curly () = output "{"; inc_indent ()
28 let close_curly fmt = dec_indent (); indent "}"; print fmt
29 let start_struct name =
30 output "struct %s
" name;
33 close_curly "; // struct %s
" name;
35 let out_public () = dec_indent(); output "public
:"; inc_indent()
36 let out_private () = dec_indent(); output "private:"; inc_indent()
37 let in_namespace name f =
38 output "namespace %s
" name;
41 close_curly " // namespace %s
" name;
45 let name_of attr index =
46 match attr.RA.name with
47 | "" -> sprintf "_%u
" index
50 let set_column attr index =
51 output "Traits
::set_column_%s
(stmt
, %u
, obj
.%s
);"
52 (Type.to_string attr.RA.domain)
56 let get_column attr index =
57 output "Traits
::get_column_%s
(stmt
, %u
, obj
.%s
);"
58 (Type.to_string attr.RA.domain)
62 let param_type_to_string t = Option.map_default Type.to_string "Any
" t
63 let as_cxx_type str = "typename Traits
::" ^ str
65 let set_param index param =
67 output "Traits
::set_param_%s
(stmt
, %s
, %u
);"
68 (param_type_to_string t)
69 (param_name_to_string id index)
72 let output_scheme_binder index scheme =
74 let name = default_name "output
" index in
75 output "template
<class T
>";
78 output "static void of_stmt
(typename Traits
::statement stmt
, T
& obj
)";
80 List.iteri (fun index attr -> get_column attr index) scheme;
86 let output_scheme_binder index scheme =
89 | _ -> Some (output_scheme_binder index scheme)
91 let params_to_values = List.mapi (fun i (n,t) -> param_name_to_string n i, t >> param_type_to_string >> as_cxx_type)
92 let params_to_values = List.unique & params_to_values
93 let make_const_values = List.map (fun (name,t) -> name, sprintf "%s const
&" t)
95 let output_value_defs vals =
96 vals >> List.iter (fun (name,t) -> output "%s %s
;" t name)
98 let scheme_to_values = List.mapi (fun i attr -> name_of attr i, attr.RA.domain >> Type.to_string >> as_cxx_type)
100 let output_scheme_data index scheme =
102 let name = default_name "data
" index in
104 scheme >> scheme_to_values >> output_value_defs;
107 let output_value_inits vals =
112 (String.concat "," (List.map (fun (name,_) -> sprintf "%s
(%s
)" name name) vals))
114 let output_params_binder index params =
116 let name = default_name "params
" index in
118 let values = params_to_values params in
119 values >> make_const_values >> output_value_defs;
121 output "%s
(%s
)" name (values >> make_const_values >> Values.to_string);
122 output_value_inits values;
126 output "void set_params
(typename Traits
::statement stmt
)";
128 List.iteri set_param params;
134 let output_params_binder index params =
136 | [] -> "typename Traits
::no_params
"
137 | _ -> output_params_binder index params
139 let generate_code index scheme params kind props =
140 let scheme_binder_name = output_scheme_binder index scheme in
141 let params_binder_name = output_params_binder index params in
142 if (Option.is_some scheme_binder_name) then output_scheme_data index scheme;
144 if (Option.is_some scheme_binder_name) then output "template
<class T
>";
145 let values = params_to_values params in
146 let result = match scheme_binder_name with None -> [] | Some _ -> ["result","T
&"] in
147 let all_params = Values.to_string
148 (["db
","typename Traits
::connection
"] @ result @ (make_const_values values))
150 let name = choose_name props kind index in
151 let sql = quote (get_sql props kind params) in
152 let inline_params =Values.inline (make_const_values values) in
153 output "static
bool %s
(%s
)" name all_params;
155 begin match scheme_binder_name with
156 | None -> output "return Traits
::do_execute
(db
,_T
(%s
),%s
(%s
));" sql params_binder_name inline_params
157 | Some scheme_name ->output "return Traits
::do_select
(db
,result,_T
(%s
),%s
(),%s
(%s
));"
158 sql (scheme_name ^ "<typename T
::value_type
>") params_binder_name inline_params
163 let start_output () =
164 output "#pragma once
";
166 output "template
<class Traits
>";
169 let finish_output () = end_struct "sqlgg
"