This repository has been archived by the owner on Feb 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDatabase.php
180 lines (143 loc) · 3.61 KB
/
Database.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
<?php
class Database {
/**
* @var PDO
*/
private $connection;
/**
* Creates a connection to a MySQL database
*
* @param string $db Name of the database to connect to
* @param string $user Username
* @param string $passwd Password
* @param string $host Database host. Defaults to 127.0.0.1 (localhost)
* @param array $options PDO Driver options {@see http://php.net/manual/en/ref.pdo-mysql.php}
*/
public function __construct($db, $user, $passwd, $host = '127.0.0.1', $options = [])
{
$this->connection = new \PDO("mysql:dbname={$db};host={$host}", $user, $passwd, $options);
}
public function prepareInsert($data, $table)
{
$columns = array_keys($data);
$values = array_values($data);
$placeholders = $this->generatePlaceholders($columns);
$strColumns = implode(',', $columns);
$strPlaceholders = implode(',', $placeholders);
$statement = $this->connection->prepare("INSERT INTO {$table} ({$strColumns}) VALUES ({$strPlaceholders})");
foreach (array_combine($placeholders, $values) as $placeholder => $value)
{
$statement->bindValue($placeholder, $value);
}
return $statement;
}
public function prepareRead($table, $columns, $where, $limit)
{
if (is_array($columns))
{
$columns = implode(',', $columns);
}
$query = "SELECT {$columns} FROM {$table}";
$placeholders = [];
if (!empty($where))
{
$query .= is_array($where)
? $this->processWhere($where, $placeholders)
: " {$where}";
}
if ($limit > 0)
{
$query .= " LIMIT {$limit}";
}
$statement = $this->connection->prepare($query);
if (!empty($placeholders))
{
foreach ($placeholders as $placeholder => $value)
{
$statement->bindValue($placeholder, $value);
}
}
return $statement;
}
public function prepareUpdate($table, $data, $where)
{
$placeholders = $this->generatePlaceholders(array_keys($data));
$pColumns = array_combine($placeholders, array_keys($data));
$pValues = array_combine($placeholders, array_values($data));
$set = '';
foreach ($pColumns as $placeholder => $column)
{
$set .= "{$column}={$placeholder},";
}
$set = rtrim(',', $set);
$query = "UPDATE {$table} SET {$set}";
if (!empty($where))
{
$query .= is_array($where)
? $this->processWhere($where, $pValues)
: " {$where}";
}
$statement = $this->connection->prepare($query);
unset($placeholder);
foreach ($pValues as $placeholder => $value)
{
$statement->bindValue($placeholder, $value);
}
return $statement;
}
public function prepareDelete($table, $where)
{
$query = "DELETE FROM {$table}";
$placeholders = [];
if (!empty($where))
{
$query .= is_array($where)
? $this->processWhere($where, $placeholders)
: " {$where}";
}
$statement = $this->connection->prepare($query);
if (!empty($placeholders))
{
foreach ($placeholders as $placeholder => $value)
{
$statement->bindValue($placeholder, $value);
}
}
return $statement;
}
private function generatePlaceholders($columns)
{
foreach ($columns as &$column)
{
$column = ":{$column}";
}
return $columns;
}
private function processWhere($where, &$placeholders)
{
$string = '';
foreach ($where as $clause)
{
if (count($clause) !== 4)
{
throw new RuntimeException('All where clauses must contain 4 parameters.');
}
foreach ($clause as $key => $value)
{
if ($key < 3)
{
$string .= " {$value}";
}
elseif ($key === 3)
{
$string .= " :{$clause[$key-2]}";
}
if (1 === $key)
{
$placeholders[":{$value}"] = $clause[$key+2];
}
}
}
return $string;
}
}