1 (* Java code generation *)
14 let comment = G.comment
15 let empty_line = G.empty_line
19 output
"%s %s" cls name
;
23 G.close_curly
" // %s %s" cls name
;
28 let (start_class
,end_class
) = start_ "public class"
29 let (start_intf
,end_intf
) = start_ "public interface"
31 let as_java_type = function
33 | Type.Text
-> "String"
34 | Type.Float
-> "float"
36 | Type.Bool
-> "boolean"
37 | Type.Datetime
-> "Timestamp"
39 let get_column attr index
=
40 sprintf
"res.get%s(%u)"
41 (attr
.RA.domain
>> as_java_type >> String.capitalize
)
44 let param_type_to_string t
= t
>> Option.default
Type.Text
>> as_java_type
46 let set_param index param
=
48 output
"pstmt.set%s(%u, %s);"
49 (t
>> param_type_to_string >> String.capitalize
)
51 (param_name_to_string id index
)
53 let schema_to_values = List.mapi
(fun i attr
-> name_of attr i
, attr
.RA.domain
>> as_java_type)
55 let output_schema_binder index schema
=
56 let name = "output" in
58 output
"public void callback(%s);" (G.Values.to_string
(schema_to_values schema
));
62 let output_schema_binder index schema
=
65 | _
-> Some
(output_schema_binder index schema
)
67 let params_to_values = List.mapi
(fun i
(n
,t
) -> param_name_to_string n i
, t
>> param_type_to_string)
68 let params_to_values = List.unique
& params_to_values
70 let output_value_defs vals
=
71 vals
>> List.iter
(fun (name,t
) -> output
"%s %s;" t
name)
73 let output_schema_data index schema
=
74 let name = default_name
"data" index
in
76 schema
>> schema_to_values >> output_value_defs;
79 let output_params_binder index params
= List.iteri
set_param params
85 let generate_code () index schema params kind props
=
86 let values = params_to_values params
in
87 let name = choose_name props kind index
in
88 let sql = G.quote
(get_sql props kind params
) in
90 output
"PreparedStatement pstmt;";
92 G.func
"public" name ["db","Connection"] (fun () ->
93 output
"pstmt = db.prepareStatement(%s);" sql;
96 let schema_binder_name = output_schema_binder index schema
in
97 let result = match schema_binder_name with None
-> [] | Some
name -> ["result",name] in
98 let all_params = values @ result in
99 G.func
"public int" "execute" all_params ~tail
:"throws SQLException" (fun () ->
100 output_params_binder index params
;
101 begin match schema_binder_name with
102 | None
-> output
"return pstmt.executeUpdate();"
104 output
"ResultSet res = pstmt.executeQuery();";
105 let args = List.mapi
(fun index attr
-> get_column attr index
) schema
in
106 let args = String.concat
"," args in
107 output
"int count = 0;";
108 output
"while (res.next())";
110 output
"result.callback(%s);" args;
113 output
"return count;"
117 let start_output () name =
118 output
"import java.sql.*;";
122 let finish_output () name = end_class
name