comparison src/postgres.sml @ 1953:0992323fa264

noMangleSql .urp directive
author Adam Chlipala <adam@chlipala.net>
date Sat, 04 Jan 2014 19:02:14 -0500
parents d02c1a0d8082
children 4ef0c6605b3a
comparison
equal deleted inserted replaced
1952:cf7f7e51b0a2 1953:0992323fa264
61 | Nullable t => p_sql_type_base t 61 | Nullable t => p_sql_type_base t
62 62
63 fun checkRel (table, checkNullable) (s, xts) = 63 fun checkRel (table, checkNullable) (s, xts) =
64 let 64 let
65 val sl = CharVector.map Char.toLower s 65 val sl = CharVector.map Char.toLower s
66 66 val sl = if size sl > 1 andalso String.sub (sl, 0) = #"\"" then
67 val q = "SELECT COUNT(*) FROM information_schema." ^ table ^ " WHERE table_name = '" 67 String.substring (sl, 1, size sl - 2)
68 else
69 sl
70
71 val q = "SELECT COUNT(*) FROM information_schema." ^ table ^ " WHERE LOWER(table_name) = '"
68 ^ sl ^ "'" 72 ^ sl ^ "'"
69 73
70 val q' = String.concat ["SELECT COUNT(*) FROM information_schema.columns WHERE table_name = '", 74 val q' = String.concat ["SELECT COUNT(*) FROM information_schema.columns WHERE table_name = '",
71 sl, 75 sl,
72 "' AND (", 76 "' AND (",
73 case String.concatWith " OR " 77 case String.concatWith " OR "
74 (map (fn (x, t) => 78 (map (fn (x, t) =>
75 String.concat ["(column_name = 'uw_", 79 String.concat ["(LOWER(column_name) = '",
76 CharVector.map 80 Settings.mangleSqlCatalog
77 Char.toLower (ident x), 81 (CharVector.map
82 Char.toLower (ident x)),
78 (case p_sql_type_base t of 83 (case p_sql_type_base t of
79 "bigint" => 84 "bigint" =>
80 "' AND data_type IN ('bigint', 'numeric')" 85 "' AND data_type IN ('bigint', 'numeric', 'integer')"
86 | "text" =>
87 "' AND data_type IN ('text', 'character varying')"
81 | t => 88 | t =>
82 String.concat ["' AND data_type = '", 89 String.concat ["' AND data_type = '",
83 t, 90 t,
84 "'"]), 91 "'"]),
85 if checkNullable then 92 if checkNullable then
96 | s => s, 103 | s => s,
97 ")"] 104 ")"]
98 105
99 val q'' = String.concat ["SELECT COUNT(*) FROM information_schema.columns WHERE table_name = '", 106 val q'' = String.concat ["SELECT COUNT(*) FROM information_schema.columns WHERE table_name = '",
100 sl, 107 sl,
101 "' AND column_name LIKE 'uw_%'"] 108 "' AND LOWER(column_name) LIKE '", Settings.mangleSqlCatalog "%'"]
102 in 109 in
103 box [string "res = PQexec(conn, \"", 110 box [string "res = PQexec(conn, \"",
104 string q, 111 string q,
105 string "\");", 112 string "\");",
106 newline, 113 newline,
138 box [string "PQclear(res);", 145 box [string "PQclear(res);",
139 newline, 146 newline,
140 string "PQfinish(conn);", 147 string "PQfinish(conn);",
141 newline, 148 newline,
142 string "uw_error(ctx, FATAL, \"Table '", 149 string "uw_error(ctx, FATAL, \"Table '",
143 string s, 150 string sl,
144 string "' does not exist.\");", 151 string "' does not exist.\");",
145 newline], 152 newline],
146 string "}", 153 string "}",
147 newline, 154 newline,
148 newline, 155 newline,
189 box [string "PQclear(res);", 196 box [string "PQclear(res);",
190 newline, 197 newline,
191 string "PQfinish(conn);", 198 string "PQfinish(conn);",
192 newline, 199 newline,
193 string "uw_error(ctx, FATAL, \"Table '", 200 string "uw_error(ctx, FATAL, \"Table '",
194 string s, 201 string sl,
195 string "' has the wrong column types.\");", 202 string "' has the wrong column types.\");",
196 newline], 203 newline],
197 string "}", 204 string "}",
198 newline, 205 newline,
199 newline, 206 newline,
241 box [string "PQclear(res);", 248 box [string "PQclear(res);",
242 newline, 249 newline,
243 string "PQfinish(conn);", 250 string "PQfinish(conn);",
244 newline, 251 newline,
245 string "uw_error(ctx, FATAL, \"Table '", 252 string "uw_error(ctx, FATAL, \"Table '",
246 string s, 253 string sl,
247 string "' has extra columns.\");", 254 string "' has extra columns.\");",
248 newline], 255 newline],
249 string "}", 256 string "}",
250 newline, 257 newline,
251 newline, 258 newline,