1 (* Java code generation *)
14 let comment = G.comment
15 let empty_line = G.empty_line
17 let quote = String.replace_chars
(function '
\n'
-> "\" +\n\"" | '
\r'
-> "" | '
"' -> "\\\"" | c -> String.make 1 c)
18 let quote s = "\"" ^ quote s ^ "\""
22 output "%s %s
" cls name;
26 G.close_curly " // %s %s
" cls name;
31 let (start_class,end_class) = start_ "public
class"
32 let (start_intf,end_intf) = start_ "public static interface
"
34 let as_java_type = function
36 | Type.Text -> "String
"
37 | Type.Float -> "float"
39 | Type.Bool -> "boolean
"
40 | Type.Datetime -> "Timestamp
"
42 let get_column attr index =
43 sprintf "res
.get%s
(%u
)"
44 (attr.RA.domain >> as_java_type >> String.capitalize)
47 let param_type_to_string t = t >> Option.default Type.Text >> as_java_type
49 let schema_to_values = List.mapi (fun i attr -> name_of attr i, attr.RA.domain >> as_java_type)
51 let output_schema_binder name index schema =
52 let name = sprintf "%s_callback
" name in
54 output "public void callback
(%s
);" (G.Values.to_string (schema_to_values schema));
58 let output_schema_binder name index schema =
61 | _ -> Some (output_schema_binder name index schema)
63 let params_to_values = List.mapi (fun i (n,t) -> param_name_to_string n i, t >> param_type_to_string)
64 let params_to_values = List.unique & params_to_values
66 let output_value_defs vals =
67 vals >> List.iter (fun (name,t) -> output "%s %s
;" t name)
69 let output_schema_data index schema =
70 let name = default_name "data
" index in
72 schema >> schema_to_values >> output_value_defs;
75 let set_param name index param =
77 output "pstmt_%s
.set%s
(%u
, %s
);"
79 (t >> param_type_to_string >> String.capitalize)
81 (param_name_to_string id index)
83 let output_params_binder name index params = List.iteri (set_param name) params
89 let generate_code index stmt =
90 let values = params_to_values stmt.params in
91 let name = choose_name stmt.props stmt.kind index in
92 let sql = quote (get_sql stmt) in
93 output "PreparedStatement pstmt_%s
;" name;
95 let schema_binder_name = output_schema_binder name index stmt.schema in
96 let result = match schema_binder_name with None -> [] | Some name -> ["result",name] in
97 let all_params = values @ result in
98 G.func "public
int" name all_params ~tail:"throws SQLException
" (fun () ->
99 output "if (null
== pstmt_%s
)" name;
100 output " pstmt_%s
= db
.prepareStatement
(%s
);" name sql;
101 output_params_binder name index stmt.params;
102 begin match schema_binder_name with
103 | None -> output "return pstmt_%s
.executeUpdate
();" name
105 output "ResultSet res
= pstmt_%s
.executeQuery
();" name;
106 let args = List.mapi (fun index attr -> get_column attr index) stmt.schema in
107 let args = String.concat "," args in
108 output "int count
= 0;";
109 output "while (res
.next
())";
111 output "result.callback
(%s
);" args;
114 output "return count
;"
118 let generate () name stmts =
119 params_mode := Some Unnamed; (* allow only unnamed params *)
120 output "import java
.sql.*;";
123 output "Connection db
;";
125 G.func "public
" name ["aDb
","Connection
"] (fun () ->
129 Enum.iteri generate_code stmts;