1 (* Java code generation *)
12 let comment = G.comment
13 let empty_line = G.empty_line
15 let quote = String.replace_chars
(function '
\n'
-> "\" +\n\"" | '
\r'
-> "" | '
"' -> "\\\"" | c -> String.make 1 c)
16 let quote s = "\"" ^ quote s ^ "\""
20 output "%s %s
" cls name;
24 G.close_curly " // %s %s
" cls name;
29 let (start_class,end_class) = start_ "public
class"
30 let (start_intf,end_intf) = start_ "public static interface
"
34 let as_lang_type = function
36 | Type.Text -> "String
"
37 | Type.Any -> "String
"
38 | Type.Float -> "float"
40 | Type.Bool -> "boolean
"
41 | Type.Datetime -> "Timestamp
"
43 let as_api_type = String.capitalize $ as_lang_type
47 module T = Translate(L)
52 let get_column attr index =
53 sprintf "res
.get%s
(%u
)"
54 (attr.domain |> as_api_type)
57 let output_schema_binder name _ schema =
58 let name = sprintf "%s_callback
" name in
60 output "public void callback
(%s
);" (G.Values.to_string (schema_to_values schema));
64 let output_schema_binder name index schema =
67 | _ -> Some (output_schema_binder name index schema)
69 let output_value_defs vals =
70 vals |> List.iter (fun (name,t) -> output "%s %s
;" t name)
72 let output_schema_data index schema =
73 let name = default_name "data
" index in
75 schema |> schema_to_values |> output_value_defs;
78 let set_param name index param =
80 output "pstmt_%s
.set%s
(%u
, %s
);"
82 (t |> param_type_to_string |> String.capitalize)
84 (param_name_to_string id index)
86 let output_params_binder name _ params = List.iteri (set_param name) params
92 let generate_code index stmt =
93 let values = params_to_values stmt.params in
94 let name = choose_name stmt.props stmt.kind index in
95 let sql = quote (get_sql stmt) in
96 output "PreparedStatement pstmt_%s
;" name;
98 let schema_binder_name = output_schema_binder name index stmt.schema in
99 let result = match schema_binder_name with None -> [] | Some name -> ["result",name] in
100 let all_params = values @ result in
101 G.func "public
int" name all_params ~tail:"throws SQLException
" (fun () ->
102 output "if (null
== pstmt_%s
)" name;
103 output " pstmt_%s
= db
.prepareStatement
(%s
);" name sql;
104 output_params_binder name index stmt.params;
105 begin match schema_binder_name with
106 | None -> output "return pstmt_%s
.executeUpdate
();" name
108 output "ResultSet res
= pstmt_%s
.executeQuery
();" name;
109 let args = List.mapi (fun index attr -> get_column attr index) stmt.schema in
110 let args = String.concat "," args in
111 output "int count
= 0;";
112 output "while (res
.next
())";
114 output "result.callback
(%s
);" args;
117 output "return count
;"
121 let generate () name stmts =
122 params_mode := Some Unnamed; (* allow only unnamed params *)
123 output "import java
.sql.*;";
126 output "Connection db
;";
128 G.func "public
" name ["aDb
","Connection
"] (fun () ->
132 List.iteri generate_code stmts;