forked from vortrixs/simple_pdo_crud
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Crud.php
126 lines (107 loc) · 2.87 KB
/
Crud.php
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
<?php
class Crud {
/**
* @var PDO
*/
private $db;
/**
* @var string
*/
private $table;
/**
* Stores the database connection and the table we are using
*
* @param Database $db Database object containing the PDO connection
* @param string $table Database table we are going to access
*/
public function __construct(Database $db, $table)
{
$this->db = $db;
$this->table = $table;
}
/**
* Creates a new record in the database table
*
* @param array<string,string|int|float|boolean> $data Data to insert formatted as array(columnName => value)
*
* @return integer
*/
public function create($data)
{
$sql = $this->db->prepareInsert($this->table, $data);
if (false === $sql->execute())
{
throw new \RuntimeException($sql->errorInfo(), $sql->errorCode());
}
return $this->db->lastInsertId();
}
/**
* Read rows from the table
*
* @param array $columns A list of columns to fetch
* @param array|string $where Multi-dimensional array or a SQL string for complex restrictions
* Refer to the readme for examples
* @param integer $limit How many rows to fetch
*
* @return array
*/
public function read($columns = ['*'], $where = [], $limit = 0)
{
$sql = $this->db->prepareRead($this->table, $columns, $where);
if (false === $sql->execute())
{
throw new \RuntimeException($this->db->errorInfo(), $this->db->errorCode());
}
return $sql->fetchAll();
}
/**
* Updates specified columns in the table
*
* @param array $data The data that is being updated, formatted as array(columnName => newValue)
* @param array|string $where Multi-dimensional array or a SQL string for complex restrictions
* Refer to the readme for examples
*
* @return integer
*/
public function update($data, $where)
{
$sql = $this->db->prepareUpdate($this->table, $data, $where);
if (false === $sql->execute())
{
throw new \RuntimeException($sql->errorInfo(), $sql->errorCode());
}
return $this->db->rowCount();
}
/**
* Deletes rows from the table
*
* @param array|string $where Multi-dimensional array or a SQL string for complex restrictions
* Refer to the readme for examples
*
* @return boolean
*/
public function delete($where = [])
{
$sql = $this->db->prepareDelete($this->table, $where);
if (false === $sql->execute())
{
throw new \RuntimeException($this->db->errorInfo(), $this->db->errorCode());
}
return true;
}
/**
* Manually execute a SQL query
*
* @param string $query [description]
* @return array
*/
public function execute($query)
{
$sql = $this->db->query($query);
if (false === $sql || false === $sql->execute())
{
throw new \RuntimeException($this->db->errorInfo(), $this->db->errorCode());
}
return $sql->fetchAll();
}
}