Open only one DB connection per PHP call
This commit is contained in:
parent
e270333055
commit
7fc7a002e3
6 changed files with 34 additions and 84 deletions
|
|
@ -5,7 +5,7 @@ if(!defined('ONYX')) exit;
|
|||
function cmp_team_pts($i1, $i2)
|
||||
{
|
||||
if ($i1->get_pts() == $i2->get_pts()){
|
||||
$db = new BDD();
|
||||
global $db;
|
||||
|
||||
$timestampi1 = $db->unique_query("SELECT MAX(s.time) AS maxTime
|
||||
FROM solved AS s
|
||||
|
|
@ -13,7 +13,6 @@ function cmp_team_pts($i1, $i2)
|
|||
$timestampi2 = $db->unique_query("SELECT MAX(s.time) AS maxTime
|
||||
FROM solved AS s
|
||||
WHERE s.id_team = ".$i2->get_id());
|
||||
$db->deconnexion();
|
||||
|
||||
if (strtotime($timestampi1['maxTime']) > strtotime($timestampi2['maxTime']))
|
||||
return 1;
|
||||
|
|
@ -41,7 +40,7 @@ class Team
|
|||
{
|
||||
if (!empty($id))
|
||||
{
|
||||
$db = new BDD();
|
||||
global $db;
|
||||
$res = $db->unique_query("SELECT `id`, `team_name`, `key_hash`, `slogan`,
|
||||
`auth_level`, `revoked`
|
||||
FROM teams WHERE id = ".intval($id)) or die($db->erreur());
|
||||
|
|
@ -55,7 +54,6 @@ class Team
|
|||
$this->auth_level = $res['auth_level'];
|
||||
$this->revoked = $res['revoked'];
|
||||
}
|
||||
$db->deconnexion();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -84,7 +82,7 @@ class Team
|
|||
$team_name = $this->team_name;
|
||||
$revoked = $this->revoked;
|
||||
|
||||
$db = new BDD();
|
||||
global $db;
|
||||
$db->escape($key_hash);
|
||||
$db->escape($slogan);
|
||||
$db->escape($team_name);
|
||||
|
|
@ -106,7 +104,6 @@ class Team
|
|||
WHERE id = ".intval($this->id));
|
||||
$aff = $db->affected();
|
||||
}
|
||||
$db->deconnexion();
|
||||
|
||||
return ($aff == 1);
|
||||
}
|
||||
|
|
@ -114,13 +111,12 @@ class Team
|
|||
// TODO: Drop members ?
|
||||
function drop()
|
||||
{
|
||||
$db = new BDD();
|
||||
global $db;
|
||||
if (!empty($this->id))
|
||||
$db->query("DELETE FROM teams WHERE id = ".intval($this->id));
|
||||
$aff = $db->affected();
|
||||
if ($aff == 1)
|
||||
$this->id = 0;
|
||||
$db->deconnexion();
|
||||
return $aff;
|
||||
}
|
||||
|
||||
|
|
@ -152,13 +148,11 @@ class Team
|
|||
{
|
||||
if(count($this->members) == 0)
|
||||
{
|
||||
$db = new BDD();
|
||||
global $db;
|
||||
|
||||
$res = $db->query("SELECT `id` FROM team_members
|
||||
WHERE id_team = ".intval($this->id));
|
||||
|
||||
$db->deconnexion();
|
||||
|
||||
foreach($res as $member)
|
||||
$this->members[] = new Member($member["id"], $this);
|
||||
}
|
||||
|
|
@ -170,7 +164,7 @@ class Team
|
|||
{
|
||||
if(!isset($this->points))
|
||||
{
|
||||
$db = new BDD();
|
||||
global $db;
|
||||
|
||||
$res = $db->unique_query("SELECT E.id, S.id_team, SUM(E.points) as sum_points
|
||||
FROM exercices E
|
||||
|
|
@ -178,8 +172,6 @@ class Team
|
|||
WHERE S.id_team = ".$this->id."
|
||||
GROUP BY S.id_team");
|
||||
|
||||
$db->deconnexion();
|
||||
|
||||
if (!empty($res))
|
||||
$this->points = $res['sum_points'];
|
||||
else
|
||||
|
|
@ -191,10 +183,9 @@ class Team
|
|||
|
||||
function get_group_ids()
|
||||
{
|
||||
$db = new BDD();
|
||||
global $db;
|
||||
|
||||
$res = $db->query("SELECT `id` FROM `users`");
|
||||
$db->deconnexion();
|
||||
|
||||
return $res['id'];
|
||||
}
|
||||
|
|
@ -213,13 +204,12 @@ class Team
|
|||
|
||||
function get_nb_res_exercises_by_theme($id_theme)
|
||||
{
|
||||
$db = new BDD();
|
||||
global $db;
|
||||
$res = $db->unique_query("SELECT E.id_theme AS theme, count(S.id) AS solved
|
||||
FROM solved S
|
||||
RIGHT OUTER JOIN exercices E ON E.id = S.id_exercice
|
||||
AND S.id_team = ".$this->id."
|
||||
AND E.id_theme = ".$id_theme);
|
||||
$db->deconnexion();
|
||||
|
||||
return $res['solved'];
|
||||
}
|
||||
|
|
@ -228,12 +218,11 @@ class Team
|
|||
{
|
||||
if ($id_theme != -1)
|
||||
{
|
||||
$db = new BDD();
|
||||
global $db;
|
||||
$ids = $db->query("SELECT `id_theme`, `id_exercice`
|
||||
FROM solved S
|
||||
LEFT OUTER JOIN exercices E ON S.id_exercice = E.id
|
||||
WHERE id_team =".$this->id." AND id_theme =".$id_theme);
|
||||
$db->deconnexion();
|
||||
|
||||
$array = array();
|
||||
$i = 0;
|
||||
|
|
@ -251,28 +240,25 @@ class Team
|
|||
// Static methods
|
||||
public static function set_revoked($bool, $name)
|
||||
{
|
||||
$db = new BDD();
|
||||
global $db;
|
||||
$db->query("UPDATE teams SET revoked = ".intval($bool)." WHERE team_name = '".$name."'");
|
||||
$aff = $db->affected();
|
||||
$db->deconnexion();
|
||||
|
||||
return ($aff == 1);
|
||||
}
|
||||
|
||||
public static function remove_team($id)
|
||||
{
|
||||
$db = new BDD();
|
||||
global $db;
|
||||
$db->query("DELETE FROM team_members WHERE id_team = ".$id);
|
||||
$db->query("DELETE FROM solved WHERE id_team = ".$id);
|
||||
$db->query("DELETE FROM teams WHERE id = ".$id);
|
||||
$db->deconnexion();
|
||||
}
|
||||
|
||||
public static function get_teams()
|
||||
{
|
||||
$db = new BDD();
|
||||
global $db;
|
||||
$ids = $db->query("SELECT `id` FROM `teams`");
|
||||
$db->deconnexion();
|
||||
|
||||
$array = array();
|
||||
if ($ids)
|
||||
|
|
@ -302,9 +288,8 @@ class Team
|
|||
|
||||
public static function get_nb_teams()
|
||||
{
|
||||
$db = new BDD();
|
||||
global $db;
|
||||
$res = $db->unique_query("SELECT COUNT(id) as count_teams FROM teams");
|
||||
$db->deconnexion();
|
||||
|
||||
return $res['count_teams'];
|
||||
}
|
||||
|
|
|
|||
Reference in a new issue