connector.class.php 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <?php
  2. /*******************************************************************************
  3. * Classe Connector *
  4. * Auteur : Brendan Abolivier *
  5. * Fonction : Permettre une gestion plus facile et plus claire de la connexion *
  6. * au serveur MySQL *
  7. * *
  8. * Attribut : *
  9. * $bdd : objet PDO *
  10. * *
  11. * Méthodes : *
  12. * __construct() *
  13. * Select() *
  14. * Insert() *
  15. * Update() *
  16. *******************************************************************************/
  17. include(__DIR__."/../../DbIds.php");
  18. class Connector {
  19. private $bdd;
  20. function __construct() {
  21. $dbconnect = getParams();
  22. $options = array(
  23. PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8",
  24. );
  25. $this->bdd = new PDO("mysql:host=".$dbconnect[0].";dbname=".$dbconnect[1],
  26. $dbconnect[2], $dbconnect[3], $options);
  27. $this->bdd->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  28. $this->bdd->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
  29. }
  30. /**
  31. * @param string $fields Fields to select
  32. * @param string $tables Tables to select from
  33. * @param mixed[] $options Array filled with the selection options, such as "WHERE", "ORDER BY" and "LIMIT"
  34. * @return mixed[]|null Array of values if the request went OK, NULL else
  35. * @throws InvalidArgumentException There was an error within the arguments array
  36. */
  37. function Select($fields, $tables, $options = array()) {
  38. $request = "SELECT $fields FROM $tables ";
  39. $arrayVerif = array();
  40. foreach($options as $name=>$value) {
  41. if(($upName = strtoupper($name)) == "WHERE") {
  42. $whereClause = " $upName ";
  43. foreach($value as $array) {
  44. if(sizeof($array) != 3 && sizeof($array) != 4) {
  45. throw new InvalidArgumentException("wrong_arg_nmbr_where");
  46. }
  47. if(sizeof($array) == 3) {
  48. $whereClause .= $array[0]." ".$array[1]." ? AND ";
  49. array_push($arrayVerif, $array[2]);
  50. } else {
  51. $whereClause .= $array[0]." ".$array[1]." ".$array[2]
  52. ." AND ";
  53. }
  54. }
  55. $request .= substr($whereClause, 0, -5);
  56. } else if(($upName = strtoupper($name)) == "ORDER BY") {
  57. if(sizeof($value) != 2 && substr($value[0], -2) != "()") {
  58. throw new InvalidArgumentException("wrong_arg_nmbr_order_by");
  59. }
  60. $request .= " ".$upName." ".implode(" ", $value);
  61. } else if(($upName = strtoupper($name)) == "LIMIT") {
  62. if(sizeof($value) == 1) {
  63. // La colonne "limit" ne contient qu'un nombre de champs
  64. $request .= " $upName ".$value[0];
  65. } else if(sizeof($value) == 2) {
  66. // La colonne "limit" contient un index de départ et un
  67. // nombre de champs
  68. $request .= " $upName ".$value[0].",".$value[1];
  69. } else {
  70. throw new InvalidArgumentException("wrong_arg_numbr_limit");
  71. }
  72. } else {
  73. throw new InvalidArgumentException("unknown_arg");
  74. }
  75. }
  76. $stmt = $this->bdd->prepare($request);
  77. if($stmt->execute($arrayVerif)) {
  78. return $stmt->fetchAll();
  79. } else {
  80. return null;
  81. }
  82. }
  83. /**
  84. * @param string $table Name of the table where the insertion takes place
  85. * @param string[] $values Array of values to insert in the database
  86. */
  87. function Insert($table, $values) {
  88. $request = "INSERT INTO $table(";
  89. $valeurs = "VALUES(";
  90. $arrayVerif = array();
  91. foreach($values as $name=>$value) {
  92. $request .= $name.",";
  93. $valeurs .= "?,";
  94. array_push($arrayVerif, $value);
  95. }
  96. $request = substr($request, 0, -1).") ".substr($valeurs, 0, -1).")";
  97. $stmt = $this->bdd->prepare($request);
  98. $stmt->execute($arrayVerif);
  99. }
  100. /**
  101. * @param string $table Name of the table where the update takes place
  102. * @param mixed[] $update Array of fields to update with new values, with a "set"
  103. * row filled with values, and a "where" row with conditions
  104. */
  105. function Update($table, $update) {
  106. $request = "UPDATE $table SET ";
  107. $arrayVerif = array();
  108. foreach($update["set"] as $name=>$value) {
  109. $request .= $name."=?,";
  110. array_push($arrayVerif, $value);
  111. }
  112. $request = substr($request, 0, -1)." WHERE ";
  113. foreach($update["where"] as $value) {
  114. $request .= $value[0].$value[1]."? AND ";
  115. array_push($arrayVerif, $value[2]);
  116. }
  117. $request = substr($request, 0, -5);
  118. $stmt = $this->bdd->prepare($request);
  119. $stmt->execute($arrayVerif);
  120. }
  121. /**
  122. * @param string $table Name of the table where the deletion takes place
  123. * @param mixed[] $where Array of conditions to select the right row(s) to delete
  124. */
  125. function Delete($table, $where = array()) {
  126. $request = "DELETE FROM $table";
  127. $arrayVerif = array();
  128. $request .= " WHERE ";
  129. foreach($where as $value) {
  130. $request .= $value[0].$value[1]."? AND ";
  131. array_push($arrayVerif, $value[2]);
  132. }
  133. $request = substr($request, 0, -5);
  134. $stmt = $this->bdd->prepare($request);
  135. $stmt->execute($arrayVerif);
  136. }
  137. function beginTransaction() {
  138. $this->bdd->beginTransaction();
  139. }
  140. function commit() {
  141. $this->bdd->commit();
  142. }
  143. }