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
"
36 let as_lang_type = function
38 | Type.Text -> "String
"
39 | Type.Any -> "String
"
40 | Type.Float -> "float"
42 | Type.Bool -> "boolean
"
43 | Type.Datetime -> "Timestamp
"
45 let as_api_type = String.capitalize & as_lang_type
49 module T = Translate(L)
54 let get_column attr index =
55 sprintf "res
.get%s
(%u
)"
56 (attr.RA.domain >> as_api_type)
59 let output_schema_binder name index schema =
60 let name = sprintf "%s_callback
" name in
62 output "public void callback
(%s
);" (G.Values.to_string (schema_to_values schema));
66 let output_schema_binder name index schema =
69 | _ -> Some (output_schema_binder name index schema)
71 let output_value_defs vals =
72 vals >> List.iter (fun (name,t) -> output "%s %s
;" t name)
74 let output_schema_data index schema =
75 let name = default_name "data
" index in
77 schema >> schema_to_values >> output_value_defs;
80 let set_param name index param =
82 output "pstmt_%s
.set%s
(%u
, %s
);"
84 (t >> param_type_to_string >> String.capitalize)
86 (param_name_to_string id index)
88 let output_params_binder name index params = List.iteri (set_param name) params
94 let generate_code index stmt =
95 let values = params_to_values stmt.params in
96 let name = choose_name stmt.props stmt.kind index in
97 let sql = quote (get_sql stmt) in
98 output "PreparedStatement pstmt_%s
;" name;
100 let schema_binder_name = output_schema_binder name index stmt.schema in
101 let result = match schema_binder_name with None -> [] | Some name -> ["result",name] in
102 let all_params = values @ result in
103 G.func "public
int" name all_params ~tail:"throws SQLException
" (fun () ->
104 output "if (null
== pstmt_%s
)" name;
105 output " pstmt_%s
= db
.prepareStatement
(%s
);" name sql;
106 output_params_binder name index stmt.params;
107 begin match schema_binder_name with
108 | None -> output "return pstmt_%s
.executeUpdate
();" name
110 output "ResultSet res
= pstmt_%s
.executeQuery
();" name;
111 let args = List.mapi (fun index attr -> get_column attr index) stmt.schema in
112 let args = String.concat "," args in
113 output "int count
= 0;";
114 output "while (res
.next
())";
116 output "result.callback
(%s
);" args;
119 output "return count
;"
123 let generate () name stmts =
124 params_mode := Some Unnamed; (* allow only unnamed params *)
125 output "import java
.sql.*;";
128 output "Connection db
;";
130 G.func "public
" name ["aDb
","Connection
"] (fun () ->
134 Enum.iteri generate_code stmts;