# HG changeset patch
# User Sylvain <syt@logilab.fr>
# Date 1209108761 -7200
#      Fri Apr 25 09:32:41 2008 +0200
# Node ID 4c3630f25fee6ad4d7e32d5ced35a70b431989f9
# Parent  ab3c51fd46bfc83eac878a8c5bc2a6e97550b027
stcheck fixes

diff --git a/stcheck.py b/stcheck.py
--- a/stcheck.py
+++ b/stcheck.py
@@ -39,7 +39,6 @@
         errors = []
         self._visit(node, errors)
         if errors:
-            print node
             raise BadRQLQuery('%s\n** %s' % (node, '\n** '.join(errors)))
         #if node.TYPE == 'select' and \
         #       not node.defined_vars and not node.get_restriction():
@@ -155,6 +154,16 @@
             for select in sortterm.root.children:
                 if len(select.selection) < term.value:
                     errors.append('order column out of bound %s' % term.value)
+        else:
+            stmt = term.stmt
+            for tvref in term.iget_nodes(VariableRef):
+                for vref in tvref.variable.references():
+                    if vref.relation() or vref in stmt.selection:
+                        break
+                else:
+                    msg = 'sort variable %s is not referenced any where else'
+                    errors.append(msg % tvref.name)
+                    
     def leave_sortterm(self, node, errors):
         pass
     
@@ -193,6 +202,8 @@
         pass
     
     def visit_relation(self, relation, errors):
+        if not relation.r_type in self.schema:
+            errors.append('unknown relation %s' % relation.r_type)
         if relation.optional and relation.neged():
                 errors.append("can use optional relation under NOT (%s)"
                               % relation.as_string())
@@ -260,9 +271,13 @@
 
     def visit_constant(self, constant, errors):
         assert len(constant.children)==0
-        if constant.type == 'etype' and constant.relation().r_type != 'is':
-            msg ='using an entity type in only allowed with "is" relation'
-            errors.append(msg)
+        if constant.type == 'etype':
+            if constant.relation().r_type != 'is':
+                msg ='using an entity type in only allowed with "is" relation'
+                errors.append(msg)
+            if not constant.value in self.schema:
+                errors.append('unknown entity type %s' % constant.value)
+                
     def leave_constant(self, node, errors):
         pass 
 
diff --git a/test/unittest_stcheck.py b/test/unittest_stcheck.py
--- a/test/unittest_stcheck.py
+++ b/test/unittest_stcheck.py
@@ -15,6 +15,10 @@
     'Any X WHERE X name nofunction(Y)',
     
     'Any Y WHERE X name "toto"',
+
+    'Any X WHERE X noattr "toto"',
+    
+    'Any X WHERE X is NonExistant',
     
     'Any UPPER(Y) WHERE X name "toto"',
 
@@ -153,9 +157,9 @@
         stmt = root.children[0]
         self.assertEquals(stmt.defined_vars['U'].valuable_references(), 3)
         copy = stmts.Select()
-        copy.append_selected(stmt.selected[0].copy(copy))
-        copy.append_selected(stmt.selected[1].copy(copy))
-        copy.append(stmt.get_restriction().copy(copy))
+        copy.append_selected(stmt.selection[0].copy(copy))
+        copy.append_selected(stmt.selection[1].copy(copy))
+        copy.set_where(stmt.where.copy(copy))
         newroot = stmts.Union()
         newroot.append(copy)
         self.annotate(newroot)