more precise typing of CASE without selector
[sqlgg.git] / src / gen_java.ml
blobf576f2da9a75611a720791f619f81df1c79a4dd6
1 (* Java code generation *)
3 open ExtLib
4 open Prelude
5 open Printf
7 open Stmt
8 open Gen
9 open Sql
11 module G = Gen_cxx
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 ^ "\""
19 let start_ cls =
20 let f1 name =
21 output "%s %s" cls name;
22 G.open_curly ()
24 let f2 name =
25 G.close_curly " // %s %s" cls name;
26 empty_line ()
28 f1,f2
30 let (start_class,end_class) = start_ "public class"
31 let (start_intf,end_intf) = start_ "public static interface"
33 module L = struct
35 let as_lang_type = function
36 | Type.Int -> "int"
37 | Type.Text -> "String"
38 | Type.Any -> "String"
39 | Type.Float -> "float"
40 | Type.Blob -> "Blob"
41 | Type.Bool -> "boolean"
42 | Type.Datetime -> "Timestamp"
44 let as_api_type = String.capitalize $ as_lang_type
46 end
48 module T = Translate(L)
50 open L
51 open T
53 let get_column attr index =
54 sprintf "res.get%s(%u)"
55 (attr.domain |> as_api_type)
56 (index + 1)
58 let output_schema_binder name _ schema =
59 let name = sprintf "%s_callback" name in
60 start_intf name;
61 output "public void callback(%s);" (G.Values.to_string (schema_to_values schema));
62 end_intf name;
63 name
65 let output_schema_binder name index schema =
66 match schema with
67 | [] -> None
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
75 start_class name;
76 schema |> schema_to_values |> output_value_defs;
77 end_class name
79 let set_param name index param =
80 let (id,t) = param in
81 output "pstmt_%s.set%s(%u, %s);"
82 name
83 (t |> param_type_to_string |> String.capitalize)
84 (index+1)
85 (param_name_to_string id index)
87 let output_params_binder name _ params = List.iteri (set_param name) params
89 type t = unit
91 let start () = ()
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;
98 empty_line ();
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
108 | Some _ ->
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())";
114 G.open_curly ();
115 output "result.callback(%s);" args;
116 output "count++;";
117 G.close_curly "";
118 output "return count;"
119 end);
120 empty_line ()
122 let generate () name stmts =
123 params_mode := Some Unnamed; (* allow only unnamed params *)
124 output "import java.sql.*;";
125 empty_line ();
126 start_class name;
127 output "Connection db;";
128 empty_line ();
129 G.func "public" name ["aDb","Connection"] (fun () ->
130 output "db = aDb;";
132 empty_line ();
133 Enum.iteri generate_code stmts;
134 end_class name