22 let parse_one_exn (sql
,props
) =
23 if Config.debug1
() then prerr_endline sql
;
24 let (s
,p
,k
) = Parser.parse_stmt sql
in
26 let (sql
,p
) = match k
with
27 | Stmt.Insert
(Some s
,_
) ->
28 let module B
= Buffer
in
29 let b = B.create
100 in
32 let params = ref [] in
33 s
>> List.iter
(fun attr
->
34 if !params <> [] then B.add_string
b ",";
35 let name = "@" ^ attr
.RA.name in
36 let param = ((Some attr
.RA.name,(B.length
b,B.length
b + String.length
name)),attr
.RA.domain
) in
38 params := param :: !params
41 (B.contents
b, p
@ (List.rev
!params))
44 {Stmt.schema
=s
; params=p
; kind
=k
; props
=Props.set props
"sql" sql
}
48 Some
(parse_one_exn x
)
50 | Parser_utils.Error
(exn
,(line
,cnum
,tok
,tail
)) ->
52 let extra = Printexc.to_string exn
in
54 Error.log
"==> %s" sql;
55 if cnum
= String.length
sql && tok
= "" then
56 Error.log
"Exception %s" extra
58 Error.log
"Exception %s in %u:%u at \"%s%s\"" extra line cnum tok
(String.slice ~last
:32 tail
);
62 let parse_one (sql,props
as x
) =
63 match Props.get props
"noparse" with
64 | Some _
-> Some
{ Stmt.schema
=[]; params=[]; kind
=Stmt.Other
; props
=Props.set props
"sql" sql }
67 let get_statements ch
=
68 let lexbuf = Lexing.from_channel ch
in
69 let f () = try Sql_lexer.ruleStatement
Props.empty
lexbuf with _
-> None
in
72 | None
-> raise
Enum.No_more_elements
74 begin match parse_one sql with
77 if not
(RA.Schema.is_unique stmt
.Stmt.schema
) then
78 Error.log
"Error: this SQL statement will produce rowset with duplicate column names:\n%s\n" (fst
sql);
84 let with_channel filename
f =
85 match catch open_in filename
with
86 | None
-> Error.log
"cannot open file : %s" filename
; f None
87 | Some ch
-> Std.finally
(fun () -> close_in_noerr ch
) f (Some ch
)