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 empty_line () = print_newline ()
28 let open_curly () = output "{"; inc_indent ()
29 let close_curly fmt = dec_indent (); indent "}"; print fmt
30 let start_struct name =
31 output "struct %s
" name;
34 close_curly "; // struct %s
" name;
36 let out_public () = dec_indent(); output "public
:"; inc_indent()
37 let out_private () = dec_indent(); output "private:"; inc_indent()
38 let in_namespace name f =
39 output "namespace %s
" name;
42 close_curly " // namespace %s
" name;
46 let name_of attr index =
47 match attr.RA.name with
48 | "" -> sprintf "_%u
" index
51 let column_action action attr index =
52 output "Traits
::%s_column
(row
, %u
, obj
.%s
);"
54 (* (Type.to_string attr.RA.domain) *)
59 let get_column = column_action "get
"
60 let bind_column = column_action "bind
"
63 let param_type_to_string t = Option.map_default Type.to_string "Any
" t
64 let as_cxx_type str = "typename Traits
::" ^ str
66 let set_param index param =
68 output "Traits
::set_param(x
, %s
, %u
);"
69 (* (param_type_to_string t) *)
70 (param_name_to_string id index)
73 let output_schema_binder index schema =
75 let name = default_name "output
" index in
76 output "template
<class T
>";
79 output "enum
{ count
= %u
};" (List.length schema);
83 output "static void %s
(typename Traits
::row row
, T
& obj
)" action;
85 List.iteri (fun index attr -> column_action action attr index) schema;
95 let output_schema_binder index schema =
98 | _ -> Some (output_schema_binder index schema)
100 let params_to_values = List.mapi (fun i (n,t) -> param_name_to_string n i, t >> param_type_to_string >> as_cxx_type)
101 let params_to_values = List.unique & params_to_values
102 let make_const_values = List.map (fun (name,t) -> name, sprintf "%s const
&" t)
104 let output_value_defs vals =
105 vals >> List.iter (fun (name,t) -> output "%s %s
;" t name)
107 let schema_to_values = List.mapi (fun i attr -> name_of attr i, attr.RA.domain >> Type.to_string >> as_cxx_type)
109 let output_schema_data index schema =
111 let name = default_name "data
" index in
113 schema >> schema_to_values >> output_value_defs;
116 let output_value_inits vals =
121 (String.concat "," (List.map (fun (name,_) -> sprintf "%s
(%s
)" name name) vals))
123 let output_params_binder index params =
125 let name = default_name "params
" index in
127 let values = params_to_values params in
128 values >> make_const_values >> output_value_defs;
130 output "%s
(%s
)" name (values >> make_const_values >> Values.to_string);
131 output_value_inits values;
135 output "enum
{ count
= %u
};" (List.length values);
137 output "template
<class T
>";
138 output "void set_params
(T
& x
)";
140 List.iteri set_param params;
146 let output_params_binder index params =
148 | [] -> "typename Traits
::no_params
"
149 | _ -> output_params_binder index params
155 let generate_code () index schema params kind props =
156 let schema_binder_name = output_schema_binder index schema in
157 let params_binder_name = output_params_binder index params in
158 if (Option.is_some schema_binder_name) then output_schema_data index schema;
160 if (Option.is_some schema_binder_name) then output "template
<class T
>";
161 let values = params_to_values params in
162 let result = match schema_binder_name with None -> [] | Some _ -> ["result","T
&"] in
163 let all_params = Values.to_string
164 (["db
","typename Traits
::connection
"] @ result @ (make_const_values values))
166 let name = choose_name props kind index in
167 let sql = quote (get_sql props kind params) in
168 let inline_params =Values.inline (make_const_values values) in
169 output "static
bool %s
(%s
)" name all_params;
171 begin match schema_binder_name with
172 | None -> output "return Traits
::do_execute
(db
,SQLGG_STR
(%s
),%s
(%s
));" sql params_binder_name inline_params
173 | Some schema_name ->output "return Traits
::do_select
(db
,result,SQLGG_STR
(%s
),%s
(),%s
(%s
));"
174 sql (schema_name ^ "<typename T
::value_type
>") params_binder_name inline_params
179 let start_output () name =
180 output "#pragma once
";
182 output "template
<class Traits
>";
185 let finish_output () name = end_struct name