todo -1
[sqlgg.git] / gen_java.ml
blobb5a30518294715832b6915f85a1352c6049dcdd8
1 (* Java code generation *)
3 open ExtList
4 open ExtString
5 open Operators
6 open Printf
8 open Stmt
9 open Gen
10 open Sql
12 module G = Gen_cxx
14 let comment = G.comment
15 let empty_line = G.empty_line
17 let start_ cls =
18 let f1 name =
19 output "%s %s" cls name;
20 G.open_curly ()
22 let f2 name =
23 G.close_curly " // %s %s" cls name;
24 empty_line ()
26 f1,f2
28 let (start_class,end_class) = start_ "public class"
29 let (start_intf,end_intf) = start_ "public interface"
31 let as_java_type = function
32 | Type.Int -> "int"
33 | Type.Text -> "String"
34 | Type.Float -> "float"
35 | Type.Blob -> "Blob"
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)
42 (index + 1)
44 let param_type_to_string t = t >> Option.default Type.Text >> as_java_type
46 let set_param index param =
47 let (id,t) = param in
48 output "pstmt.set%s(%u, %s);"
49 (t >> param_type_to_string >> String.capitalize)
50 (index+1)
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
57 start_intf name;
58 output "public void callback(%s);" (G.Values.to_string (schema_to_values schema));
59 end_intf name;
60 name
62 let output_schema_binder index schema =
63 match schema with
64 | [] -> None
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
75 start_class name;
76 schema >> schema_to_values >> output_value_defs;
77 end_class name
79 let output_params_binder index params = List.iteri set_param params
81 type t = unit
83 let start () = ()
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
89 start_class name;
90 output "PreparedStatement pstmt;";
91 empty_line ();
92 G.func "public" name ["db","Connection"] (fun () ->
93 output "pstmt = db.prepareStatement(%s);" sql;
95 empty_line ();
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();"
103 | Some name ->
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())";
109 G.open_curly ();
110 output "result.callback(%s);" args;
111 output "count++;";
112 G.close_curly "";
113 output "return count;"
114 end);
115 end_class name
117 let start_output () name =
118 output "import java.sql.*;";
119 empty_line ();
120 start_class name
122 let finish_output () name = end_class name