8 type subst_mode
= | Named
| Unnamed
| Oracle
| PostgreSQL
10 (** defines substitution function for parameter literals *)
11 let params_mode = ref None
13 let (inc_indent
,dec_indent
,make_indent
) =
15 (fun () -> v := !v + 2),
16 (fun () -> v := !v - 2),
17 (fun () -> String.make
!v ' '
)
19 let print_indent () = print_string
(make_indent
())
20 let indent s
= print_indent (); print_string s
21 let indent_endline s
= print_indent (); print_endline s
22 let output fmt
= kprintf
indent_endline fmt
23 let output_l = List.iter
indent_endline
24 let print fmt
= kprintf print_endline fmt
25 let indented k
= inc_indent
(); k
(); dec_indent
()
27 let name_of attr index
=
28 match attr
.Sql.name
with
29 | "" -> sprintf
"_%u" index
32 let param_name_to_string ((name
,_
):Sql.param_id
) index
=
34 | None
-> sprintf
"_%u" index
37 let make_name props default
= Option.default default
(Props.get props
"name")
38 let default_name str index
= sprintf
"%s_%u" str index
40 let choose_name props kind index
=
41 let fix = String.map
begin function
42 | ('a'
..'z'
| 'A'
..'Z'
| '
0'
..'
9'
| '_'
as c
) -> c
46 match Props.get props
"subst" with
47 | Some x
-> let (_
,s
) = String.replace ~str
:s ~sub
:("%%"^x^
"%%") ~by
:x
in fix s
50 let name = match kind
with
51 | Create t
-> sprintf
"create_%s" (fix t
)
52 | CreateIndex t
-> sprintf
"create_index_%s" (fix t
)
53 | Update
(Some t
) -> sprintf
"update_%s_%u" (fix t
) index
54 | Update None
-> sprintf
"update_%u" index
55 | Insert
(_
,t
) -> sprintf
"insert_%s_%u" (fix t
) index
56 | Delete t
-> sprintf
"delete_%s_%u" (fix t
) index
57 | Alter t
-> sprintf
"alter_%s_%u" (fix t
) index
58 | Drop t
-> sprintf
"drop_%s" (fix t
)
59 | Select _
-> sprintf
"select_%u" index
60 | Other
-> sprintf
"statement_%u" index
64 let substitute_params s params f
=
66 let b = Buffer.create
(String.length s
) in
67 let last = List.fold_left
(fun i
((_
,(i1
,i2
)),_
as param
) ->
68 let prefix = String.slice ~first
:i ~
last:i1 s
in
69 Buffer.add_string
b prefix;
70 Buffer.add_string
b (f
!index param
);
73 Buffer.add_string
b (String.slice ~first
:last s
);
76 let subst_named index (id
,_
) = "@" ^
(param_name_to_string id
index)
77 let subst_oracle index (id
,_
) = ":" ^
(param_name_to_string id
index)
78 let subst_postgresql index _
= "$" ^ string_of_int
(index + 1)
79 let subst_unnamed _ _
= "?"
82 let sql = Props.get stmt
.props
"sql" |> Option.get
in
83 match !params_mode with
86 let f = match subst
with
87 | Named
-> subst_named
88 | Unnamed
-> subst_unnamed
89 | Oracle
-> subst_oracle
90 | PostgreSQL
-> subst_postgresql
92 substitute_params sql stmt
.params
f
95 let module U
= Unix
in
96 let t = U.time
() |> U.gmtime
in
97 sprintf
"%04u-%02u-%02uT%02u:%02uZ" (1900 + t.U.tm_year
) (t.U.tm_mon
+1) t.U.tm_mday
t.U.tm_hour
t.U.tm_min
99 module type LangTypes
= sig
101 val as_api_type
: Sql.Type.t -> string
102 val as_lang_type
: Sql.Type.t -> string
106 module Translate
(T
: LangTypes
) = struct
108 let param_type_to_string = T.as_api_type
109 let schema_to_values = List.mapi
(fun i attr
-> name_of attr i
, T.as_lang_type attr
.Sql.domain
)
110 (* let schema_to_string = G.Values.to_string $ schema_to_values *)
111 let all_params_to_values = List.mapi
(fun i
(n
,t) -> param_name_to_string n i
, T.as_lang_type
t)
112 (* rev unique rev -- to preserve ordering with respect to first occurrences *)
113 let params_to_values = List.rev $
List.unique ~cmp
:(=) $
List.rev $
all_params_to_values
117 module type Generator
= sig
119 val generate
: t -> string -> Stmt.t Enum.t -> unit
120 val start
: unit -> t
121 val comment
: t -> ('a
,unit,string,unit) format4
-> 'a
122 val empty_line
: t -> unit
125 module Make
(S
: Generator
) = struct
127 let generate_header out
=
128 S.comment out
"DO NOT EDIT MANUALLY";
130 S.comment out
"generated by sqlgg %s on %s" Sqlgg_config.version
(time_string ());
133 let process name stmts
=
134 let out = S.start
() in
135 if !Sqlgg_config.gen_header
then generate_header out;
136 S.generate
out name stmts