1 (** Java code generation *)
13 let comment = G.comment
14 let empty_line = G.empty_line
16 let quote = String.replace_chars
(function '
\n'
-> "\" +\n\"" | '
\r'
-> "" | '
"' -> "\\\"" | c -> String.make 1 c)
17 let quote s = "\"" ^ quote s ^ "\""
21 output "%s %s
" cls name;
25 G.close_curly " // %s %s
" cls name;
30 let (start_class,end_class) = start_ "public
class"
31 let (start_intf,end_intf) = start_ "public static interface
"
35 let as_lang_type = function
42 | Decimal -> "float" (* BigDecimal? *)
43 | Datetime -> "Timestamp
"
44 | Unit _ -> assert false
46 let as_api_type = String.capitalize $ as_lang_type
50 module T = Translate(L)
55 let get_column attr index =
56 sprintf "res
.get%s
(%u
)"
57 (attr.domain |> as_api_type)
60 let output_schema_binder name _ schema =
61 let name = sprintf "%s_callback
" name in
63 output "public void callback
(%s
);" (G.Values.to_string @@ G.Values.inject @@ schema_to_values schema);
67 let output_schema_binder name index schema =
70 | _ -> Some (output_schema_binder name index schema)
72 let output_value_defs vals =
73 vals |> List.iter (fun (name,t) -> output "%s %s
;" t name)
75 let output_schema_data index schema =
76 let name = default_name "data
" index in
78 schema |> schema_to_values |> G.Values.inject |> output_value_defs;
81 let set_param name index param =
82 output "pstmt_%s
.set%s
(%u
, %s
);"
84 (param.typ |> Sql.Type.show |> String.capitalize)
86 (make_param_name index param.id)
88 let output_params_binder name _ params = List.iteri (set_param name) params
94 let generate_code index stmt =
95 let params = params_only stmt.vars in
96 let values = G.Values.inject @@ values_of_params params in
97 let name = choose_name stmt.props stmt.kind index in
98 let sql = quote (get_sql_string_only stmt) in
99 output "PreparedStatement pstmt_%s
;" name;
101 let schema_binder_name = output_schema_binder name index stmt.schema in
102 let result = match schema_binder_name with None -> [] | Some name -> ["result",name] in
103 let all_params = values @ result in
104 G.func "public
int" name all_params ~tail:"throws SQLException
" (fun () ->
105 output "if (null
== pstmt_%s
)" name;
106 output " pstmt_%s
= db
.prepareStatement
(%s
);" name sql;
107 output_params_binder name index params;
108 begin match schema_binder_name with
109 | None -> output "return pstmt_%s
.executeUpdate
();" name
111 output "ResultSet res
= pstmt_%s
.executeQuery
();" name;
112 let args = List.mapi (fun index attr -> get_column attr index) stmt.schema in
113 let args = String.concat "," args in
114 output "int count
= 0;";
115 output "while (res
.next
())";
117 output "result.callback
(%s
);" args;
120 output "return count
;"
124 let generate () name stmts =
125 params_mode := Some Unnamed; (* allow only unnamed params *)
126 output "import java
.sql.*;";
129 output "Connection db
;";
131 G.func "public
" name ["aDb
","Connection
"] (fun () ->
135 List.iteri generate_code stmts;