fix paths
[sqlgg.git] / src / gen_java.ml
blobd587ccb5c89bebca64edd17a256e334df02ed174
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 quote = String.replace_chars (function '\n' -> "\" +\n\"" | '\r' -> "" | '"' -> "\\\"" | c -> String.make 1 c)
18 let quote s = "\"" ^ quote s ^ "\""
20 let start_ cls =
21 let f1 name =
22 output "%s %s" cls name;
23 G.open_curly ()
25 let f2 name =
26 G.close_curly " // %s %s" cls name;
27 empty_line ()
29 f1,f2
31 let (start_class,end_class) = start_ "public class"
32 let (start_intf,end_intf) = start_ "public static interface"
34 module L = struct
36 let as_lang_type = function
37 | Type.Int -> "int"
38 | Type.Text -> "String"
39 | Type.Any -> "String"
40 | Type.Float -> "float"
41 | Type.Blob -> "Blob"
42 | Type.Bool -> "boolean"
43 | Type.Datetime -> "Timestamp"
45 let as_api_type = String.capitalize & as_lang_type
47 end
49 module T = Translate(L)
51 open L
52 open T
54 let get_column attr index =
55 sprintf "res.get%s(%u)"
56 (attr.RA.domain >> as_api_type)
57 (index + 1)
59 let output_schema_binder name index schema =
60 let name = sprintf "%s_callback" name in
61 start_intf name;
62 output "public void callback(%s);" (G.Values.to_string (schema_to_values schema));
63 end_intf name;
64 name
66 let output_schema_binder name index schema =
67 match schema with
68 | [] -> None
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
76 start_class name;
77 schema >> schema_to_values >> output_value_defs;
78 end_class name
80 let set_param name index param =
81 let (id,t) = param in
82 output "pstmt_%s.set%s(%u, %s);"
83 name
84 (t >> param_type_to_string >> String.capitalize)
85 (index+1)
86 (param_name_to_string id index)
88 let output_params_binder name index params = List.iteri (set_param name) params
90 type t = unit
92 let start () = ()
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;
99 empty_line ();
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
109 | Some _ ->
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())";
115 G.open_curly ();
116 output "result.callback(%s);" args;
117 output "count++;";
118 G.close_curly "";
119 output "return count;"
120 end);
121 empty_line ()
123 let generate () name stmts =
124 params_mode := Some Unnamed; (* allow only unnamed params *)
125 output "import java.sql.*;";
126 empty_line ();
127 start_class name;
128 output "Connection db;";
129 empty_line ();
130 G.func "public" name ["aDb","Connection"] (fun () ->
131 output "db = aDb;";
133 empty_line ();
134 Enum.iteri generate_code stmts;
135 end_class name