Merge branch 'master' of git@localhost:sqlgg
[sqlgg.git] / sqlgg.ml
blobbb3ad6d5a5830367a258640ae04225e629eeace7
1 (** command-line *)
3 open Printf
4 open Operators
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)
12 let generate = ref Cxx.process
13 let name = ref "sqlgg"
15 let set_out s =
16 generate :=
17 match (String.lowercase s) with
18 | "cxx" | "c++" | "cpp" -> Cxx.process
19 | "caml" | "ocaml" | "ml" -> Caml.process
20 | "xml" -> Xml_gen.process
21 | "java" -> Java.process
22 | "csharp" | "c#" | "cs" -> CSharp.process
23 | _ -> failwith (sprintf "Unknown output language: %s" s)
25 let set_name s = name := s
26 let set_params_mode s =
27 Gen.params_mode :=
28 match String.lowercase s with
29 | "named" -> Some Gen.Named
30 | "unnamed" -> Some Gen.Unnamed
31 | _ -> None
33 let work =
34 let run ch = ch >> Main.get_statements >> !generate !name in
35 function
36 | "-" -> run stdin
37 | filename -> Main.with_channel filename run
39 let usage_msg =
40 let s1 = sprintf "SQL Guided (code) Generator ver. %s\n" Config.version in
41 let s2 = sprintf "Usage: %s <options> <file.sql>\n" (Filename.basename Sys.executable_name) in
42 let s3 = "Options are:" in
43 s1 ^ s2 ^ s3
45 let show_version () = print_endline Config.version
47 let main () =
48 let args =
50 "-version", Arg.Unit show_version, " Show version";
51 "-gen", Arg.String set_out, "cxx|caml|java|xml|csharp Set output language";
52 "-name", Arg.String set_name, "<identifier> Set output module name";
53 "-params", Arg.String set_params_mode, "named|unnamed|input Output query parameters substitution";
54 "-", Arg.Unit (fun () -> work "-"), " Read sql from stdin";
55 "-test", Arg.Unit Test.run, " Run unit tests";
58 Arg.parse (Arg.align args) work usage_msg
60 let _ = Printexc.print main ()