start with gen
[sqlgg.git] / sql2cpp.ml
blob9057a96c74221da645f073be2d3c85789e2b6939
1 (*
2 $Id$
3 *)
5 open Printf
6 open Operators
7 open ListMore
8 open ExtString
10 module L = List
11 module S = String
13 let statements s =
14 let lexbuf = Lexing.from_string s in
15 let rec loop l =
16 match (try Sql_lexer.ruleStatement Props.empty lexbuf with exn -> None) with
17 | Some x -> loop (x::l)
18 | None -> l
20 List.rev (loop [])
22 let parse_one (stmt,props) =
23 try
24 print_endline stmt;
25 Some ((Parser.parse_stmt stmt), props)
26 with
27 | exn ->
28 begin
29 print_endline (Printexc.to_string exn);
30 None
31 end
33 let show_one ((s,p),props) =
34 RA.Scheme.print s;
35 print_endline (Show.show<Stmt.Raw.params>(p))
37 let catch f x = try Some (f x) with _ -> None
39 let with_file filename f =
40 match catch Std.input_file filename with
41 | None -> Error.log "cannot open file : %s" filename
42 | Some s -> f s
44 let tee f x = f x; x
46 let work filename =
47 with_file filename (fun s -> s
48 >> statements >> L.map parse_one >> L.filter_valid
49 >> tee (L.map show_one) >> Gen.process)
52 let stmts =
53 List.map
54 (fun (str,props) -> match P.parse_string str with
55 | Some ((k,n,placeholders) as stmt) ->
56 (*Error.logs (Show.show<Stmt.Raw.parsed> stmt); *)
57 Some (k,n,placeholders,props,str)
58 | None -> Error.log "Failed to parse : %s" str; None)
59 all
60 >> List.filter_valid
62 Gen.process (Stmt.resolve stmts)
65 let show_help () =
66 Error.log "SQL to C++ Code Generator Version %s (%s)" Version.version (Version.revision >> S.explode >> L.take 8 >> S.implode);
67 Error.log "";
68 Error.log " Usage: %s file_with_statements.sql" (Filename.basename Sys.executable_name);
69 Error.log "";
70 Error.log " Parse given file (treating content as SQL statements) and emit corresponding code to stdout"
72 let main () =
73 match Array.to_list Sys.argv with
74 | _::"-test"::_ -> Test.run ()
75 | _::[file] -> work file
76 | _ -> show_help ()
78 let _ = Printexc.print main ()