server/test/unittest_rql2sql.py
branchtls-sprint
changeset 1398 5fe84a5f7035
parent 1263 01152fffd593
child 1787 71c143c0ada3
equal deleted inserted replaced
1397:6cbc7bc8ea6d 1398:5fe84a5f7035
   119     ("Any X WHERE X creation_date TODAY, X is Affaire",
   119     ("Any X WHERE X creation_date TODAY, X is Affaire",
   120      '''SELECT X.cw_eid
   120      '''SELECT X.cw_eid
   121 FROM cw_Affaire AS X
   121 FROM cw_Affaire AS X
   122 WHERE DATE(X.cw_creation_date)=CURRENT_DATE'''),
   122 WHERE DATE(X.cw_creation_date)=CURRENT_DATE'''),
   123 
   123 
   124     ("Any N WHERE G is EGroup, G name N, E eid 12, E read_permission G",
   124     ("Any N WHERE G is CWGroup, G name N, E eid 12, E read_permission G",
   125      '''SELECT G.cw_name
   125      '''SELECT G.cw_name
   126 FROM cw_EGroup AS G, read_permission_relation AS rel_read_permission0
   126 FROM cw_CWGroup AS G, read_permission_relation AS rel_read_permission0
   127 WHERE rel_read_permission0.eid_from=12 AND rel_read_permission0.eid_to=G.cw_eid'''),
   127 WHERE rel_read_permission0.eid_from=12 AND rel_read_permission0.eid_to=G.cw_eid'''),
   128 
   128 
   129     ('Any Y WHERE U login "admin", U login Y', # stupid but valid...
   129     ('Any Y WHERE U login "admin", U login Y', # stupid but valid...
   130      """SELECT U.cw_login
   130      """SELECT U.cw_login
   131 FROM cw_EUser AS U
   131 FROM cw_CWUser AS U
   132 WHERE U.cw_login=admin"""),
   132 WHERE U.cw_login=admin"""),
   133 
   133 
   134     ('Any T WHERE T tags X, X is State',
   134     ('Any T WHERE T tags X, X is State',
   135      '''SELECT rel_tags0.eid_from
   135      '''SELECT rel_tags0.eid_from
   136 FROM cw_State AS X, tags_relation AS rel_tags0
   136 FROM cw_State AS X, tags_relation AS rel_tags0
   143 
   143 
   144     ("Any X WHERE X prenom 'lulu',"
   144     ("Any X WHERE X prenom 'lulu',"
   145      "EXISTS(X owned_by U, U in_group G, G name 'lulufanclub' OR G name 'managers');",
   145      "EXISTS(X owned_by U, U in_group G, G name 'lulufanclub' OR G name 'managers');",
   146      '''SELECT X.cw_eid
   146      '''SELECT X.cw_eid
   147 FROM cw_Personne AS X
   147 FROM cw_Personne AS X
   148 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_EGroup 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)))'''),
   148 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)))'''),
   149 
   149 
   150     ("Any X WHERE X prenom 'lulu',"
   150     ("Any X WHERE X prenom 'lulu',"
   151      "NOT EXISTS(X owned_by U, U in_group G, G name 'lulufanclub' OR G name 'managers');",
   151      "NOT EXISTS(X owned_by U, U in_group G, G name 'lulufanclub' OR G name 'managers');",
   152      '''SELECT X.cw_eid
   152      '''SELECT X.cw_eid
   153 FROM cw_Personne AS X
   153 FROM cw_Personne AS X
   154 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_EGroup 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)))'''),
   154 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)))'''),
   155 ]
   155 ]
   156 
   156 
   157 ADVANCED= [
   157 ADVANCED= [
   158 
   158 
   159     ("Societe S WHERE S nom 'Logilab' OR S nom 'Caesium'",
   159     ("Societe S WHERE S nom 'Logilab' OR S nom 'Caesium'",
   232 FROM in_basket_relation AS rel_in_basket0
   232 FROM in_basket_relation AS rel_in_basket0
   233 WHERE rel_in_basket0.eid_to=12'''),
   233 WHERE rel_in_basket0.eid_to=12'''),
   234     
   234     
   235     ('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',
   235     ('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',
   236      '''SELECT SE.cw_name, R.cw_name, OE.cw_name
   236      '''SELECT SE.cw_name, R.cw_name, OE.cw_name
   237 FROM cw_EEType AS OE, cw_EEType AS SE, cw_EFRDef AS X, cw_ERType AS R
   237 FROM cw_CWEType AS OE, cw_CWEType AS SE, cw_CWAttribute AS X, cw_CWRType AS R
   238 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
   238 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
   239 UNION ALL
   239 UNION ALL
   240 SELECT SE.cw_name, R.cw_name, OE.cw_name
   240 SELECT SE.cw_name, R.cw_name, OE.cw_name
   241 FROM cw_EEType AS OE, cw_EEType AS SE, cw_ENFRDef AS X, cw_ERType AS R
   241 FROM cw_CWEType AS OE, cw_CWEType AS SE, cw_CWRelation AS X, cw_CWRType AS R
   242 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'''),
   242 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'''),
   243 
   243 
   244     # 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
   244     # 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
   245     ('Any O WHERE NOT S ecrit_par O, S eid 1, S inline1 P, O inline2 P',
   245     ('Any O WHERE NOT S ecrit_par O, S eid 1, S inline1 P, O inline2 P',
   246      '''SELECT DISTINCT O.cw_eid
   246      '''SELECT DISTINCT O.cw_eid
   270 FROM owned_by_relation AS rel_owned_by0
   270 FROM owned_by_relation AS rel_owned_by0
   271 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)'''),
   271 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)'''),
   272 
   272 
   273     ('Any GN WHERE X in_group G, G name GN, (G name "managers" OR EXISTS(X copain T, T login in ("comme", "cochon")))',
   273     ('Any GN WHERE X in_group G, G name GN, (G name "managers" OR EXISTS(X copain T, T login in ("comme", "cochon")))',
   274      '''SELECT G.cw_name
   274      '''SELECT G.cw_name
   275 FROM cw_EGroup AS G, in_group_relation AS rel_in_group0
   275 FROM cw_CWGroup AS G, in_group_relation AS rel_in_group0
   276 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_EUser 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))))'''),
   276 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))))'''),
   277 
   277 
   278     ('Any C WHERE C is Card, EXISTS(X documented_by C)',
   278     ('Any C WHERE C is Card, EXISTS(X documented_by C)',
   279       """SELECT C.cw_eid
   279       """SELECT C.cw_eid
   280 FROM cw_Card AS C
   280 FROM cw_Card AS C
   281 WHERE EXISTS(SELECT 1 FROM documented_by_relation AS rel_documented_by0 WHERE rel_documented_by0.eid_to=C.cw_eid)"""),
   281 WHERE EXISTS(SELECT 1 FROM documented_by_relation AS rel_documented_by0 WHERE rel_documented_by0.eid_to=C.cw_eid)"""),
   290 FROM cw_Card AS C
   290 FROM cw_Card AS C
   291 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)"""),
   291 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)"""),
   292 
   292 
   293     ('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"))',
   293     ('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"))',
   294      '''SELECT G.cw_name, X.cw_login
   294      '''SELECT G.cw_name, X.cw_login
   295 FROM cw_EGroup AS G, cw_EUser AS X, in_group_relation AS rel_in_group0
   295 FROM cw_CWGroup AS G, cw_CWUser AS X, in_group_relation AS rel_in_group0
   296 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_EUser 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))'''),
   296 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))'''),
   297 
   297 
   298     ('Any X,S, MAX(T) GROUPBY X,S ORDERBY S WHERE X is EUser, T tags X, S eid IN(32), X in_state S',
   298     ('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',
   299      '''SELECT X.cw_eid, 32, MAX(rel_tags0.eid_from)
   299      '''SELECT X.cw_eid, 32, MAX(rel_tags0.eid_from)
   300 FROM cw_EUser AS X, tags_relation AS rel_tags0
   300 FROM cw_CWUser AS X, tags_relation AS rel_tags0
   301 WHERE rel_tags0.eid_to=X.cw_eid AND X.cw_in_state=32
   301 WHERE rel_tags0.eid_to=X.cw_eid AND X.cw_in_state=32
   302 GROUP BY X.cw_eid'''),
   302 GROUP BY X.cw_eid'''),
   303 
   303 
   304     ('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"))',
   304     ('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"))',
   305      '''SELECT COUNT(rel_concerne0.eid_from), C.cw_nom
   305      '''SELECT COUNT(rel_concerne0.eid_from), C.cw_nom
   307 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)))
   307 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)))
   308 GROUP BY C.cw_nom
   308 GROUP BY C.cw_nom
   309 ORDER BY 1 DESC
   309 ORDER BY 1 DESC
   310 LIMIT 10'''),
   310 LIMIT 10'''),
   311 
   311 
   312     ('Any X WHERE Y evaluee X, Y is EUser',
   312     ('Any X WHERE Y evaluee X, Y is CWUser',
   313      '''SELECT rel_evaluee0.eid_to
   313      '''SELECT rel_evaluee0.eid_to
   314 FROM cw_EUser AS Y, evaluee_relation AS rel_evaluee0
   314 FROM cw_CWUser AS Y, evaluee_relation AS rel_evaluee0
   315 WHERE rel_evaluee0.eid_from=Y.cw_eid'''),
   315 WHERE rel_evaluee0.eid_from=Y.cw_eid'''),
   316 
   316 
   317     ('Any L WHERE X login "admin", X identity Y, Y login L',
   317     ('Any L WHERE X login "admin", X identity Y, Y login L',
   318      '''SELECT Y.cw_login
   318      '''SELECT Y.cw_login
   319 FROM cw_EUser AS X, cw_EUser AS Y
   319 FROM cw_CWUser AS X, cw_CWUser AS Y
   320 WHERE X.cw_login=admin AND X.cw_eid=Y.cw_eid'''),
   320 WHERE X.cw_login=admin AND X.cw_eid=Y.cw_eid'''),
   321 
   321 
   322     ('Any L WHERE X login "admin", NOT X identity Y, Y login L',
   322     ('Any L WHERE X login "admin", NOT X identity Y, Y login L',
   323      '''SELECT Y.cw_login
   323      '''SELECT Y.cw_login
   324 FROM cw_EUser AS X, cw_EUser AS Y
   324 FROM cw_CWUser AS X, cw_CWUser AS Y
   325 WHERE X.cw_login=admin AND NOT X.cw_eid=Y.cw_eid'''),
   325 WHERE X.cw_login=admin AND NOT X.cw_eid=Y.cw_eid'''),
   326     
   326     
   327     ('Any L WHERE X login "admin", X identity Y?, Y login L',
   327     ('Any L WHERE X login "admin", X identity Y?, Y login L',
   328      '''SELECT Y.cw_login
   328      '''SELECT Y.cw_login
   329 FROM cw_EUser AS X LEFT OUTER JOIN cw_EUser AS Y ON (X.cw_eid=Y.cw_eid)
   329 FROM cw_CWUser AS X LEFT OUTER JOIN cw_CWUser AS Y ON (X.cw_eid=Y.cw_eid)
   330 WHERE X.cw_login=admin'''),
   330 WHERE X.cw_login=admin'''),
   331 
   331 
   332     ('Any XN ORDERBY XN WHERE X name XN',
   332     ('Any XN ORDERBY XN WHERE X name XN',
   333      '''SELECT X.cw_name
   333      '''SELECT X.cw_name
   334 FROM cw_Basket AS X
   334 FROM cw_Basket AS X
   335 UNION ALL
   335 UNION ALL
   336 SELECT X.cw_name
   336 SELECT X.cw_name
   337 FROM cw_ECache AS X
   337 FROM cw_CWCache AS X
   338 UNION ALL
   338 UNION ALL
   339 SELECT X.cw_name
   339 SELECT X.cw_name
   340 FROM cw_EConstraintType AS X
   340 FROM cw_CWConstraintType AS X
   341 UNION ALL
   341 UNION ALL
   342 SELECT X.cw_name
   342 SELECT X.cw_name
   343 FROM cw_EEType AS X
   343 FROM cw_CWEType AS X
   344 UNION ALL
   344 UNION ALL
   345 SELECT X.cw_name
   345 SELECT X.cw_name
   346 FROM cw_EGroup AS X
   346 FROM cw_CWGroup AS X
   347 UNION ALL
   347 UNION ALL
   348 SELECT X.cw_name
   348 SELECT X.cw_name
   349 FROM cw_EPermission AS X
   349 FROM cw_CWPermission AS X
   350 UNION ALL
   350 UNION ALL
   351 SELECT X.cw_name
   351 SELECT X.cw_name
   352 FROM cw_ERType AS X
   352 FROM cw_CWRType AS X
   353 UNION ALL
   353 UNION ALL
   354 SELECT X.cw_name
   354 SELECT X.cw_name
   355 FROM cw_File AS X
   355 FROM cw_File AS X
   356 UNION ALL
   356 UNION ALL
   357 SELECT X.cw_name
   357 SELECT X.cw_name
   374 #     ''''''),
   374 #     ''''''),
   375 #    ('Any XN, COUNT(X) WHERE X name XN GROUPBY XN',
   375 #    ('Any XN, COUNT(X) WHERE X name XN GROUPBY XN',
   376 #     ''''''),
   376 #     ''''''),
   377 
   377 
   378     # DISTINCT, can use relatin under exists scope as principal
   378     # DISTINCT, can use relatin under exists scope as principal
   379     ('DISTINCT Any X,Y WHERE X name "EGroup", Y eid IN(1, 2, 3), EXISTS(X read_permission Y)',
   379     ('DISTINCT Any X,Y WHERE X name "CWGroup", Y eid IN(1, 2, 3), EXISTS(X read_permission Y)',
   380      '''SELECT DISTINCT X.cw_eid, rel_read_permission0.eid_to
   380      '''SELECT DISTINCT X.cw_eid, rel_read_permission0.eid_to
   381 FROM cw_EEType AS X, read_permission_relation AS rel_read_permission0
   381 FROM cw_CWEType AS X, read_permission_relation AS rel_read_permission0
   382 WHERE X.cw_name=EGroup AND rel_read_permission0.eid_to IN(1, 2, 3) AND EXISTS(SELECT 1 WHERE rel_read_permission0.eid_from=X.cw_eid)
   382 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)
   383 UNION
   383 UNION
   384 SELECT DISTINCT X.cw_eid, rel_read_permission0.eid_to
   384 SELECT DISTINCT X.cw_eid, rel_read_permission0.eid_to
   385 FROM cw_ERType AS X, read_permission_relation AS rel_read_permission0
   385 FROM cw_CWRType AS X, read_permission_relation AS rel_read_permission0
   386 WHERE X.cw_name=EGroup AND rel_read_permission0.eid_to IN(1, 2, 3) AND EXISTS(SELECT 1 WHERE rel_read_permission0.eid_from=X.cw_eid)'''),
   386 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)'''),
   387 
   387 
   388     # no distinct, Y can't be invariant
   388     # no distinct, Y can't be invariant
   389     ('Any X,Y WHERE X name "EGroup", Y eid IN(1, 2, 3), EXISTS(X read_permission Y)',
   389     ('Any X,Y WHERE X name "CWGroup", Y eid IN(1, 2, 3), EXISTS(X read_permission Y)',
   390      '''SELECT X.cw_eid, Y.cw_eid
   390      '''SELECT X.cw_eid, Y.cw_eid
   391 FROM cw_EEType AS X, cw_EGroup AS Y
   391 FROM cw_CWEType AS X, cw_CWGroup AS Y
   392 WHERE X.cw_name=EGroup 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)
   392 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)
   393 UNION ALL
   393 UNION ALL
   394 SELECT X.cw_eid, Y.cw_eid
   394 SELECT X.cw_eid, Y.cw_eid
   395 FROM cw_EEType AS X, cw_RQLExpression AS Y
   395 FROM cw_CWEType AS X, cw_RQLExpression AS Y
   396 WHERE X.cw_name=EGroup 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)
   396 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)
   397 UNION ALL
   397 UNION ALL
   398 SELECT X.cw_eid, Y.cw_eid
   398 SELECT X.cw_eid, Y.cw_eid
   399 FROM cw_EGroup AS Y, cw_ERType AS X
   399 FROM cw_CWGroup AS Y, cw_CWRType AS X
   400 WHERE X.cw_name=EGroup 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)
   400 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)
   401 UNION ALL
   401 UNION ALL
   402 SELECT X.cw_eid, Y.cw_eid
   402 SELECT X.cw_eid, Y.cw_eid
   403 FROM cw_ERType AS X, cw_RQLExpression AS Y
   403 FROM cw_CWRType AS X, cw_RQLExpression AS Y
   404 WHERE X.cw_name=EGroup 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)'''),
   404 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)'''),
   405 
   405 
   406     # DISTINCT but NEGED exists, can't be invariant
   406     # DISTINCT but NEGED exists, can't be invariant
   407     ('DISTINCT Any X,Y WHERE X name "EGroup", Y eid IN(1, 2, 3), NOT EXISTS(X read_permission Y)',
   407     ('DISTINCT Any X,Y WHERE X name "CWGroup", Y eid IN(1, 2, 3), NOT EXISTS(X read_permission Y)',
   408      '''SELECT DISTINCT X.cw_eid, Y.cw_eid
   408      '''SELECT DISTINCT X.cw_eid, Y.cw_eid
   409 FROM cw_EEType AS X, cw_EGroup AS Y
   409 FROM cw_CWEType AS X, cw_CWGroup AS Y
   410 WHERE X.cw_name=EGroup 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 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)
   411 UNION
   411 UNION
   412 SELECT DISTINCT X.cw_eid, Y.cw_eid
   412 SELECT DISTINCT X.cw_eid, Y.cw_eid
   413 FROM cw_EEType AS X, cw_RQLExpression AS Y
   413 FROM cw_CWEType AS X, cw_RQLExpression AS Y
   414 WHERE X.cw_name=EGroup 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 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)
   415 UNION
   415 UNION
   416 SELECT DISTINCT X.cw_eid, Y.cw_eid
   416 SELECT DISTINCT X.cw_eid, Y.cw_eid
   417 FROM cw_EGroup AS Y, cw_ERType AS X
   417 FROM cw_CWGroup AS Y, cw_CWRType AS X
   418 WHERE X.cw_name=EGroup 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)
   418 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)
   419 UNION
   419 UNION
   420 SELECT DISTINCT X.cw_eid, Y.cw_eid
   420 SELECT DISTINCT X.cw_eid, Y.cw_eid
   421 FROM cw_ERType AS X, cw_RQLExpression AS Y
   421 FROM cw_CWRType AS X, cw_RQLExpression AS Y
   422 WHERE X.cw_name=EGroup 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)'''),
   422 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 
   423 
   424     # should generate the same query as above
   424     # should generate the same query as above
   425     ('DISTINCT Any X,Y WHERE X name "EGroup", Y eid IN(1, 2, 3), NOT X read_permission Y',
   425     ('DISTINCT Any X,Y WHERE X name "CWGroup", Y eid IN(1, 2, 3), NOT X read_permission Y',
   426      '''SELECT DISTINCT X.cw_eid, Y.cw_eid
   426      '''SELECT DISTINCT X.cw_eid, Y.cw_eid
   427 FROM cw_EEType AS X, cw_EGroup AS Y
   427 FROM cw_CWEType AS X, cw_CWGroup AS Y
   428 WHERE X.cw_name=EGroup 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 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)
   429 UNION
   429 UNION
   430 SELECT DISTINCT X.cw_eid, Y.cw_eid
   430 SELECT DISTINCT X.cw_eid, Y.cw_eid
   431 FROM cw_EEType AS X, cw_RQLExpression AS Y
   431 FROM cw_CWEType AS X, cw_RQLExpression AS Y
   432 WHERE X.cw_name=EGroup 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 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)
   433 UNION
   433 UNION
   434 SELECT DISTINCT X.cw_eid, Y.cw_eid
   434 SELECT DISTINCT X.cw_eid, Y.cw_eid
   435 FROM cw_EGroup AS Y, cw_ERType AS X
   435 FROM cw_CWGroup AS Y, cw_CWRType AS X
   436 WHERE X.cw_name=EGroup 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)
   436 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)
   437 UNION
   437 UNION
   438 SELECT DISTINCT X.cw_eid, Y.cw_eid
   438 SELECT DISTINCT X.cw_eid, Y.cw_eid
   439 FROM cw_ERType AS X, cw_RQLExpression AS Y
   439 FROM cw_CWRType AS X, cw_RQLExpression AS Y
   440 WHERE X.cw_name=EGroup 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)'''),
   440 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)'''),
   441     
   441     
   442     # neged relation, can't be inveriant
   442     # neged relation, can't be inveriant
   443     ('Any X,Y WHERE X name "EGroup", Y eid IN(1, 2, 3), NOT X read_permission Y',
   443     ('Any X,Y WHERE X name "CWGroup", Y eid IN(1, 2, 3), NOT X read_permission Y',
   444      '''SELECT X.cw_eid, Y.cw_eid
   444      '''SELECT X.cw_eid, Y.cw_eid
   445 FROM cw_EEType AS X, cw_EGroup AS Y
   445 FROM cw_CWEType AS X, cw_CWGroup AS Y
   446 WHERE X.cw_name=EGroup 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)
   446 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)
   447 UNION ALL
   447 UNION ALL
   448 SELECT X.cw_eid, Y.cw_eid
   448 SELECT X.cw_eid, Y.cw_eid
   449 FROM cw_EEType AS X, cw_RQLExpression AS Y
   449 FROM cw_CWEType AS X, cw_RQLExpression AS Y
   450 WHERE X.cw_name=EGroup 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)
   450 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)
   451 UNION ALL
   451 UNION ALL
   452 SELECT X.cw_eid, Y.cw_eid
   452 SELECT X.cw_eid, Y.cw_eid
   453 FROM cw_EGroup AS Y, cw_ERType AS X
   453 FROM cw_CWGroup AS Y, cw_CWRType AS X
   454 WHERE X.cw_name=EGroup 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)
   454 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)
   455 UNION ALL
   455 UNION ALL
   456 SELECT X.cw_eid, Y.cw_eid
   456 SELECT X.cw_eid, Y.cw_eid
   457 FROM cw_ERType AS X, cw_RQLExpression AS Y
   457 FROM cw_CWRType AS X, cw_RQLExpression AS Y
   458 WHERE X.cw_name=EGroup 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)'''),
   458 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)'''),
   459 
   459 
   460     ('Any MAX(X)+MIN(X), N GROUPBY N WHERE X name N;',
   460     ('Any MAX(X)+MIN(X), N GROUPBY N WHERE X name N;',
   461      '''SELECT (MAX(T1.C0) + MIN(T1.C0)), T1.C1 FROM (SELECT X.cw_eid AS C0, X.cw_name AS C1
   461      '''SELECT (MAX(T1.C0) + MIN(T1.C0)), T1.C1 FROM (SELECT X.cw_eid AS C0, X.cw_name AS C1
   462 FROM cw_Basket AS X
   462 FROM cw_Basket AS X
   463 UNION ALL
   463 UNION ALL
   464 SELECT X.cw_eid AS C0, X.cw_name AS C1
   464 SELECT X.cw_eid AS C0, X.cw_name AS C1
   465 FROM cw_ECache AS X
   465 FROM cw_CWCache AS X
   466 UNION ALL
   466 UNION ALL
   467 SELECT X.cw_eid AS C0, X.cw_name AS C1
   467 SELECT X.cw_eid AS C0, X.cw_name AS C1
   468 FROM cw_EConstraintType AS X
   468 FROM cw_CWConstraintType AS X
   469 UNION ALL
   469 UNION ALL
   470 SELECT X.cw_eid AS C0, X.cw_name AS C1
   470 SELECT X.cw_eid AS C0, X.cw_name AS C1
   471 FROM cw_EEType AS X
   471 FROM cw_CWEType AS X
   472 UNION ALL
   472 UNION ALL
   473 SELECT X.cw_eid AS C0, X.cw_name AS C1
   473 SELECT X.cw_eid AS C0, X.cw_name AS C1
   474 FROM cw_EGroup AS X
   474 FROM cw_CWGroup AS X
   475 UNION ALL
   475 UNION ALL
   476 SELECT X.cw_eid AS C0, X.cw_name AS C1
   476 SELECT X.cw_eid AS C0, X.cw_name AS C1
   477 FROM cw_EPermission AS X
   477 FROM cw_CWPermission AS X
   478 UNION ALL
   478 UNION ALL
   479 SELECT X.cw_eid AS C0, X.cw_name AS C1
   479 SELECT X.cw_eid AS C0, X.cw_name AS C1
   480 FROM cw_ERType AS X
   480 FROM cw_CWRType AS X
   481 UNION ALL
   481 UNION ALL
   482 SELECT X.cw_eid AS C0, X.cw_name AS C1
   482 SELECT X.cw_eid AS C0, X.cw_name AS C1
   483 FROM cw_File AS X
   483 FROM cw_File AS X
   484 UNION ALL
   484 UNION ALL
   485 SELECT X.cw_eid AS C0, X.cw_name AS C1
   485 SELECT X.cw_eid AS C0, X.cw_name AS C1
   521 GROUP BY T1.C2,T1.C3
   521 GROUP BY T1.C2,T1.C3
   522 ORDER BY 2,3) AS T1
   522 ORDER BY 2,3) AS T1
   523 '''),
   523 '''),
   524 
   524 
   525     # ambiguity in EXISTS() -> should union the sub-query
   525     # ambiguity in EXISTS() -> should union the sub-query
   526     ('Any T WHERE T is Tag, NOT T name in ("t1", "t2"), EXISTS(T tags X, X is IN (EUser, EGroup))',
   526     ('Any T WHERE T is Tag, NOT T name in ("t1", "t2"), EXISTS(T tags X, X is IN (CWUser, CWGroup))',
   527      '''SELECT T.cw_eid
   527      '''SELECT T.cw_eid
   528 FROM cw_Tag AS T
   528 FROM cw_Tag AS T
   529 WHERE NOT (T.cw_name IN(t1, t2)) AND EXISTS(SELECT 1 FROM tags_relation AS rel_tags0, cw_EGroup 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_EUser AS X WHERE rel_tags1.eid_from=T.cw_eid AND rel_tags1.eid_to=X.cw_eid)'''),
   529 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)'''),
   530 
   530 
   531     # must not use a relation in EXISTS scope to inline a variable 
   531     # must not use a relation in EXISTS scope to inline a variable 
   532     ('Any U WHERE U eid IN (1,2), EXISTS(X owned_by U)',
   532     ('Any U WHERE U eid IN (1,2), EXISTS(X owned_by U)',
   533      '''SELECT U.cw_eid
   533      '''SELECT U.cw_eid
   534 FROM cw_EUser AS U
   534 FROM cw_CWUser AS U
   535 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)'''),
   535 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)'''),
   536 
   536 
   537     ('Any U WHERE EXISTS(U eid IN (1,2), X owned_by U)',
   537     ('Any U WHERE EXISTS(U eid IN (1,2), X owned_by U)',
   538      '''SELECT U.cw_eid
   538      '''SELECT U.cw_eid
   539 FROM cw_EUser AS U
   539 FROM cw_CWUser AS U
   540 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)'''),
   540 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)'''),
   541 
   541 
   542     ('Any COUNT(U) WHERE EXISTS (P owned_by U, P is IN (Note, Affaire))',
   542     ('Any COUNT(U) WHERE EXISTS (P owned_by U, P is IN (Note, Affaire))',
   543      '''SELECT COUNT(U.cw_eid)
   543      '''SELECT COUNT(U.cw_eid)
   544 FROM cw_EUser AS U
   544 FROM cw_CWUser AS U
   545 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)'''),
   545 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)'''),
   546 
   546 
   547     ('Any MAX(X)',
   547     ('Any MAX(X)',
   548      '''SELECT MAX(X.eid)
   548      '''SELECT MAX(X.eid)
   549 FROM entities AS X'''),
   549 FROM entities AS X'''),
   582     ('Any X GROUPBY X WHERE X eid 12',
   582     ('Any X GROUPBY X WHERE X eid 12',
   583      '''SELECT 12'''),
   583      '''SELECT 12'''),
   584     
   584     
   585     ('Any X GROUPBY X ORDERBY Y WHERE X eid 12, X login Y',
   585     ('Any X GROUPBY X ORDERBY Y WHERE X eid 12, X login Y',
   586      '''SELECT X.cw_eid
   586      '''SELECT X.cw_eid
   587 FROM cw_EUser AS X
   587 FROM cw_CWUser AS X
   588 WHERE X.cw_eid=12
   588 WHERE X.cw_eid=12
   589 GROUP BY X.cw_eid
   589 GROUP BY X.cw_eid
   590 ORDER BY X.cw_login'''),
   590 ORDER BY X.cw_login'''),
   591     
   591     
   592     ('Any U,COUNT(X) GROUPBY U WHERE U eid 12, X owned_by U HAVING COUNT(X) > 10',
   592     ('Any U,COUNT(X) GROUPBY U WHERE U eid 12, X owned_by U HAVING COUNT(X) > 10',
   596 GROUP BY rel_owned_by0.eid_to
   596 GROUP BY rel_owned_by0.eid_to
   597 HAVING COUNT(rel_owned_by0.eid_from)>10'''),
   597 HAVING COUNT(rel_owned_by0.eid_from)>10'''),
   598 
   598 
   599     ('DISTINCT Any X ORDERBY stockproc(X) WHERE U login X',
   599     ('DISTINCT Any X ORDERBY stockproc(X) WHERE U login X',
   600      '''SELECT T1.C0 FROM (SELECT DISTINCT U.cw_login AS C0, STOCKPROC(U.cw_login) AS C1
   600      '''SELECT T1.C0 FROM (SELECT DISTINCT U.cw_login AS C0, STOCKPROC(U.cw_login) AS C1
   601 FROM cw_EUser AS U
   601 FROM cw_CWUser AS U
   602 ORDER BY 2) AS T1'''),
   602 ORDER BY 2) AS T1'''),
   603     
   603     
   604     ('DISTINCT Any X ORDERBY Y WHERE B bookmarked_by X, X login Y',
   604     ('DISTINCT Any X ORDERBY Y WHERE B bookmarked_by X, X login Y',
   605      '''SELECT T1.C0 FROM (SELECT DISTINCT X.cw_eid AS C0, X.cw_login AS C1
   605      '''SELECT T1.C0 FROM (SELECT DISTINCT X.cw_eid AS C0, X.cw_login AS C1
   606 FROM bookmarked_by_relation AS rel_bookmarked_by0, cw_EUser AS X
   606 FROM bookmarked_by_relation AS rel_bookmarked_by0, cw_CWUser AS X
   607 WHERE rel_bookmarked_by0.eid_to=X.cw_eid
   607 WHERE rel_bookmarked_by0.eid_to=X.cw_eid
   608 ORDER BY 2) AS T1'''),
   608 ORDER BY 2) AS T1'''),
   609 
   609 
   610     ('DISTINCT Any X ORDERBY SN WHERE X in_state S, S name SN',
   610     ('DISTINCT Any X ORDERBY SN WHERE X in_state S, S name SN',
   611      '''SELECT T1.C0 FROM (SELECT DISTINCT X.cw_eid AS C0, S.cw_name AS C1
   611      '''SELECT T1.C0 FROM (SELECT DISTINCT X.cw_eid AS C0, S.cw_name AS C1
   612 FROM cw_Affaire AS X, cw_State AS S
   612 FROM cw_Affaire AS X, cw_State AS S
   613 WHERE X.cw_in_state=S.cw_eid
   613 WHERE X.cw_in_state=S.cw_eid
   614 UNION
   614 UNION
   615 SELECT DISTINCT X.cw_eid AS C0, S.cw_name AS C1
   615 SELECT DISTINCT X.cw_eid AS C0, S.cw_name AS C1
   616 FROM cw_EUser AS X, cw_State AS S
   616 FROM cw_CWUser AS X, cw_State AS S
   617 WHERE X.cw_in_state=S.cw_eid
   617 WHERE X.cw_in_state=S.cw_eid
   618 UNION
   618 UNION
   619 SELECT DISTINCT X.cw_eid AS C0, S.cw_name AS C1
   619 SELECT DISTINCT X.cw_eid AS C0, S.cw_name AS C1
   620 FROM cw_Note AS X, cw_State AS S
   620 FROM cw_Note AS X, cw_State AS S
   621 WHERE X.cw_in_state=S.cw_eid
   621 WHERE X.cw_in_state=S.cw_eid
   677 FROM tags_relation AS rel_tags0
   677 FROM tags_relation AS rel_tags0
   678 WHERE NOT (rel_tags0.eid_from=28258)'''),
   678 WHERE NOT (rel_tags0.eid_from=28258)'''),
   679     
   679     
   680     ('Any S WHERE T is Tag, T name TN, NOT T eid 28258, T tags S, S name SN',
   680     ('Any S WHERE T is Tag, T name TN, NOT T eid 28258, T tags S, S name SN',
   681      '''SELECT S.cw_eid
   681      '''SELECT S.cw_eid
   682 FROM cw_EGroup AS S, cw_Tag AS T, tags_relation AS rel_tags0
   682 FROM cw_CWGroup AS S, cw_Tag AS T, tags_relation AS rel_tags0
   683 WHERE NOT (T.cw_eid=28258) AND rel_tags0.eid_from=T.cw_eid AND rel_tags0.eid_to=S.cw_eid
   683 WHERE NOT (T.cw_eid=28258) AND rel_tags0.eid_from=T.cw_eid AND rel_tags0.eid_to=S.cw_eid
   684 UNION ALL
   684 UNION ALL
   685 SELECT S.cw_eid
   685 SELECT S.cw_eid
   686 FROM cw_State AS S, cw_Tag AS T, tags_relation AS rel_tags0
   686 FROM cw_State AS S, cw_Tag AS T, tags_relation AS rel_tags0
   687 WHERE NOT (T.cw_eid=28258) AND rel_tags0.eid_from=T.cw_eid AND rel_tags0.eid_to=S.cw_eid
   687 WHERE NOT (T.cw_eid=28258) AND rel_tags0.eid_from=T.cw_eid AND rel_tags0.eid_to=S.cw_eid
   705      '''SELECT Y.cw_eid
   705      '''SELECT Y.cw_eid
   706 FROM cw_Division AS Y
   706 FROM cw_Division AS Y
   707 WHERE NOT EXISTS(SELECT 1 FROM evaluee_relation AS rel_evaluee0 WHERE rel_evaluee0.eid_from=Y.cw_eid)
   707 WHERE NOT EXISTS(SELECT 1 FROM evaluee_relation AS rel_evaluee0 WHERE rel_evaluee0.eid_from=Y.cw_eid)
   708 UNION ALL
   708 UNION ALL
   709 SELECT Y.cw_eid
   709 SELECT Y.cw_eid
   710 FROM cw_EUser AS Y
   710 FROM cw_CWUser AS Y
   711 WHERE NOT EXISTS(SELECT 1 FROM evaluee_relation AS rel_evaluee0 WHERE rel_evaluee0.eid_from=Y.cw_eid)
   711 WHERE NOT EXISTS(SELECT 1 FROM evaluee_relation AS rel_evaluee0 WHERE rel_evaluee0.eid_from=Y.cw_eid)
   712 UNION ALL
   712 UNION ALL
   713 SELECT Y.cw_eid
   713 SELECT Y.cw_eid
   714 FROM cw_Personne AS Y
   714 FROM cw_Personne AS Y
   715 WHERE NOT EXISTS(SELECT 1 FROM evaluee_relation AS rel_evaluee0 WHERE rel_evaluee0.eid_from=Y.cw_eid)
   715 WHERE NOT EXISTS(SELECT 1 FROM evaluee_relation AS rel_evaluee0 WHERE rel_evaluee0.eid_from=Y.cw_eid)
   720 UNION ALL
   720 UNION ALL
   721 SELECT Y.cw_eid
   721 SELECT Y.cw_eid
   722 FROM cw_SubDivision AS Y
   722 FROM cw_SubDivision AS Y
   723 WHERE NOT EXISTS(SELECT 1 FROM evaluee_relation AS rel_evaluee0 WHERE rel_evaluee0.eid_from=Y.cw_eid)'''),
   723 WHERE NOT EXISTS(SELECT 1 FROM evaluee_relation AS rel_evaluee0 WHERE rel_evaluee0.eid_from=Y.cw_eid)'''),
   724 
   724 
   725     ('Any X WHERE NOT Y evaluee X, Y is EUser',
   725     ('Any X WHERE NOT Y evaluee X, Y is CWUser',
   726      '''SELECT X.cw_eid
   726      '''SELECT X.cw_eid
   727 FROM cw_Note AS X
   727 FROM cw_Note AS X
   728 WHERE NOT EXISTS(SELECT 1 FROM evaluee_relation AS rel_evaluee0,cw_EUser AS Y WHERE rel_evaluee0.eid_from=Y.cw_eid AND rel_evaluee0.eid_to=X.cw_eid)'''),
   728 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)'''),
   729     
   729     
   730     ('Any X,T WHERE X title T, NOT X is Bookmark',
   730     ('Any X,T WHERE X title T, NOT X is Bookmark',
   731      '''SELECT DISTINCT X.cw_eid, X.cw_title
   731      '''SELECT DISTINCT X.cw_eid, X.cw_title
   732 FROM cw_Card AS X
   732 FROM cw_Card AS X
   733 UNION
   733 UNION
   734 SELECT DISTINCT X.cw_eid, X.cw_title
   734 SELECT DISTINCT X.cw_eid, X.cw_title
   735 FROM cw_EmailThread AS X'''),
   735 FROM cw_EmailThread AS X'''),
   736 
   736 
   737     ('Any K,V WHERE P is EProperty, P pkey K, P value V, NOT P for_user U',
   737     ('Any K,V WHERE P is CWProperty, P pkey K, P value V, NOT P for_user U',
   738      '''SELECT DISTINCT P.cw_pkey, P.cw_value
   738      '''SELECT DISTINCT P.cw_pkey, P.cw_value
   739 FROM cw_EProperty AS P
   739 FROM cw_CWProperty AS P
   740 WHERE P.cw_for_user IS NULL'''),
   740 WHERE P.cw_for_user IS NULL'''),
   741 
   741 
   742     ('Any S WHERE NOT X in_state S, X is IN(Affaire, EUser)',
   742     ('Any S WHERE NOT X in_state S, X is IN(Affaire, CWUser)',
   743      '''SELECT DISTINCT S.cw_eid
   743      '''SELECT DISTINCT S.cw_eid
   744 FROM cw_Affaire AS X, cw_State AS S
   744 FROM cw_Affaire AS X, cw_State AS S
   745 WHERE (X.cw_in_state IS NULL OR X.cw_in_state!=S.cw_eid)
   745 WHERE (X.cw_in_state IS NULL OR X.cw_in_state!=S.cw_eid)
   746 INTERSECT
   746 INTERSECT
   747 SELECT DISTINCT S.cw_eid
   747 SELECT DISTINCT S.cw_eid
   748 FROM cw_EUser AS X, cw_State AS S
   748 FROM cw_CWUser AS X, cw_State AS S
   749 WHERE (X.cw_in_state IS NULL OR X.cw_in_state!=S.cw_eid)'''),
   749 WHERE (X.cw_in_state IS NULL OR X.cw_in_state!=S.cw_eid)'''),
   750     ]
   750     ]
   751 
   751 
   752 OUTER_JOIN = [
   752 OUTER_JOIN = [
   753     ('Any X,S WHERE X travaille S?',
   753     ('Any X,S WHERE X travaille S?',
   789 ORDER BY 4 DESC'''),
   789 ORDER BY 4 DESC'''),
   790 
   790 
   791     ('Any X WHERE X is Affaire, S is Societe, EXISTS(X owned_by U OR (X concerne S?, S owned_by U))',
   791     ('Any X WHERE X is Affaire, S is Societe, EXISTS(X owned_by U OR (X concerne S?, S owned_by U))',
   792      '''SELECT X.cw_eid
   792      '''SELECT X.cw_eid
   793 FROM cw_Affaire AS X
   793 FROM cw_Affaire AS X
   794 WHERE EXISTS(SELECT 1 FROM owned_by_relation AS rel_owned_by0, cw_EUser 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)'''),
   794 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)'''),
   795 
   795 
   796     ('Any C,M WHERE C travaille G?, G evaluee M?, G is Societe',
   796     ('Any C,M WHERE C travaille G?, G evaluee M?, G is Societe',
   797      '''SELECT C.cw_eid, rel_evaluee1.eid_to
   797      '''SELECT C.cw_eid, rel_evaluee1.eid_to
   798 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)'''
   798 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)'''
   799 #SELECT C.cw_eid, M.cw_eid
   799 #SELECT C.cw_eid, M.cw_eid
   802 
   802 
   803     ('Any A,C WHERE A documented_by C?, (C is NULL) OR (EXISTS(C require_permission F, '
   803     ('Any A,C WHERE A documented_by C?, (C is NULL) OR (EXISTS(C require_permission F, '
   804      'F name "read", F require_group E, U in_group E)), U eid 1',
   804      'F name "read", F require_group E, U in_group E)), U eid 1',
   805      '''SELECT A.cw_eid, rel_documented_by0.eid_to
   805      '''SELECT A.cw_eid, rel_documented_by0.eid_to
   806 FROM cw_Affaire AS A LEFT OUTER JOIN documented_by_relation AS rel_documented_by0 ON (rel_documented_by0.eid_from=A.cw_eid)
   806 FROM cw_Affaire AS A LEFT OUTER JOIN documented_by_relation AS rel_documented_by0 ON (rel_documented_by0.eid_from=A.cw_eid)
   807 WHERE ((rel_documented_by0.eid_to IS NULL) OR (EXISTS(SELECT 1 FROM require_permission_relation AS rel_require_permission1, cw_EPermission 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)))'''),
   807 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)))'''),
   808 
   808 
   809     ("Any X WHERE X eid 12, P? connait X",
   809     ("Any X WHERE X eid 12, P? connait X",
   810      '''SELECT X.cw_eid
   810      '''SELECT X.cw_eid
   811 FROM cw_Personne AS X LEFT OUTER JOIN connait_relation AS rel_connait0 ON (rel_connait0.eid_to=12)
   811 FROM cw_Personne AS X LEFT OUTER JOIN connait_relation AS rel_connait0 ON (rel_connait0.eid_to=12)
   812 WHERE X.cw_eid=12'''
   812 WHERE X.cw_eid=12'''
   816     ),
   816     ),
   817 
   817 
   818     ('Any GN, TN ORDERBY GN WHERE T tags G?, T name TN, G name GN',
   818     ('Any GN, TN ORDERBY GN WHERE T tags G?, T name TN, G name GN',
   819     '''SELECT _T0.C1, T.cw_name
   819     '''SELECT _T0.C1, T.cw_name
   820 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
   820 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
   821 FROM cw_EGroup AS G
   821 FROM cw_CWGroup AS G
   822 UNION ALL
   822 UNION ALL
   823 SELECT G.cw_eid AS C0, G.cw_name AS C1
   823 SELECT G.cw_eid AS C0, G.cw_name AS C1
   824 FROM cw_State AS G
   824 FROM cw_State AS G
   825 UNION ALL
   825 UNION ALL
   826 SELECT G.cw_eid AS C0, G.cw_name AS C1
   826 SELECT G.cw_eid AS C0, G.cw_name AS C1
   827 FROM cw_Tag AS G) AS _T0 ON (rel_tags0.eid_to=_T0.C0)
   827 FROM cw_Tag AS G) AS _T0 ON (rel_tags0.eid_to=_T0.C0)
   828 ORDER BY 1'''),
   828 ORDER BY 1'''),
   829 
   829 
   830 
   830 
   831     # optional variable with additional restriction
   831     # optional variable with additional restriction
   832     ('Any T,G WHERE T tags G?, G name "hop", G is EGroup',
   832     ('Any T,G WHERE T tags G?, G name "hop", G is CWGroup',
   833      '''SELECT T.cw_eid, G.cw_eid
   833      '''SELECT T.cw_eid, G.cw_eid
   834 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_EGroup AS G ON (rel_tags0.eid_to=G.cw_eid AND G.cw_name=hop)'''),
   834 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)'''),
   835 
   835 
   836     # optional variable with additional invariant restriction
   836     # optional variable with additional invariant restriction
   837     ('Any T,G WHERE T tags G?, G eid 12',
   837     ('Any T,G WHERE T tags G?, G eid 12',
   838      '''SELECT T.cw_eid, rel_tags0.eid_to
   838      '''SELECT T.cw_eid, rel_tags0.eid_to
   839 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)'''),
   839 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)'''),
   840 
   840 
   841     # optional variable with additional restriction appearing before the relation
   841     # optional variable with additional restriction appearing before the relation
   842     ('Any T,G WHERE G name "hop", T tags G?, G is EGroup',
   842     ('Any T,G WHERE G name "hop", T tags G?, G is CWGroup',
   843      '''SELECT T.cw_eid, G.cw_eid
   843      '''SELECT T.cw_eid, G.cw_eid
   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_EGroup AS G ON (rel_tags0.eid_to=G.cw_eid AND G.cw_name=hop)'''),
   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_CWGroup AS G ON (rel_tags0.eid_to=G.cw_eid AND G.cw_name=hop)'''),
   845 
   845 
   846     # optional variable with additional restriction on inlined relation
   846     # optional variable with additional restriction on inlined relation
   847     # XXX the expected result should be as the query below. So what, raise BadRQLQuery ?
   847     # XXX the expected result should be as the query below. So what, raise BadRQLQuery ?
   848     ('Any T,G,S WHERE T tags G?, G in_state S, S name "hop", G is EUser',
   848     ('Any T,G,S WHERE T tags G?, G in_state S, S name "hop", G is CWUser',
   849      '''SELECT T.cw_eid, G.cw_eid, S.cw_eid
   849      '''SELECT T.cw_eid, G.cw_eid, S.cw_eid
   850 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_EUser AS G ON (rel_tags0.eid_to=G.cw_eid)
   850 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)
   851 WHERE G.cw_in_state=S.cw_eid AND S.cw_name=hop
   851 WHERE G.cw_in_state=S.cw_eid AND S.cw_name=hop
   852 '''),
   852 '''),
   853 
   853 
   854     # optional variable with additional invariant restriction on an inlined relation
   854     # optional variable with additional invariant restriction on an inlined relation
   855     ('Any T,G,S WHERE T tags G, G in_state S?, S eid 1, G is EUser',
   855     ('Any T,G,S WHERE T tags G, G in_state S?, S eid 1, G is CWUser',
   856      '''SELECT rel_tags0.eid_from, G.cw_eid, G.cw_in_state
   856      '''SELECT rel_tags0.eid_from, G.cw_eid, G.cw_in_state
   857 FROM cw_EUser AS G, tags_relation AS rel_tags0
   857 FROM cw_CWUser AS G, tags_relation AS rel_tags0
   858 WHERE rel_tags0.eid_to=G.cw_eid AND (G.cw_in_state=1 OR G.cw_in_state IS NULL)'''),
   858 WHERE rel_tags0.eid_to=G.cw_eid AND (G.cw_in_state=1 OR G.cw_in_state IS NULL)'''),
   859 
   859 
   860     # two optional variables with additional invariant restriction on an inlined relation
   860     # two optional variables with additional invariant restriction on an inlined relation
   861     ('Any T,G,S WHERE T tags G?, G in_state S?, S eid 1, G is EUser',
   861     ('Any T,G,S WHERE T tags G?, G in_state S?, S eid 1, G is CWUser',
   862      '''SELECT T.cw_eid, G.cw_eid, G.cw_in_state
   862      '''SELECT T.cw_eid, G.cw_eid, G.cw_in_state
   863 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_EUser AS G ON (rel_tags0.eid_to=G.cw_eid AND (G.cw_in_state=1 OR G.cw_in_state IS NULL))'''),
   863 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))'''),
   864 
   864 
   865     # two optional variables with additional restriction on an inlined relation
   865     # two optional variables with additional restriction on an inlined relation
   866     ('Any T,G,S WHERE T tags G?, G in_state S?, S name "hop", G is EUser',
   866     ('Any T,G,S WHERE T tags G?, G in_state S?, S name "hop", G is CWUser',
   867      '''SELECT T.cw_eid, G.cw_eid, S.cw_eid
   867      '''SELECT T.cw_eid, G.cw_eid, S.cw_eid
   868 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_EUser 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)'''),
   868 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)'''),
   869     
   869     
   870     # two optional variables with additional restriction on an ambigous inlined relation
   870     # two optional variables with additional restriction on an ambigous inlined relation
   871     ('Any T,G,S WHERE T tags G?, G in_state S?, S name "hop"',
   871     ('Any T,G,S WHERE T tags G?, G in_state S?, S name "hop"',
   872      '''SELECT T.cw_eid, _T0.C0, _T0.C1
   872      '''SELECT T.cw_eid, _T0.C0, _T0.C1
   873 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
   873 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
   874 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) 
   874 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) 
   875 UNION ALL
   875 UNION ALL
   876 SELECT G.cw_eid AS C0, S.cw_eid AS C1
   876 SELECT G.cw_eid AS C0, S.cw_eid AS C1
   877 FROM cw_EUser AS G LEFT OUTER JOIN cw_State AS S ON (G.cw_in_state=S.cw_eid AND S.cw_name=hop) 
   877 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) 
   878 UNION ALL
   878 UNION ALL
   879 SELECT G.cw_eid AS C0, S.cw_eid AS C1
   879 SELECT G.cw_eid AS C0, S.cw_eid AS C1
   880 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)'''),
   880 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)'''),
   881 
   881 
   882     ]
   882     ]
  1037     ('Any P WHERE NOT N ecrit_par P, P is Personne, N eid 512',
  1037     ('Any P WHERE NOT N ecrit_par P, P is Personne, N eid 512',
  1038      '''SELECT DISTINCT P.cw_eid
  1038      '''SELECT DISTINCT P.cw_eid
  1039 FROM cw_Note AS N, cw_Personne AS P
  1039 FROM cw_Note AS N, cw_Personne AS P
  1040 WHERE (N.cw_ecrit_par IS NULL OR N.cw_ecrit_par!=P.cw_eid) AND N.cw_eid=512'''),
  1040 WHERE (N.cw_ecrit_par IS NULL OR N.cw_ecrit_par!=P.cw_eid) AND N.cw_eid=512'''),
  1041 
  1041 
  1042     ('Any S,ES,T WHERE S state_of ET, ET name "EUser", ES allowed_transition T, T destination_state S',
  1042     ('Any S,ES,T WHERE S state_of ET, ET name "CWUser", ES allowed_transition T, T destination_state S',
  1043      '''SELECT T.cw_destination_state, rel_allowed_transition1.eid_from, T.cw_eid
  1043      '''SELECT T.cw_destination_state, rel_allowed_transition1.eid_from, T.cw_eid
  1044 FROM allowed_transition_relation AS rel_allowed_transition1, cw_EEType AS ET, cw_Transition AS T, state_of_relation AS rel_state_of0
  1044 FROM allowed_transition_relation AS rel_allowed_transition1, cw_CWEType AS ET, cw_Transition AS T, state_of_relation AS rel_state_of0
  1045 WHERE T.cw_destination_state=rel_state_of0.eid_from AND rel_state_of0.eid_to=ET.cw_eid AND ET.cw_name=EUser AND rel_allowed_transition1.eid_to=T.cw_eid'''),
  1045 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'''),
  1046     ('Any O WHERE S eid 0, S in_state O',
  1046     ('Any O WHERE S eid 0, S in_state O',
  1047      '''SELECT S.cw_in_state
  1047      '''SELECT S.cw_in_state
  1048 FROM cw_Affaire AS S
  1048 FROM cw_Affaire AS S
  1049 WHERE S.cw_eid=0 AND S.cw_in_state IS NOT NULL
  1049 WHERE S.cw_eid=0 AND S.cw_in_state IS NOT NULL
  1050 UNION ALL
  1050 UNION ALL
  1051 SELECT S.cw_in_state
  1051 SELECT S.cw_in_state
  1052 FROM cw_EUser AS S
  1052 FROM cw_CWUser AS S
  1053 WHERE S.cw_eid=0 AND S.cw_in_state IS NOT NULL
  1053 WHERE S.cw_eid=0 AND S.cw_in_state IS NOT NULL
  1054 UNION ALL
  1054 UNION ALL
  1055 SELECT S.cw_in_state
  1055 SELECT S.cw_in_state
  1056 FROM cw_Note AS S
  1056 FROM cw_Note AS S
  1057 WHERE S.cw_eid=0 AND S.cw_in_state IS NOT NULL''')
  1057 WHERE S.cw_eid=0 AND S.cw_in_state IS NOT NULL''')
  1063      '''SELECT DISTINCT S.cw_name
  1063      '''SELECT DISTINCT S.cw_name
  1064 FROM cw_Affaire AS X, cw_State AS S
  1064 FROM cw_Affaire AS X, cw_State AS S
  1065 WHERE (X.cw_in_state IS NULL OR X.cw_in_state!=S.cw_eid)
  1065 WHERE (X.cw_in_state IS NULL OR X.cw_in_state!=S.cw_eid)
  1066 INTERSECT
  1066 INTERSECT
  1067 SELECT DISTINCT S.cw_name
  1067 SELECT DISTINCT S.cw_name
  1068 FROM cw_EUser AS X, cw_State AS S
  1068 FROM cw_CWUser AS X, cw_State AS S
  1069 WHERE (X.cw_in_state IS NULL OR X.cw_in_state!=S.cw_eid)
  1069 WHERE (X.cw_in_state IS NULL OR X.cw_in_state!=S.cw_eid)
  1070 INTERSECT
  1070 INTERSECT
  1071 SELECT DISTINCT S.cw_name
  1071 SELECT DISTINCT S.cw_name
  1072 FROM cw_Note AS X, cw_State AS S
  1072 FROM cw_Note AS X, cw_State AS S
  1073 WHERE (X.cw_in_state IS NULL OR X.cw_in_state!=S.cw_eid)'''),
  1073 WHERE (X.cw_in_state IS NULL OR X.cw_in_state!=S.cw_eid)'''),
  1169 #                 raise
  1169 #                 raise
  1170 
  1170 
  1171     def test1(self):
  1171     def test1(self):
  1172         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',
  1173                        ("""SELECT COUNT(T1.C0) FROM (SELECT RDEF.cw_eid AS C0
  1173                        ("""SELECT COUNT(T1.C0) FROM (SELECT RDEF.cw_eid AS C0
  1174 FROM cw_EFRDef AS RDEF
  1174 FROM cw_CWAttribute AS RDEF
  1175 WHERE RDEF.cw_relation_type=%(x)s
  1175 WHERE RDEF.cw_relation_type=%(x)s
  1176 UNION ALL
  1176 UNION ALL
  1177 SELECT RDEF.cw_eid AS C0
  1177 SELECT RDEF.cw_eid AS C0
  1178 FROM cw_ENFRDef AS RDEF
  1178 FROM cw_CWRelation AS RDEF
  1179 WHERE RDEF.cw_relation_type=%(x)s) AS T1""", {}),
  1179 WHERE RDEF.cw_relation_type=%(x)s) AS T1""", {}),
  1180                        )
  1180                        )
  1181 
  1181 
  1182     def test2(self):
  1182     def test2(self):
  1183         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',
  1196                     '''SELECT rel_in_basket0.eid_from
  1196                     '''SELECT rel_in_basket0.eid_from
  1197 FROM in_basket_relation AS rel_in_basket0
  1197 FROM in_basket_relation AS rel_in_basket0
  1198 WHERE rel_in_basket0.eid_to=12''')
  1198 WHERE rel_in_basket0.eid_to=12''')
  1199 
  1199 
  1200     def test_varmap(self):
  1200     def test_varmap(self):
  1201         self._check('Any X,L WHERE X is EUser, 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"',
  1202                     '''SELECT T00.x, T00.l
  1202                     '''SELECT T00.x, T00.l
  1203 FROM T00, cw_EGroup AS G, in_group_relation AS rel_in_group0
  1203 FROM T00, cw_CWGroup AS G, in_group_relation AS rel_in_group0
  1204 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''',
  1205                     varmap={'X': 'T00.x', 'X.login': 'T00.l'})
  1205                     varmap={'X': 'T00.x', 'X.login': 'T00.l'})
  1206         self._check('Any X,L,GN WHERE X is EUser, X in_group G, X login L, G name GN',
  1206         self._check('Any X,L,GN WHERE X is CWUser, X in_group G, X login L, G name GN',
  1207                     '''SELECT T00.x, T00.l, G.cw_name
  1207                     '''SELECT T00.x, T00.l, G.cw_name
  1208 FROM T00, cw_EGroup AS G, in_group_relation AS rel_in_group0
  1208 FROM T00, cw_CWGroup AS G, in_group_relation AS rel_in_group0
  1209 WHERE rel_in_group0.eid_from=T00.x AND rel_in_group0.eid_to=G.cw_eid''',
  1209 WHERE rel_in_group0.eid_from=T00.x AND rel_in_group0.eid_to=G.cw_eid''',
  1210                     varmap={'X': 'T00.x', 'X.login': 'T00.l'})
  1210                     varmap={'X': 'T00.x', 'X.login': 'T00.l'})
  1211 
  1211 
  1212     def test_parser_parse(self):
  1212     def test_parser_parse(self):
  1213         for t in self._parse(PARSER):
  1213         for t in self._parse(PARSER):
  1309 FROM ((SELECT X.cw_eid AS C0
  1309 FROM ((SELECT X.cw_eid AS C0
  1310 FROM cw_Societe AS X)
  1310 FROM cw_Societe AS X)
  1311 UNION ALL
  1311 UNION ALL
  1312 (SELECT X.cw_eid AS C0
  1312 (SELECT X.cw_eid AS C0
  1313 FROM cw_Affaire AS X
  1313 FROM cw_Affaire AS X
  1314 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_EEType AS ET, is_relation AS rel_is0
  1314 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
  1315 WHERE rel_is0.eid_from=_T0.C0 AND rel_is0.eid_to=ET.cw_eid
  1315 WHERE rel_is0.eid_from=_T0.C0 AND rel_is0.eid_to=ET.cw_eid
  1316 GROUP BY ET.cw_name'''),
  1316 GROUP BY ET.cw_name'''),
  1317             )):
  1317             )):
  1318             yield t
  1318             yield t
  1319 
  1319 
  1387             )):
  1387             )):
  1388             yield t
  1388             yield t
  1389 
  1389 
  1390 
  1390 
  1391     def test_from_clause_needed(self):
  1391     def test_from_clause_needed(self):
  1392         queries = [("Any 1 WHERE EXISTS(T is EGroup, T name 'managers')",
  1392         queries = [("Any 1 WHERE EXISTS(T is CWGroup, T name 'managers')",
  1393                     '''SELECT 1
  1393                     '''SELECT 1
  1394 WHERE EXISTS(SELECT 1 FROM cw_EGroup AS T WHERE T.cw_name=managers)'''),
  1394 WHERE EXISTS(SELECT 1 FROM cw_CWGroup AS T WHERE T.cw_name=managers)'''),
  1395                    ('Any X,Y WHERE NOT X created_by Y, X eid 5, Y eid 6',
  1395                    ('Any X,Y WHERE NOT X created_by Y, X eid 5, Y eid 6',
  1396                     '''SELECT 5, 6
  1396                     '''SELECT 5, 6
  1397 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)'''),
  1397 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)'''),
  1398                    ]
  1398                    ]
  1399         for t in self._parse(queries):
  1399         for t in self._parse(queries):
  1546 
  1546 
  1547     def _norm_sql(self, sql):
  1547     def _norm_sql(self, sql):
  1548         return sql.strip().replace(' ILIKE ', ' LIKE ')
  1548         return sql.strip().replace(' ILIKE ', ' LIKE ')
  1549 
  1549 
  1550     def test_from_clause_needed(self):
  1550     def test_from_clause_needed(self):
  1551         queries = [("Any 1 WHERE EXISTS(T is EGroup, T name 'managers')",
  1551         queries = [("Any 1 WHERE EXISTS(T is CWGroup, T name 'managers')",
  1552                     '''SELECT 1
  1552                     '''SELECT 1
  1553 FROM (SELECT 1) AS _T
  1553 FROM (SELECT 1) AS _T
  1554 WHERE EXISTS(SELECT 1 FROM cw_EGroup AS T WHERE T.cw_name=managers)'''),
  1554 WHERE EXISTS(SELECT 1 FROM cw_CWGroup AS T WHERE T.cw_name=managers)'''),
  1555                    ('Any X,Y WHERE NOT X created_by Y, X eid 5, Y eid 6',
  1555                    ('Any X,Y WHERE NOT X created_by Y, X eid 5, Y eid 6',
  1556                     '''SELECT 5, 6
  1556                     '''SELECT 5, 6
  1557 FROM (SELECT 1) AS _T
  1557 FROM (SELECT 1) AS _T
  1558 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)'''),
  1558 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)'''),
  1559                    ]
  1559                    ]