comparison src/postgres.sml @ 872:9654bce27cff

Validating views
author Adam Chlipala <adamc@hcoop.net>
date Tue, 30 Jun 2009 16:17:32 -0400
parents 3ae6b655ced0
children 41971801b62d
comparison
equal deleted inserted replaced
871:3ae6b655ced0 872:9654bce27cff
44 | Blob => "bytea" 44 | Blob => "bytea"
45 | Channel => "bigint" 45 | Channel => "bigint"
46 | Client => "integer" 46 | Client => "integer"
47 | Nullable t => p_sql_type_base t 47 | Nullable t => p_sql_type_base t
48 48
49 fun checkRel (s, xts) = 49 fun checkRel (table, checkNullable) (s, xts) =
50 let 50 let
51 val sl = CharVector.map Char.toLower s 51 val sl = CharVector.map Char.toLower s
52 52
53 val q = "SELECT COUNT(*) FROM information_schema.tables WHERE table_name = '" 53 val q = "SELECT COUNT(*) FROM information_schema." ^ table ^ " WHERE table_name = '"
54 ^ sl ^ "'" 54 ^ sl ^ "'"
55 55
56 val q' = String.concat ["SELECT COUNT(*) FROM information_schema.columns WHERE table_name = '", 56 val q' = String.concat ["SELECT COUNT(*) FROM information_schema.columns WHERE table_name = '",
57 sl, 57 sl,
58 "' AND (", 58 "' AND (",
61 String.concat ["(column_name = 'uw_", 61 String.concat ["(column_name = 'uw_",
62 CharVector.map 62 CharVector.map
63 Char.toLower (ident x), 63 Char.toLower (ident x),
64 "' AND data_type = '", 64 "' AND data_type = '",
65 p_sql_type_base t, 65 p_sql_type_base t,
66 "' AND is_nullable = '", 66 "'",
67 if isNotNull t then 67 if checkNullable then
68 "NO" 68 (" AND is_nullable = '"
69 ^ (if isNotNull t then
70 "NO"
71 else
72 "YES")
73 ^ "'")
69 else 74 else
70 "YES", 75 "",
71 "')"]) xts), 76 ")"]) xts),
72 ")"] 77 ")"]
73 78
74 val q'' = String.concat ["SELECT COUNT(*) FROM information_schema.columns WHERE table_name = '", 79 val q'' = String.concat ["SELECT COUNT(*) FROM information_schema.columns WHERE table_name = '",
75 sl, 80 sl,
76 "' AND column_name LIKE 'uw_%'"] 81 "' AND column_name LIKE 'uw_%'"]
226 newline, 231 newline,
227 string "PQclear(res);", 232 string "PQclear(res);",
228 newline] 233 newline]
229 end 234 end
230 235
231 fun init {dbstring, prepared = ss, tables, sequences} = 236 fun init {dbstring, prepared = ss, tables, views, sequences} =
232 box [if #persistent (currentProtocol ()) then 237 box [if #persistent (currentProtocol ()) then
233 box [string "static void uw_db_validate(uw_context ctx) {", 238 box [string "static void uw_db_validate(uw_context ctx) {",
234 newline, 239 newline,
235 string "PGconn *conn = uw_get_db(ctx);", 240 string "PGconn *conn = uw_get_db(ctx);",
236 newline, 241 newline,
237 string "PGresult *res;", 242 string "PGresult *res;",
238 newline, 243 newline,
239 newline, 244 newline,
240 p_list_sep newline checkRel tables, 245 p_list_sep newline (checkRel ("tables", true)) tables,
246 p_list_sep newline (checkRel ("views", false)) views,
241 247
242 p_list_sep newline 248 p_list_sep newline
243 (fn s => 249 (fn s =>
244 let 250 let
245 val sl = CharVector.map Char.toLower s 251 val sl = CharVector.map Char.toLower s