· 9 years ago · Nov 15, 2016, 11:18 PM
1<?php
2namespace ingsw10;
3
4use \PDO;
5use \PDOException;
6
7
8include "DAO.php";
9
10/**
11 *
12 */
13
14class mysqlUserDAO implements DAO
15{
16
17 private $dbUrl;
18 private $dbUser;
19 private $schemaTableName;
20 private $dbPassword;
21 private $PDOconnection;
22 private $createUserStmt;
23 private $getAllStmt;
24 private $getByIDStmt;
25 private $deleteByIDStmt;
26 private $deleteAllStmt;
27
28 public function __construct($servername, $schema, $tableName, $username, $password)
29 {
30
31 $this->dbUrl = $servername;
32 $this->dbUser = $username;
33 $this->dbPassword = $password;
34 $this->schemaTableName = $schema . "." . $tableName;
35 try {
36 #stringa caratteristica mysql
37 $this->PDOconnection = new PDO("mysql:host=$this->dbUrl", $this->dbUser, $this->dbPassword);
38 //echo "Connection successful";
39 #imposta quanti errori mostrare in caso di eccezione
40 $this->PDOconnection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
41
42 $this->createUserStmt = $this->PDOconnection->prepare('INSERT INTO ' . $this->schemaTableName . ' ( name, surname ) value (:name, :surname)');
43 $this->getAllStmt = $this->PDOconnection->prepare('SELECT * FROM ' . $this->schemaTableName . ' ORDER BY ID;');
44 $this->getByIDStmt = $this->PDOconnection->prepare('SELECT * FROM ' . $this->schemaTableName . ' WHERE ID = :id;');
45 $this->deleteByIDStmt = $this->PDOconnection->prepare('DELETE FROM ' . $this->schemaTableName . ' WHERE ID = :id;');
46 $this->deleteAllStmt = $this->PDOconnection->prepare('DELETE FROM ' . $this->schemaTableName . ';');
47
48 $schema = "CREATE SCHEMA IF NOT EXISTS " . $schema . ";";
49 /** @noinspection SqlDialectInspection */
50 $sql = "CREATE TABLE IF NOT EXISTS " . $this->schemaTableName . " (
51 id MEDIUMINT NOT NULL AUTO_INCREMENT,
52 name CHAR(30) NOT NULL,
53 surname CHAR(30) NOT NULL,
54 PRIMARY KEY (id)
55 );";
56
57 } catch (PDOException $e) {
58 echo "<br>";
59 echo "Connection failed: " . $e->getMessage();
60 }
61 echo "<br>";
62 }
63
64 public function create(User $user)
65 {
66 $this->createUserStmt->bindParam(':name', $user->getName());
67 $this->createUserStmt->bindParam(':surname', $user->getSurname());
68 try {
69 $this->createUserStmt->execute();
70 return $this->PDOconnection->lastInsertId('ID');
71
72 } catch (PDOException $e) {
73 echo "<br>";
74 echo "Connection failed: " . $e->getMessage();
75 return -1;
76 }
77 }
78
79 public function get($ID)
80 {
81 $this->getByIDStmt->bindParam(':id', $ID);
82 try {
83 $users = array();
84 if ($this->getByIDStmt->execute()) {
85 return User::recast($this->getByIDStmt->fetch());
86 }
87
88 } catch (PDOException $e) {
89 echo "<br>";
90 echo $e->getMessage();
91 }
92 return null;
93 }
94
95 public function getAll()
96 {
97
98 try {
99 $users = array();
100 if ($this->getAllStmt->execute()) {
101 while ($row = $this->getAllStmt->fetch()) {
102 $users[] = User::recast($row);
103 }
104 }
105 return $users;
106 } catch (PDOException $e) {
107 echo "<br>";
108 echo $e->getMessage();
109 }
110 return null;
111 }
112
113 public function update($u)
114 {
115 // TODO: implement here
116 }
117
118 public function delete($u)
119 {
120
121 }
122
123 public function deleteByID($ID)
124 {
125 $this->deleteByIDStmt->bindParam(':id', $ID);
126 try {
127 if ($this->deleteByIDStmt->execute()) return true;
128 } catch (PDOException $e) {
129 echo "<br>";
130 echo $e->getMessage();
131 }
132 return false;
133 }
134
135 public function deleteAll()
136 {
137 echo "table: " . $this->schemaTableName;
138 try {
139 if ($this->deleteAllStmt->execute()) return true;
140 } catch (PDOException $e) {
141 echo "<br>";
142 echo $e->getMessage();
143 }
144 return false;
145 }
146
147 function configure($query)
148 {
149 $updatedRows = 0;
150 try {
151 $updatedRows = $this->PDOconnection->exec($query);
152 } catch (PDOException $e) {
153 throw new PDOException($e->getMessage());
154 }
155 return $updatedRows;
156
157 }
158}
159
160?>