connector.class.php 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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(dirname(__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. function Select($fields, $tables, $options = array()) {
  31. $request = "SELECT $fields FROM $tables ";
  32. $arrayVerif = array();
  33. foreach($options as $name=>$value) {
  34. if(($upName = strtoupper($name)) == "WHERE") {
  35. $whereClause = " $upName ";
  36. foreach($value as $array) {
  37. if(sizeof($array) != 3 && sizeof($array) != 4) {
  38. throw new Exception("wrong_arg_nmbr_where");
  39. }
  40. if(sizeof($array) == 3) {
  41. $whereClause .= $array[0]." ".$array[1]." ? AND ";
  42. array_push($arrayVerif, $array[2]);
  43. } else {
  44. $whereClause .= $array[0]." ".$array[1]." ".$array[2]
  45. ." AND ";
  46. }
  47. }
  48. $request .= substr($whereClause, 0, -5);
  49. } else if(($upName = strtoupper($name)) == "ORDER BY") {
  50. if(sizeof($value) != 2 && substr($value[0], -2) != "()") {
  51. throw new Exception("wrong_arg_nmbr_order_by");
  52. }
  53. $request .= " ".$upName." ".implode(" ", $value);
  54. } else if(($upName = strtoupper($name)) == "LIMIT") {
  55. if(sizeof($value) == 1) {
  56. // La colonne "limit" ne contient qu'un nombre de champs
  57. $request .= " $upName ".$value[0];
  58. } else if(sizeof($value) == 2) {
  59. // La colonne "limit" contient un index de départ et un
  60. // nombre de champs
  61. $request .= " $upName ".$value[0].",".$value[1];
  62. } else {
  63. throw new Exception("wrong_arg_numbr_limit");
  64. }
  65. } else {
  66. throw new Exception("unknown_arg");
  67. }
  68. }
  69. $stmt = $this->bdd->prepare($request);
  70. if($stmt->execute($arrayVerif)) {
  71. return $stmt->fetchAll();
  72. } else {
  73. return null;
  74. }
  75. }
  76. function Insert($table, $values) {
  77. $request = "INSERT INTO $table(";
  78. $valeurs = "VALUES(";
  79. $arrayVerif = array();
  80. foreach($values as $name=>$value) {
  81. $request .= $name.",";
  82. $valeurs .= "?,";
  83. array_push($arrayVerif, $value);
  84. }
  85. $request = substr($request, 0, -1).") ".substr($valeurs, 0, -1).")";
  86. $stmt = $this->bdd->prepare($request);
  87. $stmt->execute($arrayVerif);
  88. }
  89. function Update($table, $update) {
  90. $request = "UPDATE $table SET ";
  91. $arrayVerif = array();
  92. foreach($update["set"] as $name=>$value) {
  93. $request .= $name."=?,";
  94. array_push($arrayVerif, $value);
  95. }
  96. $request = substr($request, 0, -1)." WHERE ";
  97. foreach($update["where"] as $value) {
  98. $request .= $value[0].$value[1]."? AND ";
  99. array_push($arrayVerif, $value[2]);
  100. }
  101. $request = substr($request, 0, -5);
  102. $stmt = $this->bdd->prepare($request);
  103. $stmt->execute($arrayVerif);
  104. }
  105. function Delete($table, $where = array()) {
  106. $request = "DELETE FROM $table";
  107. $arrayVerif = array();
  108. $request .= " WHERE ";
  109. foreach($where as $value) {
  110. $request .= $value[0].$value[1]."? AND ";
  111. array_push($arrayVerif, $value[2]);
  112. }
  113. $request = substr($request, 0, -5);
  114. $stmt = $this->bdd->prepare($request);
  115. $stmt->execute($arrayVerif);
  116. }
  117. function beginTransaction() {
  118. $this->bdd->beginTransaction();
  119. }
  120. function commit() {
  121. $this->bdd->commit();
  122. }
  123. }