diff 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
line wrap: on
line diff
--- a/src/postgres.sml	Tue Jun 30 15:59:41 2009 -0400
+++ b/src/postgres.sml	Tue Jun 30 16:17:32 2009 -0400
@@ -46,11 +46,11 @@
       | Client => "integer"
       | Nullable t => p_sql_type_base t
 
-fun checkRel (s, xts) =
+fun checkRel (table, checkNullable) (s, xts) =
     let
         val sl = CharVector.map Char.toLower s
 
-        val q = "SELECT COUNT(*) FROM information_schema.tables WHERE table_name = '"
+        val q = "SELECT COUNT(*) FROM information_schema." ^ table ^ " WHERE table_name = '"
                 ^ sl ^ "'"
 
         val q' = String.concat ["SELECT COUNT(*) FROM information_schema.columns WHERE table_name = '",
@@ -63,12 +63,17 @@
                                                                               Char.toLower (ident x),
                                                                           "' AND data_type = '",
                                                                           p_sql_type_base t,
-                                                                          "' AND is_nullable = '",
-                                                                          if isNotNull t then
-                                                                              "NO"
+                                                                          "'",
+                                                                          if checkNullable then
+                                                                              (" AND is_nullable = '"
+                                                                               ^ (if isNotNull t then
+                                                                                      "NO"
+                                                                                  else
+                                                                                      "YES")
+                                                                               ^ "'")
                                                                           else
-                                                                              "YES",
-                                                                          "')"]) xts),
+                                                                              "",
+                                                                          ")"]) xts),
                                 ")"]
 
         val q'' = String.concat ["SELECT COUNT(*) FROM information_schema.columns WHERE table_name = '",
@@ -228,7 +233,7 @@
              newline]
     end
 
-fun init {dbstring, prepared = ss, tables, sequences} =
+fun init {dbstring, prepared = ss, tables, views, sequences} =
     box [if #persistent (currentProtocol ()) then
              box [string "static void uw_db_validate(uw_context ctx) {",
                   newline,
@@ -237,7 +242,8 @@
                   string "PGresult *res;",
                   newline,
                   newline,
-                  p_list_sep newline checkRel tables,
+                  p_list_sep newline (checkRel ("tables", true)) tables,
+                  p_list_sep newline (checkRel ("views", false)) views,
 
                   p_list_sep newline
                              (fn s =>