server/test/unittest_rql2sql.py
changeset 3777 3ef8cdb5fb1c
parent 3720 5376aaadd16b
parent 3762 e416186fb91c
child 3890 d7a270f50f54
equal deleted inserted replaced
3739:817e96eeac5c 3777:3ef8cdb5fb1c
    37 schema['state_of'].inlined = False
    37 schema['state_of'].inlined = False
    38 schema['comments'].inlined = False
    38 schema['comments'].inlined = False
    39 
    39 
    40 PARSER = [
    40 PARSER = [
    41     (r"Personne P WHERE P nom 'Zig\'oto';",
    41     (r"Personne P WHERE P nom 'Zig\'oto';",
    42      '''SELECT P.cw_eid
    42      '''SELECT _P.cw_eid
    43 FROM cw_Personne AS P
    43 FROM cw_Personne AS _P
    44 WHERE P.cw_nom=Zig\'oto'''),
    44 WHERE _P.cw_nom=Zig\'oto'''),
    45 
    45 
    46     (r'Personne P WHERE P nom ~= "Zig\"oto%";',
    46     (r'Personne P WHERE P nom ~= "Zig\"oto%";',
    47      '''SELECT P.cw_eid
    47      '''SELECT _P.cw_eid
    48 FROM cw_Personne AS P
    48 FROM cw_Personne AS _P
    49 WHERE P.cw_nom ILIKE Zig"oto%'''),
    49 WHERE _P.cw_nom ILIKE Zig"oto%'''),
    50     ]
    50     ]
    51 
    51 
    52 BASIC = [
    52 BASIC = [
    53 
    53 
       
    54     ("Any AS WHERE AS is Affaire",
       
    55      '''SELECT _AS.cw_eid
       
    56 FROM cw_Affaire AS _AS'''),
       
    57 
    54     ("Any X WHERE X is Affaire",
    58     ("Any X WHERE X is Affaire",
    55      '''SELECT X.cw_eid
    59      '''SELECT _X.cw_eid
    56 FROM cw_Affaire AS X'''),
    60 FROM cw_Affaire AS _X'''),
    57 
    61 
    58     ("Any X WHERE X eid 0",
    62     ("Any X WHERE X eid 0",
    59      '''SELECT 0'''),
    63      '''SELECT 0'''),
    60 
    64 
    61     ("Personne P",
    65     ("Personne P",
    62      '''SELECT P.cw_eid
    66      '''SELECT _P.cw_eid
    63 FROM cw_Personne AS P'''),
    67 FROM cw_Personne AS _P'''),
    64 
    68 
    65     ("Personne P WHERE P test TRUE",
    69     ("Personne P WHERE P test TRUE",
    66      '''SELECT P.cw_eid
    70      '''SELECT _P.cw_eid
    67 FROM cw_Personne AS P
    71 FROM cw_Personne AS _P
    68 WHERE P.cw_test=TRUE'''),
    72 WHERE _P.cw_test=TRUE'''),
    69 
    73 
    70     ("Personne P WHERE P test false",
    74     ("Personne P WHERE P test false",
    71      '''SELECT P.cw_eid
    75      '''SELECT _P.cw_eid
    72 FROM cw_Personne AS P
    76 FROM cw_Personne AS _P
    73 WHERE P.cw_test=FALSE'''),
    77 WHERE _P.cw_test=FALSE'''),
    74 
    78 
    75     ("Personne P WHERE P eid -1",
    79     ("Personne P WHERE P eid -1",
    76      '''SELECT -1'''),
    80      '''SELECT -1'''),
    77 
    81 
    78     ("Personne P LIMIT 20 OFFSET 10",
    82     ("Personne P LIMIT 20 OFFSET 10",
    79      '''SELECT P.cw_eid
    83      '''SELECT _P.cw_eid
    80 FROM cw_Personne AS P
    84 FROM cw_Personne AS _P
    81 LIMIT 20
    85 LIMIT 20
    82 OFFSET 10'''),
    86 OFFSET 10'''),
    83 
    87 
    84     ("Personne P WHERE S is Societe, P travaille S, S nom 'Logilab';",
    88     ("Personne P WHERE S is Societe, P travaille S, S nom 'Logilab';",
    85      '''SELECT rel_travaille0.eid_from
    89      '''SELECT rel_travaille0.eid_from
    86 FROM cw_Societe AS S, travaille_relation AS rel_travaille0
    90 FROM cw_Societe AS _S, travaille_relation AS rel_travaille0
    87 WHERE rel_travaille0.eid_to=S.cw_eid AND S.cw_nom=Logilab'''),
    91 WHERE rel_travaille0.eid_to=_S.cw_eid AND _S.cw_nom=Logilab'''),
    88 
    92 
    89     ("Personne P WHERE P concerne A, A concerne S, S nom 'Logilab', S is Societe;",
    93     ("Personne P WHERE P concerne A, A concerne S, S nom 'Logilab', S is Societe;",
    90      '''SELECT rel_concerne0.eid_from
    94      '''SELECT rel_concerne0.eid_from
    91 FROM concerne_relation AS rel_concerne0, concerne_relation AS rel_concerne1, cw_Societe AS S
    95 FROM concerne_relation AS rel_concerne0, concerne_relation AS rel_concerne1, cw_Societe AS _S
    92 WHERE rel_concerne0.eid_to=rel_concerne1.eid_from AND rel_concerne1.eid_to=S.cw_eid AND S.cw_nom=Logilab'''),
    96 WHERE rel_concerne0.eid_to=rel_concerne1.eid_from AND rel_concerne1.eid_to=_S.cw_eid AND _S.cw_nom=Logilab'''),
    93 
    97 
    94     ("Note N WHERE X evaluee N, X nom 'Logilab';",
    98     ("Note N WHERE X evaluee N, X nom 'Logilab';",
    95      '''SELECT rel_evaluee0.eid_to
    99      '''SELECT rel_evaluee0.eid_to
    96 FROM cw_Division AS X, evaluee_relation AS rel_evaluee0
   100 FROM cw_Division AS _X, evaluee_relation AS rel_evaluee0
    97 WHERE rel_evaluee0.eid_from=X.cw_eid AND X.cw_nom=Logilab
   101 WHERE rel_evaluee0.eid_from=_X.cw_eid AND _X.cw_nom=Logilab
    98 UNION ALL
   102 UNION ALL
    99 SELECT rel_evaluee0.eid_to
   103 SELECT rel_evaluee0.eid_to
   100 FROM cw_Personne AS X, evaluee_relation AS rel_evaluee0
   104 FROM cw_Personne AS _X, evaluee_relation AS rel_evaluee0
   101 WHERE rel_evaluee0.eid_from=X.cw_eid AND X.cw_nom=Logilab
   105 WHERE rel_evaluee0.eid_from=_X.cw_eid AND _X.cw_nom=Logilab
   102 UNION ALL
   106 UNION ALL
   103 SELECT rel_evaluee0.eid_to
   107 SELECT rel_evaluee0.eid_to
   104 FROM cw_Societe AS X, evaluee_relation AS rel_evaluee0
   108 FROM cw_Societe AS _X, evaluee_relation AS rel_evaluee0
   105 WHERE rel_evaluee0.eid_from=X.cw_eid AND X.cw_nom=Logilab
   109 WHERE rel_evaluee0.eid_from=_X.cw_eid AND _X.cw_nom=Logilab
   106 UNION ALL
   110 UNION ALL
   107 SELECT rel_evaluee0.eid_to
   111 SELECT rel_evaluee0.eid_to
   108 FROM cw_SubDivision AS X, evaluee_relation AS rel_evaluee0
   112 FROM cw_SubDivision AS _X, evaluee_relation AS rel_evaluee0
   109 WHERE rel_evaluee0.eid_from=X.cw_eid AND X.cw_nom=Logilab'''),
   113 WHERE rel_evaluee0.eid_from=_X.cw_eid AND _X.cw_nom=Logilab'''),
   110 
   114 
   111     ("Note N WHERE X evaluee N, X nom in ('Logilab', 'Caesium');",
   115     ("Note N WHERE X evaluee N, X nom in ('Logilab', 'Caesium');",
   112      '''SELECT rel_evaluee0.eid_to
   116      '''SELECT rel_evaluee0.eid_to
   113 FROM cw_Division AS X, evaluee_relation AS rel_evaluee0
   117 FROM cw_Division AS _X, evaluee_relation AS rel_evaluee0
   114 WHERE rel_evaluee0.eid_from=X.cw_eid AND X.cw_nom IN(Logilab, Caesium)
   118 WHERE rel_evaluee0.eid_from=_X.cw_eid AND _X.cw_nom IN(Logilab, Caesium)
   115 UNION ALL
   119 UNION ALL
   116 SELECT rel_evaluee0.eid_to
   120 SELECT rel_evaluee0.eid_to
   117 FROM cw_Personne AS X, evaluee_relation AS rel_evaluee0
   121 FROM cw_Personne AS _X, evaluee_relation AS rel_evaluee0
   118 WHERE rel_evaluee0.eid_from=X.cw_eid AND X.cw_nom IN(Logilab, Caesium)
   122 WHERE rel_evaluee0.eid_from=_X.cw_eid AND _X.cw_nom IN(Logilab, Caesium)
   119 UNION ALL
   123 UNION ALL
   120 SELECT rel_evaluee0.eid_to
   124 SELECT rel_evaluee0.eid_to
   121 FROM cw_Societe AS X, evaluee_relation AS rel_evaluee0
   125 FROM cw_Societe AS _X, evaluee_relation AS rel_evaluee0
   122 WHERE rel_evaluee0.eid_from=X.cw_eid AND X.cw_nom IN(Logilab, Caesium)
   126 WHERE rel_evaluee0.eid_from=_X.cw_eid AND _X.cw_nom IN(Logilab, Caesium)
   123 UNION ALL
   127 UNION ALL
   124 SELECT rel_evaluee0.eid_to
   128 SELECT rel_evaluee0.eid_to
   125 FROM cw_SubDivision AS X, evaluee_relation AS rel_evaluee0
   129 FROM cw_SubDivision AS _X, evaluee_relation AS rel_evaluee0
   126 WHERE rel_evaluee0.eid_from=X.cw_eid AND X.cw_nom IN(Logilab, Caesium)'''),
   130 WHERE rel_evaluee0.eid_from=_X.cw_eid AND _X.cw_nom IN(Logilab, Caesium)'''),
   127 
   131 
   128     ("Any X WHERE X creation_date TODAY, X is Affaire",
   132     ("Any X WHERE X creation_date TODAY, X is Affaire",
   129      '''SELECT X.cw_eid
   133      '''SELECT _X.cw_eid
   130 FROM cw_Affaire AS X
   134 FROM cw_Affaire AS _X
   131 WHERE DATE(X.cw_creation_date)=CURRENT_DATE'''),
   135 WHERE DATE(_X.cw_creation_date)=CURRENT_DATE'''),
   132 
   136 
   133     ("Any N WHERE G is CWGroup, G name N, E eid 12, E read_permission G",
   137     ("Any N WHERE G is CWGroup, G name N, E eid 12, E read_permission G",
   134      '''SELECT G.cw_name
   138      '''SELECT _G.cw_name
   135 FROM cw_CWGroup AS G, read_permission_relation AS rel_read_permission0
   139 FROM cw_CWGroup AS _G, read_permission_relation AS rel_read_permission0
   136 WHERE rel_read_permission0.eid_from=12 AND rel_read_permission0.eid_to=G.cw_eid'''),
   140 WHERE rel_read_permission0.eid_from=12 AND rel_read_permission0.eid_to=_G.cw_eid'''),
   137 
   141 
   138     ('Any Y WHERE U login "admin", U login Y', # stupid but valid...
   142     ('Any Y WHERE U login "admin", U login Y', # stupid but valid...
   139      """SELECT U.cw_login
   143      """SELECT _U.cw_login
   140 FROM cw_CWUser AS U
   144 FROM cw_CWUser AS _U
   141 WHERE U.cw_login=admin"""),
   145 WHERE _U.cw_login=admin"""),
   142 
   146 
   143     ('Any T WHERE T tags X, X is State',
   147     ('Any T WHERE T tags X, X is State',
   144      '''SELECT rel_tags0.eid_from
   148      '''SELECT rel_tags0.eid_from
   145 FROM cw_State AS X, tags_relation AS rel_tags0
   149 FROM cw_State AS _X, tags_relation AS rel_tags0
   146 WHERE rel_tags0.eid_to=X.cw_eid'''),
   150 WHERE rel_tags0.eid_to=_X.cw_eid'''),
   147 
   151 
   148     ('Any X,Y WHERE X eid 0, Y eid 1, X concerne Y',
   152     ('Any X,Y WHERE X eid 0, Y eid 1, X concerne Y',
   149      '''SELECT 0, 1
   153      '''SELECT 0, 1
   150 FROM concerne_relation AS rel_concerne0
   154 FROM concerne_relation AS rel_concerne0
   151 WHERE rel_concerne0.eid_from=0 AND rel_concerne0.eid_to=1'''),
   155 WHERE rel_concerne0.eid_from=0 AND rel_concerne0.eid_to=1'''),
   152 
   156 
   153     ("Any X WHERE X prenom 'lulu',"
   157     ("Any X WHERE X prenom 'lulu',"
   154      "EXISTS(X owned_by U, U in_group G, G name 'lulufanclub' OR G name 'managers');",
   158      "EXISTS(X owned_by U, U in_group G, G name 'lulufanclub' OR G name 'managers');",
   155      '''SELECT X.cw_eid
   159      '''SELECT _X.cw_eid
   156 FROM cw_Personne AS X
   160 FROM cw_Personne AS _X
   157 WHERE X.cw_prenom=lulu AND EXISTS(SELECT 1 FROM owned_by_relation AS rel_owned_by0, in_group_relation AS rel_in_group1, cw_CWGroup AS G WHERE rel_owned_by0.eid_from=X.cw_eid AND rel_in_group1.eid_from=rel_owned_by0.eid_to AND rel_in_group1.eid_to=G.cw_eid AND ((G.cw_name=lulufanclub) OR (G.cw_name=managers)))'''),
   161 WHERE _X.cw_prenom=lulu AND EXISTS(SELECT 1 FROM owned_by_relation AS rel_owned_by0, in_group_relation AS rel_in_group1, cw_CWGroup AS _G WHERE rel_owned_by0.eid_from=_X.cw_eid AND rel_in_group1.eid_from=rel_owned_by0.eid_to AND rel_in_group1.eid_to=_G.cw_eid AND ((_G.cw_name=lulufanclub) OR (_G.cw_name=managers)))'''),
   158 
   162 
   159     ("Any X WHERE X prenom 'lulu',"
   163     ("Any X WHERE X prenom 'lulu',"
   160      "NOT EXISTS(X owned_by U, U in_group G, G name 'lulufanclub' OR G name 'managers');",
   164      "NOT EXISTS(X owned_by U, U in_group G, G name 'lulufanclub' OR G name 'managers');",
   161      '''SELECT X.cw_eid
   165      '''SELECT _X.cw_eid
   162 FROM cw_Personne AS X
   166 FROM cw_Personne AS _X
   163 WHERE X.cw_prenom=lulu AND NOT EXISTS(SELECT 1 FROM owned_by_relation AS rel_owned_by0, in_group_relation AS rel_in_group1, cw_CWGroup AS G WHERE rel_owned_by0.eid_from=X.cw_eid AND rel_in_group1.eid_from=rel_owned_by0.eid_to AND rel_in_group1.eid_to=G.cw_eid AND ((G.cw_name=lulufanclub) OR (G.cw_name=managers)))'''),
   167 WHERE _X.cw_prenom=lulu AND NOT EXISTS(SELECT 1 FROM owned_by_relation AS rel_owned_by0, in_group_relation AS rel_in_group1, cw_CWGroup AS _G WHERE rel_owned_by0.eid_from=_X.cw_eid AND rel_in_group1.eid_from=rel_owned_by0.eid_to AND rel_in_group1.eid_to=_G.cw_eid AND ((_G.cw_name=lulufanclub) OR (_G.cw_name=managers)))'''),
   164 ]
   168 ]
   165 
   169 
   166 ADVANCED= [
   170 ADVANCED= [
   167     ("Societe S WHERE S nom 'Logilab' OR S nom 'Caesium'",
   171     ("Societe S WHERE S nom 'Logilab' OR S nom 'Caesium'",
   168      '''SELECT S.cw_eid
   172      '''SELECT _S.cw_eid
   169 FROM cw_Societe AS S
   173 FROM cw_Societe AS _S
   170 WHERE ((S.cw_nom=Logilab) OR (S.cw_nom=Caesium))'''),
   174 WHERE ((_S.cw_nom=Logilab) OR (_S.cw_nom=Caesium))'''),
   171 
   175 
   172     ('Any X WHERE X nom "toto", X eid IN (9700, 9710, 1045, 674)',
   176     ('Any X WHERE X nom "toto", X eid IN (9700, 9710, 1045, 674)',
   173     '''SELECT X.cw_eid
   177     '''SELECT _X.cw_eid
   174 FROM cw_Division AS X
   178 FROM cw_Division AS _X
   175 WHERE X.cw_nom=toto AND X.cw_eid IN(9700, 9710, 1045, 674)
   179 WHERE _X.cw_nom=toto AND _X.cw_eid IN(9700, 9710, 1045, 674)
   176 UNION ALL
   180 UNION ALL
   177 SELECT X.cw_eid
   181 SELECT _X.cw_eid
   178 FROM cw_Personne AS X
   182 FROM cw_Personne AS _X
   179 WHERE X.cw_nom=toto AND X.cw_eid IN(9700, 9710, 1045, 674)
   183 WHERE _X.cw_nom=toto AND _X.cw_eid IN(9700, 9710, 1045, 674)
   180 UNION ALL
   184 UNION ALL
   181 SELECT X.cw_eid
   185 SELECT _X.cw_eid
   182 FROM cw_Societe AS X
   186 FROM cw_Societe AS _X
   183 WHERE X.cw_nom=toto AND X.cw_eid IN(9700, 9710, 1045, 674)
   187 WHERE _X.cw_nom=toto AND _X.cw_eid IN(9700, 9710, 1045, 674)
   184 UNION ALL
   188 UNION ALL
   185 SELECT X.cw_eid
   189 SELECT _X.cw_eid
   186 FROM cw_SubDivision AS X
   190 FROM cw_SubDivision AS _X
   187 WHERE X.cw_nom=toto AND X.cw_eid IN(9700, 9710, 1045, 674)'''),
   191 WHERE _X.cw_nom=toto AND _X.cw_eid IN(9700, 9710, 1045, 674)'''),
   188 
   192 
   189     ('Any Y, COUNT(N) GROUPBY Y WHERE Y evaluee N;',
   193     ('Any Y, COUNT(N) GROUPBY Y WHERE Y evaluee N;',
   190      '''SELECT rel_evaluee0.eid_from, COUNT(rel_evaluee0.eid_to)
   194      '''SELECT rel_evaluee0.eid_from, COUNT(rel_evaluee0.eid_to)
   191 FROM evaluee_relation AS rel_evaluee0
   195 FROM evaluee_relation AS rel_evaluee0
   192 GROUP BY rel_evaluee0.eid_from'''),
   196 GROUP BY rel_evaluee0.eid_from'''),
   193 
   197 
   194     ("Any X WHERE X concerne B or C concerne X",
   198     ("Any X WHERE X concerne B or C concerne X",
   195      '''SELECT X.cw_eid
   199      '''SELECT _X.cw_eid
   196 FROM concerne_relation AS rel_concerne0, concerne_relation AS rel_concerne1, cw_Affaire AS X
   200 FROM concerne_relation AS rel_concerne0, concerne_relation AS rel_concerne1, cw_Affaire AS _X
   197 WHERE ((rel_concerne0.eid_from=X.cw_eid) OR (rel_concerne1.eid_to=X.cw_eid))'''),
   201 WHERE ((rel_concerne0.eid_from=_X.cw_eid) OR (rel_concerne1.eid_to=_X.cw_eid))'''),
   198 
   202 
   199     ("Any X WHERE X travaille S or X concerne A",
   203     ("Any X WHERE X travaille S or X concerne A",
   200      '''SELECT X.cw_eid
   204      '''SELECT _X.cw_eid
   201 FROM concerne_relation AS rel_concerne1, cw_Personne AS X, travaille_relation AS rel_travaille0
   205 FROM concerne_relation AS rel_concerne1, cw_Personne AS _X, travaille_relation AS rel_travaille0
   202 WHERE ((rel_travaille0.eid_from=X.cw_eid) OR (rel_concerne1.eid_from=X.cw_eid))'''),
   206 WHERE ((rel_travaille0.eid_from=_X.cw_eid) OR (rel_concerne1.eid_from=_X.cw_eid))'''),
   203 
   207 
   204     ("Any N WHERE A evaluee N or N ecrit_par P",
   208     ("Any N WHERE A evaluee N or N ecrit_par P",
   205      '''SELECT N.cw_eid
   209      '''SELECT _N.cw_eid
   206 FROM cw_Note AS N, evaluee_relation AS rel_evaluee0
   210 FROM cw_Note AS _N, evaluee_relation AS rel_evaluee0
   207 WHERE ((rel_evaluee0.eid_to=N.cw_eid) OR (N.cw_ecrit_par IS NOT NULL))'''),
   211 WHERE ((rel_evaluee0.eid_to=_N.cw_eid) OR (_N.cw_ecrit_par IS NOT NULL))'''),
   208 
   212 
   209     ("Any N WHERE A evaluee N or EXISTS(N todo_by U)",
   213     ("Any N WHERE A evaluee N or EXISTS(N todo_by U)",
   210      '''SELECT N.cw_eid
   214      '''SELECT _N.cw_eid
   211 FROM cw_Note AS N, evaluee_relation AS rel_evaluee0
   215 FROM cw_Note AS _N, evaluee_relation AS rel_evaluee0
   212 WHERE ((rel_evaluee0.eid_to=N.cw_eid) OR (EXISTS(SELECT 1 FROM todo_by_relation AS rel_todo_by1 WHERE rel_todo_by1.eid_from=N.cw_eid)))'''),
   216 WHERE ((rel_evaluee0.eid_to=_N.cw_eid) OR (EXISTS(SELECT 1 FROM todo_by_relation AS rel_todo_by1 WHERE rel_todo_by1.eid_from=_N.cw_eid)))'''),
   213 
   217 
   214     ("Any N WHERE A evaluee N or N todo_by U",
   218     ("Any N WHERE A evaluee N or N todo_by U",
   215      '''SELECT N.cw_eid
   219      '''SELECT _N.cw_eid
   216 FROM cw_Note AS N, evaluee_relation AS rel_evaluee0, todo_by_relation AS rel_todo_by1
   220 FROM cw_Note AS _N, evaluee_relation AS rel_evaluee0, todo_by_relation AS rel_todo_by1
   217 WHERE ((rel_evaluee0.eid_to=N.cw_eid) OR (rel_todo_by1.eid_from=N.cw_eid))'''),
   221 WHERE ((rel_evaluee0.eid_to=_N.cw_eid) OR (rel_todo_by1.eid_from=_N.cw_eid))'''),
   218 
   222 
   219     ("Any X WHERE X concerne B or C concerne X, B eid 12, C eid 13",
   223     ("Any X WHERE X concerne B or C concerne X, B eid 12, C eid 13",
   220      '''SELECT X.cw_eid
   224      '''SELECT _X.cw_eid
   221 FROM concerne_relation AS rel_concerne0, concerne_relation AS rel_concerne1, cw_Affaire AS X
   225 FROM concerne_relation AS rel_concerne0, concerne_relation AS rel_concerne1, cw_Affaire AS _X
   222 WHERE ((rel_concerne0.eid_from=X.cw_eid AND rel_concerne0.eid_to=12) OR (rel_concerne1.eid_from=13 AND rel_concerne1.eid_to=X.cw_eid))'''),
   226 WHERE ((rel_concerne0.eid_from=_X.cw_eid AND rel_concerne0.eid_to=12) OR (rel_concerne1.eid_from=13 AND rel_concerne1.eid_to=_X.cw_eid))'''),
   223 
   227 
   224     ('Any X WHERE X created_by U, X concerne B OR C concerne X, B eid 12, C eid 13',
   228     ('Any X WHERE X created_by U, X concerne B OR C concerne X, B eid 12, C eid 13',
   225      '''SELECT rel_created_by0.eid_from
   229      '''SELECT rel_created_by0.eid_from
   226 FROM concerne_relation AS rel_concerne1, concerne_relation AS rel_concerne2, created_by_relation AS rel_created_by0
   230 FROM concerne_relation AS rel_concerne1, concerne_relation AS rel_concerne2, created_by_relation AS rel_created_by0
   227 WHERE ((rel_concerne1.eid_from=rel_created_by0.eid_from AND rel_concerne1.eid_to=12) OR (rel_concerne2.eid_from=13 AND rel_concerne2.eid_to=rel_created_by0.eid_from))'''),
   231 WHERE ((rel_concerne1.eid_from=rel_created_by0.eid_from AND rel_concerne1.eid_to=12) OR (rel_concerne2.eid_from=13 AND rel_concerne2.eid_to=rel_created_by0.eid_from))'''),
   228 
   232 
   229     ('Any P WHERE P travaille_subdivision S1 OR P travaille_subdivision S2, S1 nom "logilab", S2 nom "caesium"',
   233     ('Any P WHERE P travaille_subdivision S1 OR P travaille_subdivision S2, S1 nom "logilab", S2 nom "caesium"',
   230      '''SELECT P.cw_eid
   234      '''SELECT _P.cw_eid
   231 FROM cw_Personne AS P, cw_SubDivision AS S1, cw_SubDivision AS S2, travaille_subdivision_relation AS rel_travaille_subdivision0, travaille_subdivision_relation AS rel_travaille_subdivision1
   235 FROM cw_Personne AS _P, cw_SubDivision AS _S1, cw_SubDivision AS _S2, travaille_subdivision_relation AS rel_travaille_subdivision0, travaille_subdivision_relation AS rel_travaille_subdivision1
   232 WHERE ((rel_travaille_subdivision0.eid_from=P.cw_eid AND rel_travaille_subdivision0.eid_to=S1.cw_eid) OR (rel_travaille_subdivision1.eid_from=P.cw_eid AND rel_travaille_subdivision1.eid_to=S2.cw_eid)) AND S1.cw_nom=logilab AND S2.cw_nom=caesium'''),
   236 WHERE ((rel_travaille_subdivision0.eid_from=_P.cw_eid AND rel_travaille_subdivision0.eid_to=_S1.cw_eid) OR (rel_travaille_subdivision1.eid_from=_P.cw_eid AND rel_travaille_subdivision1.eid_to=_S2.cw_eid)) AND _S1.cw_nom=logilab AND _S2.cw_nom=caesium'''),
   233 
   237 
   234     ('Any X WHERE T tags X',
   238     ('Any X WHERE T tags X',
   235      '''SELECT rel_tags0.eid_to
   239      '''SELECT rel_tags0.eid_to
   236 FROM tags_relation AS rel_tags0'''),
   240 FROM tags_relation AS rel_tags0'''),
   237 
   241 
   239      '''SELECT rel_in_basket0.eid_from
   243      '''SELECT rel_in_basket0.eid_from
   240 FROM in_basket_relation AS rel_in_basket0
   244 FROM in_basket_relation AS rel_in_basket0
   241 WHERE rel_in_basket0.eid_to=12'''),
   245 WHERE rel_in_basket0.eid_to=12'''),
   242 
   246 
   243     ('Any SEN,RN,OEN WHERE X from_entity SE, SE eid 44, X relation_type R, R eid 139, X to_entity OE, OE eid 42, R name RN, SE name SEN, OE name OEN',
   247     ('Any SEN,RN,OEN WHERE X from_entity SE, SE eid 44, X relation_type R, R eid 139, X to_entity OE, OE eid 42, R name RN, SE name SEN, OE name OEN',
   244      '''SELECT SE.cw_name, R.cw_name, OE.cw_name
   248      '''SELECT _SE.cw_name, _R.cw_name, _OE.cw_name
   245 FROM cw_CWAttribute AS X, cw_CWEType AS OE, cw_CWEType AS SE, cw_CWRType AS R
   249 FROM cw_CWAttribute AS _X, cw_CWEType AS _OE, cw_CWEType AS _SE, cw_CWRType AS _R
   246 WHERE X.cw_from_entity=44 AND SE.cw_eid=44 AND X.cw_relation_type=139 AND R.cw_eid=139 AND X.cw_to_entity=42 AND OE.cw_eid=42
   250 WHERE _X.cw_from_entity=44 AND _SE.cw_eid=44 AND _X.cw_relation_type=139 AND _R.cw_eid=139 AND _X.cw_to_entity=42 AND _OE.cw_eid=42
   247 UNION ALL
   251 UNION ALL
   248 SELECT SE.cw_name, R.cw_name, OE.cw_name
   252 SELECT _SE.cw_name, _R.cw_name, _OE.cw_name
   249 FROM cw_CWEType AS OE, cw_CWEType AS SE, cw_CWRType AS R, cw_CWRelation AS X
   253 FROM cw_CWEType AS _OE, cw_CWEType AS _SE, cw_CWRType AS _R, cw_CWRelation AS _X
   250 WHERE X.cw_from_entity=44 AND SE.cw_eid=44 AND X.cw_relation_type=139 AND R.cw_eid=139 AND X.cw_to_entity=42 AND OE.cw_eid=42'''),
   254 WHERE _X.cw_from_entity=44 AND _SE.cw_eid=44 AND _X.cw_relation_type=139 AND _R.cw_eid=139 AND _X.cw_to_entity=42 AND _OE.cw_eid=42'''),
   251 
   255 
   252     # Any O WHERE NOT S corrected_in O, S eid %(x)s, S concerns P, O version_of P, O in_state ST, NOT ST name "published", O modification_date MTIME ORDERBY MTIME DESC LIMIT 9
   256     # Any O WHERE NOT S corrected_in O, S eid %(x)s, S concerns P, O version_of P, O in_state ST, NOT ST name "published", O modification_date MTIME ORDERBY MTIME DESC LIMIT 9
   253     ('Any O WHERE NOT S ecrit_par O, S eid 1, S inline1 P, O inline2 P',
   257     ('Any O WHERE NOT S ecrit_par O, S eid 1, S inline1 P, O inline2 P',
   254      '''SELECT O.cw_eid
   258      '''SELECT _O.cw_eid
   255 FROM cw_Note AS S, cw_Personne AS O
   259 FROM cw_Note AS _S, cw_Personne AS _O
   256 WHERE NOT EXISTS(SELECT 1 WHERE S.cw_ecrit_par=O.cw_eid) AND S.cw_eid=1 AND O.cw_inline2=S.cw_inline1'''),
   260 WHERE NOT EXISTS(SELECT 1 WHERE _S.cw_ecrit_par=_O.cw_eid) AND _S.cw_eid=1 AND _O.cw_inline2=_S.cw_inline1'''),
   257 
   261 
   258     ('DISTINCT Any S ORDERBY stockproc(SI) WHERE NOT S ecrit_par O, S para SI',
   262     ('DISTINCT Any S ORDERBY stockproc(SI) WHERE NOT S ecrit_par O, S para SI',
   259      '''SELECT T1.C0 FROM (SELECT DISTINCT S.cw_eid AS C0, STOCKPROC(S.cw_para) AS C1
   263      '''SELECT T1.C0 FROM (SELECT DISTINCT _S.cw_eid AS C0, STOCKPROC(_S.cw_para) AS C1
   260 FROM cw_Note AS S
   264 FROM cw_Note AS _S
   261 WHERE S.cw_ecrit_par IS NULL
   265 WHERE _S.cw_ecrit_par IS NULL
   262 ORDER BY 2) AS T1'''),
   266 ORDER BY 2) AS T1'''),
   263 
   267 
   264     ('Any N WHERE N todo_by U, N is Note, U eid 2, N filed_under T, T eid 3',
   268     ('Any N WHERE N todo_by U, N is Note, U eid 2, N filed_under T, T eid 3',
   265      # N would actually be invarient if U eid 2 had given a specific type to U
   269      # N would actually be invarient if U eid 2 had given a specific type to U
   266      '''SELECT N.cw_eid
   270      '''SELECT _N.cw_eid
   267 FROM cw_Note AS N, filed_under_relation AS rel_filed_under1, todo_by_relation AS rel_todo_by0
   271 FROM cw_Note AS _N, filed_under_relation AS rel_filed_under1, todo_by_relation AS rel_todo_by0
   268 WHERE rel_todo_by0.eid_from=N.cw_eid AND rel_todo_by0.eid_to=2 AND rel_filed_under1.eid_from=N.cw_eid AND rel_filed_under1.eid_to=3'''),
   272 WHERE rel_todo_by0.eid_from=_N.cw_eid AND rel_todo_by0.eid_to=2 AND rel_filed_under1.eid_from=_N.cw_eid AND rel_filed_under1.eid_to=3'''),
   269 
   273 
   270     ('Any N WHERE N todo_by U, U eid 2, P evaluee N, P eid 3',
   274     ('Any N WHERE N todo_by U, U eid 2, P evaluee N, P eid 3',
   271      '''SELECT rel_evaluee1.eid_to
   275      '''SELECT rel_evaluee1.eid_to
   272 FROM evaluee_relation AS rel_evaluee1, todo_by_relation AS rel_todo_by0
   276 FROM evaluee_relation AS rel_evaluee1, todo_by_relation AS rel_todo_by0
   273 WHERE rel_evaluee1.eid_to=rel_todo_by0.eid_from AND rel_todo_by0.eid_to=2 AND rel_evaluee1.eid_from=3'''),
   277 WHERE rel_evaluee1.eid_to=rel_todo_by0.eid_from AND rel_todo_by0.eid_to=2 AND rel_evaluee1.eid_from=3'''),
   277      '''SELECT 2, rel_owned_by0.eid_to
   281      '''SELECT 2, rel_owned_by0.eid_to
   278 FROM owned_by_relation AS rel_owned_by0
   282 FROM owned_by_relation AS rel_owned_by0
   279 WHERE rel_owned_by0.eid_from=1 AND NOT EXISTS(SELECT 1 FROM owned_by_relation AS rel_owned_by1 WHERE rel_owned_by1.eid_from=2 AND rel_owned_by0.eid_to=rel_owned_by1.eid_to)'''),
   283 WHERE rel_owned_by0.eid_from=1 AND NOT EXISTS(SELECT 1 FROM owned_by_relation AS rel_owned_by1 WHERE rel_owned_by1.eid_from=2 AND rel_owned_by0.eid_to=rel_owned_by1.eid_to)'''),
   280 
   284 
   281     ('Any GN WHERE X in_group G, G name GN, (G name "managers" OR EXISTS(X copain T, T login in ("comme", "cochon")))',
   285     ('Any GN WHERE X in_group G, G name GN, (G name "managers" OR EXISTS(X copain T, T login in ("comme", "cochon")))',
   282      '''SELECT G.cw_name
   286      '''SELECT _G.cw_name
   283 FROM cw_CWGroup AS G, in_group_relation AS rel_in_group0
   287 FROM cw_CWGroup AS _G, in_group_relation AS rel_in_group0
   284 WHERE rel_in_group0.eid_to=G.cw_eid AND ((G.cw_name=managers) OR (EXISTS(SELECT 1 FROM copain_relation AS rel_copain1, cw_CWUser AS T WHERE rel_copain1.eid_from=rel_in_group0.eid_from AND rel_copain1.eid_to=T.cw_eid AND T.cw_login IN(comme, cochon))))'''),
   288 WHERE rel_in_group0.eid_to=_G.cw_eid AND ((_G.cw_name=managers) OR (EXISTS(SELECT 1 FROM copain_relation AS rel_copain1, cw_CWUser AS _T WHERE rel_copain1.eid_from=rel_in_group0.eid_from AND rel_copain1.eid_to=_T.cw_eid AND _T.cw_login IN(comme, cochon))))'''),
   285 
   289 
   286     ('Any C WHERE C is Card, EXISTS(X documented_by C)',
   290     ('Any C WHERE C is Card, EXISTS(X documented_by C)',
   287       """SELECT C.cw_eid
   291       """SELECT _C.cw_eid
   288 FROM cw_Card AS C
   292 FROM cw_Card AS _C
   289 WHERE EXISTS(SELECT 1 FROM documented_by_relation AS rel_documented_by0 WHERE rel_documented_by0.eid_to=C.cw_eid)"""),
   293 WHERE EXISTS(SELECT 1 FROM documented_by_relation AS rel_documented_by0 WHERE rel_documented_by0.eid_to=_C.cw_eid)"""),
   290 
   294 
   291     ('Any C WHERE C is Card, EXISTS(X documented_by C, X eid 12)',
   295     ('Any C WHERE C is Card, EXISTS(X documented_by C, X eid 12)',
   292       """SELECT C.cw_eid
   296       """SELECT _C.cw_eid
   293 FROM cw_Card AS C
   297 FROM cw_Card AS _C
   294 WHERE EXISTS(SELECT 1 FROM documented_by_relation AS rel_documented_by0 WHERE rel_documented_by0.eid_from=12 AND rel_documented_by0.eid_to=C.cw_eid)"""),
   298 WHERE EXISTS(SELECT 1 FROM documented_by_relation AS rel_documented_by0 WHERE rel_documented_by0.eid_from=12 AND rel_documented_by0.eid_to=_C.cw_eid)"""),
   295 
   299 
   296     ('Any T WHERE C is Card, C title T, EXISTS(X documented_by C, X eid 12)',
   300     ('Any T WHERE C is Card, C title T, EXISTS(X documented_by C, X eid 12)',
   297       """SELECT C.cw_title
   301       """SELECT _C.cw_title
   298 FROM cw_Card AS C
   302 FROM cw_Card AS _C
   299 WHERE EXISTS(SELECT 1 FROM documented_by_relation AS rel_documented_by0 WHERE rel_documented_by0.eid_from=12 AND rel_documented_by0.eid_to=C.cw_eid)"""),
   303 WHERE EXISTS(SELECT 1 FROM documented_by_relation AS rel_documented_by0 WHERE rel_documented_by0.eid_from=12 AND rel_documented_by0.eid_to=_C.cw_eid)"""),
   300 
   304 
   301     ('Any GN,L WHERE X in_group G, X login L, G name GN, EXISTS(X copain T, T login L, T login IN("comme", "cochon"))',
   305     ('Any GN,L WHERE X in_group G, X login L, G name GN, EXISTS(X copain T, T login L, T login IN("comme", "cochon"))',
   302      '''SELECT G.cw_name, X.cw_login
   306      '''SELECT _G.cw_name, _X.cw_login
   303 FROM cw_CWGroup AS G, cw_CWUser AS X, in_group_relation AS rel_in_group0
   307 FROM cw_CWGroup AS _G, cw_CWUser AS _X, in_group_relation AS rel_in_group0
   304 WHERE rel_in_group0.eid_from=X.cw_eid AND rel_in_group0.eid_to=G.cw_eid AND EXISTS(SELECT 1 FROM copain_relation AS rel_copain1, cw_CWUser AS T WHERE rel_copain1.eid_from=X.cw_eid AND rel_copain1.eid_to=T.cw_eid AND T.cw_login=X.cw_login AND T.cw_login IN(comme, cochon))'''),
   308 WHERE rel_in_group0.eid_from=_X.cw_eid AND rel_in_group0.eid_to=_G.cw_eid AND EXISTS(SELECT 1 FROM copain_relation AS rel_copain1, cw_CWUser AS _T WHERE rel_copain1.eid_from=_X.cw_eid AND rel_copain1.eid_to=_T.cw_eid AND _T.cw_login=_X.cw_login AND _T.cw_login IN(comme, cochon))'''),
   305 
   309 
   306     ('Any X,S, MAX(T) GROUPBY X,S ORDERBY S WHERE X is CWUser, T tags X, S eid IN(32), X in_state S',
   310     ('Any X,S, MAX(T) GROUPBY X,S ORDERBY S WHERE X is CWUser, T tags X, S eid IN(32), X in_state S',
   307      '''SELECT X.cw_eid, 32, MAX(rel_tags0.eid_from)
   311      '''SELECT _X.cw_eid, 32, MAX(rel_tags0.eid_from)
   308 FROM cw_CWUser AS X, tags_relation AS rel_tags0
   312 FROM cw_CWUser AS _X, tags_relation AS rel_tags0
   309 WHERE rel_tags0.eid_to=X.cw_eid AND X.cw_in_state=32
   313 WHERE rel_tags0.eid_to=_X.cw_eid AND _X.cw_in_state=32
   310 GROUP BY X.cw_eid'''),
   314 GROUP BY _X.cw_eid'''),
   311 
   315 
   312     ('Any COUNT(S),CS GROUPBY CS ORDERBY 1 DESC LIMIT 10 WHERE S is Affaire, C is Societe, S concerne C, C nom CS, (EXISTS(S owned_by 1)) OR (EXISTS(S documented_by N, N title "published"))',
   316     ('Any COUNT(S),CS GROUPBY CS ORDERBY 1 DESC LIMIT 10 WHERE S is Affaire, C is Societe, S concerne C, C nom CS, (EXISTS(S owned_by 1)) OR (EXISTS(S documented_by N, N title "published"))',
   313      '''SELECT COUNT(rel_concerne0.eid_from), C.cw_nom
   317      '''SELECT COUNT(rel_concerne0.eid_from), _C.cw_nom
   314 FROM concerne_relation AS rel_concerne0, cw_Societe AS C
   318 FROM concerne_relation AS rel_concerne0, cw_Societe AS _C
   315 WHERE rel_concerne0.eid_to=C.cw_eid AND ((EXISTS(SELECT 1 FROM owned_by_relation AS rel_owned_by1 WHERE rel_concerne0.eid_from=rel_owned_by1.eid_from AND rel_owned_by1.eid_to=1)) OR (EXISTS(SELECT 1 FROM documented_by_relation AS rel_documented_by2, cw_Card AS N WHERE rel_concerne0.eid_from=rel_documented_by2.eid_from AND rel_documented_by2.eid_to=N.cw_eid AND N.cw_title=published)))
   319 WHERE rel_concerne0.eid_to=_C.cw_eid AND ((EXISTS(SELECT 1 FROM owned_by_relation AS rel_owned_by1 WHERE rel_concerne0.eid_from=rel_owned_by1.eid_from AND rel_owned_by1.eid_to=1)) OR (EXISTS(SELECT 1 FROM documented_by_relation AS rel_documented_by2, cw_Card AS _N WHERE rel_concerne0.eid_from=rel_documented_by2.eid_from AND rel_documented_by2.eid_to=_N.cw_eid AND _N.cw_title=published)))
   316 GROUP BY C.cw_nom
   320 GROUP BY _C.cw_nom
   317 ORDER BY 1 DESC
   321 ORDER BY 1 DESC
   318 LIMIT 10'''),
   322 LIMIT 10'''),
   319 
   323 
   320     ('Any X WHERE Y evaluee X, Y is CWUser',
   324     ('Any X WHERE Y evaluee X, Y is CWUser',
   321      '''SELECT rel_evaluee0.eid_to
   325      '''SELECT rel_evaluee0.eid_to
   322 FROM cw_CWUser AS Y, evaluee_relation AS rel_evaluee0
   326 FROM cw_CWUser AS _Y, evaluee_relation AS rel_evaluee0
   323 WHERE rel_evaluee0.eid_from=Y.cw_eid'''),
   327 WHERE rel_evaluee0.eid_from=_Y.cw_eid'''),
   324 
   328 
   325     ('Any L WHERE X login "admin", X identity Y, Y login L',
   329     ('Any L WHERE X login "admin", X identity Y, Y login L',
   326      '''SELECT Y.cw_login
   330      '''SELECT _Y.cw_login
   327 FROM cw_CWUser AS X, cw_CWUser AS Y
   331 FROM cw_CWUser AS _X, cw_CWUser AS _Y
   328 WHERE X.cw_login=admin AND X.cw_eid=Y.cw_eid'''),
   332 WHERE _X.cw_login=admin AND _X.cw_eid=_Y.cw_eid'''),
   329 
   333 
   330     ('Any L WHERE X login "admin", NOT X identity Y, Y login L',
   334     ('Any L WHERE X login "admin", NOT X identity Y, Y login L',
   331      '''SELECT Y.cw_login
   335      '''SELECT _Y.cw_login
   332 FROM cw_CWUser AS X, cw_CWUser AS Y
   336 FROM cw_CWUser AS _X, cw_CWUser AS _Y
   333 WHERE X.cw_login=admin AND NOT X.cw_eid=Y.cw_eid'''),
   337 WHERE _X.cw_login=admin AND NOT _X.cw_eid=_Y.cw_eid'''),
   334 
   338 
   335     ('Any L WHERE X login "admin", X identity Y?, Y login L',
   339     ('Any L WHERE X login "admin", X identity Y?, Y login L',
   336      '''SELECT Y.cw_login
   340      '''SELECT _Y.cw_login
   337 FROM cw_CWUser AS X LEFT OUTER JOIN cw_CWUser AS Y ON (X.cw_eid=Y.cw_eid)
   341 FROM cw_CWUser AS _X LEFT OUTER JOIN cw_CWUser AS _Y ON (_X.cw_eid=_Y.cw_eid)
   338 WHERE X.cw_login=admin'''),
   342 WHERE _X.cw_login=admin'''),
   339 
   343 
   340     ('Any XN ORDERBY XN WHERE X name XN, X is IN (Basket,Folder,Tag)',
   344     ('Any XN ORDERBY XN WHERE X name XN, X is IN (Basket,Folder,Tag)',
   341      '''SELECT X.cw_name
   345      '''SELECT _X.cw_name
   342 FROM cw_Basket AS X
   346 FROM cw_Basket AS _X
   343 UNION ALL
   347 UNION ALL
   344 SELECT X.cw_name
   348 SELECT _X.cw_name
   345 FROM cw_Folder AS X
   349 FROM cw_Folder AS _X
   346 UNION ALL
   350 UNION ALL
   347 SELECT X.cw_name
   351 SELECT _X.cw_name
   348 FROM cw_Tag AS X
   352 FROM cw_Tag AS _X
   349 ORDER BY 1'''),
   353 ORDER BY 1'''),
   350 
   354 
   351     # DISTINCT, can use relation under exists scope as principal
   355     # DISTINCT, can use relation under exists scope as principal
   352     ('DISTINCT Any X,Y WHERE X name "CWGroup", Y eid IN(1, 2, 3), EXISTS(X read_permission Y)',
   356     ('DISTINCT Any X,Y WHERE X name "CWGroup", Y eid IN(1, 2, 3), EXISTS(X read_permission Y)',
   353      '''SELECT DISTINCT X.cw_eid, rel_read_permission0.eid_to
   357      '''SELECT DISTINCT _X.cw_eid, rel_read_permission0.eid_to
   354 FROM cw_CWEType AS X, read_permission_relation AS rel_read_permission0
   358 FROM cw_CWEType AS _X, read_permission_relation AS rel_read_permission0
   355 WHERE X.cw_name=CWGroup AND rel_read_permission0.eid_to IN(1, 2, 3) AND EXISTS(SELECT 1 WHERE rel_read_permission0.eid_from=X.cw_eid)
   359 WHERE _X.cw_name=CWGroup AND rel_read_permission0.eid_to IN(1, 2, 3) AND EXISTS(SELECT 1 WHERE rel_read_permission0.eid_from=_X.cw_eid)
   356 UNION
   360 UNION
   357 SELECT DISTINCT X.cw_eid, rel_read_permission0.eid_to
   361 SELECT DISTINCT _X.cw_eid, rel_read_permission0.eid_to
   358 FROM cw_CWRType AS X, read_permission_relation AS rel_read_permission0
   362 FROM cw_CWRType AS _X, read_permission_relation AS rel_read_permission0
   359 WHERE X.cw_name=CWGroup AND rel_read_permission0.eid_to IN(1, 2, 3) AND EXISTS(SELECT 1 WHERE rel_read_permission0.eid_from=X.cw_eid)'''),
   363 WHERE _X.cw_name=CWGroup AND rel_read_permission0.eid_to IN(1, 2, 3) AND EXISTS(SELECT 1 WHERE rel_read_permission0.eid_from=_X.cw_eid)'''),
   360 
   364 
   361     # no distinct, Y can't be invariant
   365     # no distinct, Y can't be invariant
   362     ('Any X,Y WHERE X name "CWGroup", Y eid IN(1, 2, 3), EXISTS(X read_permission Y)',
   366     ('Any X,Y WHERE X name "CWGroup", Y eid IN(1, 2, 3), EXISTS(X read_permission Y)',
   363      '''SELECT X.cw_eid, Y.cw_eid
   367      '''SELECT _X.cw_eid, _Y.cw_eid
   364 FROM cw_CWEType AS X, cw_CWGroup AS Y
   368 FROM cw_CWEType AS _X, cw_CWGroup AS _Y
   365 WHERE X.cw_name=CWGroup AND Y.cw_eid IN(1, 2, 3) AND EXISTS(SELECT 1 FROM read_permission_relation AS rel_read_permission0 WHERE rel_read_permission0.eid_from=X.cw_eid AND rel_read_permission0.eid_to=Y.cw_eid)
   369 WHERE _X.cw_name=CWGroup AND _Y.cw_eid IN(1, 2, 3) AND EXISTS(SELECT 1 FROM read_permission_relation AS rel_read_permission0 WHERE rel_read_permission0.eid_from=_X.cw_eid AND rel_read_permission0.eid_to=_Y.cw_eid)
   366 UNION ALL
   370 UNION ALL
   367 SELECT X.cw_eid, Y.cw_eid
   371 SELECT _X.cw_eid, _Y.cw_eid
   368 FROM cw_CWEType AS X, cw_RQLExpression AS Y
   372 FROM cw_CWEType AS _X, cw_RQLExpression AS _Y
   369 WHERE X.cw_name=CWGroup AND Y.cw_eid IN(1, 2, 3) AND EXISTS(SELECT 1 FROM read_permission_relation AS rel_read_permission0 WHERE rel_read_permission0.eid_from=X.cw_eid AND rel_read_permission0.eid_to=Y.cw_eid)
   373 WHERE _X.cw_name=CWGroup AND _Y.cw_eid IN(1, 2, 3) AND EXISTS(SELECT 1 FROM read_permission_relation AS rel_read_permission0 WHERE rel_read_permission0.eid_from=_X.cw_eid AND rel_read_permission0.eid_to=_Y.cw_eid)
   370 UNION ALL
   374 UNION ALL
   371 SELECT X.cw_eid, Y.cw_eid
   375 SELECT _X.cw_eid, _Y.cw_eid
   372 FROM cw_CWGroup AS Y, cw_CWRType AS X
   376 FROM cw_CWGroup AS _Y, cw_CWRType AS _X
   373 WHERE X.cw_name=CWGroup AND Y.cw_eid IN(1, 2, 3) AND EXISTS(SELECT 1 FROM read_permission_relation AS rel_read_permission0 WHERE rel_read_permission0.eid_from=X.cw_eid AND rel_read_permission0.eid_to=Y.cw_eid)
   377 WHERE _X.cw_name=CWGroup AND _Y.cw_eid IN(1, 2, 3) AND EXISTS(SELECT 1 FROM read_permission_relation AS rel_read_permission0 WHERE rel_read_permission0.eid_from=_X.cw_eid AND rel_read_permission0.eid_to=_Y.cw_eid)
   374 UNION ALL
   378 UNION ALL
   375 SELECT X.cw_eid, Y.cw_eid
   379 SELECT _X.cw_eid, _Y.cw_eid
   376 FROM cw_CWRType AS X, cw_RQLExpression AS Y
   380 FROM cw_CWRType AS _X, cw_RQLExpression AS _Y
   377 WHERE X.cw_name=CWGroup AND Y.cw_eid IN(1, 2, 3) AND EXISTS(SELECT 1 FROM read_permission_relation AS rel_read_permission0 WHERE rel_read_permission0.eid_from=X.cw_eid AND rel_read_permission0.eid_to=Y.cw_eid)'''),
   381 WHERE _X.cw_name=CWGroup AND _Y.cw_eid IN(1, 2, 3) AND EXISTS(SELECT 1 FROM read_permission_relation AS rel_read_permission0 WHERE rel_read_permission0.eid_from=_X.cw_eid AND rel_read_permission0.eid_to=_Y.cw_eid)'''),
   378 
   382 
   379     # DISTINCT but NEGED exists, can't be invariant
   383     # DISTINCT but NEGED exists, can't be invariant
   380     ('DISTINCT Any X,Y WHERE X name "CWGroup", Y eid IN(1, 2, 3), NOT EXISTS(X read_permission Y)',
   384     ('DISTINCT Any X,Y WHERE X name "CWGroup", Y eid IN(1, 2, 3), NOT EXISTS(X read_permission Y)',
   381      '''SELECT DISTINCT X.cw_eid, Y.cw_eid
   385      '''SELECT DISTINCT _X.cw_eid, _Y.cw_eid
   382 FROM cw_CWEType AS X, cw_CWGroup AS Y
   386 FROM cw_CWEType AS _X, cw_CWGroup AS _Y
   383 WHERE X.cw_name=CWGroup AND Y.cw_eid IN(1, 2, 3) AND NOT EXISTS(SELECT 1 FROM read_permission_relation AS rel_read_permission0 WHERE rel_read_permission0.eid_from=X.cw_eid AND rel_read_permission0.eid_to=Y.cw_eid)
   387 WHERE _X.cw_name=CWGroup AND _Y.cw_eid IN(1, 2, 3) AND NOT EXISTS(SELECT 1 FROM read_permission_relation AS rel_read_permission0 WHERE rel_read_permission0.eid_from=_X.cw_eid AND rel_read_permission0.eid_to=_Y.cw_eid)
   384 UNION
   388 UNION
   385 SELECT DISTINCT X.cw_eid, Y.cw_eid
   389 SELECT DISTINCT _X.cw_eid, _Y.cw_eid
   386 FROM cw_CWEType AS X, cw_RQLExpression AS Y
   390 FROM cw_CWEType AS _X, cw_RQLExpression AS _Y
   387 WHERE X.cw_name=CWGroup AND Y.cw_eid IN(1, 2, 3) AND NOT EXISTS(SELECT 1 FROM read_permission_relation AS rel_read_permission0 WHERE rel_read_permission0.eid_from=X.cw_eid AND rel_read_permission0.eid_to=Y.cw_eid)
   391 WHERE _X.cw_name=CWGroup AND _Y.cw_eid IN(1, 2, 3) AND NOT EXISTS(SELECT 1 FROM read_permission_relation AS rel_read_permission0 WHERE rel_read_permission0.eid_from=_X.cw_eid AND rel_read_permission0.eid_to=_Y.cw_eid)
   388 UNION
   392 UNION
   389 SELECT DISTINCT X.cw_eid, Y.cw_eid
   393 SELECT DISTINCT _X.cw_eid, _Y.cw_eid
   390 FROM cw_CWGroup AS Y, cw_CWRType AS X
   394 FROM cw_CWGroup AS _Y, cw_CWRType AS _X
   391 WHERE X.cw_name=CWGroup AND Y.cw_eid IN(1, 2, 3) AND NOT EXISTS(SELECT 1 FROM read_permission_relation AS rel_read_permission0 WHERE rel_read_permission0.eid_from=X.cw_eid AND rel_read_permission0.eid_to=Y.cw_eid)
   395 WHERE _X.cw_name=CWGroup AND _Y.cw_eid IN(1, 2, 3) AND NOT EXISTS(SELECT 1 FROM read_permission_relation AS rel_read_permission0 WHERE rel_read_permission0.eid_from=_X.cw_eid AND rel_read_permission0.eid_to=_Y.cw_eid)
   392 UNION
   396 UNION
   393 SELECT DISTINCT X.cw_eid, Y.cw_eid
   397 SELECT DISTINCT _X.cw_eid, _Y.cw_eid
   394 FROM cw_CWRType AS X, cw_RQLExpression AS Y
   398 FROM cw_CWRType AS _X, cw_RQLExpression AS _Y
   395 WHERE X.cw_name=CWGroup AND Y.cw_eid IN(1, 2, 3) AND NOT EXISTS(SELECT 1 FROM read_permission_relation AS rel_read_permission0 WHERE rel_read_permission0.eid_from=X.cw_eid AND rel_read_permission0.eid_to=Y.cw_eid)'''),
   399 WHERE _X.cw_name=CWGroup AND _Y.cw_eid IN(1, 2, 3) AND NOT EXISTS(SELECT 1 FROM read_permission_relation AS rel_read_permission0 WHERE rel_read_permission0.eid_from=_X.cw_eid AND rel_read_permission0.eid_to=_Y.cw_eid)'''),
   396 
   400 
   397     # should generate the same query as above
   401     # should generate the same query as above
   398     ('DISTINCT Any X,Y WHERE X name "CWGroup", Y eid IN(1, 2, 3), NOT X read_permission Y',
   402     ('DISTINCT Any X,Y WHERE X name "CWGroup", Y eid IN(1, 2, 3), NOT X read_permission Y',
   399      '''SELECT DISTINCT X.cw_eid, Y.cw_eid
   403      '''SELECT DISTINCT _X.cw_eid, _Y.cw_eid
   400 FROM cw_CWEType AS X, cw_CWGroup AS Y
   404 FROM cw_CWEType AS _X, cw_CWGroup AS _Y
   401 WHERE X.cw_name=CWGroup AND Y.cw_eid IN(1, 2, 3) AND NOT EXISTS(SELECT 1 FROM read_permission_relation AS rel_read_permission0 WHERE rel_read_permission0.eid_from=X.cw_eid AND rel_read_permission0.eid_to=Y.cw_eid)
   405 WHERE _X.cw_name=CWGroup AND _Y.cw_eid IN(1, 2, 3) AND NOT EXISTS(SELECT 1 FROM read_permission_relation AS rel_read_permission0 WHERE rel_read_permission0.eid_from=_X.cw_eid AND rel_read_permission0.eid_to=_Y.cw_eid)
   402 UNION
   406 UNION
   403 SELECT DISTINCT X.cw_eid, Y.cw_eid
   407 SELECT DISTINCT _X.cw_eid, _Y.cw_eid
   404 FROM cw_CWEType AS X, cw_RQLExpression AS Y
   408 FROM cw_CWEType AS _X, cw_RQLExpression AS _Y
   405 WHERE X.cw_name=CWGroup AND Y.cw_eid IN(1, 2, 3) AND NOT EXISTS(SELECT 1 FROM read_permission_relation AS rel_read_permission0 WHERE rel_read_permission0.eid_from=X.cw_eid AND rel_read_permission0.eid_to=Y.cw_eid)
   409 WHERE _X.cw_name=CWGroup AND _Y.cw_eid IN(1, 2, 3) AND NOT EXISTS(SELECT 1 FROM read_permission_relation AS rel_read_permission0 WHERE rel_read_permission0.eid_from=_X.cw_eid AND rel_read_permission0.eid_to=_Y.cw_eid)
   406 UNION
   410 UNION
   407 SELECT DISTINCT X.cw_eid, Y.cw_eid
   411 SELECT DISTINCT _X.cw_eid, _Y.cw_eid
   408 FROM cw_CWGroup AS Y, cw_CWRType AS X
   412 FROM cw_CWGroup AS _Y, cw_CWRType AS _X
   409 WHERE X.cw_name=CWGroup AND Y.cw_eid IN(1, 2, 3) AND NOT EXISTS(SELECT 1 FROM read_permission_relation AS rel_read_permission0 WHERE rel_read_permission0.eid_from=X.cw_eid AND rel_read_permission0.eid_to=Y.cw_eid)
   413 WHERE _X.cw_name=CWGroup AND _Y.cw_eid IN(1, 2, 3) AND NOT EXISTS(SELECT 1 FROM read_permission_relation AS rel_read_permission0 WHERE rel_read_permission0.eid_from=_X.cw_eid AND rel_read_permission0.eid_to=_Y.cw_eid)
   410 UNION
   414 UNION
   411 SELECT DISTINCT X.cw_eid, Y.cw_eid
   415 SELECT DISTINCT _X.cw_eid, _Y.cw_eid
   412 FROM cw_CWRType AS X, cw_RQLExpression AS Y
   416 FROM cw_CWRType AS _X, cw_RQLExpression AS _Y
   413 WHERE X.cw_name=CWGroup AND Y.cw_eid IN(1, 2, 3) AND NOT EXISTS(SELECT 1 FROM read_permission_relation AS rel_read_permission0 WHERE rel_read_permission0.eid_from=X.cw_eid AND rel_read_permission0.eid_to=Y.cw_eid)'''),
   417 WHERE _X.cw_name=CWGroup AND _Y.cw_eid IN(1, 2, 3) AND NOT EXISTS(SELECT 1 FROM read_permission_relation AS rel_read_permission0 WHERE rel_read_permission0.eid_from=_X.cw_eid AND rel_read_permission0.eid_to=_Y.cw_eid)'''),
   414 
   418 
   415     # neged relation, can't be inveriant
   419     # neged relation, can't be inveriant
   416     ('Any X,Y WHERE X name "CWGroup", Y eid IN(1, 2, 3), NOT X read_permission Y',
   420     ('Any X,Y WHERE X name "CWGroup", Y eid IN(1, 2, 3), NOT X read_permission Y',
   417      '''SELECT X.cw_eid, Y.cw_eid
   421      '''SELECT _X.cw_eid, _Y.cw_eid
   418 FROM cw_CWEType AS X, cw_CWGroup AS Y
   422 FROM cw_CWEType AS _X, cw_CWGroup AS _Y
   419 WHERE X.cw_name=CWGroup AND Y.cw_eid IN(1, 2, 3) AND NOT EXISTS(SELECT 1 FROM read_permission_relation AS rel_read_permission0 WHERE rel_read_permission0.eid_from=X.cw_eid AND rel_read_permission0.eid_to=Y.cw_eid)
   423 WHERE _X.cw_name=CWGroup AND _Y.cw_eid IN(1, 2, 3) AND NOT EXISTS(SELECT 1 FROM read_permission_relation AS rel_read_permission0 WHERE rel_read_permission0.eid_from=_X.cw_eid AND rel_read_permission0.eid_to=_Y.cw_eid)
   420 UNION ALL
   424 UNION ALL
   421 SELECT X.cw_eid, Y.cw_eid
   425 SELECT _X.cw_eid, _Y.cw_eid
   422 FROM cw_CWEType AS X, cw_RQLExpression AS Y
   426 FROM cw_CWEType AS _X, cw_RQLExpression AS _Y
   423 WHERE X.cw_name=CWGroup AND Y.cw_eid IN(1, 2, 3) AND NOT EXISTS(SELECT 1 FROM read_permission_relation AS rel_read_permission0 WHERE rel_read_permission0.eid_from=X.cw_eid AND rel_read_permission0.eid_to=Y.cw_eid)
   427 WHERE _X.cw_name=CWGroup AND _Y.cw_eid IN(1, 2, 3) AND NOT EXISTS(SELECT 1 FROM read_permission_relation AS rel_read_permission0 WHERE rel_read_permission0.eid_from=_X.cw_eid AND rel_read_permission0.eid_to=_Y.cw_eid)
   424 UNION ALL
   428 UNION ALL
   425 SELECT X.cw_eid, Y.cw_eid
   429 SELECT _X.cw_eid, _Y.cw_eid
   426 FROM cw_CWGroup AS Y, cw_CWRType AS X
   430 FROM cw_CWGroup AS _Y, cw_CWRType AS _X
   427 WHERE X.cw_name=CWGroup AND Y.cw_eid IN(1, 2, 3) AND NOT EXISTS(SELECT 1 FROM read_permission_relation AS rel_read_permission0 WHERE rel_read_permission0.eid_from=X.cw_eid AND rel_read_permission0.eid_to=Y.cw_eid)
   431 WHERE _X.cw_name=CWGroup AND _Y.cw_eid IN(1, 2, 3) AND NOT EXISTS(SELECT 1 FROM read_permission_relation AS rel_read_permission0 WHERE rel_read_permission0.eid_from=_X.cw_eid AND rel_read_permission0.eid_to=_Y.cw_eid)
   428 UNION ALL
   432 UNION ALL
   429 SELECT X.cw_eid, Y.cw_eid
   433 SELECT _X.cw_eid, _Y.cw_eid
   430 FROM cw_CWRType AS X, cw_RQLExpression AS Y
   434 FROM cw_CWRType AS _X, cw_RQLExpression AS _Y
   431 WHERE X.cw_name=CWGroup AND Y.cw_eid IN(1, 2, 3) AND NOT EXISTS(SELECT 1 FROM read_permission_relation AS rel_read_permission0 WHERE rel_read_permission0.eid_from=X.cw_eid AND rel_read_permission0.eid_to=Y.cw_eid)'''),
   435 WHERE _X.cw_name=CWGroup AND _Y.cw_eid IN(1, 2, 3) AND NOT EXISTS(SELECT 1 FROM read_permission_relation AS rel_read_permission0 WHERE rel_read_permission0.eid_from=_X.cw_eid AND rel_read_permission0.eid_to=_Y.cw_eid)'''),
   432 
   436 
   433     ('Any MAX(X)+MIN(X), N GROUPBY N WHERE X name N, X is IN (Basket, Folder, Tag);',
   437     ('Any MAX(X)+MIN(X), N GROUPBY N WHERE X name N, X is IN (Basket, Folder, Tag);',
   434      '''SELECT (MAX(T1.C0) + MIN(T1.C0)), T1.C1 FROM (SELECT X.cw_eid AS C0, X.cw_name AS C1
   438      '''SELECT (MAX(T1.C0) + MIN(T1.C0)), T1.C1 FROM (SELECT _X.cw_eid AS C0, _X.cw_name AS C1
   435 FROM cw_Basket AS X
   439 FROM cw_Basket AS _X
   436 UNION ALL
   440 UNION ALL
   437 SELECT X.cw_eid AS C0, X.cw_name AS C1
   441 SELECT _X.cw_eid AS C0, _X.cw_name AS C1
   438 FROM cw_Folder AS X
   442 FROM cw_Folder AS _X
   439 UNION ALL
   443 UNION ALL
   440 SELECT X.cw_eid AS C0, X.cw_name AS C1
   444 SELECT _X.cw_eid AS C0, _X.cw_name AS C1
   441 FROM cw_Tag AS X) AS T1
   445 FROM cw_Tag AS _X) AS T1
   442 GROUP BY T1.C1'''),
   446 GROUP BY T1.C1'''),
   443 
   447 
   444     ('Any MAX(X)+MIN(LENGTH(D)), N GROUPBY N ORDERBY 1, N, DF WHERE X data_name N, X data D, X data_format DF;',
   448     ('Any MAX(X)+MIN(LENGTH(D)), N GROUPBY N ORDERBY 1, N, DF WHERE X data_name N, X data D, X data_format DF;',
   445      '''SELECT (MAX(T1.C1) + MIN(LENGTH(T1.C0))), T1.C2 FROM (SELECT X.cw_data AS C0, X.cw_eid AS C1, X.cw_data_name AS C2, X.cw_data_format AS C3
   449      '''SELECT (MAX(T1.C1) + MIN(LENGTH(T1.C0))), T1.C2 FROM (SELECT _X.cw_data AS C0, _X.cw_eid AS C1, _X.cw_data_name AS C2, _X.cw_data_format AS C3
   446 FROM cw_File AS X
   450 FROM cw_File AS _X
   447 UNION ALL
   451 UNION ALL
   448 SELECT X.cw_data AS C0, X.cw_eid AS C1, X.cw_data_name AS C2, X.cw_data_format AS C3
   452 SELECT _X.cw_data AS C0, _X.cw_eid AS C1, _X.cw_data_name AS C2, _X.cw_data_format AS C3
   449 FROM cw_Image AS X) AS T1
   453 FROM cw_Image AS _X) AS T1
   450 GROUP BY T1.C2
   454 GROUP BY T1.C2,T1.C3
   451 ORDER BY 1,2,T1.C3'''),
   455 ORDER BY 1,2,T1.C3'''),
   452 
   456 
   453     ('DISTINCT Any S ORDERBY R WHERE A is Affaire, A sujet S, A ref R',
   457     ('DISTINCT Any S ORDERBY R WHERE A is Affaire, A sujet S, A ref R',
   454      '''SELECT T1.C0 FROM (SELECT DISTINCT A.cw_sujet AS C0, A.cw_ref AS C1
   458      '''SELECT T1.C0 FROM (SELECT DISTINCT _A.cw_sujet AS C0, _A.cw_ref AS C1
   455 FROM cw_Affaire AS A
   459 FROM cw_Affaire AS _A
   456 ORDER BY 2) AS T1'''),
   460 ORDER BY 2) AS T1'''),
   457 
   461 
   458     ('DISTINCT Any MAX(X)+MIN(LENGTH(D)), N GROUPBY N ORDERBY 2, DF WHERE X data_name N, X data D, X data_format DF;',
   462     ('DISTINCT Any MAX(X)+MIN(LENGTH(D)), N GROUPBY N ORDERBY 2, DF WHERE X data_name N, X data D, X data_format DF;',
   459      '''SELECT T1.C0,T1.C1 FROM (SELECT DISTINCT (MAX(T1.C1) + MIN(LENGTH(T1.C0))) AS C0, T1.C2 AS C1, T1.C3 AS C2 FROM (SELECT DISTINCT X.cw_data AS C0, X.cw_eid AS C1, X.cw_data_name AS C2, X.cw_data_format AS C3
   463      '''SELECT T1.C0,T1.C1 FROM (SELECT DISTINCT (MAX(T1.C1) + MIN(LENGTH(T1.C0))) AS C0, T1.C2 AS C1, T1.C3 AS C2 FROM (SELECT DISTINCT _X.cw_data AS C0, _X.cw_eid AS C1, _X.cw_data_name AS C2, _X.cw_data_format AS C3
   460 FROM cw_File AS X
   464 FROM cw_File AS _X
   461 UNION
   465 UNION
   462 SELECT DISTINCT X.cw_data AS C0, X.cw_eid AS C1, X.cw_data_name AS C2, X.cw_data_format AS C3
   466 SELECT DISTINCT _X.cw_data AS C0, _X.cw_eid AS C1, _X.cw_data_name AS C2, _X.cw_data_format AS C3
   463 FROM cw_Image AS X) AS T1
   467 FROM cw_Image AS _X) AS T1
   464 GROUP BY T1.C2,T1.C3
   468 GROUP BY T1.C2,T1.C3
   465 ORDER BY 2,3) AS T1
   469 ORDER BY 2,3) AS T1
   466 '''),
   470 '''),
   467 
   471 
   468     # ambiguity in EXISTS() -> should union the sub-query
   472     # ambiguity in EXISTS() -> should union the sub-query
   469     ('Any T WHERE T is Tag, NOT T name in ("t1", "t2"), EXISTS(T tags X, X is IN (CWUser, CWGroup))',
   473     ('Any T WHERE T is Tag, NOT T name in ("t1", "t2"), EXISTS(T tags X, X is IN (CWUser, CWGroup))',
   470      '''SELECT T.cw_eid
   474      '''SELECT _T.cw_eid
   471 FROM cw_Tag AS T
   475 FROM cw_Tag AS _T
   472 WHERE NOT (T.cw_name IN(t1, t2)) AND EXISTS(SELECT 1 FROM tags_relation AS rel_tags0, cw_CWGroup AS X WHERE rel_tags0.eid_from=T.cw_eid AND rel_tags0.eid_to=X.cw_eid UNION SELECT 1 FROM tags_relation AS rel_tags1, cw_CWUser AS X WHERE rel_tags1.eid_from=T.cw_eid AND rel_tags1.eid_to=X.cw_eid)'''),
   476 WHERE NOT (_T.cw_name IN(t1, t2)) AND EXISTS(SELECT 1 FROM tags_relation AS rel_tags0, cw_CWGroup AS _X WHERE rel_tags0.eid_from=_T.cw_eid AND rel_tags0.eid_to=_X.cw_eid UNION SELECT 1 FROM tags_relation AS rel_tags1, cw_CWUser AS _X WHERE rel_tags1.eid_from=_T.cw_eid AND rel_tags1.eid_to=_X.cw_eid)'''),
   473 
   477 
   474     # must not use a relation in EXISTS scope to inline a variable
   478     # must not use a relation in EXISTS scope to inline a variable
   475     ('Any U WHERE U eid IN (1,2), EXISTS(X owned_by U)',
   479     ('Any U WHERE U eid IN (1,2), EXISTS(X owned_by U)',
   476      '''SELECT U.cw_eid
   480      '''SELECT _U.cw_eid
   477 FROM cw_CWUser AS U
   481 FROM cw_CWUser AS _U
   478 WHERE U.cw_eid IN(1, 2) AND EXISTS(SELECT 1 FROM owned_by_relation AS rel_owned_by0 WHERE rel_owned_by0.eid_to=U.cw_eid)'''),
   482 WHERE _U.cw_eid IN(1, 2) AND EXISTS(SELECT 1 FROM owned_by_relation AS rel_owned_by0 WHERE rel_owned_by0.eid_to=_U.cw_eid)'''),
   479 
   483 
   480     ('Any U WHERE EXISTS(U eid IN (1,2), X owned_by U)',
   484     ('Any U WHERE EXISTS(U eid IN (1,2), X owned_by U)',
   481      '''SELECT U.cw_eid
   485      '''SELECT _U.cw_eid
   482 FROM cw_CWUser AS U
   486 FROM cw_CWUser AS _U
   483 WHERE EXISTS(SELECT 1 FROM owned_by_relation AS rel_owned_by0 WHERE U.cw_eid IN(1, 2) AND rel_owned_by0.eid_to=U.cw_eid)'''),
   487 WHERE EXISTS(SELECT 1 FROM owned_by_relation AS rel_owned_by0 WHERE _U.cw_eid IN(1, 2) AND rel_owned_by0.eid_to=_U.cw_eid)'''),
   484 
   488 
   485     ('Any COUNT(U) WHERE EXISTS (P owned_by U, P is IN (Note, Affaire))',
   489     ('Any COUNT(U) WHERE EXISTS (P owned_by U, P is IN (Note, Affaire))',
   486      '''SELECT COUNT(U.cw_eid)
   490      '''SELECT COUNT(_U.cw_eid)
   487 FROM cw_CWUser AS U
   491 FROM cw_CWUser AS _U
   488 WHERE EXISTS(SELECT 1 FROM owned_by_relation AS rel_owned_by0, cw_Affaire AS P WHERE rel_owned_by0.eid_from=P.cw_eid AND rel_owned_by0.eid_to=U.cw_eid UNION SELECT 1 FROM owned_by_relation AS rel_owned_by1, cw_Note AS P WHERE rel_owned_by1.eid_from=P.cw_eid AND rel_owned_by1.eid_to=U.cw_eid)'''),
   492 WHERE EXISTS(SELECT 1 FROM owned_by_relation AS rel_owned_by0, cw_Affaire AS _P WHERE rel_owned_by0.eid_from=_P.cw_eid AND rel_owned_by0.eid_to=_U.cw_eid UNION SELECT 1 FROM owned_by_relation AS rel_owned_by1, cw_Note AS _P WHERE rel_owned_by1.eid_from=_P.cw_eid AND rel_owned_by1.eid_to=_U.cw_eid)'''),
   489 
   493 
   490     ('Any MAX(X)',
   494     ('Any MAX(X)',
   491      '''SELECT MAX(X.eid)
   495      '''SELECT MAX(_X.eid)
   492 FROM entities AS X'''),
   496 FROM entities AS _X'''),
   493 
   497 
   494     ('Any MAX(X) WHERE X is Note',
   498     ('Any MAX(X) WHERE X is Note',
   495      '''SELECT MAX(X.cw_eid)
   499      '''SELECT MAX(_X.cw_eid)
   496 FROM cw_Note AS X'''),
   500 FROM cw_Note AS _X'''),
   497 
   501 
   498     ('Any X WHERE X eid > 12',
   502     ('Any X WHERE X eid > 12',
   499      '''SELECT X.eid
   503      '''SELECT _X.eid
   500 FROM entities AS X
   504 FROM entities AS _X
   501 WHERE X.eid>12'''),
   505 WHERE _X.eid>12'''),
   502 
   506 
   503     ('Any X WHERE X eid > 12, X is Note',
   507     ('Any X WHERE X eid > 12, X is Note',
   504      """SELECT X.eid
   508      """SELECT _X.eid
   505 FROM entities AS X
   509 FROM entities AS _X
   506 WHERE X.type='Note' AND X.eid>12"""),
   510 WHERE _X.type='Note' AND _X.eid>12"""),
   507 
   511 
   508     ('Any X, T WHERE X eid > 12, X title T, X is IN (Bookmark, Card)',
   512     ('Any X, T WHERE X eid > 12, X title T, X is IN (Bookmark, Card)',
   509      """SELECT X.cw_eid, X.cw_title
   513      """SELECT _X.cw_eid, _X.cw_title
   510 FROM cw_Bookmark AS X
   514 FROM cw_Bookmark AS _X
   511 WHERE X.cw_eid>12
   515 WHERE _X.cw_eid>12
   512 UNION ALL
   516 UNION ALL
   513 SELECT X.cw_eid, X.cw_title
   517 SELECT _X.cw_eid, _X.cw_title
   514 FROM cw_Card AS X
   518 FROM cw_Card AS _X
   515 WHERE X.cw_eid>12"""),
   519 WHERE _X.cw_eid>12"""),
   516 
   520 
   517     ('Any X',
   521     ('Any X',
   518      '''SELECT X.eid
   522      '''SELECT _X.eid
   519 FROM entities AS X'''),
   523 FROM entities AS _X'''),
   520 
   524 
   521     ('Any X GROUPBY X WHERE X eid 12',
   525     ('Any X GROUPBY X WHERE X eid 12',
   522      '''SELECT 12'''),
   526      '''SELECT 12'''),
   523 
   527 
   524     ('Any X GROUPBY X ORDERBY Y WHERE X eid 12, X login Y',
   528     ('Any X GROUPBY X ORDERBY Y WHERE X eid 12, X login Y',
   525      '''SELECT X.cw_eid
   529      '''SELECT _X.cw_eid
   526 FROM cw_CWUser AS X
   530 FROM cw_CWUser AS _X
   527 WHERE X.cw_eid=12
   531 WHERE _X.cw_eid=12
   528 GROUP BY X.cw_eid
   532 GROUP BY _X.cw_eid,_X.cw_login
   529 ORDER BY X.cw_login'''),
   533 ORDER BY _X.cw_login'''),
   530 
   534 
   531     ('Any U,COUNT(X) GROUPBY U WHERE U eid 12, X owned_by U HAVING COUNT(X) > 10',
   535     ('Any U,COUNT(X) GROUPBY U WHERE U eid 12, X owned_by U HAVING COUNT(X) > 10',
   532      '''SELECT rel_owned_by0.eid_to, COUNT(rel_owned_by0.eid_from)
   536      '''SELECT rel_owned_by0.eid_to, COUNT(rel_owned_by0.eid_from)
   533 FROM owned_by_relation AS rel_owned_by0
   537 FROM owned_by_relation AS rel_owned_by0
   534 WHERE rel_owned_by0.eid_to=12
   538 WHERE rel_owned_by0.eid_to=12
   535 GROUP BY rel_owned_by0.eid_to
   539 GROUP BY rel_owned_by0.eid_to
   536 HAVING COUNT(rel_owned_by0.eid_from)>10'''),
   540 HAVING COUNT(rel_owned_by0.eid_from)>10'''),
   537 
   541 
   538     ('DISTINCT Any X ORDERBY stockproc(X) WHERE U login X',
   542     ('DISTINCT Any X ORDERBY stockproc(X) WHERE U login X',
   539      '''SELECT T1.C0 FROM (SELECT DISTINCT U.cw_login AS C0, STOCKPROC(U.cw_login) AS C1
   543      '''SELECT T1.C0 FROM (SELECT DISTINCT _U.cw_login AS C0, STOCKPROC(_U.cw_login) AS C1
   540 FROM cw_CWUser AS U
   544 FROM cw_CWUser AS _U
   541 ORDER BY 2) AS T1'''),
   545 ORDER BY 2) AS T1'''),
   542 
   546 
   543     ('DISTINCT Any X ORDERBY Y WHERE B bookmarked_by X, X login Y',
   547     ('DISTINCT Any X ORDERBY Y WHERE B bookmarked_by X, X login Y',
   544      '''SELECT T1.C0 FROM (SELECT DISTINCT X.cw_eid AS C0, X.cw_login AS C1
   548      '''SELECT T1.C0 FROM (SELECT DISTINCT _X.cw_eid AS C0, _X.cw_login AS C1
   545 FROM bookmarked_by_relation AS rel_bookmarked_by0, cw_CWUser AS X
   549 FROM bookmarked_by_relation AS rel_bookmarked_by0, cw_CWUser AS _X
   546 WHERE rel_bookmarked_by0.eid_to=X.cw_eid
   550 WHERE rel_bookmarked_by0.eid_to=_X.cw_eid
   547 ORDER BY 2) AS T1'''),
   551 ORDER BY 2) AS T1'''),
   548 
   552 
   549     ('DISTINCT Any X ORDERBY SN WHERE X in_state S, S name SN',
   553     ('DISTINCT Any X ORDERBY SN WHERE X in_state S, S name SN',
   550      '''SELECT T1.C0 FROM (SELECT DISTINCT X.cw_eid AS C0, S.cw_name AS C1
   554      '''SELECT T1.C0 FROM (SELECT DISTINCT _X.cw_eid AS C0, _S.cw_name AS C1
   551 FROM cw_Affaire AS X, cw_State AS S
   555 FROM cw_Affaire AS _X, cw_State AS _S
   552 WHERE X.cw_in_state=S.cw_eid
   556 WHERE _X.cw_in_state=_S.cw_eid
   553 UNION
   557 UNION
   554 SELECT DISTINCT X.cw_eid AS C0, S.cw_name AS C1
   558 SELECT DISTINCT _X.cw_eid AS C0, _S.cw_name AS C1
   555 FROM cw_CWUser AS X, cw_State AS S
   559 FROM cw_CWUser AS _X, cw_State AS _S
   556 WHERE X.cw_in_state=S.cw_eid
   560 WHERE _X.cw_in_state=_S.cw_eid
   557 UNION
   561 UNION
   558 SELECT DISTINCT X.cw_eid AS C0, S.cw_name AS C1
   562 SELECT DISTINCT _X.cw_eid AS C0, _S.cw_name AS C1
   559 FROM cw_Note AS X, cw_State AS S
   563 FROM cw_Note AS _X, cw_State AS _S
   560 WHERE X.cw_in_state=S.cw_eid
   564 WHERE _X.cw_in_state=_S.cw_eid
   561 ORDER BY 2) AS T1'''),
   565 ORDER BY 2) AS T1'''),
   562 
   566 
   563     ('Any O,AA,AB,AC ORDERBY AC DESC '
   567     ('Any O,AA,AB,AC ORDERBY AC DESC '
   564      'WHERE NOT S use_email O, S eid 1, O is EmailAddress, O address AA, O alias AB, O modification_date AC, '
   568      'WHERE NOT S use_email O, S eid 1, O is EmailAddress, O address AA, O alias AB, O modification_date AC, '
   565      'EXISTS(A use_email O, EXISTS(A identity B, NOT B in_group D, D name "guests", D is CWGroup), A is CWUser), B eid 2',
   569      'EXISTS(A use_email O, EXISTS(A identity B, NOT B in_group D, D name "guests", D is CWGroup), A is CWUser), B eid 2',
   566      '''SELECT O.cw_eid, O.cw_address, O.cw_alias, O.cw_modification_date
   570      '''SELECT _O.cw_eid, _O.cw_address, _O.cw_alias, _O.cw_modification_date
   567 FROM cw_EmailAddress AS O
   571 FROM cw_EmailAddress AS _O
   568 WHERE NOT EXISTS(SELECT 1 FROM use_email_relation AS rel_use_email0 WHERE rel_use_email0.eid_from=1 AND rel_use_email0.eid_to=O.cw_eid) AND EXISTS(SELECT 1 FROM use_email_relation AS rel_use_email1 WHERE rel_use_email1.eid_to=O.cw_eid AND EXISTS(SELECT 1 FROM cw_CWGroup AS D WHERE rel_use_email1.eid_from=2 AND NOT EXISTS(SELECT 1 FROM in_group_relation AS rel_in_group2 WHERE rel_in_group2.eid_from=2 AND rel_in_group2.eid_to=D.cw_eid) AND D.cw_name=guests))
   572 WHERE NOT EXISTS(SELECT 1 FROM use_email_relation AS rel_use_email0 WHERE rel_use_email0.eid_from=1 AND rel_use_email0.eid_to=_O.cw_eid) AND EXISTS(SELECT 1 FROM use_email_relation AS rel_use_email1 WHERE rel_use_email1.eid_to=_O.cw_eid AND EXISTS(SELECT 1 FROM cw_CWGroup AS _D WHERE rel_use_email1.eid_from=2 AND NOT EXISTS(SELECT 1 FROM in_group_relation AS rel_in_group2 WHERE rel_in_group2.eid_from=2 AND rel_in_group2.eid_to=_D.cw_eid) AND _D.cw_name=guests))
   569 ORDER BY 4 DESC'''),
   573 ORDER BY 4 DESC'''),
   570 
   574 
   571 
   575 
   572     ("Any X WHERE X eid 0, X eid 0",
   576     ("Any X WHERE X eid 0, X eid 0",
   573      '''SELECT 0'''),
   577      '''SELECT 0'''),
   574 
   578 
   575     ("Any X WHERE X eid 0, X eid 0, X test TRUE",
   579     ("Any X WHERE X eid 0, X eid 0, X test TRUE",
   576      '''SELECT X.cw_eid
   580      '''SELECT _X.cw_eid
   577 FROM cw_Personne AS X
   581 FROM cw_Personne AS _X
   578 WHERE X.cw_eid=0 AND X.cw_eid=0 AND X.cw_test='''),
   582 WHERE _X.cw_eid=0 AND _X.cw_eid=0 AND _X.cw_test=TRUE'''),
       
   583 
       
   584     ("Any X,GROUP_CONCAT(TN) GROUPBY X ORDERBY XN WHERE T tags X, X name XN, T name TN, X is CWGroup",
       
   585      '''SELECT _X.cw_eid, GROUP_CONCAT(_T.cw_name)
       
   586 FROM cw_CWGroup AS _X, cw_Tag AS _T, tags_relation AS rel_tags0
       
   587 WHERE rel_tags0.eid_from=_T.cw_eid AND rel_tags0.eid_to=_X.cw_eid
       
   588 GROUP BY _X.cw_eid,_X.cw_name
       
   589 ORDER BY _X.cw_name'''),
       
   590 
       
   591     ("Any X,GROUP_CONCAT(TN) GROUPBY X ORDERBY XN WHERE T tags X, X name XN, T name TN",
       
   592      '''SELECT T1.C0, GROUP_CONCAT(T1.C1) FROM (SELECT _X.cw_eid AS C0, _T.cw_name AS C1, _X.cw_name AS C2
       
   593 FROM cw_CWGroup AS _X, cw_Tag AS _T, tags_relation AS rel_tags0
       
   594 WHERE rel_tags0.eid_from=_T.cw_eid AND rel_tags0.eid_to=_X.cw_eid
       
   595 UNION ALL
       
   596 SELECT _X.cw_eid AS C0, _T.cw_name AS C1, _X.cw_name AS C2
       
   597 FROM cw_State AS _X, cw_Tag AS _T, tags_relation AS rel_tags0
       
   598 WHERE rel_tags0.eid_from=_T.cw_eid AND rel_tags0.eid_to=_X.cw_eid
       
   599 UNION ALL
       
   600 SELECT _X.cw_eid AS C0, _T.cw_name AS C1, _X.cw_name AS C2
       
   601 FROM cw_Tag AS _T, cw_Tag AS _X, tags_relation AS rel_tags0
       
   602 WHERE rel_tags0.eid_from=_T.cw_eid AND rel_tags0.eid_to=_X.cw_eid) AS T1
       
   603 GROUP BY T1.C0,T1.C2
       
   604 ORDER BY T1.C2'''),
       
   605 
   579     ]
   606     ]
   580 
   607 
   581 MULTIPLE_SEL = [
   608 MULTIPLE_SEL = [
   582     ("DISTINCT Any X,Y where P is Personne, P nom X , P prenom Y;",
   609     ("DISTINCT Any X,Y where P is Personne, P nom X , P prenom Y;",
   583      '''SELECT DISTINCT P.cw_nom, P.cw_prenom
   610      '''SELECT DISTINCT _P.cw_nom, _P.cw_prenom
   584 FROM cw_Personne AS P'''),
   611 FROM cw_Personne AS _P'''),
   585     ("Any X,Y where P is Personne, P nom X , P prenom Y, not P nom NULL;",
   612     ("Any X,Y where P is Personne, P nom X , P prenom Y, not P nom NULL;",
   586      '''SELECT P.cw_nom, P.cw_prenom
   613      '''SELECT _P.cw_nom, _P.cw_prenom
   587 FROM cw_Personne AS P
   614 FROM cw_Personne AS _P
   588 WHERE NOT (P.cw_nom IS NULL)'''),
   615 WHERE NOT (_P.cw_nom IS NULL)'''),
   589     ("Personne X,Y where X nom NX, Y nom NX, X eid XE, not Y eid XE",
   616     ("Personne X,Y where X nom NX, Y nom NX, X eid XE, not Y eid XE",
   590      '''SELECT X.cw_eid, Y.cw_eid
   617      '''SELECT _X.cw_eid, _Y.cw_eid
   591 FROM cw_Personne AS X, cw_Personne AS Y
   618 FROM cw_Personne AS _X, cw_Personne AS _Y
   592 WHERE Y.cw_nom=X.cw_nom AND NOT (Y.cw_eid=X.cw_eid)''')
   619 WHERE _Y.cw_nom=_X.cw_nom AND NOT (_Y.cw_eid=_X.cw_eid)''')
   593     ]
   620     ]
   594 
   621 
   595 NEGATIONS = [
   622 NEGATIONS = [
   596     ("Personne X WHERE NOT X evaluee Y;",
   623     ("Personne X WHERE NOT X evaluee Y;",
   597      '''SELECT X.cw_eid
   624      '''SELECT _X.cw_eid
   598 FROM cw_Personne AS X
   625 FROM cw_Personne AS _X
   599 WHERE NOT EXISTS(SELECT 1 FROM evaluee_relation AS rel_evaluee0 WHERE rel_evaluee0.eid_from=X.cw_eid)'''),
   626 WHERE NOT EXISTS(SELECT 1 FROM evaluee_relation AS rel_evaluee0 WHERE rel_evaluee0.eid_from=_X.cw_eid)'''),
   600 
   627 
   601     ("Note N WHERE NOT X evaluee N, X eid 0",
   628     ("Note N WHERE NOT X evaluee N, X eid 0",
   602      '''SELECT N.cw_eid
   629      '''SELECT _N.cw_eid
   603 FROM cw_Note AS N
   630 FROM cw_Note AS _N
   604 WHERE NOT EXISTS(SELECT 1 FROM evaluee_relation AS rel_evaluee0 WHERE rel_evaluee0.eid_from=0 AND rel_evaluee0.eid_to=N.cw_eid)'''),
   631 WHERE NOT EXISTS(SELECT 1 FROM evaluee_relation AS rel_evaluee0 WHERE rel_evaluee0.eid_from=0 AND rel_evaluee0.eid_to=_N.cw_eid)'''),
   605 
   632 
   606     ('Any X WHERE NOT X travaille S, X is Personne',
   633     ('Any X WHERE NOT X travaille S, X is Personne',
   607      '''SELECT X.cw_eid
   634      '''SELECT _X.cw_eid
   608 FROM cw_Personne AS X
   635 FROM cw_Personne AS _X
   609 WHERE NOT EXISTS(SELECT 1 FROM travaille_relation AS rel_travaille0 WHERE rel_travaille0.eid_from=X.cw_eid)'''),
   636 WHERE NOT EXISTS(SELECT 1 FROM travaille_relation AS rel_travaille0 WHERE rel_travaille0.eid_from=_X.cw_eid)'''),
   610 
   637 
   611     ("Personne P where not P datenaiss TODAY",
   638     ("Personne P where not P datenaiss TODAY",
   612      '''SELECT P.cw_eid
   639      '''SELECT _P.cw_eid
   613 FROM cw_Personne AS P
   640 FROM cw_Personne AS _P
   614 WHERE NOT (DATE(P.cw_datenaiss)=CURRENT_DATE)'''),
   641 WHERE NOT (DATE(_P.cw_datenaiss)=CURRENT_DATE)'''),
   615 
   642 
   616     ("Personne P where NOT P concerne A",
   643     ("Personne P where NOT P concerne A",
   617      '''SELECT P.cw_eid
   644      '''SELECT _P.cw_eid
   618 FROM cw_Personne AS P
   645 FROM cw_Personne AS _P
   619 WHERE NOT EXISTS(SELECT 1 FROM concerne_relation AS rel_concerne0 WHERE rel_concerne0.eid_from=P.cw_eid)'''),
   646 WHERE NOT EXISTS(SELECT 1 FROM concerne_relation AS rel_concerne0 WHERE rel_concerne0.eid_from=_P.cw_eid)'''),
   620 
   647 
   621     ("Affaire A where not P concerne A",
   648     ("Affaire A where not P concerne A",
   622      '''SELECT A.cw_eid
   649      '''SELECT _A.cw_eid
   623 FROM cw_Affaire AS A
   650 FROM cw_Affaire AS _A
   624 WHERE NOT EXISTS(SELECT 1 FROM concerne_relation AS rel_concerne0 WHERE rel_concerne0.eid_to=A.cw_eid)'''),
   651 WHERE NOT EXISTS(SELECT 1 FROM concerne_relation AS rel_concerne0 WHERE rel_concerne0.eid_to=_A.cw_eid)'''),
   625     ("Personne P where not P concerne A, A sujet ~= 'TEST%'",
   652     ("Personne P where not P concerne A, A sujet ~= 'TEST%'",
   626      '''SELECT P.cw_eid
   653      '''SELECT _P.cw_eid
   627 FROM cw_Affaire AS A, cw_Personne AS P
   654 FROM cw_Affaire AS _A, cw_Personne AS _P
   628 WHERE NOT EXISTS(SELECT 1 FROM concerne_relation AS rel_concerne0 WHERE rel_concerne0.eid_from=P.cw_eid AND rel_concerne0.eid_to=A.cw_eid) AND A.cw_sujet ILIKE TEST%'''),
   655 WHERE NOT EXISTS(SELECT 1 FROM concerne_relation AS rel_concerne0 WHERE rel_concerne0.eid_from=_P.cw_eid AND rel_concerne0.eid_to=_A.cw_eid) AND _A.cw_sujet ILIKE TEST%'''),
   629 
   656 
   630     ('Any S WHERE NOT T eid 28258, T tags S',
   657     ('Any S WHERE NOT T eid 28258, T tags S',
   631      '''SELECT rel_tags0.eid_to
   658      '''SELECT rel_tags0.eid_to
   632 FROM tags_relation AS rel_tags0
   659 FROM tags_relation AS rel_tags0
   633 WHERE NOT (rel_tags0.eid_from=28258)'''),
   660 WHERE NOT (rel_tags0.eid_from=28258)'''),
   634 
   661 
   635     ('Any S WHERE T is Tag, T name TN, NOT T eid 28258, T tags S, S name SN',
   662     ('Any S WHERE T is Tag, T name TN, NOT T eid 28258, T tags S, S name SN',
   636      '''SELECT S.cw_eid
   663      '''SELECT _S.cw_eid
   637 FROM cw_CWGroup AS S, cw_Tag AS T, tags_relation AS rel_tags0
   664 FROM cw_CWGroup AS _S, cw_Tag AS _T, tags_relation AS rel_tags0
   638 WHERE NOT (T.cw_eid=28258) AND rel_tags0.eid_from=T.cw_eid AND rel_tags0.eid_to=S.cw_eid
   665 WHERE NOT (_T.cw_eid=28258) AND rel_tags0.eid_from=_T.cw_eid AND rel_tags0.eid_to=_S.cw_eid
   639 UNION ALL
   666 UNION ALL
   640 SELECT S.cw_eid
   667 SELECT _S.cw_eid
   641 FROM cw_State AS S, cw_Tag AS T, tags_relation AS rel_tags0
   668 FROM cw_State AS _S, cw_Tag AS _T, tags_relation AS rel_tags0
   642 WHERE NOT (T.cw_eid=28258) AND rel_tags0.eid_from=T.cw_eid AND rel_tags0.eid_to=S.cw_eid
   669 WHERE NOT (_T.cw_eid=28258) AND rel_tags0.eid_from=_T.cw_eid AND rel_tags0.eid_to=_S.cw_eid
   643 UNION ALL
   670 UNION ALL
   644 SELECT S.cw_eid
   671 SELECT _S.cw_eid
   645 FROM cw_Tag AS S, cw_Tag AS T, tags_relation AS rel_tags0
   672 FROM cw_Tag AS _S, cw_Tag AS _T, tags_relation AS rel_tags0
   646 WHERE NOT (T.cw_eid=28258) AND rel_tags0.eid_from=T.cw_eid AND rel_tags0.eid_to=S.cw_eid'''),
   673 WHERE NOT (_T.cw_eid=28258) AND rel_tags0.eid_from=_T.cw_eid AND rel_tags0.eid_to=_S.cw_eid'''),
   647 
   674 
   648     ('Any X,Y WHERE X created_by Y, X eid 5, NOT Y eid 6',
   675     ('Any X,Y WHERE X created_by Y, X eid 5, NOT Y eid 6',
   649      '''SELECT 5, rel_created_by0.eid_to
   676      '''SELECT 5, rel_created_by0.eid_to
   650 FROM created_by_relation AS rel_created_by0
   677 FROM created_by_relation AS rel_created_by0
   651 WHERE rel_created_by0.eid_from=5 AND NOT (rel_created_by0.eid_to=6)'''),
   678 WHERE rel_created_by0.eid_from=5 AND NOT (rel_created_by0.eid_to=6)'''),
   652 
   679 
   653     ('Note X WHERE NOT Y evaluee X',
   680     ('Note X WHERE NOT Y evaluee X',
   654      '''SELECT X.cw_eid
   681      '''SELECT _X.cw_eid
   655 FROM cw_Note AS X
   682 FROM cw_Note AS _X
   656 WHERE NOT EXISTS(SELECT 1 FROM evaluee_relation AS rel_evaluee0 WHERE rel_evaluee0.eid_to=X.cw_eid)'''),
   683 WHERE NOT EXISTS(SELECT 1 FROM evaluee_relation AS rel_evaluee0 WHERE rel_evaluee0.eid_to=_X.cw_eid)'''),
   657 
   684 
   658     ('Any Y WHERE NOT Y evaluee X',
   685     ('Any Y WHERE NOT Y evaluee X',
   659      '''SELECT Y.cw_eid
   686      '''SELECT _Y.cw_eid
   660 FROM cw_CWUser AS Y
   687 FROM cw_CWUser AS _Y
   661 WHERE NOT EXISTS(SELECT 1 FROM evaluee_relation AS rel_evaluee0 WHERE rel_evaluee0.eid_from=Y.cw_eid)
   688 WHERE NOT EXISTS(SELECT 1 FROM evaluee_relation AS rel_evaluee0 WHERE rel_evaluee0.eid_from=_Y.cw_eid)
   662 UNION ALL
   689 UNION ALL
   663 SELECT Y.cw_eid
   690 SELECT _Y.cw_eid
   664 FROM cw_Division AS Y
   691 FROM cw_Division AS _Y
   665 WHERE NOT EXISTS(SELECT 1 FROM evaluee_relation AS rel_evaluee0 WHERE rel_evaluee0.eid_from=Y.cw_eid)
   692 WHERE NOT EXISTS(SELECT 1 FROM evaluee_relation AS rel_evaluee0 WHERE rel_evaluee0.eid_from=_Y.cw_eid)
   666 UNION ALL
   693 UNION ALL
   667 SELECT Y.cw_eid
   694 SELECT _Y.cw_eid
   668 FROM cw_Personne AS Y
   695 FROM cw_Personne AS _Y
   669 WHERE NOT EXISTS(SELECT 1 FROM evaluee_relation AS rel_evaluee0 WHERE rel_evaluee0.eid_from=Y.cw_eid)
   696 WHERE NOT EXISTS(SELECT 1 FROM evaluee_relation AS rel_evaluee0 WHERE rel_evaluee0.eid_from=_Y.cw_eid)
   670 UNION ALL
   697 UNION ALL
   671 SELECT Y.cw_eid
   698 SELECT _Y.cw_eid
   672 FROM cw_Societe AS Y
   699 FROM cw_Societe AS _Y
   673 WHERE NOT EXISTS(SELECT 1 FROM evaluee_relation AS rel_evaluee0 WHERE rel_evaluee0.eid_from=Y.cw_eid)
   700 WHERE NOT EXISTS(SELECT 1 FROM evaluee_relation AS rel_evaluee0 WHERE rel_evaluee0.eid_from=_Y.cw_eid)
   674 UNION ALL
   701 UNION ALL
   675 SELECT Y.cw_eid
   702 SELECT _Y.cw_eid
   676 FROM cw_SubDivision AS Y
   703 FROM cw_SubDivision AS _Y
   677 WHERE NOT EXISTS(SELECT 1 FROM evaluee_relation AS rel_evaluee0 WHERE rel_evaluee0.eid_from=Y.cw_eid)'''),
   704 WHERE NOT EXISTS(SELECT 1 FROM evaluee_relation AS rel_evaluee0 WHERE rel_evaluee0.eid_from=_Y.cw_eid)'''),
   678 
   705 
   679     ('Any X WHERE NOT Y evaluee X, Y is CWUser',
   706     ('Any X WHERE NOT Y evaluee X, Y is CWUser',
   680      '''SELECT X.cw_eid
   707      '''SELECT _X.cw_eid
   681 FROM cw_Note AS X
   708 FROM cw_Note AS _X
   682 WHERE NOT EXISTS(SELECT 1 FROM evaluee_relation AS rel_evaluee0,cw_CWUser AS Y WHERE rel_evaluee0.eid_from=Y.cw_eid AND rel_evaluee0.eid_to=X.cw_eid)'''),
   709 WHERE NOT EXISTS(SELECT 1 FROM evaluee_relation AS rel_evaluee0,cw_CWUser AS _Y WHERE rel_evaluee0.eid_from=_Y.cw_eid AND rel_evaluee0.eid_to=_X.cw_eid)'''),
   683 
   710 
   684     ('Any X,RT WHERE X relation_type RT, NOT X is CWAttribute',
   711     ('Any X,RT WHERE X relation_type RT, NOT X is CWAttribute',
   685      '''SELECT X.cw_eid, X.cw_relation_type
   712      '''SELECT _X.cw_eid, _X.cw_relation_type
   686 FROM cw_CWRelation AS X
   713 FROM cw_CWRelation AS _X
   687 WHERE X.cw_relation_type IS NOT NULL'''),
   714 WHERE _X.cw_relation_type IS NOT NULL'''),
   688 
   715 
   689     ('Any K,V WHERE P is CWProperty, P pkey K, P value V, NOT P for_user U',
   716     ('Any K,V WHERE P is CWProperty, P pkey K, P value V, NOT P for_user U',
   690      '''SELECT P.cw_pkey, P.cw_value
   717      '''SELECT _P.cw_pkey, _P.cw_value
   691 FROM cw_CWProperty AS P
   718 FROM cw_CWProperty AS _P
   692 WHERE P.cw_for_user IS NULL'''),
   719 WHERE _P.cw_for_user IS NULL'''),
   693 
   720 
   694     ('Any S WHERE NOT X in_state S, X is IN(Affaire, CWUser)',
   721     ('Any S WHERE NOT X in_state S, X is IN(Affaire, CWUser)',
   695      '''SELECT S.cw_eid
   722      '''SELECT _S.cw_eid
   696 FROM cw_State AS S
   723 FROM cw_State AS _S
   697 WHERE NOT EXISTS(SELECT 1 FROM cw_Affaire AS X WHERE X.cw_in_state=S.cw_eid)
   724 WHERE NOT EXISTS(SELECT 1 FROM cw_Affaire AS _X WHERE _X.cw_in_state=_S.cw_eid)
   698 INTERSECT
   725 INTERSECT
   699 SELECT S.cw_eid
   726 SELECT _S.cw_eid
   700 FROM cw_State AS S
   727 FROM cw_State AS _S
   701 WHERE NOT EXISTS(SELECT 1 FROM cw_CWUser AS X WHERE X.cw_in_state=S.cw_eid)'''),
   728 WHERE NOT EXISTS(SELECT 1 FROM cw_CWUser AS _X WHERE _X.cw_in_state=_S.cw_eid)'''),
   702 
   729 
   703     ('Any S WHERE NOT(X in_state S, S name "somename"), X is CWUser',
   730     ('Any S WHERE NOT(X in_state S, S name "somename"), X is CWUser',
   704      '''SELECT S.cw_eid
   731      '''SELECT _S.cw_eid
   705 FROM cw_State AS S
   732 FROM cw_State AS _S
   706 WHERE NOT EXISTS(SELECT 1 FROM cw_CWUser AS X WHERE X.cw_in_state=S.cw_eid AND S.cw_name=somename)'''),
   733 WHERE NOT EXISTS(SELECT 1 FROM cw_CWUser AS _X WHERE _X.cw_in_state=_S.cw_eid AND _S.cw_name=somename)'''),
   707    
   734    
   708 # XXXFIXME fail
   735 # XXXFIXME fail
   709 #         ('Any X,RT WHERE X relation_type RT?, NOT X is CWAttribute',
   736 #         ('Any X,RT WHERE X relation_type RT?, NOT X is CWAttribute',
   710 #      '''SELECT X.cw_eid, X.cw_relation_type
   737 #      '''SELECT _X.cw_eid, _X.cw_relation_type
   711 # FROM cw_CWRelation AS X'''),
   738 # FROM cw_CWRelation AS _X'''),
   712 ]
   739 ]
   713 
   740 
   714 OUTER_JOIN = [
   741 OUTER_JOIN = [
   715     ('Any X,S WHERE X travaille S?',
   742     ('Any X,S WHERE X travaille S?',
   716      '''SELECT X.cw_eid, rel_travaille0.eid_to
   743      '''SELECT _X.cw_eid, rel_travaille0.eid_to
   717 FROM cw_Personne AS X LEFT OUTER JOIN travaille_relation AS rel_travaille0 ON (rel_travaille0.eid_from=X.cw_eid)'''
   744 FROM cw_Personne AS _X LEFT OUTER JOIN travaille_relation AS rel_travaille0 ON (rel_travaille0.eid_from=_X.cw_eid)'''
   718     ),
   745     ),
   719     ('Any S,X WHERE X? travaille S, S is Societe',
   746     ('Any S,X WHERE X? travaille S, S is Societe',
   720      '''SELECT S.cw_eid, rel_travaille0.eid_from
   747      '''SELECT _S.cw_eid, rel_travaille0.eid_from
   721 FROM cw_Societe AS S LEFT OUTER JOIN travaille_relation AS rel_travaille0 ON (rel_travaille0.eid_to=S.cw_eid)'''
   748 FROM cw_Societe AS _S LEFT OUTER JOIN travaille_relation AS rel_travaille0 ON (rel_travaille0.eid_to=_S.cw_eid)'''
   722     ),
   749     ),
   723 
   750 
   724     ('Any N,A WHERE N inline1 A?',
   751     ('Any N,A WHERE N inline1 A?',
   725      '''SELECT N.cw_eid, N.cw_inline1
   752      '''SELECT _N.cw_eid, _N.cw_inline1
   726 FROM cw_Note AS N'''),
   753 FROM cw_Note AS _N'''),
   727 
   754 
   728     ('Any SN WHERE X from_state S?, S name SN',
   755     ('Any SN WHERE X from_state S?, S name SN',
   729      '''SELECT S.cw_name
   756      '''SELECT _S.cw_name
   730 FROM cw_TrInfo AS X LEFT OUTER JOIN cw_State AS S ON (X.cw_from_state=S.cw_eid)'''
   757 FROM cw_TrInfo AS _X LEFT OUTER JOIN cw_State AS _S ON (_X.cw_from_state=_S.cw_eid)'''
   731     ),
   758     ),
   732 
   759 
   733     ('Any A,N WHERE N? inline1 A',
   760     ('Any A,N WHERE N? inline1 A',
   734      '''SELECT A.cw_eid, N.cw_eid
   761      '''SELECT _A.cw_eid, _N.cw_eid
   735 FROM cw_Affaire AS A LEFT OUTER JOIN cw_Note AS N ON (N.cw_inline1=A.cw_eid)'''
   762 FROM cw_Affaire AS _A LEFT OUTER JOIN cw_Note AS _N ON (_N.cw_inline1=_A.cw_eid)'''
   736     ),
   763     ),
   737 
   764 
   738     ('Any A,B,C,D,E,F,G WHERE A eid 12,A creation_date B,A modification_date C,A comment D,A from_state E?,A to_state F?,A wf_info_for G?',
   765     ('Any A,B,C,D,E,F,G WHERE A eid 12,A creation_date B,A modification_date C,A comment D,A from_state E?,A to_state F?,A wf_info_for G?',
   739     '''SELECT A.cw_eid, A.cw_creation_date, A.cw_modification_date, A.cw_comment, A.cw_from_state, A.cw_to_state, A.cw_wf_info_for
   766     '''SELECT _A.cw_eid, _A.cw_creation_date, _A.cw_modification_date, _A.cw_comment, _A.cw_from_state, _A.cw_to_state, _A.cw_wf_info_for
   740 FROM cw_TrInfo AS A
   767 FROM cw_TrInfo AS _A
   741 WHERE A.cw_eid=12'''),
   768 WHERE _A.cw_eid=12'''),
   742 
   769 
   743     ('Any FS,TS,C,D,U ORDERBY D DESC WHERE WF wf_info_for X,WF from_state FS?, WF to_state TS, WF comment C,WF creation_date D, WF owned_by U, X eid 1',
   770     ('Any FS,TS,C,D,U ORDERBY D DESC WHERE WF wf_info_for X,WF from_state FS?, WF to_state TS, WF comment C,WF creation_date D, WF owned_by U, X eid 1',
   744      '''SELECT WF.cw_from_state, WF.cw_to_state, WF.cw_comment, WF.cw_creation_date, rel_owned_by0.eid_to
   771      '''SELECT _WF.cw_from_state, _WF.cw_to_state, _WF.cw_comment, _WF.cw_creation_date, rel_owned_by0.eid_to
   745 FROM cw_TrInfo AS WF, owned_by_relation AS rel_owned_by0
   772 FROM cw_TrInfo AS _WF, owned_by_relation AS rel_owned_by0
   746 WHERE WF.cw_wf_info_for=1 AND WF.cw_to_state IS NOT NULL AND rel_owned_by0.eid_from=WF.cw_eid
   773 WHERE _WF.cw_wf_info_for=1 AND _WF.cw_to_state IS NOT NULL AND rel_owned_by0.eid_from=_WF.cw_eid
   747 ORDER BY 4 DESC'''),
   774 ORDER BY 4 DESC'''),
   748 
   775 
   749     ('Any X WHERE X is Affaire, S is Societe, EXISTS(X owned_by U OR (X concerne S?, S owned_by U))',
   776     ('Any X WHERE X is Affaire, S is Societe, EXISTS(X owned_by U OR (X concerne S?, S owned_by U))',
   750      '''SELECT X.cw_eid
   777      '''SELECT _X.cw_eid
   751 FROM cw_Affaire AS X
   778 FROM cw_Affaire AS _X
   752 WHERE EXISTS(SELECT 1 FROM owned_by_relation AS rel_owned_by0, cw_CWUser AS U, cw_Affaire AS A LEFT OUTER JOIN concerne_relation AS rel_concerne1 ON (rel_concerne1.eid_from=A.cw_eid) LEFT OUTER JOIN cw_Societe AS S ON (rel_concerne1.eid_to=S.cw_eid), owned_by_relation AS rel_owned_by2 WHERE ((rel_owned_by0.eid_from=A.cw_eid AND rel_owned_by0.eid_to=U.cw_eid) OR (rel_owned_by2.eid_from=S.cw_eid AND rel_owned_by2.eid_to=U.cw_eid)) AND X.cw_eid=A.cw_eid)'''),
   779 WHERE EXISTS(SELECT 1 FROM owned_by_relation AS rel_owned_by0, cw_CWUser AS _U, cw_Affaire AS _A LEFT OUTER JOIN concerne_relation AS rel_concerne1 ON (rel_concerne1.eid_from=_A.cw_eid) LEFT OUTER JOIN cw_Societe AS _S ON (rel_concerne1.eid_to=_S.cw_eid), owned_by_relation AS rel_owned_by2 WHERE ((rel_owned_by0.eid_from=_A.cw_eid AND rel_owned_by0.eid_to=_U.cw_eid) OR (rel_owned_by2.eid_from=_S.cw_eid AND rel_owned_by2.eid_to=_U.cw_eid)) AND _X.cw_eid=_A.cw_eid)'''),
   753 
   780 
   754     ('Any C,M WHERE C travaille G?, G evaluee M?, G is Societe',
   781     ('Any C,M WHERE C travaille G?, G evaluee M?, G is Societe',
   755      '''SELECT C.cw_eid, rel_evaluee1.eid_to
   782      '''SELECT _C.cw_eid, rel_evaluee1.eid_to
   756 FROM cw_Personne AS C LEFT OUTER JOIN travaille_relation AS rel_travaille0 ON (rel_travaille0.eid_from=C.cw_eid) LEFT OUTER JOIN cw_Societe AS G ON (rel_travaille0.eid_to=G.cw_eid) LEFT OUTER JOIN evaluee_relation AS rel_evaluee1 ON (rel_evaluee1.eid_from=G.cw_eid)'''
   783 FROM cw_Personne AS _C LEFT OUTER JOIN travaille_relation AS rel_travaille0 ON (rel_travaille0.eid_from=_C.cw_eid) LEFT OUTER JOIN cw_Societe AS _G ON (rel_travaille0.eid_to=_G.cw_eid) LEFT OUTER JOIN evaluee_relation AS rel_evaluee1 ON (rel_evaluee1.eid_from=_G.cw_eid)'''
   757      ),
   784      ),
   758 
   785 
   759     ('Any A,C WHERE A documented_by C?, (C is NULL) OR (EXISTS(C require_permission F, '
   786     ('Any A,C WHERE A documented_by C?, (C is NULL) OR (EXISTS(C require_permission F, '
   760      'F name "read", F require_group E, U in_group E)), U eid 1',
   787      'F name "read", F require_group E, U in_group E)), U eid 1',
   761      '''SELECT A.cw_eid, rel_documented_by0.eid_to
   788      '''SELECT _A.cw_eid, rel_documented_by0.eid_to
   762 FROM cw_Affaire AS A LEFT OUTER JOIN documented_by_relation AS rel_documented_by0 ON (rel_documented_by0.eid_from=A.cw_eid)
   789 FROM cw_Affaire AS _A LEFT OUTER JOIN documented_by_relation AS rel_documented_by0 ON (rel_documented_by0.eid_from=_A.cw_eid)
   763 WHERE ((rel_documented_by0.eid_to IS NULL) OR (EXISTS(SELECT 1 FROM require_permission_relation AS rel_require_permission1, cw_CWPermission AS F, require_group_relation AS rel_require_group2, in_group_relation AS rel_in_group3 WHERE rel_documented_by0.eid_to=rel_require_permission1.eid_from AND rel_require_permission1.eid_to=F.cw_eid AND F.cw_name=read AND rel_require_group2.eid_from=F.cw_eid AND rel_in_group3.eid_to=rel_require_group2.eid_to AND rel_in_group3.eid_from=1)))'''),
   790 WHERE ((rel_documented_by0.eid_to IS NULL) OR (EXISTS(SELECT 1 FROM require_permission_relation AS rel_require_permission1, cw_CWPermission AS _F, require_group_relation AS rel_require_group2, in_group_relation AS rel_in_group3 WHERE rel_documented_by0.eid_to=rel_require_permission1.eid_from AND rel_require_permission1.eid_to=_F.cw_eid AND _F.cw_name=read AND rel_require_group2.eid_from=_F.cw_eid AND rel_in_group3.eid_to=rel_require_group2.eid_to AND rel_in_group3.eid_from=1)))'''),
   764 
   791 
   765     ("Any X WHERE X eid 12, P? connait X",
   792     ("Any X WHERE X eid 12, P? connait X",
   766      '''SELECT X.cw_eid
   793      '''SELECT _X.cw_eid
   767 FROM cw_Personne AS X LEFT OUTER JOIN connait_relation AS rel_connait0 ON (rel_connait0.eid_to=12)
   794 FROM cw_Personne AS _X LEFT OUTER JOIN connait_relation AS rel_connait0 ON (rel_connait0.eid_to=12)
   768 WHERE X.cw_eid=12'''
   795 WHERE _X.cw_eid=12'''
   769     ),
   796     ),
   770 
   797 
   771     ('Any GN, TN ORDERBY GN WHERE T tags G?, T name TN, G name GN',
   798     ('Any GN, TN ORDERBY GN WHERE T tags G?, T name TN, G name GN',
   772     '''
   799     '''
   773 SELECT _T0.C1, T.cw_name
   800 SELECT _T0.C1, _T.cw_name
   774 FROM cw_Tag AS T LEFT OUTER JOIN tags_relation AS rel_tags0 ON (rel_tags0.eid_from=T.cw_eid) LEFT OUTER JOIN (SELECT G.cw_eid AS C0, G.cw_name AS C1
   801 FROM cw_Tag AS _T LEFT OUTER JOIN tags_relation AS rel_tags0 ON (rel_tags0.eid_from=_T.cw_eid) LEFT OUTER JOIN (SELECT _G.cw_eid AS C0, _G.cw_name AS C1
   775 FROM cw_CWGroup AS G
   802 FROM cw_CWGroup AS _G
   776 UNION ALL
   803 UNION ALL
   777 SELECT G.cw_eid AS C0, G.cw_name AS C1
   804 SELECT _G.cw_eid AS C0, _G.cw_name AS C1
   778 FROM cw_State AS G
   805 FROM cw_State AS _G
   779 UNION ALL
   806 UNION ALL
   780 SELECT G.cw_eid AS C0, G.cw_name AS C1
   807 SELECT _G.cw_eid AS C0, _G.cw_name AS C1
   781 FROM cw_Tag AS G) AS _T0 ON (rel_tags0.eid_to=_T0.C0)
   808 FROM cw_Tag AS _G) AS _T0 ON (rel_tags0.eid_to=_T0.C0)
   782 ORDER BY 1'''),
   809 ORDER BY 1'''),
   783 
   810 
   784 
   811 
   785     # optional variable with additional restriction
   812     # optional variable with additional restriction
   786     ('Any T,G WHERE T tags G?, G name "hop", G is CWGroup',
   813     ('Any T,G WHERE T tags G?, G name "hop", G is CWGroup',
   787      '''SELECT T.cw_eid, G.cw_eid
   814      '''SELECT _T.cw_eid, _G.cw_eid
   788 FROM cw_Tag AS T LEFT OUTER JOIN tags_relation AS rel_tags0 ON (rel_tags0.eid_from=T.cw_eid) LEFT OUTER JOIN cw_CWGroup AS G ON (rel_tags0.eid_to=G.cw_eid AND G.cw_name=hop)'''),
   815 FROM cw_Tag AS _T LEFT OUTER JOIN tags_relation AS rel_tags0 ON (rel_tags0.eid_from=_T.cw_eid) LEFT OUTER JOIN cw_CWGroup AS _G ON (rel_tags0.eid_to=_G.cw_eid AND _G.cw_name=hop)'''),
   789 
   816 
   790     # optional variable with additional invariant restriction
   817     # optional variable with additional invariant restriction
   791     ('Any T,G WHERE T tags G?, G eid 12',
   818     ('Any T,G WHERE T tags G?, G eid 12',
   792      '''SELECT T.cw_eid, rel_tags0.eid_to
   819      '''SELECT _T.cw_eid, rel_tags0.eid_to
   793 FROM cw_Tag AS T LEFT OUTER JOIN tags_relation AS rel_tags0 ON (rel_tags0.eid_from=T.cw_eid AND rel_tags0.eid_to=12)'''),
   820 FROM cw_Tag AS _T LEFT OUTER JOIN tags_relation AS rel_tags0 ON (rel_tags0.eid_from=_T.cw_eid AND rel_tags0.eid_to=12)'''),
   794 
   821 
   795     # optional variable with additional restriction appearing before the relation
   822     # optional variable with additional restriction appearing before the relation
   796     ('Any T,G WHERE G name "hop", T tags G?, G is CWGroup',
   823     ('Any T,G WHERE G name "hop", T tags G?, G is CWGroup',
   797      '''SELECT T.cw_eid, G.cw_eid
   824      '''SELECT _T.cw_eid, _G.cw_eid
   798 FROM cw_Tag AS T LEFT OUTER JOIN tags_relation AS rel_tags0 ON (rel_tags0.eid_from=T.cw_eid) LEFT OUTER JOIN cw_CWGroup AS G ON (rel_tags0.eid_to=G.cw_eid AND G.cw_name=hop)'''),
   825 FROM cw_Tag AS _T LEFT OUTER JOIN tags_relation AS rel_tags0 ON (rel_tags0.eid_from=_T.cw_eid) LEFT OUTER JOIN cw_CWGroup AS _G ON (rel_tags0.eid_to=_G.cw_eid AND _G.cw_name=hop)'''),
   799 
   826 
   800     # optional variable with additional restriction on inlined relation
   827     # optional variable with additional restriction on inlined relation
   801     # XXX the expected result should be as the query below. So what, raise BadRQLQuery ?
   828     # XXX the expected result should be as the query below. So what, raise BadRQLQuery ?
   802     ('Any T,G,S WHERE T tags G?, G in_state S, S name "hop", G is CWUser',
   829     ('Any T,G,S WHERE T tags G?, G in_state S, S name "hop", G is CWUser',
   803      '''SELECT T.cw_eid, G.cw_eid, S.cw_eid
   830      '''SELECT _T.cw_eid, _G.cw_eid, _S.cw_eid
   804 FROM cw_State AS S, cw_Tag AS T LEFT OUTER JOIN tags_relation AS rel_tags0 ON (rel_tags0.eid_from=T.cw_eid) LEFT OUTER JOIN cw_CWUser AS G ON (rel_tags0.eid_to=G.cw_eid)
   831 FROM cw_State AS _S, cw_Tag AS _T LEFT OUTER JOIN tags_relation AS rel_tags0 ON (rel_tags0.eid_from=_T.cw_eid) LEFT OUTER JOIN cw_CWUser AS _G ON (rel_tags0.eid_to=_G.cw_eid)
   805 WHERE G.cw_in_state=S.cw_eid AND S.cw_name=hop
   832 WHERE _G.cw_in_state=_S.cw_eid AND _S.cw_name=hop
   806 '''),
   833 '''),
   807 
   834 
   808     # optional variable with additional invariant restriction on an inlined relation
   835     # optional variable with additional invariant restriction on an inlined relation
   809     ('Any T,G,S WHERE T tags G, G in_state S?, S eid 1, G is CWUser',
   836     ('Any T,G,S WHERE T tags G, G in_state S?, S eid 1, G is CWUser',
   810      '''SELECT rel_tags0.eid_from, G.cw_eid, G.cw_in_state
   837      '''SELECT rel_tags0.eid_from, _G.cw_eid, _G.cw_in_state
   811 FROM cw_CWUser AS G, tags_relation AS rel_tags0
   838 FROM cw_CWUser AS _G, tags_relation AS rel_tags0
   812 WHERE rel_tags0.eid_to=G.cw_eid AND (G.cw_in_state=1 OR G.cw_in_state IS NULL)'''),
   839 WHERE rel_tags0.eid_to=_G.cw_eid AND (_G.cw_in_state=1 OR _G.cw_in_state IS NULL)'''),
   813 
   840 
   814     # two optional variables with additional invariant restriction on an inlined relation
   841     # two optional variables with additional invariant restriction on an inlined relation
   815     ('Any T,G,S WHERE T tags G?, G in_state S?, S eid 1, G is CWUser',
   842     ('Any T,G,S WHERE T tags G?, G in_state S?, S eid 1, G is CWUser',
   816      '''SELECT T.cw_eid, G.cw_eid, G.cw_in_state
   843      '''SELECT _T.cw_eid, _G.cw_eid, _G.cw_in_state
   817 FROM cw_Tag AS T LEFT OUTER JOIN tags_relation AS rel_tags0 ON (rel_tags0.eid_from=T.cw_eid) LEFT OUTER JOIN cw_CWUser AS G ON (rel_tags0.eid_to=G.cw_eid AND (G.cw_in_state=1 OR G.cw_in_state IS NULL))'''),
   844 FROM cw_Tag AS _T LEFT OUTER JOIN tags_relation AS rel_tags0 ON (rel_tags0.eid_from=_T.cw_eid) LEFT OUTER JOIN cw_CWUser AS _G ON (rel_tags0.eid_to=_G.cw_eid AND (_G.cw_in_state=1 OR _G.cw_in_state IS NULL))'''),
   818 
   845 
   819     # two optional variables with additional restriction on an inlined relation
   846     # two optional variables with additional restriction on an inlined relation
   820     ('Any T,G,S WHERE T tags G?, G in_state S?, S name "hop", G is CWUser',
   847     ('Any T,G,S WHERE T tags G?, G in_state S?, S name "hop", G is CWUser',
   821      '''SELECT T.cw_eid, G.cw_eid, S.cw_eid
   848      '''SELECT _T.cw_eid, _G.cw_eid, _S.cw_eid
   822 FROM cw_Tag AS T LEFT OUTER JOIN tags_relation AS rel_tags0 ON (rel_tags0.eid_from=T.cw_eid) LEFT OUTER JOIN cw_CWUser AS G ON (rel_tags0.eid_to=G.cw_eid) LEFT OUTER JOIN cw_State AS S ON (G.cw_in_state=S.cw_eid AND S.cw_name=hop)'''),
   849 FROM cw_Tag AS _T LEFT OUTER JOIN tags_relation AS rel_tags0 ON (rel_tags0.eid_from=_T.cw_eid) LEFT OUTER JOIN cw_CWUser AS _G ON (rel_tags0.eid_to=_G.cw_eid) LEFT OUTER JOIN cw_State AS _S ON (_G.cw_in_state=_S.cw_eid AND _S.cw_name=hop)'''),
   823 
   850 
   824     # two optional variables with additional restriction on an ambigous inlined relation
   851     # two optional variables with additional restriction on an ambigous inlined relation
   825     ('Any T,G,S WHERE T tags G?, G in_state S?, S name "hop"',
   852     ('Any T,G,S WHERE T tags G?, G in_state S?, S name "hop"',
   826      '''
   853      '''
   827 SELECT T.cw_eid, _T0.C0, _T0.C1
   854 SELECT _T.cw_eid, _T0.C0, _T0.C1
   828 FROM cw_Tag AS T LEFT OUTER JOIN tags_relation AS rel_tags0 ON (rel_tags0.eid_from=T.cw_eid) LEFT OUTER JOIN (SELECT G.cw_eid AS C0, S.cw_eid AS C1
   855 FROM cw_Tag AS _T LEFT OUTER JOIN tags_relation AS rel_tags0 ON (rel_tags0.eid_from=_T.cw_eid) LEFT OUTER JOIN (SELECT _G.cw_eid AS C0, _S.cw_eid AS C1
   829 FROM cw_Affaire AS G LEFT OUTER JOIN cw_State AS S ON (G.cw_in_state=S.cw_eid AND S.cw_name=hop)
   856 FROM cw_Affaire AS _G LEFT OUTER JOIN cw_State AS _S ON (_G.cw_in_state=_S.cw_eid AND _S.cw_name=hop)
   830 UNION ALL
   857 UNION ALL
   831 SELECT G.cw_eid AS C0, S.cw_eid AS C1
   858 SELECT _G.cw_eid AS C0, _S.cw_eid AS C1
   832 FROM cw_CWUser AS G LEFT OUTER JOIN cw_State AS S ON (G.cw_in_state=S.cw_eid AND S.cw_name=hop)
   859 FROM cw_CWUser AS _G LEFT OUTER JOIN cw_State AS _S ON (_G.cw_in_state=_S.cw_eid AND _S.cw_name=hop)
   833 UNION ALL
   860 UNION ALL
   834 SELECT G.cw_eid AS C0, S.cw_eid AS C1
   861 SELECT _G.cw_eid AS C0, _S.cw_eid AS C1
   835 FROM cw_Note AS G LEFT OUTER JOIN cw_State AS S ON (G.cw_in_state=S.cw_eid AND S.cw_name=hop) ) AS _T0 ON (rel_tags0.eid_to=_T0.C0)'''),
   862 FROM cw_Note AS _G LEFT OUTER JOIN cw_State AS _S ON (_G.cw_in_state=_S.cw_eid AND _S.cw_name=hop) ) AS _T0 ON (rel_tags0.eid_to=_T0.C0)'''),
   836 
   863 
   837     ('Any O,AD  WHERE NOT S inline1 O, S eid 123, O todo_by AD?',
   864     ('Any O,AD  WHERE NOT S inline1 O, S eid 123, O todo_by AD?',
   838      '''SELECT O.cw_eid, rel_todo_by0.eid_to
   865      '''SELECT _O.cw_eid, rel_todo_by0.eid_to
   839 FROM cw_Affaire AS O LEFT OUTER JOIN todo_by_relation AS rel_todo_by0 ON (rel_todo_by0.eid_from=O.cw_eid), cw_Note AS S
   866 FROM cw_Affaire AS _O LEFT OUTER JOIN todo_by_relation AS rel_todo_by0 ON (rel_todo_by0.eid_from=_O.cw_eid), cw_Note AS _S
   840 WHERE NOT EXISTS(SELECT 1 WHERE S.cw_inline1=O.cw_eid) AND S.cw_eid=123''')
   867 WHERE NOT EXISTS(SELECT 1 WHERE _S.cw_inline1=_O.cw_eid) AND _S.cw_eid=123''')
   841     ]
   868     ]
   842 
   869 
   843 VIRTUAL_VARS = [
   870 VIRTUAL_VARS = [
   844     ("Personne P WHERE P travaille S, S tel T, S fax T, S is Societe;",
   871     ("Personne P WHERE P travaille S, S tel T, S fax T, S is Societe;",
   845      '''SELECT rel_travaille0.eid_from
   872      '''SELECT rel_travaille0.eid_from
   846 FROM cw_Societe AS S, travaille_relation AS rel_travaille0
   873 FROM cw_Societe AS _S, travaille_relation AS rel_travaille0
   847 WHERE rel_travaille0.eid_to=S.cw_eid AND S.cw_fax=S.cw_tel'''),
   874 WHERE rel_travaille0.eid_to=_S.cw_eid AND _S.cw_fax=_S.cw_tel'''),
   848 
   875 
   849     ("Personne P where X eid 0, X creation_date D, P datenaiss < D, X is Affaire",
   876     ("Personne P where X eid 0, X creation_date D, P datenaiss < D, X is Affaire",
   850      '''SELECT P.cw_eid
   877      '''SELECT _P.cw_eid
   851 FROM cw_Affaire AS X, cw_Personne AS P
   878 FROM cw_Affaire AS _X, cw_Personne AS _P
   852 WHERE X.cw_eid=0 AND P.cw_datenaiss<X.cw_creation_date'''),
   879 WHERE _X.cw_eid=0 AND _P.cw_datenaiss<_X.cw_creation_date'''),
   853 
   880 
   854     ("Any N,T WHERE N is Note, N type T;",
   881     ("Any N,T WHERE N is Note, N type T;",
   855      '''SELECT N.cw_eid, N.cw_type
   882      '''SELECT _N.cw_eid, _N.cw_type
   856 FROM cw_Note AS N'''),
   883 FROM cw_Note AS _N'''),
   857 
   884 
   858     ("Personne P where X is Personne, X tel T, X fax F, P fax T+F",
   885     ("Personne P where X is Personne, X tel T, X fax F, P fax T+F",
   859      '''SELECT P.cw_eid
   886      '''SELECT _P.cw_eid
   860 FROM cw_Personne AS P, cw_Personne AS X
   887 FROM cw_Personne AS _P, cw_Personne AS _X
   861 WHERE P.cw_fax=(X.cw_tel + X.cw_fax)'''),
   888 WHERE _P.cw_fax=(_X.cw_tel + _X.cw_fax)'''),
   862 
   889 
   863     ("Personne P where X tel T, X fax F, P fax IN (T,F)",
   890     ("Personne P where X tel T, X fax F, P fax IN (T,F)",
   864      '''SELECT P.cw_eid
   891      '''SELECT _P.cw_eid
   865 FROM cw_Division AS X, cw_Personne AS P
   892 FROM cw_Division AS _X, cw_Personne AS _P
   866 WHERE P.cw_fax IN(X.cw_tel, X.cw_fax)
   893 WHERE _P.cw_fax IN(_X.cw_tel, _X.cw_fax)
   867 UNION ALL
   894 UNION ALL
   868 SELECT P.cw_eid
   895 SELECT _P.cw_eid
   869 FROM cw_Personne AS P, cw_Personne AS X
   896 FROM cw_Personne AS _P, cw_Personne AS _X
   870 WHERE P.cw_fax IN(X.cw_tel, X.cw_fax)
   897 WHERE _P.cw_fax IN(_X.cw_tel, _X.cw_fax)
   871 UNION ALL
   898 UNION ALL
   872 SELECT P.cw_eid
   899 SELECT _P.cw_eid
   873 FROM cw_Personne AS P, cw_Societe AS X
   900 FROM cw_Personne AS _P, cw_Societe AS _X
   874 WHERE P.cw_fax IN(X.cw_tel, X.cw_fax)
   901 WHERE _P.cw_fax IN(_X.cw_tel, _X.cw_fax)
   875 UNION ALL
   902 UNION ALL
   876 SELECT P.cw_eid
   903 SELECT _P.cw_eid
   877 FROM cw_Personne AS P, cw_SubDivision AS X
   904 FROM cw_Personne AS _P, cw_SubDivision AS _X
   878 WHERE P.cw_fax IN(X.cw_tel, X.cw_fax)'''),
   905 WHERE _P.cw_fax IN(_X.cw_tel, _X.cw_fax)'''),
   879 
   906 
   880     ("Personne P where X tel T, X fax F, P fax IN (T,F,0832542332)",
   907     ("Personne P where X tel T, X fax F, P fax IN (T,F,0832542332)",
   881      '''SELECT P.cw_eid
   908      '''SELECT _P.cw_eid
   882 FROM cw_Division AS X, cw_Personne AS P
   909 FROM cw_Division AS _X, cw_Personne AS _P
   883 WHERE P.cw_fax IN(X.cw_tel, X.cw_fax, 832542332)
   910 WHERE _P.cw_fax IN(_X.cw_tel, _X.cw_fax, 832542332)
   884 UNION ALL
   911 UNION ALL
   885 SELECT P.cw_eid
   912 SELECT _P.cw_eid
   886 FROM cw_Personne AS P, cw_Personne AS X
   913 FROM cw_Personne AS _P, cw_Personne AS _X
   887 WHERE P.cw_fax IN(X.cw_tel, X.cw_fax, 832542332)
   914 WHERE _P.cw_fax IN(_X.cw_tel, _X.cw_fax, 832542332)
   888 UNION ALL
   915 UNION ALL
   889 SELECT P.cw_eid
   916 SELECT _P.cw_eid
   890 FROM cw_Personne AS P, cw_Societe AS X
   917 FROM cw_Personne AS _P, cw_Societe AS _X
   891 WHERE P.cw_fax IN(X.cw_tel, X.cw_fax, 832542332)
   918 WHERE _P.cw_fax IN(_X.cw_tel, _X.cw_fax, 832542332)
   892 UNION ALL
   919 UNION ALL
   893 SELECT P.cw_eid
   920 SELECT _P.cw_eid
   894 FROM cw_Personne AS P, cw_SubDivision AS X
   921 FROM cw_Personne AS _P, cw_SubDivision AS _X
   895 WHERE P.cw_fax IN(X.cw_tel, X.cw_fax, 832542332)'''),
   922 WHERE _P.cw_fax IN(_X.cw_tel, _X.cw_fax, 832542332)'''),
   896     ]
   923     ]
   897 
   924 
   898 FUNCS = [
   925 FUNCS = [
   899     ("Any COUNT(P) WHERE P is Personne",
   926     ("Any COUNT(P) WHERE P is Personne",
   900      '''SELECT COUNT(P.cw_eid)
   927      '''SELECT COUNT(_P.cw_eid)
   901 FROM cw_Personne AS P'''),
   928 FROM cw_Personne AS _P'''),
   902     ]
   929     ]
   903 
   930 
   904 SYMETRIC = [
   931 SYMETRIC = [
   905     ('Any P WHERE X eid 0, X connait P',
   932     ('Any P WHERE X eid 0, X connait P',
   906      '''SELECT DISTINCT P.cw_eid
   933      '''SELECT DISTINCT _P.cw_eid
   907 FROM connait_relation AS rel_connait0, cw_Personne AS P
   934 FROM connait_relation AS rel_connait0, cw_Personne AS _P
   908 WHERE (rel_connait0.eid_from=0 AND rel_connait0.eid_to=P.cw_eid OR rel_connait0.eid_to=0 AND rel_connait0.eid_from=P.cw_eid)'''
   935 WHERE (rel_connait0.eid_from=0 AND rel_connait0.eid_to=_P.cw_eid OR rel_connait0.eid_to=0 AND rel_connait0.eid_from=_P.cw_eid)'''
   909      ),
   936      ),
   910 
   937 
   911     ('Any P WHERE X connait P',
   938     ('Any P WHERE X connait P',
   912     '''SELECT DISTINCT P.cw_eid
   939     '''SELECT DISTINCT _P.cw_eid
   913 FROM connait_relation AS rel_connait0, cw_Personne AS P
   940 FROM connait_relation AS rel_connait0, cw_Personne AS _P
   914 WHERE (rel_connait0.eid_to=P.cw_eid OR rel_connait0.eid_from=P.cw_eid)'''
   941 WHERE (rel_connait0.eid_to=_P.cw_eid OR rel_connait0.eid_from=_P.cw_eid)'''
   915     ),
   942     ),
   916 
   943 
   917     ('Any X WHERE X connait P',
   944     ('Any X WHERE X connait P',
   918     '''SELECT DISTINCT X.cw_eid
   945     '''SELECT DISTINCT _X.cw_eid
   919 FROM connait_relation AS rel_connait0, cw_Personne AS X
   946 FROM connait_relation AS rel_connait0, cw_Personne AS _X
   920 WHERE (rel_connait0.eid_from=X.cw_eid OR rel_connait0.eid_to=X.cw_eid)'''
   947 WHERE (rel_connait0.eid_from=_X.cw_eid OR rel_connait0.eid_to=_X.cw_eid)'''
   921      ),
   948      ),
   922 
   949 
   923     ('Any P WHERE X eid 0, NOT X connait P',
   950     ('Any P WHERE X eid 0, NOT X connait P',
   924      '''SELECT P.cw_eid
   951      '''SELECT _P.cw_eid
   925 FROM cw_Personne AS P
   952 FROM cw_Personne AS _P
   926 WHERE NOT EXISTS(SELECT 1 FROM connait_relation AS rel_connait0 WHERE (rel_connait0.eid_from=0 AND rel_connait0.eid_to=P.cw_eid OR rel_connait0.eid_to=0 AND rel_connait0.eid_from=P.cw_eid))'''),
   953 WHERE NOT EXISTS(SELECT 1 FROM connait_relation AS rel_connait0 WHERE (rel_connait0.eid_from=0 AND rel_connait0.eid_to=_P.cw_eid OR rel_connait0.eid_to=0 AND rel_connait0.eid_from=_P.cw_eid))'''),
   927 
   954 
   928     ('Any P WHERE NOT X connait P',
   955     ('Any P WHERE NOT X connait P',
   929     '''SELECT P.cw_eid
   956     '''SELECT _P.cw_eid
   930 FROM cw_Personne AS P
   957 FROM cw_Personne AS _P
   931 WHERE NOT EXISTS(SELECT 1 FROM connait_relation AS rel_connait0 WHERE (rel_connait0.eid_to=P.cw_eid OR rel_connait0.eid_from=P.cw_eid))'''),
   958 WHERE NOT EXISTS(SELECT 1 FROM connait_relation AS rel_connait0 WHERE (rel_connait0.eid_to=_P.cw_eid OR rel_connait0.eid_from=_P.cw_eid))'''),
   932 
   959 
   933     ('Any X WHERE NOT X connait P',
   960     ('Any X WHERE NOT X connait P',
   934     '''SELECT X.cw_eid
   961     '''SELECT _X.cw_eid
   935 FROM cw_Personne AS X
   962 FROM cw_Personne AS _X
   936 WHERE NOT EXISTS(SELECT 1 FROM connait_relation AS rel_connait0 WHERE (rel_connait0.eid_from=X.cw_eid OR rel_connait0.eid_to=X.cw_eid))'''),
   963 WHERE NOT EXISTS(SELECT 1 FROM connait_relation AS rel_connait0 WHERE (rel_connait0.eid_from=_X.cw_eid OR rel_connait0.eid_to=_X.cw_eid))'''),
   937 
   964 
   938     ('Any P WHERE X connait P, P nom "nom"',
   965     ('Any P WHERE X connait P, P nom "nom"',
   939      '''SELECT DISTINCT P.cw_eid
   966      '''SELECT DISTINCT _P.cw_eid
   940 FROM connait_relation AS rel_connait0, cw_Personne AS P
   967 FROM connait_relation AS rel_connait0, cw_Personne AS _P
   941 WHERE (rel_connait0.eid_to=P.cw_eid OR rel_connait0.eid_from=P.cw_eid) AND P.cw_nom=nom'''),
   968 WHERE (rel_connait0.eid_to=_P.cw_eid OR rel_connait0.eid_from=_P.cw_eid) AND _P.cw_nom=nom'''),
   942 
   969 
   943     ('Any X WHERE X connait P, P nom "nom"',
   970     ('Any X WHERE X connait P, P nom "nom"',
   944      '''SELECT DISTINCT X.cw_eid
   971      '''SELECT DISTINCT _X.cw_eid
   945 FROM connait_relation AS rel_connait0, cw_Personne AS P, cw_Personne AS X
   972 FROM connait_relation AS rel_connait0, cw_Personne AS _P, cw_Personne AS _X
   946 WHERE (rel_connait0.eid_from=X.cw_eid AND rel_connait0.eid_to=P.cw_eid OR rel_connait0.eid_to=X.cw_eid AND rel_connait0.eid_from=P.cw_eid) AND P.cw_nom=nom'''
   973 WHERE (rel_connait0.eid_from=_X.cw_eid AND rel_connait0.eid_to=_P.cw_eid OR rel_connait0.eid_to=_X.cw_eid AND rel_connait0.eid_from=_P.cw_eid) AND _P.cw_nom=nom'''
   947     ),
   974     ),
   948 
   975 
   949     ('Any X ORDERBY X DESC LIMIT 9 WHERE E eid 0, E connait X',
   976     ('Any X ORDERBY X DESC LIMIT 9 WHERE E eid 0, E connait X',
   950     '''SELECT DISTINCT X.cw_eid
   977     '''SELECT DISTINCT _X.cw_eid
   951 FROM connait_relation AS rel_connait0, cw_Personne AS X
   978 FROM connait_relation AS rel_connait0, cw_Personne AS _X
   952 WHERE (rel_connait0.eid_from=0 AND rel_connait0.eid_to=X.cw_eid OR rel_connait0.eid_to=0 AND rel_connait0.eid_from=X.cw_eid)
   979 WHERE (rel_connait0.eid_from=0 AND rel_connait0.eid_to=_X.cw_eid OR rel_connait0.eid_to=0 AND rel_connait0.eid_from=_X.cw_eid)
   953 ORDER BY 1 DESC
   980 ORDER BY 1 DESC
   954 LIMIT 9'''
   981 LIMIT 9'''
   955      ),
   982      ),
   956 
   983 
   957     ('DISTINCT Any P WHERE P connait S OR S connait P, S nom "chouette"',
   984     ('DISTINCT Any P WHERE P connait S OR S connait P, S nom "chouette"',
   958      '''SELECT DISTINCT P.cw_eid
   985      '''SELECT DISTINCT _P.cw_eid
   959 FROM connait_relation AS rel_connait0, cw_Personne AS P, cw_Personne AS S
   986 FROM connait_relation AS rel_connait0, cw_Personne AS _P, cw_Personne AS _S
   960 WHERE (rel_connait0.eid_from=P.cw_eid AND rel_connait0.eid_to=S.cw_eid OR rel_connait0.eid_to=P.cw_eid AND rel_connait0.eid_from=S.cw_eid) AND S.cw_nom=chouette'''
   987 WHERE (rel_connait0.eid_from=_P.cw_eid AND rel_connait0.eid_to=_S.cw_eid OR rel_connait0.eid_to=_P.cw_eid AND rel_connait0.eid_from=_S.cw_eid) AND _S.cw_nom=chouette'''
   961      )
   988      )
   962     ]
   989     ]
   963 
   990 
   964 INLINE = [
   991 INLINE = [
   965     ('Any P, L WHERE N ecrit_par P, P nom L, N eid 0',
   992     ('Any P, L WHERE N ecrit_par P, P nom L, N eid 0',
   966      '''SELECT P.cw_eid, P.cw_nom
   993      '''SELECT _P.cw_eid, _P.cw_nom
   967 FROM cw_Note AS N, cw_Personne AS P
   994 FROM cw_Note AS _N, cw_Personne AS _P
   968 WHERE N.cw_ecrit_par=P.cw_eid AND N.cw_eid=0'''),
   995 WHERE _N.cw_ecrit_par=_P.cw_eid AND _N.cw_eid=0'''),
   969 
   996 
   970     ('Any N WHERE NOT N ecrit_par P, P nom "toto"',
   997     ('Any N WHERE NOT N ecrit_par P, P nom "toto"',
   971      '''SELECT N.cw_eid
   998      '''SELECT _N.cw_eid
   972 FROM cw_Note AS N, cw_Personne AS P
   999 FROM cw_Note AS _N, cw_Personne AS _P
   973 WHERE NOT EXISTS(SELECT 1 WHERE N.cw_ecrit_par=P.cw_eid) AND P.cw_nom=toto'''),
  1000 WHERE NOT EXISTS(SELECT 1 WHERE _N.cw_ecrit_par=_P.cw_eid) AND _P.cw_nom=toto'''),
   974 
  1001 
   975     ('Any P WHERE N ecrit_par P, N eid 0',
  1002     ('Any P WHERE N ecrit_par P, N eid 0',
   976     '''SELECT N.cw_ecrit_par
  1003     '''SELECT _N.cw_ecrit_par
   977 FROM cw_Note AS N
  1004 FROM cw_Note AS _N
   978 WHERE N.cw_ecrit_par IS NOT NULL AND N.cw_eid=0'''),
  1005 WHERE _N.cw_ecrit_par IS NOT NULL AND _N.cw_eid=0'''),
   979 
  1006 
   980     ('Any P WHERE N ecrit_par P, P is Personne, N eid 0',
  1007     ('Any P WHERE N ecrit_par P, P is Personne, N eid 0',
   981     '''SELECT P.cw_eid
  1008     '''SELECT _P.cw_eid
   982 FROM cw_Note AS N, cw_Personne AS P
  1009 FROM cw_Note AS _N, cw_Personne AS _P
   983 WHERE N.cw_ecrit_par=P.cw_eid AND N.cw_eid=0'''),
  1010 WHERE _N.cw_ecrit_par=_P.cw_eid AND _N.cw_eid=0'''),
   984 
  1011 
   985     ('Any P WHERE NOT N ecrit_par P, P is Personne, N eid 512',
  1012     ('Any P WHERE NOT N ecrit_par P, P is Personne, N eid 512',
   986      '''SELECT P.cw_eid
  1013      '''SELECT _P.cw_eid
   987 FROM cw_Note AS N, cw_Personne AS P
  1014 FROM cw_Note AS _N, cw_Personne AS _P
   988 WHERE NOT EXISTS(SELECT 1 WHERE N.cw_ecrit_par=P.cw_eid) AND N.cw_eid=512'''),
  1015 WHERE NOT EXISTS(SELECT 1 WHERE _N.cw_ecrit_par=_P.cw_eid) AND _N.cw_eid=512'''),
   989 
  1016 
   990     ('Any S,ES,T WHERE S state_of ET, ET name "CWUser", ES allowed_transition T, T destination_state S',
  1017     ('Any S,ES,T WHERE S state_of ET, ET name "CWUser", ES allowed_transition T, T destination_state S',
   991      '''SELECT T.cw_destination_state, rel_allowed_transition1.eid_from, T.cw_eid
  1018      '''SELECT _T.cw_destination_state, rel_allowed_transition1.eid_from, _T.cw_eid
   992 FROM allowed_transition_relation AS rel_allowed_transition1, cw_Transition AS T, cw_Workflow AS ET, state_of_relation AS rel_state_of0
  1019 FROM allowed_transition_relation AS rel_allowed_transition1, cw_Transition AS _T, cw_Workflow AS _ET, state_of_relation AS rel_state_of0
   993 WHERE T.cw_destination_state=rel_state_of0.eid_from AND rel_state_of0.eid_to=ET.cw_eid AND ET.cw_name=CWUser AND rel_allowed_transition1.eid_to=T.cw_eid'''),
  1020 WHERE _T.cw_destination_state=rel_state_of0.eid_from AND rel_state_of0.eid_to=_ET.cw_eid AND _ET.cw_name=CWUser AND rel_allowed_transition1.eid_to=_T.cw_eid'''),
   994 
  1021 
   995     ('Any O WHERE S eid 0, S in_state O',
  1022     ('Any O WHERE S eid 0, S in_state O',
   996      '''SELECT S.cw_in_state
  1023      '''SELECT _S.cw_in_state
   997 FROM cw_Affaire AS S
  1024 FROM cw_Affaire AS _S
   998 WHERE S.cw_eid=0 AND S.cw_in_state IS NOT NULL
  1025 WHERE _S.cw_eid=0 AND _S.cw_in_state IS NOT NULL
   999 UNION ALL
  1026 UNION ALL
  1000 SELECT S.cw_in_state
  1027 SELECT _S.cw_in_state
  1001 FROM cw_CWUser AS S
  1028 FROM cw_CWUser AS _S
  1002 WHERE S.cw_eid=0 AND S.cw_in_state IS NOT NULL
  1029 WHERE _S.cw_eid=0 AND _S.cw_in_state IS NOT NULL
  1003 UNION ALL
  1030 UNION ALL
  1004 SELECT S.cw_in_state
  1031 SELECT _S.cw_in_state
  1005 FROM cw_Note AS S
  1032 FROM cw_Note AS _S
  1006 WHERE S.cw_eid=0 AND S.cw_in_state IS NOT NULL''')
  1033 WHERE _S.cw_eid=0 AND _S.cw_in_state IS NOT NULL''')
  1007 
  1034 
  1008     ]
  1035     ]
  1009 
  1036 
  1010 INTERSECT = [
  1037 INTERSECT = [
  1011     ('Any SN WHERE NOT X in_state S, S name SN',
  1038     ('Any SN WHERE NOT X in_state S, S name SN',
  1012      '''SELECT S.cw_name
  1039      '''SELECT _S.cw_name
  1013 FROM cw_State AS S
  1040 FROM cw_State AS _S
  1014 WHERE NOT EXISTS(SELECT 1 FROM cw_Affaire AS X WHERE X.cw_in_state=S.cw_eid)
  1041 WHERE NOT EXISTS(SELECT 1 FROM cw_Affaire AS _X WHERE _X.cw_in_state=_S.cw_eid)
  1015 INTERSECT
  1042 INTERSECT
  1016 SELECT S.cw_name
  1043 SELECT _S.cw_name
  1017 FROM cw_State AS S
  1044 FROM cw_State AS _S
  1018 WHERE NOT EXISTS(SELECT 1 FROM cw_CWUser AS X WHERE X.cw_in_state=S.cw_eid)
  1045 WHERE NOT EXISTS(SELECT 1 FROM cw_CWUser AS _X WHERE _X.cw_in_state=_S.cw_eid)
  1019 INTERSECT
  1046 INTERSECT
  1020 SELECT S.cw_name
  1047 SELECT _S.cw_name
  1021 FROM cw_State AS S
  1048 FROM cw_State AS _S
  1022 WHERE NOT EXISTS(SELECT 1 FROM cw_Note AS X WHERE X.cw_in_state=S.cw_eid)'''),
  1049 WHERE NOT EXISTS(SELECT 1 FROM cw_Note AS _X WHERE _X.cw_in_state=_S.cw_eid)'''),
  1023 
  1050 
  1024     ('Any PN WHERE NOT X travaille S, X nom PN, S is IN(Division, Societe)',
  1051     ('Any PN WHERE NOT X travaille S, X nom PN, S is IN(Division, Societe)',
  1025      '''SELECT X.cw_nom
  1052      '''SELECT _X.cw_nom
  1026 FROM cw_Personne AS X
  1053 FROM cw_Personne AS _X
  1027 WHERE NOT EXISTS(SELECT 1 FROM travaille_relation AS rel_travaille0,cw_Division AS S WHERE rel_travaille0.eid_from=X.cw_eid AND rel_travaille0.eid_to=S.cw_eid)
  1054 WHERE NOT EXISTS(SELECT 1 FROM travaille_relation AS rel_travaille0,cw_Division AS _S WHERE rel_travaille0.eid_from=_X.cw_eid AND rel_travaille0.eid_to=_S.cw_eid)
  1028 INTERSECT
  1055 INTERSECT
  1029 SELECT X.cw_nom
  1056 SELECT _X.cw_nom
  1030 FROM cw_Personne AS X
  1057 FROM cw_Personne AS _X
  1031 WHERE NOT EXISTS(SELECT 1 FROM travaille_relation AS rel_travaille0,cw_Societe AS S WHERE rel_travaille0.eid_from=X.cw_eid AND rel_travaille0.eid_to=S.cw_eid)'''),
  1058 WHERE NOT EXISTS(SELECT 1 FROM travaille_relation AS rel_travaille0,cw_Societe AS _S WHERE rel_travaille0.eid_from=_X.cw_eid AND rel_travaille0.eid_to=_S.cw_eid)'''),
  1032 
  1059 
  1033     ('Any PN WHERE NOT X travaille S, S nom PN, S is IN(Division, Societe)',
  1060     ('Any PN WHERE NOT X travaille S, S nom PN, S is IN(Division, Societe)',
  1034      '''SELECT S.cw_nom
  1061      '''SELECT _S.cw_nom
  1035 FROM cw_Division AS S
  1062 FROM cw_Division AS _S
  1036 WHERE NOT EXISTS(SELECT 1 FROM travaille_relation AS rel_travaille0 WHERE rel_travaille0.eid_to=S.cw_eid)
  1063 WHERE NOT EXISTS(SELECT 1 FROM travaille_relation AS rel_travaille0 WHERE rel_travaille0.eid_to=_S.cw_eid)
  1037 UNION ALL
  1064 UNION ALL
  1038 SELECT S.cw_nom
  1065 SELECT _S.cw_nom
  1039 FROM cw_Societe AS S
  1066 FROM cw_Societe AS _S
  1040 WHERE NOT EXISTS(SELECT 1 FROM travaille_relation AS rel_travaille0 WHERE rel_travaille0.eid_to=S.cw_eid)'''),
  1067 WHERE NOT EXISTS(SELECT 1 FROM travaille_relation AS rel_travaille0 WHERE rel_travaille0.eid_to=_S.cw_eid)'''),
  1041 
  1068 
  1042     ('Personne X WHERE NOT X travaille S, S nom "chouette"',
  1069     ('Personne X WHERE NOT X travaille S, S nom "chouette"',
  1043      '''SELECT X.cw_eid
  1070      '''SELECT _X.cw_eid
  1044 FROM cw_Division AS S, cw_Personne AS X
  1071 FROM cw_Division AS _S, cw_Personne AS _X
  1045 WHERE NOT EXISTS(SELECT 1 FROM travaille_relation AS rel_travaille0 WHERE rel_travaille0.eid_from=X.cw_eid AND rel_travaille0.eid_to=S.cw_eid) AND S.cw_nom=chouette
  1072 WHERE NOT EXISTS(SELECT 1 FROM travaille_relation AS rel_travaille0 WHERE rel_travaille0.eid_from=_X.cw_eid AND rel_travaille0.eid_to=_S.cw_eid) AND _S.cw_nom=chouette
  1046 UNION ALL
  1073 UNION ALL
  1047 SELECT X.cw_eid
  1074 SELECT _X.cw_eid
  1048 FROM cw_Personne AS X, cw_Societe AS S
  1075 FROM cw_Personne AS _X, cw_Societe AS _S
  1049 WHERE NOT EXISTS(SELECT 1 FROM travaille_relation AS rel_travaille0 WHERE rel_travaille0.eid_from=X.cw_eid AND rel_travaille0.eid_to=S.cw_eid) AND S.cw_nom=chouette
  1076 WHERE NOT EXISTS(SELECT 1 FROM travaille_relation AS rel_travaille0 WHERE rel_travaille0.eid_from=_X.cw_eid AND rel_travaille0.eid_to=_S.cw_eid) AND _S.cw_nom=chouette
  1050 UNION ALL
  1077 UNION ALL
  1051 SELECT X.cw_eid
  1078 SELECT _X.cw_eid
  1052 FROM cw_Personne AS X, cw_SubDivision AS S
  1079 FROM cw_Personne AS _X, cw_SubDivision AS _S
  1053 WHERE NOT EXISTS(SELECT 1 FROM travaille_relation AS rel_travaille0 WHERE rel_travaille0.eid_from=X.cw_eid AND rel_travaille0.eid_to=S.cw_eid) AND S.cw_nom=chouette'''),
  1080 WHERE NOT EXISTS(SELECT 1 FROM travaille_relation AS rel_travaille0 WHERE rel_travaille0.eid_from=_X.cw_eid AND rel_travaille0.eid_to=_S.cw_eid) AND _S.cw_nom=chouette'''),
  1054 
  1081 
  1055     ('Any X WHERE X is ET, ET eid 2',
  1082     ('Any X WHERE X is ET, ET eid 2',
  1056      '''SELECT rel_is0.eid_from
  1083      '''SELECT rel_is0.eid_from
  1057 FROM is_relation AS rel_is0
  1084 FROM is_relation AS rel_is0
  1058 WHERE rel_is0.eid_to=2'''),
  1085 WHERE rel_is0.eid_to=2'''),
  1141             raise
  1168             raise
  1142         return
  1169         return
  1143 
  1170 
  1144     def test1(self):
  1171     def test1(self):
  1145         self._checkall('Any count(RDEF) WHERE RDEF relation_type X, X eid %(x)s',
  1172         self._checkall('Any count(RDEF) WHERE RDEF relation_type X, X eid %(x)s',
  1146                        ("""SELECT COUNT(T1.C0) FROM (SELECT RDEF.cw_eid AS C0
  1173                        ("""SELECT COUNT(T1.C0) FROM (SELECT _RDEF.cw_eid AS C0
  1147 FROM cw_CWAttribute AS RDEF
  1174 FROM cw_CWAttribute AS _RDEF
  1148 WHERE RDEF.cw_relation_type=%(x)s
  1175 WHERE _RDEF.cw_relation_type=%(x)s
  1149 UNION ALL
  1176 UNION ALL
  1150 SELECT RDEF.cw_eid AS C0
  1177 SELECT _RDEF.cw_eid AS C0
  1151 FROM cw_CWRelation AS RDEF
  1178 FROM cw_CWRelation AS _RDEF
  1152 WHERE RDEF.cw_relation_type=%(x)s) AS T1""", {}),
  1179 WHERE _RDEF.cw_relation_type=%(x)s) AS T1""", {}),
  1153                        )
  1180                        )
  1154 
  1181 
  1155     def test2(self):
  1182     def test2(self):
  1156         self._checkall('Any X WHERE C comments X, C eid %(x)s',
  1183         self._checkall('Any X WHERE C comments X, C eid %(x)s',
  1157                        ('''SELECT rel_comments0.eid_to
  1184                        ('''SELECT rel_comments0.eid_to
  1171 WHERE rel_in_basket0.eid_to=12''')
  1198 WHERE rel_in_basket0.eid_to=12''')
  1172 
  1199 
  1173     def test_varmap1(self):
  1200     def test_varmap1(self):
  1174         self._check('Any X,L WHERE X is CWUser, X in_group G, X login L, G name "users"',
  1201         self._check('Any X,L WHERE X is CWUser, X in_group G, X login L, G name "users"',
  1175                     '''SELECT T00.x, T00.l
  1202                     '''SELECT T00.x, T00.l
  1176 FROM T00, cw_CWGroup AS G, in_group_relation AS rel_in_group0
  1203 FROM T00, cw_CWGroup AS _G, in_group_relation AS rel_in_group0
  1177 WHERE rel_in_group0.eid_from=T00.x AND rel_in_group0.eid_to=G.cw_eid AND G.cw_name=users''',
  1204 WHERE rel_in_group0.eid_from=T00.x AND rel_in_group0.eid_to=_G.cw_eid AND _G.cw_name=users''',
  1178                     varmap={'X': 'T00.x', 'X.login': 'T00.l'})
  1205                     varmap={'X': 'T00.x', 'X.login': 'T00.l'})
  1179 
  1206 
  1180     def test_varmap2(self):
  1207     def test_varmap2(self):
  1181         self._check('Any X,L,GN WHERE X is CWUser, X in_group G, X login L, G name GN',
  1208         self._check('Any X,L,GN WHERE X is CWUser, X in_group G, X login L, G name GN',
  1182                     '''SELECT T00.x, T00.l, G.cw_name
  1209                     '''SELECT T00.x, T00.l, _G.cw_name
  1183 FROM T00, cw_CWGroup AS G, in_group_relation AS rel_in_group0
  1210 FROM T00, cw_CWGroup AS _G, in_group_relation AS rel_in_group0
  1184 WHERE rel_in_group0.eid_from=T00.x AND rel_in_group0.eid_to=G.cw_eid''',
  1211 WHERE rel_in_group0.eid_from=T00.x AND rel_in_group0.eid_to=_G.cw_eid''',
  1185                     varmap={'X': 'T00.x', 'X.login': 'T00.l'})
  1212                     varmap={'X': 'T00.x', 'X.login': 'T00.l'})
  1186 
  1213 
  1187     def test_varmap3(self):
  1214     def test_varmap3(self):
  1188         self._check('Any %(x)s,D WHERE F data D, F is File',
  1215         self._check('Any %(x)s,D WHERE F data D, F is File',
  1189                     'SELECT 728, _TDF0.C0\nFROM _TDF0',
  1216                     'SELECT 728, _TDF0.C0\nFROM _TDF0',
  1192 
  1219 
  1193     def test_is_null_transform(self):
  1220     def test_is_null_transform(self):
  1194         union = self._prepare('Any X WHERE X login %(login)s')
  1221         union = self._prepare('Any X WHERE X login %(login)s')
  1195         r, args = self.o.generate(union, {'login': None})
  1222         r, args = self.o.generate(union, {'login': None})
  1196         self.assertLinesEquals((r % args).strip(),
  1223         self.assertLinesEquals((r % args).strip(),
  1197                                '''SELECT X.cw_eid
  1224                                '''SELECT _X.cw_eid
  1198 FROM cw_CWUser AS X
  1225 FROM cw_CWUser AS _X
  1199 WHERE X.cw_login IS NULL''')
  1226 WHERE _X.cw_login IS NULL''')
  1200 
  1227 
  1201     def test_parser_parse(self):
  1228     def test_parser_parse(self):
  1202         for t in self._parse(PARSER):
  1229         for t in self._parse(PARSER):
  1203             yield t
  1230             yield t
  1204 
  1231 
  1237     def test_union(self):
  1264     def test_union(self):
  1238         for t in self._parse((
  1265         for t in self._parse((
  1239             ('(Any N ORDERBY 1 WHERE X name N, X is State)'
  1266             ('(Any N ORDERBY 1 WHERE X name N, X is State)'
  1240              ' UNION '
  1267              ' UNION '
  1241              '(Any NN ORDERBY 1 WHERE XX name NN, XX is Transition)',
  1268              '(Any NN ORDERBY 1 WHERE XX name NN, XX is Transition)',
  1242              '''(SELECT X.cw_name
  1269              '''(SELECT _X.cw_name
  1243 FROM cw_State AS X
  1270 FROM cw_State AS _X
  1244 ORDER BY 1)
  1271 ORDER BY 1)
  1245 UNION ALL
  1272 UNION ALL
  1246 (SELECT XX.cw_name
  1273 (SELECT _XX.cw_name
  1247 FROM cw_Transition AS XX
  1274 FROM cw_Transition AS _XX
  1248 ORDER BY 1)'''),
  1275 ORDER BY 1)'''),
  1249             )):
  1276             )):
  1250             yield t
  1277             yield t
  1251 
  1278 
  1252     def test_subquery(self):
  1279     def test_subquery(self):
  1255             ('Any N ORDERBY 1 WITH N BEING '
  1282             ('Any N ORDERBY 1 WITH N BEING '
  1256              '((Any N WHERE X name N, X is State)'
  1283              '((Any N WHERE X name N, X is State)'
  1257              ' UNION '
  1284              ' UNION '
  1258              '(Any NN WHERE XX name NN, XX is Transition))',
  1285              '(Any NN WHERE XX name NN, XX is Transition))',
  1259              '''SELECT _T0.C0
  1286              '''SELECT _T0.C0
  1260 FROM ((SELECT X.cw_name AS C0
  1287 FROM ((SELECT _X.cw_name AS C0
  1261 FROM cw_State AS X)
  1288 FROM cw_State AS _X)
  1262 UNION ALL
  1289 UNION ALL
  1263 (SELECT XX.cw_name AS C0
  1290 (SELECT _XX.cw_name AS C0
  1264 FROM cw_Transition AS XX)) AS _T0
  1291 FROM cw_Transition AS _XX)) AS _T0
  1265 ORDER BY 1'''),
  1292 ORDER BY 1'''),
  1266 
  1293 
  1267             ('Any N,NX ORDERBY NX WITH N,NX BEING '
  1294             ('Any N,NX ORDERBY NX WITH N,NX BEING '
  1268              '((Any N,COUNT(X) GROUPBY N WHERE X name N, X is State HAVING COUNT(X)>1)'
  1295              '((Any N,COUNT(X) GROUPBY N WHERE X name N, X is State HAVING COUNT(X)>1)'
  1269              ' UNION '
  1296              ' UNION '
  1270              '(Any N,COUNT(X) GROUPBY N WHERE X name N, X is Transition HAVING COUNT(X)>1))',
  1297              '(Any N,COUNT(X) GROUPBY N WHERE X name N, X is Transition HAVING COUNT(X)>1))',
  1271              '''SELECT _T0.C0, _T0.C1
  1298              '''SELECT _T0.C0, _T0.C1
  1272 FROM ((SELECT X.cw_name AS C0, COUNT(X.cw_eid) AS C1
  1299 FROM ((SELECT _X.cw_name AS C0, COUNT(_X.cw_eid) AS C1
  1273 FROM cw_State AS X
  1300 FROM cw_State AS _X
  1274 GROUP BY X.cw_name
  1301 GROUP BY _X.cw_name
  1275 HAVING COUNT(X.cw_eid)>1)
  1302 HAVING COUNT(_X.cw_eid)>1)
  1276 UNION ALL
  1303 UNION ALL
  1277 (SELECT X.cw_name AS C0, COUNT(X.cw_eid) AS C1
  1304 (SELECT _X.cw_name AS C0, COUNT(_X.cw_eid) AS C1
  1278 FROM cw_Transition AS X
  1305 FROM cw_Transition AS _X
  1279 GROUP BY X.cw_name
  1306 GROUP BY _X.cw_name
  1280 HAVING COUNT(X.cw_eid)>1)) AS _T0
  1307 HAVING COUNT(_X.cw_eid)>1)) AS _T0
  1281 ORDER BY 2'''),
  1308 ORDER BY 2'''),
  1282 
  1309 
  1283             ('Any N,COUNT(X) GROUPBY N HAVING COUNT(X)>1 '
  1310             ('Any N,COUNT(X) GROUPBY N HAVING COUNT(X)>1 '
  1284              'WITH X, N BEING ((Any X, N WHERE X name N, X is State) UNION '
  1311              'WITH X, N BEING ((Any X, N WHERE X name N, X is State) UNION '
  1285              '                 (Any X, N WHERE X name N, X is Transition))',
  1312              '                 (Any X, N WHERE X name N, X is Transition))',
  1286              '''SELECT _T0.C1, COUNT(_T0.C0)
  1313              '''SELECT _T0.C1, COUNT(_T0.C0)
  1287 FROM ((SELECT X.cw_eid AS C0, X.cw_name AS C1
  1314 FROM ((SELECT _X.cw_eid AS C0, _X.cw_name AS C1
  1288 FROM cw_State AS X)
  1315 FROM cw_State AS _X)
  1289 UNION ALL
  1316 UNION ALL
  1290 (SELECT X.cw_eid AS C0, X.cw_name AS C1
  1317 (SELECT _X.cw_eid AS C0, _X.cw_name AS C1
  1291 FROM cw_Transition AS X)) AS _T0
  1318 FROM cw_Transition AS _X)) AS _T0
  1292 GROUP BY _T0.C1
  1319 GROUP BY _T0.C1
  1293 HAVING COUNT(_T0.C0)>1'''),
  1320 HAVING COUNT(_T0.C0)>1'''),
  1294 
  1321 
  1295             ('Any ETN,COUNT(X) GROUPBY ETN WHERE X is ET, ET name ETN '
  1322             ('Any ETN,COUNT(X) GROUPBY ETN WHERE X is ET, ET name ETN '
  1296              'WITH X BEING ((Any X WHERE X is Societe) UNION (Any X WHERE X is Affaire, (EXISTS(X owned_by 1)) OR ((EXISTS(D concerne B?, B owned_by 1, X identity D, B is Note)) OR (EXISTS(F concerne E?, E owned_by 1, E is Societe, X identity F)))))',
  1323              'WITH X BEING ((Any X WHERE X is Societe) UNION (Any X WHERE X is Affaire, (EXISTS(X owned_by 1)) OR ((EXISTS(D concerne B?, B owned_by 1, X identity D, B is Note)) OR (EXISTS(F concerne E?, E owned_by 1, E is Societe, X identity F)))))',
  1297              '''SELECT ET.cw_name, COUNT(_T0.C0)
  1324              '''SELECT _ET.cw_name, COUNT(_T0.C0)
  1298 FROM ((SELECT X.cw_eid AS C0
  1325 FROM ((SELECT _X.cw_eid AS C0
  1299 FROM cw_Societe AS X)
  1326 FROM cw_Societe AS _X)
  1300 UNION ALL
  1327 UNION ALL
  1301 (SELECT X.cw_eid AS C0
  1328 (SELECT _X.cw_eid AS C0
  1302 FROM cw_Affaire AS X
  1329 FROM cw_Affaire AS _X
  1303 WHERE ((EXISTS(SELECT 1 FROM owned_by_relation AS rel_owned_by0 WHERE rel_owned_by0.eid_from=X.cw_eid AND rel_owned_by0.eid_to=1)) OR (((EXISTS(SELECT 1 FROM cw_Affaire AS D LEFT OUTER JOIN concerne_relation AS rel_concerne1 ON (rel_concerne1.eid_from=D.cw_eid) LEFT OUTER JOIN cw_Note AS B ON (rel_concerne1.eid_to=B.cw_eid), owned_by_relation AS rel_owned_by2 WHERE rel_owned_by2.eid_from=B.cw_eid AND rel_owned_by2.eid_to=1 AND X.cw_eid=D.cw_eid)) OR (EXISTS(SELECT 1 FROM cw_Affaire AS F LEFT OUTER JOIN concerne_relation AS rel_concerne3 ON (rel_concerne3.eid_from=F.cw_eid) LEFT OUTER JOIN cw_Societe AS E ON (rel_concerne3.eid_to=E.cw_eid), owned_by_relation AS rel_owned_by4 WHERE rel_owned_by4.eid_from=E.cw_eid AND rel_owned_by4.eid_to=1 AND X.cw_eid=F.cw_eid))))))) AS _T0, cw_CWEType AS ET, is_relation AS rel_is0
  1330 WHERE ((EXISTS(SELECT 1 FROM owned_by_relation AS rel_owned_by0 WHERE rel_owned_by0.eid_from=_X.cw_eid AND rel_owned_by0.eid_to=1)) OR (((EXISTS(SELECT 1 FROM cw_Affaire AS _D LEFT OUTER JOIN concerne_relation AS rel_concerne1 ON (rel_concerne1.eid_from=_D.cw_eid) LEFT OUTER JOIN cw_Note AS _B ON (rel_concerne1.eid_to=_B.cw_eid), owned_by_relation AS rel_owned_by2 WHERE rel_owned_by2.eid_from=_B.cw_eid AND rel_owned_by2.eid_to=1 AND _X.cw_eid=_D.cw_eid)) OR (EXISTS(SELECT 1 FROM cw_Affaire AS _F LEFT OUTER JOIN concerne_relation AS rel_concerne3 ON (rel_concerne3.eid_from=_F.cw_eid) LEFT OUTER JOIN cw_Societe AS _E ON (rel_concerne3.eid_to=_E.cw_eid), owned_by_relation AS rel_owned_by4 WHERE rel_owned_by4.eid_from=_E.cw_eid AND rel_owned_by4.eid_to=1 AND _X.cw_eid=_F.cw_eid))))))) AS _T0, cw_CWEType AS _ET, is_relation AS rel_is0
  1304 WHERE rel_is0.eid_from=_T0.C0 AND rel_is0.eid_to=ET.cw_eid
  1331 WHERE rel_is0.eid_from=_T0.C0 AND rel_is0.eid_to=_ET.cw_eid
  1305 GROUP BY ET.cw_name'''),
  1332 GROUP BY _ET.cw_name'''),
  1306             )):
  1333             )):
  1307             yield t
  1334             yield t
  1308 
  1335 
  1309 
  1336 
  1310     def test_subquery_error(self):
  1337     def test_subquery_error(self):
  1329              """SELECT appears0.uid
  1356              """SELECT appears0.uid
  1330 FROM appears AS appears0
  1357 FROM appears AS appears0
  1331 WHERE appears0.words @@ to_tsquery('default', 'toto&tata')"""),
  1358 WHERE appears0.words @@ to_tsquery('default', 'toto&tata')"""),
  1332 
  1359 
  1333             ('Personne X WHERE X has_text "toto tata"',
  1360             ('Personne X WHERE X has_text "toto tata"',
  1334              """SELECT X.eid
  1361              """SELECT _X.eid
  1335 FROM appears AS appears0, entities AS X
  1362 FROM appears AS appears0, entities AS _X
  1336 WHERE appears0.words @@ to_tsquery('default', 'toto&tata') AND appears0.uid=X.eid AND X.type='Personne'"""),
  1363 WHERE appears0.words @@ to_tsquery('default', 'toto&tata') AND appears0.uid=_X.eid AND _X.type='Personne'"""),
  1337 
  1364 
  1338             ('Personne X WHERE X has_text %(text)s',
  1365             ('Personne X WHERE X has_text %(text)s',
  1339              """SELECT X.eid
  1366              """SELECT _X.eid
  1340 FROM appears AS appears0, entities AS X
  1367 FROM appears AS appears0, entities AS _X
  1341 WHERE appears0.words @@ to_tsquery('default', 'hip&hop&momo') AND appears0.uid=X.eid AND X.type='Personne'"""),
  1368 WHERE appears0.words @@ to_tsquery('default', 'hip&hop&momo') AND appears0.uid=_X.eid AND _X.type='Personne'"""),
  1342 
  1369 
  1343             ('Any X WHERE X has_text "toto tata", X name "tutu", X is IN (Basket,Folder)',
  1370             ('Any X WHERE X has_text "toto tata", X name "tutu", X is IN (Basket,Folder)',
  1344              """SELECT X.cw_eid
  1371              """SELECT _X.cw_eid
  1345 FROM appears AS appears0, cw_Basket AS X
  1372 FROM appears AS appears0, cw_Basket AS _X
  1346 WHERE appears0.words @@ to_tsquery('default', 'toto&tata') AND appears0.uid=X.cw_eid AND X.cw_name=tutu
  1373 WHERE appears0.words @@ to_tsquery('default', 'toto&tata') AND appears0.uid=_X.cw_eid AND _X.cw_name=tutu
  1347 UNION ALL
  1374 UNION ALL
  1348 SELECT X.cw_eid
  1375 SELECT _X.cw_eid
  1349 FROM appears AS appears0, cw_Folder AS X
  1376 FROM appears AS appears0, cw_Folder AS _X
  1350 WHERE appears0.words @@ to_tsquery('default', 'toto&tata') AND appears0.uid=X.cw_eid AND X.cw_name=tutu
  1377 WHERE appears0.words @@ to_tsquery('default', 'toto&tata') AND appears0.uid=_X.cw_eid AND _X.cw_name=tutu
  1351 """),
  1378 """),
  1352 
  1379 
  1353             ('Personne X where X has_text %(text)s, X travaille S, S has_text %(text)s',
  1380             ('Personne X where X has_text %(text)s, X travaille S, S has_text %(text)s',
  1354              """SELECT X.eid
  1381              """SELECT _X.eid
  1355 FROM appears AS appears0, appears AS appears2, entities AS X, travaille_relation AS rel_travaille1
  1382 FROM appears AS appears0, appears AS appears2, entities AS _X, travaille_relation AS rel_travaille1
  1356 WHERE appears0.words @@ to_tsquery('default', 'hip&hop&momo') AND appears0.uid=X.eid AND X.type='Personne' AND X.eid=rel_travaille1.eid_from AND appears2.uid=rel_travaille1.eid_to AND appears2.words @@ to_tsquery('default', 'hip&hop&momo')"""),
  1383 WHERE appears0.words @@ to_tsquery('default', 'hip&hop&momo') AND appears0.uid=_X.eid AND _X.type='Personne' AND _X.eid=rel_travaille1.eid_from AND appears2.uid=rel_travaille1.eid_to AND appears2.words @@ to_tsquery('default', 'hip&hop&momo')"""),
  1357             )):
  1384             )):
  1358             yield t
  1385             yield t
  1359 
  1386 
  1360 
  1387 
  1361     def test_from_clause_needed(self):
  1388     def test_from_clause_needed(self):
  1362         queries = [("Any 1 WHERE EXISTS(T is CWGroup, T name 'managers')",
  1389         queries = [("Any 1 WHERE EXISTS(T is CWGroup, T name 'managers')",
  1363                     '''SELECT 1
  1390                     '''SELECT 1
  1364 WHERE EXISTS(SELECT 1 FROM cw_CWGroup AS T WHERE T.cw_name=managers)'''),
  1391 WHERE EXISTS(SELECT 1 FROM cw_CWGroup AS _T WHERE _T.cw_name=managers)'''),
  1365                    ('Any X,Y WHERE NOT X created_by Y, X eid 5, Y eid 6',
  1392                    ('Any X,Y WHERE NOT X created_by Y, X eid 5, Y eid 6',
  1366                     '''SELECT 5, 6
  1393                     '''SELECT 5, 6
  1367 WHERE NOT EXISTS(SELECT 1 FROM created_by_relation AS rel_created_by0 WHERE rel_created_by0.eid_from=5 AND rel_created_by0.eid_to=6)'''),
  1394 WHERE NOT EXISTS(SELECT 1 FROM created_by_relation AS rel_created_by0 WHERE rel_created_by0.eid_from=5 AND rel_created_by0.eid_to=6)'''),
  1368                    ]
  1395                    ]
  1369         for t in self._parse(queries):
  1396         for t in self._parse(queries):
  1370             yield t
  1397             yield t
  1371 
  1398 
  1372     def test_ambigous_exists_no_from_clause(self):
  1399     def test_ambigous_exists_no_from_clause(self):
  1373         self._check('Any COUNT(U) WHERE U eid 1, EXISTS (P owned_by U, P is IN (Note, Affaire))',
  1400         self._check('Any COUNT(U) WHERE U eid 1, EXISTS (P owned_by U, P is IN (Note, Affaire))',
  1374                     '''SELECT COUNT(1)
  1401                     '''SELECT COUNT(1)
  1375 WHERE EXISTS(SELECT 1 FROM owned_by_relation AS rel_owned_by0, cw_Affaire AS P WHERE rel_owned_by0.eid_from=P.cw_eid AND rel_owned_by0.eid_to=1 UNION SELECT 1 FROM owned_by_relation AS rel_owned_by1, cw_Note AS P WHERE rel_owned_by1.eid_from=P.cw_eid AND rel_owned_by1.eid_to=1)''')
  1402 WHERE EXISTS(SELECT 1 FROM owned_by_relation AS rel_owned_by0, cw_Affaire AS _P WHERE rel_owned_by0.eid_from=_P.cw_eid AND rel_owned_by0.eid_to=1 UNION SELECT 1 FROM owned_by_relation AS rel_owned_by1, cw_Note AS _P WHERE rel_owned_by1.eid_from=_P.cw_eid AND rel_owned_by1.eid_to=1)''')
  1376 
  1403 
  1377     def test_attr_map(self):
  1404     def test_attr_map(self):
  1378         def generate_ref(gen, linkedvar, rel):
  1405         def generate_ref(gen, linkedvar, rel):
  1379             linkedvar.accept(gen)
  1406             linkedvar.accept(gen)
  1380             return 'VERSION_DATA(%s)' % linkedvar._q_sql
  1407             return 'VERSION_DATA(%s)' % linkedvar._q_sql
  1381         self.o.attr_map['Affaire.ref'] = generate_ref
  1408         self.o.attr_map['Affaire.ref'] = generate_ref
  1382         try:
  1409         try:
  1383             self._check('Any R WHERE X ref R',
  1410             self._check('Any R WHERE X ref R',
  1384                         '''SELECT VERSION_DATA(X.cw_eid)
  1411                         '''SELECT VERSION_DATA(_X.cw_eid)
  1385 FROM cw_Affaire AS X''')
  1412 FROM cw_Affaire AS _X''')
  1386             self._check('Any X WHERE X ref 1',
  1413             self._check('Any X WHERE X ref 1',
  1387                         '''SELECT X.cw_eid
  1414                         '''SELECT _X.cw_eid
  1388 FROM cw_Affaire AS X
  1415 FROM cw_Affaire AS _X
  1389 WHERE VERSION_DATA(X.cw_eid)=1''')
  1416 WHERE VERSION_DATA(_X.cw_eid)=1''')
  1390         finally:
  1417         finally:
  1391             self.o.attr_map.clear()
  1418             self.o.attr_map.clear()
  1392 
  1419 
  1393 
  1420 
  1394 class SqliteSQLGeneratorTC(PostgresSQLGeneratorTC):
  1421 class SqliteSQLGeneratorTC(PostgresSQLGeneratorTC):
  1409     def test_union(self):
  1436     def test_union(self):
  1410         for t in self._parse((
  1437         for t in self._parse((
  1411             ('(Any N ORDERBY 1 WHERE X name N, X is State)'
  1438             ('(Any N ORDERBY 1 WHERE X name N, X is State)'
  1412              ' UNION '
  1439              ' UNION '
  1413              '(Any NN ORDERBY 1 WHERE XX name NN, XX is Transition)',
  1440              '(Any NN ORDERBY 1 WHERE XX name NN, XX is Transition)',
  1414              '''SELECT X.cw_name
  1441              '''SELECT _X.cw_name
  1415 FROM cw_State AS X
  1442 FROM cw_State AS _X
  1416 ORDER BY 1
  1443 ORDER BY 1
  1417 UNION ALL
  1444 UNION ALL
  1418 SELECT XX.cw_name
  1445 SELECT _XX.cw_name
  1419 FROM cw_Transition AS XX
  1446 FROM cw_Transition AS _XX
  1420 ORDER BY 1'''),
  1447 ORDER BY 1'''),
  1421             )):
  1448             )):
  1422             yield t
  1449             yield t
  1423 
  1450 
  1424 
  1451 
  1429             ('Any N ORDERBY 1 WITH N BEING '
  1456             ('Any N ORDERBY 1 WITH N BEING '
  1430              '((Any N WHERE X name N, X is State)'
  1457              '((Any N WHERE X name N, X is State)'
  1431              ' UNION '
  1458              ' UNION '
  1432              '(Any NN WHERE XX name NN, XX is Transition))',
  1459              '(Any NN WHERE XX name NN, XX is Transition))',
  1433              '''SELECT _T0.C0
  1460              '''SELECT _T0.C0
  1434 FROM (SELECT X.cw_name AS C0
  1461 FROM (SELECT _X.cw_name AS C0
  1435 FROM cw_State AS X
  1462 FROM cw_State AS _X
  1436 UNION ALL
  1463 UNION ALL
  1437 SELECT XX.cw_name AS C0
  1464 SELECT _XX.cw_name AS C0
  1438 FROM cw_Transition AS XX) AS _T0
  1465 FROM cw_Transition AS _XX) AS _T0
  1439 ORDER BY 1'''),
  1466 ORDER BY 1'''),
  1440 
  1467 
  1441             ('Any N,NX ORDERBY NX WITH N,NX BEING '
  1468             ('Any N,NX ORDERBY NX WITH N,NX BEING '
  1442              '((Any N,COUNT(X) GROUPBY N WHERE X name N, X is State HAVING COUNT(X)>1)'
  1469              '((Any N,COUNT(X) GROUPBY N WHERE X name N, X is State HAVING COUNT(X)>1)'
  1443              ' UNION '
  1470              ' UNION '
  1444              '(Any N,COUNT(X) GROUPBY N WHERE X name N, X is Transition HAVING COUNT(X)>1))',
  1471              '(Any N,COUNT(X) GROUPBY N WHERE X name N, X is Transition HAVING COUNT(X)>1))',
  1445              '''SELECT _T0.C0, _T0.C1
  1472              '''SELECT _T0.C0, _T0.C1
  1446 FROM (SELECT X.cw_name AS C0, COUNT(X.cw_eid) AS C1
  1473 FROM (SELECT _X.cw_name AS C0, COUNT(_X.cw_eid) AS C1
  1447 FROM cw_State AS X
  1474 FROM cw_State AS _X
  1448 GROUP BY X.cw_name
  1475 GROUP BY _X.cw_name
  1449 HAVING COUNT(X.cw_eid)>1
  1476 HAVING COUNT(_X.cw_eid)>1
  1450 UNION ALL
  1477 UNION ALL
  1451 SELECT X.cw_name AS C0, COUNT(X.cw_eid) AS C1
  1478 SELECT _X.cw_name AS C0, COUNT(_X.cw_eid) AS C1
  1452 FROM cw_Transition AS X
  1479 FROM cw_Transition AS _X
  1453 GROUP BY X.cw_name
  1480 GROUP BY _X.cw_name
  1454 HAVING COUNT(X.cw_eid)>1) AS _T0
  1481 HAVING COUNT(_X.cw_eid)>1) AS _T0
  1455 ORDER BY 2'''),
  1482 ORDER BY 2'''),
  1456 
  1483 
  1457             ('Any N,COUNT(X) GROUPBY N HAVING COUNT(X)>1 '
  1484             ('Any N,COUNT(X) GROUPBY N HAVING COUNT(X)>1 '
  1458              'WITH X, N BEING ((Any X, N WHERE X name N, X is State) UNION '
  1485              'WITH X, N BEING ((Any X, N WHERE X name N, X is State) UNION '
  1459              '                 (Any X, N WHERE X name N, X is Transition))',
  1486              '                 (Any X, N WHERE X name N, X is Transition))',
  1460              '''SELECT _T0.C1, COUNT(_T0.C0)
  1487              '''SELECT _T0.C1, COUNT(_T0.C0)
  1461 FROM (SELECT X.cw_eid AS C0, X.cw_name AS C1
  1488 FROM (SELECT _X.cw_eid AS C0, _X.cw_name AS C1
  1462 FROM cw_State AS X
  1489 FROM cw_State AS _X
  1463 UNION ALL
  1490 UNION ALL
  1464 SELECT X.cw_eid AS C0, X.cw_name AS C1
  1491 SELECT _X.cw_eid AS C0, _X.cw_name AS C1
  1465 FROM cw_Transition AS X) AS _T0
  1492 FROM cw_Transition AS _X) AS _T0
  1466 GROUP BY _T0.C1
  1493 GROUP BY _T0.C1
  1467 HAVING COUNT(_T0.C0)>1'''),
  1494 HAVING COUNT(_T0.C0)>1'''),
  1468             )):
  1495             )):
  1469             yield t
  1496             yield t
  1470 
  1497 
  1479              """SELECT appears0.uid
  1506              """SELECT appears0.uid
  1480 FROM appears AS appears0
  1507 FROM appears AS appears0
  1481 WHERE appears0.word_id IN (SELECT word_id FROM word WHERE word in ('hip', 'hop', 'momo'))"""),
  1508 WHERE appears0.word_id IN (SELECT word_id FROM word WHERE word in ('hip', 'hop', 'momo'))"""),
  1482 
  1509 
  1483             ('Personne X WHERE X has_text "toto tata"',
  1510             ('Personne X WHERE X has_text "toto tata"',
  1484              """SELECT X.eid
  1511              """SELECT _X.eid
  1485 FROM appears AS appears0, entities AS X
  1512 FROM appears AS appears0, entities AS _X
  1486 WHERE appears0.word_id IN (SELECT word_id FROM word WHERE word in ('toto', 'tata')) AND appears0.uid=X.eid AND X.type='Personne'"""),
  1513 WHERE appears0.word_id IN (SELECT word_id FROM word WHERE word in ('toto', 'tata')) AND appears0.uid=_X.eid AND _X.type='Personne'"""),
  1487 
  1514 
  1488             ('Any X WHERE X has_text "toto tata", X name "tutu", X is IN (Basket,Folder)',
  1515             ('Any X WHERE X has_text "toto tata", X name "tutu", X is IN (Basket,Folder)',
  1489              """SELECT X.cw_eid
  1516              """SELECT _X.cw_eid
  1490 FROM appears AS appears0, cw_Basket AS X
  1517 FROM appears AS appears0, cw_Basket AS _X
  1491 WHERE appears0.word_id IN (SELECT word_id FROM word WHERE word in ('toto', 'tata')) AND appears0.uid=X.cw_eid AND X.cw_name=tutu
  1518 WHERE appears0.word_id IN (SELECT word_id FROM word WHERE word in ('toto', 'tata')) AND appears0.uid=_X.cw_eid AND _X.cw_name=tutu
  1492 UNION ALL
  1519 UNION ALL
  1493 SELECT X.cw_eid
  1520 SELECT _X.cw_eid
  1494 FROM appears AS appears0, cw_Folder AS X
  1521 FROM appears AS appears0, cw_Folder AS _X
  1495 WHERE appears0.word_id IN (SELECT word_id FROM word WHERE word in ('toto', 'tata')) AND appears0.uid=X.cw_eid AND X.cw_name=tutu
  1522 WHERE appears0.word_id IN (SELECT word_id FROM word WHERE word in ('toto', 'tata')) AND appears0.uid=_X.cw_eid AND _X.cw_name=tutu
  1496 """),
  1523 """),
  1497             )):
  1524             )):
  1498             yield t
  1525             yield t
  1499 
  1526 
  1500 
  1527 
  1516 
  1543 
  1517     def test_from_clause_needed(self):
  1544     def test_from_clause_needed(self):
  1518         queries = [("Any 1 WHERE EXISTS(T is CWGroup, T name 'managers')",
  1545         queries = [("Any 1 WHERE EXISTS(T is CWGroup, T name 'managers')",
  1519                     '''SELECT 1
  1546                     '''SELECT 1
  1520 FROM (SELECT 1) AS _T
  1547 FROM (SELECT 1) AS _T
  1521 WHERE EXISTS(SELECT 1 FROM cw_CWGroup AS T WHERE T.cw_name=managers)'''),
  1548 WHERE EXISTS(SELECT 1 FROM cw_CWGroup AS _T WHERE _T.cw_name=managers)'''),
  1522                    ('Any X,Y WHERE NOT X created_by Y, X eid 5, Y eid 6',
  1549                    ('Any X,Y WHERE NOT X created_by Y, X eid 5, Y eid 6',
  1523                     '''SELECT 5, 6
  1550                     '''SELECT 5, 6
  1524 FROM (SELECT 1) AS _T
  1551 FROM (SELECT 1) AS _T
  1525 WHERE NOT EXISTS(SELECT 1 FROM created_by_relation AS rel_created_by0 WHERE rel_created_by0.eid_from=5 AND rel_created_by0.eid_to=6)'''),
  1552 WHERE NOT EXISTS(SELECT 1 FROM created_by_relation AS rel_created_by0 WHERE rel_created_by0.eid_from=5 AND rel_created_by0.eid_to=6)'''),
  1526                    ]
  1553                    ]
  1533             ('Any X WHERE X has_text "toto tata"',
  1560             ('Any X WHERE X has_text "toto tata"',
  1534              """SELECT appears0.uid
  1561              """SELECT appears0.uid
  1535 FROM appears AS appears0
  1562 FROM appears AS appears0
  1536 WHERE MATCH (appears0.words) AGAINST ('toto tata' IN BOOLEAN MODE)"""),
  1563 WHERE MATCH (appears0.words) AGAINST ('toto tata' IN BOOLEAN MODE)"""),
  1537             ('Personne X WHERE X has_text "toto tata"',
  1564             ('Personne X WHERE X has_text "toto tata"',
  1538              """SELECT X.eid
  1565              """SELECT _X.eid
  1539 FROM appears AS appears0, entities AS X
  1566 FROM appears AS appears0, entities AS _X
  1540 WHERE MATCH (appears0.words) AGAINST ('toto tata' IN BOOLEAN MODE) AND appears0.uid=X.eid AND X.type='Personne'"""),
  1567 WHERE MATCH (appears0.words) AGAINST ('toto tata' IN BOOLEAN MODE) AND appears0.uid=_X.eid AND _X.type='Personne'"""),
  1541             ('Personne X WHERE X has_text %(text)s',
  1568             ('Personne X WHERE X has_text %(text)s',
  1542              """SELECT X.eid
  1569              """SELECT _X.eid
  1543 FROM appears AS appears0, entities AS X
  1570 FROM appears AS appears0, entities AS _X
  1544 WHERE MATCH (appears0.words) AGAINST ('hip hop momo' IN BOOLEAN MODE) AND appears0.uid=X.eid AND X.type='Personne'"""),
  1571 WHERE MATCH (appears0.words) AGAINST ('hip hop momo' IN BOOLEAN MODE) AND appears0.uid=_X.eid AND _X.type='Personne'"""),
  1545             ('Any X WHERE X has_text "toto tata", X name "tutu", X is IN (Basket,Folder)',
  1572             ('Any X WHERE X has_text "toto tata", X name "tutu", X is IN (Basket,Folder)',
  1546              """SELECT X.cw_eid
  1573              """SELECT _X.cw_eid
  1547 FROM appears AS appears0, cw_Basket AS X
  1574 FROM appears AS appears0, cw_Basket AS _X
  1548 WHERE MATCH (appears0.words) AGAINST ('toto tata' IN BOOLEAN MODE) AND appears0.uid=X.cw_eid AND X.cw_name=tutu
  1575 WHERE MATCH (appears0.words) AGAINST ('toto tata' IN BOOLEAN MODE) AND appears0.uid=_X.cw_eid AND _X.cw_name=tutu
  1549 UNION ALL
  1576 UNION ALL
  1550 SELECT X.cw_eid
  1577 SELECT _X.cw_eid
  1551 FROM appears AS appears0, cw_Folder AS X
  1578 FROM appears AS appears0, cw_Folder AS _X
  1552 WHERE MATCH (appears0.words) AGAINST ('toto tata' IN BOOLEAN MODE) AND appears0.uid=X.cw_eid AND X.cw_name=tutu
  1579 WHERE MATCH (appears0.words) AGAINST ('toto tata' IN BOOLEAN MODE) AND appears0.uid=_X.cw_eid AND _X.cw_name=tutu
  1553 """)
  1580 """)
  1554             ]
  1581             ]
  1555         for t in self._parse(queries):
  1582         for t in self._parse(queries):
  1556             yield t
  1583             yield t
  1557 
  1584 
  1558 
  1585 
  1559     def test_ambigous_exists_no_from_clause(self):
  1586     def test_ambigous_exists_no_from_clause(self):
  1560         self._check('Any COUNT(U) WHERE U eid 1, EXISTS (P owned_by U, P is IN (Note, Affaire))',
  1587         self._check('Any COUNT(U) WHERE U eid 1, EXISTS (P owned_by U, P is IN (Note, Affaire))',
  1561                     '''SELECT COUNT(1)
  1588                     '''SELECT COUNT(1)
  1562 FROM (SELECT 1) AS _T
  1589 FROM (SELECT 1) AS _T
  1563 WHERE EXISTS(SELECT 1 FROM owned_by_relation AS rel_owned_by0, cw_Affaire AS P WHERE rel_owned_by0.eid_from=P.cw_eid AND rel_owned_by0.eid_to=1 UNION SELECT 1 FROM owned_by_relation AS rel_owned_by1, cw_Note AS P WHERE rel_owned_by1.eid_from=P.cw_eid AND rel_owned_by1.eid_to=1)''')
  1590 WHERE EXISTS(SELECT 1 FROM owned_by_relation AS rel_owned_by0, cw_Affaire AS _P WHERE rel_owned_by0.eid_from=_P.cw_eid AND rel_owned_by0.eid_to=1 UNION SELECT 1 FROM owned_by_relation AS rel_owned_by1, cw_Note AS _P WHERE rel_owned_by1.eid_from=_P.cw_eid AND rel_owned_by1.eid_to=1)''')
  1564 
  1591 
  1565 
  1592 
  1566 
  1593 
  1567 
  1594 
  1568 if __name__ == '__main__':
  1595 if __name__ == '__main__':