7 module Cxx
= Gen.Make
(Gen_cxx
)
8 module Caml
= Gen.Make
(Gen_caml.Generator
)
9 module Caml_io
= Gen.Make
(Gen_caml.Generator_io
)
10 module Xml_gen
= Gen.Make
(Gen_xml
)
11 module Java
= Gen.Make
(Gen_java
)
12 module CSharp
= Gen.Make
(Gen_csharp
)
16 sqlgg [-gen none] ddl.sql -gen cxx dml.sql
18 let generate = ref None
19 let name = ref "sqlgg"
23 match (String.lowercase s
) with
24 | "cxx" | "c++" | "cpp" -> Some
Cxx.process
25 | "caml" | "ocaml" | "ml" -> Some
Caml.process
26 | "caml_io" -> Some
Caml_io.process
27 | "xml" -> Some
Xml_gen.process
28 | "java" -> Some
Java.process
29 | "csharp" | "c#" | "cs" -> Some
CSharp.process
31 | _
-> failwith
(sprintf
"Unknown output language: %s" s
)
33 let set_params_mode s
=
35 match String.lowercase s
with
36 | "named" -> Some
Gen.Named
37 | "unnamed" -> Some
Gen.Unnamed
38 | "oracle" -> Some
Gen.Oracle
39 | "postgresql" -> Some
Gen.PostgreSQL
41 | _
-> failwith
(sprintf
"Unknown params mode: %s" s
)
43 let all_categories = String.concat
" " @@ List.map
Stmt.show_category
Stmt.all_categories
44 let category_of_string s
=
45 match List.find
(fun cat
-> String.equal
(String.lowercase
@@ Stmt.show_category cat
) s
) Stmt.all_categories with
46 | exception _
-> failwith
@@ sprintf
"bad category %S" s
49 let s = String.lowercase
s in
50 Sqlgg_config.include_category
:=
54 | "" -> failwith
"bad category \"\""
55 | _
when s.[0] = '
-'
-> `Except
(List.map
category_of_string @@ String.nsplit
(String.slice ~first
:1 s) ",")
56 | _
-> `Only
(List.map
category_of_string @@ String.nsplit
s ",")
57 let filter_category cat
=
58 match !Sqlgg_config.include_category
with
61 | `Only l
-> List.mem cat l
62 | `Except l
-> not
(List.mem cat l
)
66 let l = match input
with Some ch
-> Main.get_statements ch
| None
-> [] in
69 | Some _
-> List.filter
(fun stmt
-> filter_category (Stmt.category_of_stmt_kind stmt
.Gen.kind
)) l
72 | "-" -> run (Some stdin
)
73 | filename
-> Main.with_channel filename
run
81 let s1 = sprintf
"SQL Guided (code) Generator ver. %s\n" Sqlgg_config.version
in
82 let s2 = sprintf
"Usage: %s <options> <file.sql> [<file2.sql> ...]\n" (Filename.basename
Sys.executable_name
) in
83 let s3 = "Options are:" in
86 let show_version () = print_endline
Sqlgg_config.version
90 let work s = l := each_input s :: !l in
93 "-version", Arg.Unit
show_version, " Show version";
94 "-category", Arg.String
set_category, sprintf
"{all|none|[-]<category>{,<category>}+} Only generate code for these specific query categories (possible values: %s)" all_categories;
95 "-gen", Arg.String
set_out, "cxx|caml|java|xml|csharp|none Set output language (default: none)";
96 "-name", Arg.String
(fun x
-> name := x
), "<identifier> Set output module name (default: sqlgg)";
97 "-params", Arg.String
set_params_mode, "named|unnamed|oracle|postgresql|none Output query parameters substitution (default: none)";
98 "-debug", Arg.Int
Sqlgg_config.set_debug_level
, "<N> set debug level";
99 "-no-header", Arg.Unit
(fun () -> Sqlgg_config.gen_header
:= None
),
100 "do not put version header in generated output";
101 "-no-header-timestamp", Arg.Unit
(fun () -> Sqlgg_config.gen_header
:= Some `Without_timestamp
),
102 "do not put timestamp in version header in generated output";
103 "-show-tables", Arg.Unit
Tables.print_all
, " Show all current tables";
104 "-show-table", Arg.String
Tables.print1
, "<name> Show specified table";
105 "-", Arg.Unit
(fun () -> work "-"), " Read sql from stdin";
106 "-test", Arg.Unit
Test.run, " Run unit tests";
109 Arg.parse
args work usage_msg;
111 | [] -> if Array.length
Sys.argv
= 1 then Arg.usage
args usage_msg; 0
113 if !Error.errors
then
114 begin Error.log
"Errors encountered, no code generated"; 1 end
116 begin generate @@ List.concat
@@ List.rev
l; 0 end
118 let () = exit
@@ main ()