소스 검색

Added connecto

Brendan Abolivier 9 년 전
부모
커밋
e0bc4d5c1b
1개의 변경된 파일131개의 추가작업 그리고 0개의 파일을 삭제
  1. 131
    0
      models/connector.class.php

+ 131
- 0
models/connector.class.php 파일 보기

@@ -0,0 +1,131 @@
1
+<?php
2
+
3
+/*******************************************************************************
4
+* Classe Connector                                                             *
5
+* Auteur : Brendan Abolivier                                                   *
6
+* Fonction : Permettre une gestion plus facile et plus claire de la connexion  *
7
+*            au serveur MySQL                                                  *
8
+*                                                                              *
9
+*    Attribut :                                                                *
10
+*        $bdd : objet PDO                                                      *
11
+*                                                                              *
12
+* Méthodes :                                                                   *
13
+*        __construct()                                                         *
14
+*        Select()                                                              *
15
+*        Insert()                                                              *
16
+*        Update()                                                              *
17
+*******************************************************************************/
18
+class Connector {
19
+
20
+    private $bdd;
21
+
22
+    function __construct() {
23
+        // TODO : Select params to use
24
+        $dbconnect = array();
25
+        for($i = 0; $i < sizeof($matches[0]); $i++) {
26
+            $dbconnect[$matches[1][$i]] = $matches[2][$i];
27
+        }
28
+
29
+        $options = array(
30
+            PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8',
31
+        );
32
+
33
+        $this->bdd = new PDO("mysql:host=".$dbconnect["host"].";dbname="
34
+                .$dbconnect["dbname"], $dbconnect["user"], $dbconnect["pass"],
35
+                $options);
36
+        $this->bdd->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
37
+    }
38
+
39
+    function Select($fields, $tables, $options = array()) {
40
+        $request = "SELECT $fields FROM $tables ";
41
+        $arrayVerif = array();
42
+        foreach($options as $name=>$value) {
43
+            if(($upName = strtoupper($name)) == "WHERE") {
44
+                $whereClause = " $upName ";
45
+                foreach($value as $array) {
46
+                    if(sizeof($array) != 3 && sizeof($array) != 4) {
47
+                        throw new Exception('wrong_arg_nmbr_where');
48
+                    }
49
+                    if(sizeof($array) == 3) {
50
+                        $whereClause .= $array[0]." ".$array[1]." ? AND ";
51
+                        array_push($arrayVerif, $array[2]);
52
+                    } else {
53
+                        $whereClause .= $array[0]." ".$array[1]." ".$array[2]
54
+                                        ." AND ";
55
+                    }
56
+                }
57
+                $request .= substr($whereClause, 0, -5);
58
+            } else if(($upName = strtoupper($name)) == "ORDER BY") {
59
+                if(sizeof($value) != 2 && substr($value[0], -2) != "()") {
60
+                    throw new Exception('wrong_arg_nmbr_order_by');
61
+                }
62
+
63
+                $request .= " ".$upName." ".implode(' ', $value);
64
+            } else if(($upName = strtoupper($name)) == "LIMIT") {
65
+                if(sizeof($value) == 1) {
66
+                    // La colonne "limit" ne contient qu'un nombre de champs
67
+                    $request .= " $upName ".$value[0];
68
+                } else if(sizeof($value) == 2) {
69
+                    // La colonne "limit" contient un index de départ et un
70
+                    // nombre de champs
71
+                    $request .= " $upName ".$value[0].",".$value[1];
72
+                } else {
73
+                    throw new Exception('wrong_arg_numbr_limit');
74
+                }
75
+            } else {
76
+                throw new Exception('unknown_arg');
77
+            }
78
+        }
79
+
80
+        $stmt = $this->bdd->prepare($request);
81
+
82
+        if($stmt->execute($arrayVerif)) {
83
+            return $stmt->fetchAll();
84
+        } else {
85
+            return null;
86
+        }
87
+    }
88
+
89
+    function Insert($table, $values) {
90
+        $request = "INSERT INTO $table(";
91
+        $valeurs = "VALUES(";
92
+        $arrayVerif = array();
93
+        foreach($values as $name=>$value) {
94
+            $request .= $name.",";
95
+            $valeurs .= "?,";
96
+                array_push($arrayVerif, $value);
97
+        }
98
+
99
+        $request = substr($request, 0, -1).") ".substr($valeurs, 0, -1).")";
100
+
101
+        $stmt = $this->bdd->prepare($request);
102
+
103
+        $stmt->execute($arrayVerif);
104
+    }
105
+
106
+    function Update($table, $update) {
107
+        $request = "UPDATE $table SET ";
108
+        $arrayVerif = array();
109
+        foreach($update['set'] as $name=>$value) {
110
+            $request .= $name."=?,";
111
+            array_push($arrayVerif, $value);
112
+        }
113
+        $request = substr($request, 0, -1)." WHERE ";
114
+        foreach($update['where'] as $value) {
115
+            $request .= $value[0].$value[1]."? AND ";
116
+            array_push($arrayVerif, $value[2]);
117
+        }
118
+        $request = substr($request, 0, -5);
119
+
120
+        $stmt = $this->bdd->prepare($request);
121
+        $stmt->execute($arrayVerif);
122
+    }
123
+
124
+    function beginTransaction() {
125
+        $this->bdd->beginTransaction();
126
+    }
127
+
128
+    function commit() {
129
+        $this->bdd->commit();
130
+    }
131
+}