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
37 | Type.Text -> "String
"
38 | Type.Any -> "String
"
39 | Type.Float -> "float"
41 | Type.Bool -> "boolean
"
42 | Type.Datetime -> "Timestamp
"
44 let as_api_type = String.capitalize $ as_lang_type
48 module T = Translate(L)
53 let get_column attr index =
54 sprintf "res
.get%s
(%u
)"
55 (attr.domain |> as_api_type)
58 let output_schema_binder name _ schema =
59 let name = sprintf "%s_callback
" name in
61 output "public void callback
(%s
);" (G.Values.to_string (schema_to_values schema));
65 let output_schema_binder name index schema =
68 | _ -> Some (output_schema_binder name index schema)
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 set_param name index param =
81 output "pstmt_%s
.set%s
(%u
, %s
);"
83 (t |> param_type_to_string |> String.capitalize)
85 (param_name_to_string id index)
87 let output_params_binder name _ params = List.iteri (set_param name) params
93 let generate_code index stmt =
94 let values = params_to_values stmt.params in
95 let name = choose_name stmt.props stmt.kind index in
96 let sql = quote (get_sql stmt) in
97 output "PreparedStatement pstmt_%s
;" name;
99 let schema_binder_name = output_schema_binder name index stmt.schema in
100 let result = match schema_binder_name with None -> [] | Some name -> ["result",name] in
101 let all_params = values @ result in
102 G.func "public
int" name all_params ~tail:"throws SQLException
" (fun () ->
103 output "if (null
== pstmt_%s
)" name;
104 output " pstmt_%s
= db
.prepareStatement
(%s
);" name sql;
105 output_params_binder name index stmt.params;
106 begin match schema_binder_name with
107 | None -> output "return pstmt_%s
.executeUpdate
();" name
109 output "ResultSet res
= pstmt_%s
.executeQuery
();" name;
110 let args = List.mapi (fun index attr -> get_column attr index) stmt.schema in
111 let args = String.concat "," args in
112 output "int count
= 0;";
113 output "while (res
.next
())";
115 output "result.callback
(%s
);" args;
118 output "return count
;"
122 let generate () name stmts =
123 params_mode := Some Unnamed; (* allow only unnamed params *)
124 output "import java
.sql.*;";
127 output "Connection db
;";
129 G.func "public
" name ["aDb
","Connection
"] (fun () ->
133 Enum.iteri generate_code stmts;