-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathMySQLTaskConnector.php
More file actions
109 lines (91 loc) · 2.51 KB
/
MySQLTaskConnector.php
File metadata and controls
109 lines (91 loc) · 2.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
<?php
namespace App\model;
use App\model\TaskConnector;
use PDO;
/**
* Conector MySQL para armazenamento de tarefas
*/
class MySQLTaskConnector implements TaskConnector {
/**
* @var PDO
*/
private $pdo;
/**
* Constroi o objeto que representa um conector MySQL para armazenamento
* de tarefas.
* @param string $host Host de conexão
* @param string $dbname Nome da base de dados
* @param string $user Usuário do banco
* @param string $pswd Senha do usuário do banco
*/
public function __construct( $host , $dbname , $user , $pswd ) {
$this->pdo = new PDO(
sprintf( 'mysql:host=%s;dbname=%s' , $host , $dbname ),
$user,
$pswd
);
}
/**
* @param Task $task
* @return boolean
* @see TaskConnector::create()
*/
public function create( Task $task ) {
$stm = $this->pdo->prepare( '
INSERT INTO `Task`(`taskName`,`taskDescription`,`taskStatus`,`taskOpened`)
VALUES (:taskName,:taskDescription,:taskStatus,NOW());
' );
$stm->bindValue( ':taskName' , $task->getTaskName() );
$stm->bindValue( ':taskDescription' , $task->getTaskDescription() );
$stm->bindValue( ':taskStatus' , $task->getTaskStatus() );
if ( $stm->execute() ) {
$task->setIdTask( (int) $this->pdo->lastInsertId() );
return true;
}
return false;
}
/**
* @return array
* @see TaskConnector::getTasks()
*/
public function getTasks() {
$stm = $this->pdo->prepare( 'SELECT * FROM `Task` ORDER BY `taskOpened`;' );
$stm->setFetchMode( PDO::FETCH_CLASS , 'Task' );
$stm->execute();
return $stm->fetchAll();
}
/**
* @param integer $id
* @return Task ou NULL se não encontrado
* @see TaskConnector::read()
*/
public function read( $id ) {
$stm = $this->pdo->prepare( 'SELECT * FROM `Task` WHERE `idTask`=:idTask;' );
$stm->bindParam( ':idTask' , $id , PDO::PARAM_INT );
$stm->setFetchMode( PDO::FETCH_CLASS , 'Task' );
$stm->execute();
$task = $stm->fetch();
$stm->closeCursor();
return $task;
}
/**
* @param Task $task
* @return boolean
* @see TaskConnector::update()
*/
public function update( Task $task ) {
$stm = $this->pdo->prepare( '
UPDATE `Task` SET
`taskName`=:taskName,
`taskDescription`=:taskDescription,
`taskStatus`=:taskStatus
WHERE
`idTask`=:idTask;
' );
$stm->bindValue( ':taskName' , $task->getTaskName() );
$stm->bindValue( ':taskDescription' , $task->getTaskDescription() );
$stm->bindValue( ':taskStatus' , $task->getTaskStatus() );
$stm->bindValue( ':idTask' , $task->getIdTask() );
return $stm->execute();
}
}