connector.class.php 5.0KB

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