test case for #57
[sqlgg.git] / src / cli.ml
blobb525bf8517ffc4a67cbef47785b8a3d2c137bb23
1 (** command-line *)
3 open Printf
4 open ExtLib
6 module Cxx = Gen.Make(Gen_cxx)
7 module Caml = Gen.Make(Gen_caml)
8 module Xml_gen = Gen.Make(Gen_xml)
9 module Java = Gen.Make(Gen_java)
10 module CSharp = Gen.Make(Gen_csharp)
13 common usecase:
14 sqlgg [-gen none] ddl.sql -gen cxx dml.sql
16 let generate = ref None
17 let name = ref "sqlgg"
19 let set_out s =
20 generate :=
21 match (String.lowercase s) with
22 | "cxx" | "c++" | "cpp" -> Some Cxx.process
23 | "caml" | "ocaml" | "ml" -> Some Caml.process
24 | "xml" -> Some Xml_gen.process
25 | "java" -> Some Java.process
26 | "csharp" | "c#" | "cs" -> Some CSharp.process
27 | "none" -> None
28 | _ -> failwith (sprintf "Unknown output language: %s" s)
30 let set_params_mode s =
31 Gen.params_mode :=
32 match String.lowercase s with
33 | "named" -> Some Gen.Named
34 | "unnamed" -> Some Gen.Unnamed
35 | "oracle" -> Some Gen.Oracle
36 | "postgresql" -> Some Gen.PostgreSQL
37 | "none" -> None
38 | _ -> failwith (sprintf "Unknown params mode: %s" s)
40 let each_input =
41 let run input =
42 let l = match input with Some ch -> Main.get_statements ch | None -> [] in
43 match !generate with
44 | None -> []
45 | Some _ -> l
47 function
48 | "-" -> run (Some stdin)
49 | filename -> Main.with_channel filename run
51 let generate l =
52 match !generate with
53 | None -> ()
54 | Some f -> f !name l
56 let usage_msg =
57 let s1 = sprintf "SQL Guided (code) Generator ver. %s\n" Sqlgg_config.version in
58 let s2 = sprintf "Usage: %s <options> <file.sql> [<file2.sql> ...]\n" (Filename.basename Sys.executable_name) in
59 let s3 = "Options are:" in
60 s1 ^ s2 ^ s3
62 let show_version () = print_endline Sqlgg_config.version
64 let main () =
65 let l = ref [] in
66 let work s = l := each_input s :: !l in
67 let args = Arg.align
69 "-version", Arg.Unit show_version, " Show version";
70 "-gen", Arg.String set_out, "cxx|caml|java|xml|csharp|none Set output language (default: none)";
71 "-name", Arg.String (fun x -> name := x), "<identifier> Set output module name (default: sqlgg)";
72 "-params", Arg.String set_params_mode, "named|unnamed|oracle|postgresql|none Output query parameters substitution (default: none)";
73 "-debug", Arg.Int (fun x -> Sqlgg_config.debug_level := x), "<N> set debug level";
74 "-no-header", Arg.Unit (fun () -> Sqlgg_config.gen_header := false), "do not put version header in generated output";
75 "-show-tables", Arg.Unit Tables.print_all, " Show all current tables";
76 "-show-table", Arg.String Tables.print1, "<name> Show specified table";
77 "-", Arg.Unit (fun () -> work "-"), " Read sql from stdin";
78 "-test", Arg.Unit Test.run, " Run unit tests";
81 Arg.parse args work usage_msg;
82 match !l with
83 | [] -> if Array.length Sys.argv = 1 then Arg.usage args usage_msg; 0
84 | l ->
85 if !Error.errors then
86 begin Error.log "Errors encountered, no code generated"; 1 end
87 else
88 begin generate @@ List.concat @@ List.rev l; 0 end
90 let () = exit @@ main ()