sql.inc (6603B)
1 <?php //$typer1r2 = "type in ($r1, $r2)"; 2 $banned_types = "4, 12, 36, 18, 29, 45, 46, 47, 48, 1000, 1001"; 3 4 function sqlGetPassword($user) { 5 return "SELECT hash_passwd FROM user WHERE login='".$user."';"; 6 } 7 8 function sqlGetEIDCenterNode() { 9 return "select eid from random_center_node where rowid = (abs(random()) % (select max(rowid) from random_center_node))+1;"; 10 } 11 12 function sqlGetEIRCloudNode() { 13 return "select eid from random_cloud_node where rowid = (abs(random()) % (select max(rowid) from random_cloud_node))+1;"; 14 } 15 16 // Voisins 1 saut du bon type (= relations déjà existantes) 17 function sql1JumpGoodType($r1, $r2, $centerEid) { 18 global $typer1r2; 19 20 return "select end as eid, type = $r1 as r1, type = $r2 as r2, 0 as r0, 0 as trash from relation where start = $centerEid and $typer1r2 order by random();"; 21 } 22 23 // Voisins 1 saut via r_associated (0), donc qu'on voudrait spécifier si possible. 24 function sql1JumpViaRAssociated0($centerEid) { 25 return "select end as eid, 0.25 as r1, 0.25 as r2, 0.5 as r0, 0 as trash from relation where start = $centerEid and type = 0 order by random();"; 26 } 27 28 // Voisins 1 saut via les autres relations 29 function sql1JumpViaOtherRelation($centerEid, $r1, $r2) { 30 global $banned_types; 31 32 return "select end as eid, 0.1 as r1, 0.1 as r2, 0.8 as r0, 0 as trash from relation where start = $centerEid and type not in (0, $r1, $r2, $banned_types) order by random();"; 33 } 34 35 // Voisins 2 sauts, avec un mix de R1 et R2 pour les liens. Par ex [ A -R1-> B -R2-> C ] ou bien [ A -R2-> B -R2-> C ] 36 // Version optimisée de : "select end as eid from relation where $typer1r2 and start in oneHopWithType order by random();" 37 function sql2JumpWithMixR1R2ForLinks($r1, $r2, $centerEid) { 38 global $typer1r2; 39 40 return "select B.end as eid, ((A.type = $r1) + (B.type = $r1)) / 3. as r1, ((A.type = $r2) + (B.type = $r2)) / 3. as r2, 1/6. as r0, 1/6. as trash from relation as A, relation as B where A.start = $centerEid and A.$typer1r2 and B.start = A.end and B.$typer1r2 order by random();"; 41 } 42 43 // Voisins 1 saut r1/r2 + 1 saut synonyme 44 // Version optimisée de : "select end as eid from relation where start in oneHopWithType and type = 5 order by random()"; 45 function sql1JumpR1DivR2Plus1JumpSynonymOneHopWithType($r1, $r2, $centerEid) { 46 global $typer1r2; 47 48 return "select B.end as eid, (A.type = $r1) * 0.75 as r1, (A.type = $r2) * 0.75 as r2, 0.25 as r0, 0 as trash from relation as A, relation as B where A.start = $centerEid and A.$typer1r2 and B.start = A.end and B.type = 5 order by random();"; 49 } 50 51 // Version optimisée de : "select end as eid from relation where start in (select end from relation where start = $centerEid and type = 5) and $typer1r2 order by random();" 52 function sql1JumpR1DivR2Plus1JumpSynonym($r1, $r2, $centerEid) { 53 global $typer1r2; 54 55 return "select B.end as eid, (B.type = $r1) * 0.75 as r1, (B.type = $r2) * 0.75 as r2, 0.25 as r0, 0 as trash from relation as A, relation as B where A.start = $centerEid and A.type = 5 and B.start = A.end and B.$typer1r2 order by random();"; 56 } 57 58 // Voisins 2 sauts (tous) 59 // Version optimisée de : "select end as eid, 0.1 as r1, 0.1 as r2, 0.3 as r0, 0.5 as trash from relation where start in (select end from relation where start = $centerEid and type not in ($banned_types)) and type not in ($banned_types) order by random();" 60 function sql2JumpAll($centerEid, $cloudSize) { 61 global $banned_types; 62 63 return "select x as eid, 0.1 as r1, 0.1 as r2, 0.3 as r0, 0.5 as trash from (select x from (select X.eid + Y.dumb as x from (select B.end as eid from relation as A, relation as B where A.type not in ($banned_types) and A.start = $centerEid and B.type not in ($banned_types) and B.start = A.end limit ".($cloudSize*4).") as X, (select 0 as dumb) as Y)) order by random();"; 64 } 65 66 // Centre pointe vers X, M pointe vers X aussi, on prend M. 67 // Version optimisée de : "select start as eid from relation where end in (select end from relation where start = $centerEid) and type not in ($banned_types) order by random();" 68 // Ce n'est toujours pas ça… : "select eid from (select B.start as eid from relation as A, relation as B where A.type not in ($banned_types) and A.start = $centerEid and B.type not in ($banned_types) and B.end = A.end limit 1) order by random();" 69 // Tordu, mais ça marche \o/ . En fait il faut empêcher l'optimiseur de ramener le random avant le limit (et l'optimiseur est malin… :) 70 function sqlXPointsToMMPointsToXTakeM($cloudSize) { 71 global $banned_types; 72 73 return "select x as eid, 0.1 as r1, 0.1 as r2, 0.2 as r0, 0.6 as trash from (select x from (select X.eid + Y.dumb as x from (select B.start as eid from relation as A, relation as B where A.type not in ($banned_types) and A.start = $centerEid and B.type not in ($banned_types) and B.end = A.end limit ".($cloudSize*4).") as X, (select 0 as dumb) as Y)) order by random();"; 74 } 75 76 function sqlGetNameFromNode($res) { 77 return "select name from node where eid=".$res['eid'].";"; 78 } 79 80 function sqlGetGidFromGame() { 81 return "select gid from game where gid = (abs(random()) % (select max(gid) from game))+1 or gid = (select max(gid) from game where gid > 0) order by gid limit 1;"; 82 } 83 84 function sqlGetNameFromNodeWithEid($eid) { 85 return "select name from node where eid = $eid"; 86 } 87 88 // TODO Yoann : faire des tests d'erreur pour ces select ? 89 function sqlGetGamesForId($gameId) { 90 return "select gid, (select name from node where eid = eid_central_word) as name_central_word, eid_central_word, relation_1, relation_2 from game where gid = ".$gameId.";"; 91 } 92 93 function sqlGetWordEidAndName($gameId) { 94 return "select eid_word,(select name from node where eid=eid_word) as name_word from game_cloud where gid = ".$gameId.";"; 95 } 96 97 function sqlGetInformationAboutGame($gameId) { 98 return "select eid_word,(select name from node where eid=eid_word) as name_word, num, difficulty, totalWeight, probaR1, probaR2, probaR0, probaTrash from game_cloud where gid = ".$gameId.";"; 99 } 100 101 function sqlGameIsOK($pgid, $gid, $user) { 102 return "SELECT 'ok' FROM played_game WHERE pgid = $pgid and $gid = $gid and login = '$user' and timestamp = -1;"; 103 } 104 105 function sqlGetScoreForUser($user) { 106 return "SELECT score FROM user WHERE login='".$user."';"; 107 } 108 109 function sqlGetPlayedGameTime($pgid, $gid, $user) { 110 return "SELECT timestamp FROM played_game WHERE pgid = $pgid and $gid = $gid and login = '$user';"; 111 } 112 113 function sqlGetNumAndScoreFromGame($pgid, $gid) { 114 return "SELECT num,score from played_game_cloud where pgid = $pgid and gid = $gid;"; 115 } 116 117 function sqlGetEidFromNode($node) { 118 return "SELECT eid FROM node WHERE name='".SQLite3::escapeString($node)."'"; 119 } 120 ?>