From: Greg Burri Date: Thu, 17 Sep 2009 21:41:38 +0000 (+0200) Subject: MOD Passage de MySQL à un vrai SGBDR : PostgreSQL X-Git-Url: https://git.euphorik.ch/?a=commitdiff_plain;h=6b9eac51629ad6fcec7da5f48b92f4c2b54c5901;p=cl7.git MOD Passage de MySQL à un vrai SGBDR : PostgreSQL --- diff --git a/index.php b/index.php index 9f5f1e8..eb24013 100644 --- a/index.php +++ b/index.php @@ -48,7 +48,7 @@ echo ''; # Des constantes stockées en BD accessible par le javascript. echo ''; echo ''; - $nbPeriodes = mysql_fetch_array(mysql_query("SELECT COUNT(*) FROM periodes")); + $nbPeriodes = pg_fetch_array(pg_query("SELECT COUNT(*) FROM periodes")); echo ''; ?> Corcelles-Lan 8 diff --git a/php/config.php b/php/config.php index 5730ac0..90cdbaf 100644 --- a/php/config.php +++ b/php/config.php @@ -3,8 +3,8 @@ class Config { function Config() { - $config_req = mysql_query("SELECT nom, valeur FROM config"); - while ($val = mysql_fetch_object($config_req)) + $config_req = pg_query("SELECT nom, valeur FROM config"); + while ($val = pg_fetch_object($config_req)) $this->config[$val->nom] = $val->valeur == "TRUE" ? TRUE : ($val->valeur == "FALSE" ? FALSE : $val->valeur); } diff --git a/php/connexion.php b/php/connexion.php index 1bd4f92..49c68a1 100644 --- a/php/connexion.php +++ b/php/connexion.php @@ -24,11 +24,11 @@ class Connexion } $f = fopen("php/config_bd.php", "w"); fwrite($f, '' + $NOM_BASE = "corcelles_lan_7";?>' ); fclose($f); echo "Le fichier a été créé, veuillez le compléter et recommencer."; @@ -37,15 +37,15 @@ class Connexion include_once("config_bd.php"); - $lien_mysql = mysql_connect($SQL_HOTE, $SQL_LOGIN, $SQL_PASS); - if (!$lien_mysql || !mysql_select_db($NOM_BASE)) + $conn_bd = pg_connect(sprintf("host=%s dbname=%s user=%s password=%s", $SQL_HOTE, $NOM_BASE, $SQL_LOGIN, $SQL_PASS)); + if (!$conn_bd) { echo "Connexion à la base de données impossible. Voir le fichier 'php/config_bd.php'"; exit(); } - mysql_set_charset("UTF8"); - mysql_query('SET AUTOCOMMIT=0'); + //mysql_set_charset("UTF8"); + //mysql_query('SET AUTOCOMMIT=0'); if (isset($_POST['effacer_cookie'])) # le membre se délogue { diff --git a/php/controller.php b/php/controller.php index b68834e..0906650 100644 --- a/php/controller.php +++ b/php/controller.php @@ -1,7 +1,7 @@ - email (non-public, sauf si piratage de la base ^-^´) + email (non-public) diff --git a/php/panel.php b/php/panel.php index 48d9043..f65705f 100644 --- a/php/panel.php +++ b/php/panel.php @@ -18,20 +18,20 @@ class Panel function rendre() { # selection de tous les participants - $res_SQL = mysql_query("SELECT pseudo FROM participants ORDER BY id"); + $res_SQL = pg_query("SELECT pseudo FROM participants ORDER BY id"); - echo '
', mysql_num_rows($res_SQL), ' inscrit', (mysql_num_rows($res_SQL) > 1 ? 's' : ''), '
'; + echo '
', pg_num_rows($res_SQL), ' inscrit', (pg_num_rows($res_SQL) > 1 ? 's' : ''), '
'; # affichage des participants - if (mysql_num_rows($res_SQL) > 0) + if (pg_num_rows($res_SQL) > 0) echo ''; if($this->participant->valide) diff --git a/php/participant.php b/php/participant.php index 7e549e1..9f718b0 100644 --- a/php/participant.php +++ b/php/participant.php @@ -6,7 +6,8 @@ class Participant { public $info; # Toute les infos du membre sous la forme d'un objet. - public $valide; # Savoir si le participant existe. + + private $id = 0; static private $NB_VOTES_PAR_PARTICIPANT = 3; # Concerne les votes des jeux joueés. @@ -16,41 +17,54 @@ class Participant * 2) $v1 = id, $v2 = NULL : la participant existe et est chargé à partir de son id * 3) $v1 = pseudo, $v2 = password : le participant existe et est chargé à partir de son pseudo et de son password */ - function Participant($v1=NULL, $v2=NULL) + public function Participant($v1=NULL, $v2=NULL) { # Aucunes valeurs transmise => ce n'est pas un participant valide. if ($v1 == NULL && $v2 == NULL) - { - $this->valide = 0; return; - } if (is_string($v1) && is_string($v2)) # Aucun des arguments n'est vide alors c'est le pseudo et le password qui ont été transmis - $res = mysql_query("SELECT * FROM participants WHERE pseudo = '" . addslashes($v1) . "' AND password = '" . addslashes($v2) . "'"); + $res = pg_query_params("SELECT id FROM participants WHERE pseudo = $1 AND password = $2", array($v1, $v2)); else # Sinon c'est l'id - $res = mysql_query("SELECT * FROM participants WHERE id = " . addslashes($v1)); + $res = pg_query_params("SELECT id FROM participants WHERE id = $1", array($v1)); - if (mysql_error() || mysql_num_rows($res) == 0) - { - $this->valide = FALSE; - } - else + if (pg_result_status($res) == PGSQL_COMMAND_OK && pg_num_rows($res) === 1) { - $this->info = mysql_fetch_object($res); - $this->valide = TRUE; + $this->id = pg_fetch_object($res)->id; } } + /** + * Est-ce que le participant existe ? C-à-d qu'il est inscrit. + */ + public function existe() + { + return $this->id != 0; + } + + public function chargerInfos() + { + if (!$this->existe()) + return; + + $res = pg_query_params("SELECT * FROM participants WHERE id = $1", array($this->id)); + + if (pg_result_status($res) == PGSQL_COMMAND_OK && pg_num_rows($res) === 1) + $this->info = pg_fetch_object($res); + else + $this->id = 0; + } + /** * Renvoie le nombre de votes restant pour le participant. */ function nb_vote_restant() { - $nombre_de_vote = mysql_fetch_array(mysql_query(" + $nombre_de_vote = pg_fetch_array(pg_query_params(" SELECT COUNT(*) FROM participants RIGHT JOIN jeux_choisis ON participants.id = jeux_choisis.participant_id - WHERE participants.id = " . $this->info->id . " + WHERE participants.id = $1 GROUP BY participants.id - ")); + ", array($this->id))); return Participant::$NB_VOTES_PAR_PARTICIPANT - $nombre_de_vote[0]; } @@ -60,11 +74,7 @@ class Participant */ static function nombre_participant_max_atteint() { - global $config; - $res_SQL = mysql_query("SELECT COUNT(*) FROM participants"); - $nb_participant = mysql_fetch_row($res_SQL); - - return $nb_participant[0] >= $config->get('nb_max_participant'); + return $this->nombre_place_restante() <= 0; } /** @@ -73,8 +83,8 @@ class Participant static function nombre_place_restante() { global $config; - $res_SQL = mysql_query("SELECT COUNT(*) FROM participants"); - $nb_participant = mysql_fetch_row($res_SQL); + $res_SQL = pg_query("SELECT COUNT(*) FROM participants"); + $nb_participant = pg_fetch_row($res_SQL); return $config->get('nb_max_participant') - $nb_participant[0]; } diff --git a/php/update_db.php b/php/update_db.php index 7f43381..66a3216 100644 --- a/php/update_db.php +++ b/php/update_db.php @@ -10,131 +10,125 @@ $connexion = new Connexion(); function creer_db() { - mysql_query(" - CREATE TABLE IF NOT EXISTS config ( + pg_query(" + CREATE TABLE config ( nom varchar(50) NOT NULL, valeur varchar(255) NOT NULL, PRIMARY KEY (nom) - ) ENGINE=InnoDB AUTO_INCREMENT=30 DEFAULT CHARSET=utf8 ROW_FORMAT=FIXED; + ); "); - mysql_query(" + pg_query(" CREATE TABLE pizzas ( - id mediumint(3) unsigned NOT NULL auto_increment, + id serial NOT NULL, nom varchar(40) NOT NULL, composition varchar(255) NOT NULL, - prix tinyint(3) unsigned default '0', + prix smallint NOT NULL, PRIMARY KEY (id) - ) ENGINE=InnoDB AUTO_INCREMENT=33 DEFAULT CHARSET=utf8 ROW_FORMAT=FIXED; + ); "); - mysql_query(" - CREATE TABLE IF NOT EXISTS jeux ( - id mediumint(3) unsigned NOT NULL auto_increment, - nom varchar(200) default '0', + pg_query(" + CREATE TABLE jeux ( + id serial NOT NULL, + nom varchar(200) NOT NULL, PRIMARY KEY (id), - UNIQUE KEY nom_unique (nom) - ) ENGINE=InnoDB AUTO_INCREMENT=30 DEFAULT CHARSET=utf8 ROW_FORMAT=FIXED; + UNIQUE (nom) + ); "); - mysql_query(" - CREATE TABLE IF NOT EXISTS participants ( - id mediumint(3) unsigned NOT NULL auto_increment, - pseudo varchar(50) default NULL, - clan_nom varchar(30) default NULL, - clan_tag varchar(10) default NULL, - password varchar(10) default NULL, - nom varchar(30) default NULL, - prenom varchar(30) default NULL, - age varchar(30) default NULL, - e_mail varchar(50) default NULL, - remarques varchar(255) default NULL, - admin tinyint(1) unsigned NOT NULL default '0', - a_paye tinyint(1) unsigned NOT NULL default '0', - pizza mediumint(3) unsigned default NULL, - pizza_paye tinyint(1) NOT NULL default '0', - PRIMARY KEY (id), - KEY FK_pizza (pizza), - CONSTRAINT FK_pizza FOREIGN KEY (pizza) REFERENCES pizzas (id) ON DELETE SET NULL ON UPDATE SET NULL - ) ENGINE=InnoDB AUTO_INCREMENT=47 DEFAULT CHARSET=utf8 ROW_FORMAT=DYNAMIC; + pg_query(" + CREATE TABLE participants ( + id serial NOT NULL, + pseudo varchar(50) DEFAULT NULL, + clan_nom varchar(30) DEFAULT NULL, + clan_tag varchar(10) DEFAULT NULL, + password varchar(10) DEFAULT NULL, + nom varchar(30) DEFAULT NULL, + prenom varchar(30) DEFAULT NULL, + age varchar(30) DEFAULT NULL, + e_mail varchar(50) DEFAULT NULL, + remarques varchar(255) DEFAULT NULL, + admin boolean NOT NULL default FALSE, + a_paye boolean NOT NULL default FALSE, + pizza int DEFAULT NULL, + pizza_paye boolean NOT NULL default FALSE, + PRIMARY KEY (id), + UNIQUE (pseudo), + FOREIGN KEY (pizza) REFERENCES pizzas (id) ON DELETE SET NULL ON UPDATE SET NULL + ); + "); + pg_query(" + CREATE TABLE jeux_choisis ( + participant_id int NOT NULL, + jeu_id int NOT NULL, + PRIMARY KEY (participant_id, jeu_id), + FOREIGN KEY (participant_id) REFERENCES participants (id) ON DELETE CASCADE ON UPDATE CASCADE, + FOREIGN KEY (jeu_id) REFERENCES jeux (id) ON DELETE CASCADE ON UPDATE CASCADE + ); + "); + pg_query(" + CREATE TABLE periodes ( + id serial NOT NULL, + nom varchar(200) NOT NULL, + nom_abrege varchar(20) NOT NULL DEFAULT '', + PRIMARY KEY (id) + ); "); - mysql_query(" - CREATE TABLE IF NOT EXISTS jeux_choisis ( - participant_id mediumint(3) unsigned NOT NULL, - jeu_id mediumint(3) unsigned NOT NULL, - PRIMARY KEY USING BTREE (participant_id,jeu_id), - KEY FK_jeu (jeu_id), - CONSTRAINT FK_participant FOREIGN KEY (participant_id) REFERENCES participants (id) ON DELETE CASCADE ON UPDATE CASCADE, - CONSTRAINT FK_jeu FOREIGN KEY (jeu_id) REFERENCES jeux (id) ON DELETE CASCADE ON UPDATE CASCADE - ) ENGINE=InnoDB DEFAULT CHARSET=utf8 ROW_FORMAT=DYNAMIC; + pg_query(" + CREATE TABLE participations ( + participant_id serial NOT NULL, + periode_id int NOT NULL, + PRIMARY KEY (participant_id, periode_id), + FOREIGN KEY (participant_id) REFERENCES participants (id) ON DELETE CASCADE ON UPDATE CASCADE, + FOREIGN KEY (periode_id) REFERENCES periodes (id) ON DELETE CASCADE ON UPDATE CASCADE + ); "); } function initialiser_db() { - mysql_query("INSERT INTO config (nom, valeur) VALUES ('version', 1)"); + pg_query("INSERT INTO config (nom, valeur) VALUES ('version', 1)"); + + pg_query("INSERT INTO config (nom, valeur) VALUES ('cout_periode', '15')"); + pg_query("INSERT INTO config (nom, valeur) VALUES ('cout_total', '40')"); + pg_query("INSERT INTO config (nom, valeur) VALUES ('nb_max_participant', '25')"); + pg_query("INSERT INTO config (nom, valeur) VALUES ('nb_votes_jeux', '3')"); + pg_query("INSERT INTO config (nom, valeur) VALUES ('inscription_terminees', 'FALSE')"); + pg_query("INSERT INTO config (nom, valeur) VALUES ('pizza_visible', 'FALSE')"); + pg_query("INSERT INTO config (nom, valeur) VALUES ('pizza_peut_commander', 'TRUE')"); + + pg_query("INSERT INTO periodes (nom, nom_abrege) VALUES ('vendredi soir à samedi', 'v->s')"); + pg_query("INSERT INTO periodes (nom, nom_abrege) VALUES ('samedi à dimanche', 's->d')"); + pg_query("INSERT INTO periodes (nom, nom_abrege) VALUES ('dimanche à lundi', 'd->l')"); } function update_db() { # si la table 'config' n'existe pas alors on suppose qu'aucune table n'existe $version = 0; - $res = @mysql_fetch_object(mysql_query("SELECT valeur FROM config WHERE nom = 'version'")); - if (!mysql_error()) + $res = @pg_fetch_object(pg_query("SELECT valeur FROM config WHERE nom = 'version'")); + if (pg_result_status($res) != PGSQL_COMMAND_OK) $version = $res->valeur; if($version == 0) { - mysql_query("BEGIN TRANSACTION"); + echo "Création de la base de donnée, version = 1"; + pg_query("BEGIN"); creer_db(); initialiser_db(); - mysql_query("COMMIT"); + pg_query("COMMIT"); $version = 1; } - # version 1 -> 2 + # version 1 -> 2 (à utiliser pour une utilisation future) + /* if ($version == 1) { - mysql_query("BEGIN TRANSACTION"); - mysql_query("ALTER TABLE participants ADD CONSTRAINT UNIQUE INDEX pseudo_unique (pseudo);"); - mysql_query(" - CREATE TABLE IF NOT EXISTS periodes ( - id mediumint(3) unsigned NOT NULL auto_increment, - nom varchar(200) NOT NULL, - nom_abrege varchar(20) NOT NULL DEFAULT '', - PRIMARY KEY (id) - ) ENGINE=InnoDB AUTO_INCREMENT=30 DEFAULT CHARSET=utf8 ROW_FORMAT=FIXED; - "); - mysql_query("INSERT INTO periodes (nom, nom_abrege) VALUES ('vendredi soir à samedi', 'v->s')"); - mysql_query("INSERT INTO periodes (nom, nom_abrege) VALUES ('samedi à dimanche', 's->d')"); - mysql_query("INSERT INTO periodes (nom, nom_abrege) VALUES ('dimanche à lundi', 'd->l')"); - mysql_query(" - CREATE TABLE IF NOT EXISTS participations ( - participant_id mediumint(3) unsigned NOT NULL, - periode_id mediumint(3) unsigned NOT NULL, - PRIMARY KEY USING BTREE (participant_id, periode_id), - KEY FK_periode (periode_id), - CONSTRAINT FK_participant_participations FOREIGN KEY (participant_id) REFERENCES participants (id) ON DELETE CASCADE ON UPDATE CASCADE, - CONSTRAINT FK_periode_participations FOREIGN KEY (periode_id) REFERENCES periodes (id) ON DELETE CASCADE ON UPDATE CASCADE - ) ENGINE=InnoDB DEFAULT CHARSET=utf8 ROW_FORMAT=DYNAMIC; - "); - $participations = mysql_query("SELECT participants.id as participant_id, periodes.id as periode_id FROM participants, periodes"); - while ($participation = mysql_fetch_object($participations)) - { - mysql_query(" - INSERT INTO participations (participant_id, periode_id) - VALUES (".$participation->participant_id.",".$participation->periode_id.")" - ); - } - mysql_query("UPDATE config SET valeur = '2' WHERE nom = 'version'"); + pg_query("BEGIN"); - mysql_query("INSERT INTO config (nom, valeur) VALUES ('cout_periode', '15')"); - mysql_query("INSERT INTO config (nom, valeur) VALUES ('cout_total', '40')"); - mysql_query("INSERT INTO config (nom, valeur) VALUES ('nb_max_participant', '25')"); - mysql_query("INSERT INTO config (nom, valeur) VALUES ('nb_votes_jeux', '3')"); - mysql_query("INSERT INTO config (nom, valeur) VALUES ('inscription_terminees', 'FALSE')"); - mysql_query("INSERT INTO config (nom, valeur) VALUES ('pizza_visible', 'FALSE')"); - mysql_query("INSERT INTO config (nom, valeur) VALUES ('pizza_peut_commander', 'TRUE')"); + # queries... + pg_query("UPDATE config SET valeur = '2' WHERE nom = 'version'"); - mysql_query("COMMIT"); - } + pg_query("COMMIT"); + }*/ } update_db(); diff --git a/tools/mise_en_prod.sh b/tools/mise_en_prod.sh index 9d5c9fa..f67aed3 100755 --- a/tools/mise_en_prod.sh +++ b/tools/mise_en_prod.sh @@ -7,4 +7,4 @@ cd ../urlrewrite erlc cl7_urlrewrite.erl cd .. rsync -r --delete --exclude-from=tools/exclude . $ADRESSE:$REP -ssh $ADRESSE "cd $REP; php-cgi php/update_db.php && rm php/update_db.php" \ No newline at end of file +ssh $ADRESSE "cd $REP; php php/update_db.php && rm php/update_db.php" \ No newline at end of file