Преглед на файлове

Ajout de la méthode Select du connecteur

Brendan Abolivier преди 9 години
родител
ревизия
131d5bb4cd
променени са 3 файла, в които са добавени 45 реда и са изтрити 7 реда
  1. BIN
      web/.class.connector.php.swp
  2. BIN
      web/.index.php.swp
  3. 45
    7
      web/class.connector.php

BIN
web/.class.connector.php.swp Целия файл


BIN
web/.index.php.swp Целия файл


+ 45
- 7
web/class.connector.php Целия файл

@@ -1,6 +1,6 @@
1 1
 <?php 
2 2
 
3
-class Connector() {
3
+class Connector {
4 4
 
5 5
 	private $bdd;
6 6
 	
@@ -20,16 +20,54 @@ class Connector() {
20 20
 			"where" => array(
21 21
 				array("foo", "=", "bar"),
22 22
 				array("blbl", ">", 5)
23
-			);
24
-			"order by" => array(0, 10, "desc");
23
+			),
24
+			"order by" => array("foo", "desc"),
25
+			"limit" => array(0, 10) // Ou array(10)
25 26
 		);
26 27
 	*/
27
-	function Select($fields, $table, $options) {
28
-		$request = "SELECT $fields FROM $table ";
28
+	function Select($fields, $tables, $options) {
29
+		$request = "SELECT $fields FROM $tables ";
29 30
 		$arrayVerif = array();
30 31
 		foreach($options as $name=>$value) {
31
-/*			$request += " $name :$name";
32
-			$arrayVerif[":$name"] = $value;*/
32
+			if(($upName = strtoupper($name)) == "WHERE") {
33
+				$whereClause = " $upName ";
34
+				foreach($value as $array) {
35
+					if(sizeof($array) != 3) {
36
+						throw new Exception('Nombre de paramètres incorrect (WHERE). Les paramètres passés sont : '
37
+							.implode(',',$array));
38
+					}
39
+
40
+					$whereClause .= $array[0]." ".$array[1]." :".$array[0]." AND ";
41
+					$arrayVerif[":".$array[0]] = $array[2];
42
+				}
43
+				if($substring = substr($whereClause, 0, -5)) {
44
+					$request .= $substring;
45
+				} else {
46
+					throw new Exception('Problème lors de la création du substring');
47
+				}
48
+			} else if(($upName = strtoupper($name)) == "ORDER BY") {
49
+				if(sizeof($value) != 2) {
50
+					throw new Exception('Nombre de paramètres incorrects (ORDER BY). Les paramètres passés sont : '
51
+						.implode(',', $value));
52
+				}
53
+
54
+				$request .= " ".$upName." ".implode(' ', $value);
55
+			} else if(($upName = strtoupper($name)) == "LIMIT") {
56
+				if(sizeof($value) == 1) {
57
+					// La colonne "limit" ne contient qu'un nombre de champs
58
+					$request .= " $upName ".$value[0];
59
+				} else if(sizeof($value) == 2) {
60
+					// La colonne "limit" contient un index de départ et un nombre de champs
61
+					$request .= " $upName ".$value[0].",".$value[1];
62
+				} else {
63
+					throw new Exception('Nombre de paramètres incorrects (LIMIT). Les paramètres passés sont : '
64
+						.implode(',', $value));
65
+				}
66
+			} else {
67
+				throw new Exception('Argument '.strtoupper($name).' inconnu');
68
+			}
33 69
 		}
70
+
71
+		return $bdd->prepare($request)->execute($arrayVerif)->fetchAll();
34 72
 	}
35 73
 }