Créé dans le cadre du projet de fin d'année de la promo 2018 de CIR2 de l'ISEN Brest/Rennes, le Burger Quizz est une adaptation numérique du jeu télévisé éponyme, plus précisément d'une épreuve spécifique de ce jeu : le "Sel ou Poivre".

class.connector.php 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. class Connector {
  3. private $bdd;
  4. function __construct() {
  5. $params = file_get_contents("../params.cfg");
  6. preg_match_all('/db_(.+)\: (.+)/', $params, $matches);
  7. $dbconnect = array();
  8. for($i = 0; $i < sizeof($matches[0]); $i++) {
  9. $dbconnect[$matches[1][$i]] = $matches[2][$i];
  10. }
  11. $this->bdd = new PDO("mysql:host=".$dbconnect["host"].";dbname=".$dbconnect["dbname"], $dbconnect["user"], $dbconnect["pass"]);
  12. $this->bdd->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  13. }
  14. /*
  15. Exemple de $options :
  16. $options = array(
  17. "where" => array(
  18. array("foo", "=", "bar"),
  19. array("blbl", ">", 5)
  20. ),
  21. "order by" => array("foo", "desc"),
  22. "limit" => array(0, 10) // Ou array(10)
  23. );
  24. */
  25. function Select($fields, $tables, $options = array()) {
  26. $request = "SELECT $fields FROM $tables ";
  27. $arrayVerif = array();
  28. foreach($options as $name=>$value) {
  29. if(($upName = strtoupper($name)) == "WHERE") {
  30. $whereClause = " $upName ";
  31. foreach($value as $array) {
  32. if(sizeof($array) != 3 && sizeof($array) != 4) {
  33. throw new Exception('wrong_arg_nmbr_where');
  34. }
  35. if(sizeof($array) == 3) {
  36. $whereClause .= $array[0]." ".$array[1]." ? AND ";
  37. array_push($arrayVerif, $array[2]);
  38. } else {
  39. $whereClause .= $array[0]." ".$array[1]." ".$array[2]." AND ";
  40. }
  41. }
  42. $request .= substr($whereClause, 0, -5);
  43. } else if(($upName = strtoupper($name)) == "ORDER BY") {
  44. if(sizeof($value) != 2 && substr($value[0], -2) != "()") {
  45. throw new Exception('wrong_arg_nmbr_order_by');
  46. }
  47. $request .= " ".$upName." ".implode(' ', $value);
  48. } else if(($upName = strtoupper($name)) == "LIMIT") {
  49. if(sizeof($value) == 1) {
  50. // La colonne "limit" ne contient qu'un nombre de champs
  51. $request .= " $upName ".$value[0];
  52. } else if(sizeof($value) == 2) {
  53. // La colonne "limit" contient un index de départ et un nombre de champs
  54. $request .= " $upName ".$value[0].",".$value[1];
  55. } else {
  56. throw new Exception('wrong_arg_numbr_limit');
  57. }
  58. } else {
  59. throw new Exception('unknown_arg');
  60. }
  61. }
  62. $stmt = $this->bdd->prepare($request);
  63. if($stmt->execute($arrayVerif)) {
  64. return $stmt->fetchAll();
  65. } else {
  66. return null;
  67. }
  68. }
  69. function Insert($table, $values) {
  70. $request = "INSERT INTO $table(";
  71. $valeurs = "VALUES(";
  72. $arrayVerif = array();
  73. foreach($values as $name=>$value) {
  74. $request .= $name.",";
  75. $valeurs .= "?,";
  76. array_push($arrayVerif, $value);
  77. }
  78. $request = substr($request, 0, -1).") ".substr($valeurs, 0, -1).")";
  79. $stmt = $this->bdd->prepare($request);
  80. $stmt->execute($arrayVerif);
  81. }
  82. function Update($table, $update) {
  83. $request = "UPDATE $table SET ";
  84. $arrayVerif = array();
  85. foreach($update['set'] as $name=>$value) {
  86. $request .= $name."=?,";
  87. array_push($arrayVerif, $value);
  88. }
  89. $request = substr($request, 0, -1)." WHERE ";
  90. foreach($update['where'] as $value) {
  91. $request .= $value[0].$value[1]."? AND ";
  92. array_push($arrayVerif, $value[2]);
  93. }
  94. $request = substr($request, 0, -5);
  95. $stmt = $this->bdd->prepare($request);
  96. $stmt->execute($arrayVerif);
  97. }
  98. function beginTransaction() {
  99. $this->bdd->beginTransaction();
  100. }
  101. function commit() {
  102. $this->bdd->commit();
  103. }
  104. }