-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDB.php
137 lines (116 loc) · 3.67 KB
/
DB.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
<?php
class DB {
const ENVIRONMENT_STAGE = 'stage';
const ENVIRONMENT_LIVE = 'live';
const USER = 'root';
const PASS = 'root';
private $stageDb;
private $liveDb;
public function __construct()
{
try {
$this->stageDb = new PDO("mysql:host=localhost;dbname=local", DB::USER, DB::PASS);
$this->stageDb->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$this->liveDb = new PDO("mysql:host=localhost;dbname=live", DB::USER, DB::PASS);
$this->liveDb->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(PDOException $e) {
echo "Connections failed: " . $e->getMessage();
}
}
private function getEnv(string $environment)
{
switch ($environment) {
case DB::ENVIRONMENT_STAGE:
return $this->stageDb;
break;
case DB::ENVIRONMENT_LIVE:
return $this->liveDb;
break;
}
throw new \Exception('Unknown environment');
}
public function getValue(
string $environment,
string $query,
array $parameters = []
) {
$db = $this->getEnv($environment);
$query = $db->prepare($query);
$query->execute($parameters);
return $query->fetchColumn();
}
public function getColumn(
string $environment,
string $query,
array $parameters = []
) {
$db = $this->getEnv($environment);
$query = $db->prepare($query);
$query->execute($parameters);
return $query->fetchAll(PDO::FETCH_COLUMN);
}
public function getRow(
string $environment,
string $query,
array $parameters = []
) {
$db = $this->getEnv($environment);
$query = $db->prepare($query);
$query->execute($parameters);
return $query->fetch(PDO::FETCH_ASSOC);
}
public function getRows(
string $environment,
string $query,
array $parameters = []
) {
$db = $this->getEnv($environment);
$query = $db->prepare($query);
$query->execute($parameters);
return $query->fetchAll(PDO::FETCH_ASSOC);
}
public function insert(
string $environment,
string $table,
string $idColumnName,
array $data
): ?array {
$db = $this->getEnv($environment);
$keysString = implode(',',array_keys($data));
$dataString = ':'.implode(',:', array_keys($data));
$query = $db->prepare("INSERT INTO $table ($keysString) VALUES ($dataString);");
$query->execute($data);
$insertedId = $db->lastInsertId();
if ($insertedId) {
return $this->getRow(
$environment,
"SELECT * FROM $table WHERE $idColumnName=? LIMIT 1;",
[$insertedId]
);
} else {
return null;
}
}
public function updateOne(
string $environment,
string $table,
string $idColumnName,
int $id,
array $data
): ?array {
$db = $this->getEnv($environment);
$fields = [];
foreach ($data as $fieldName => $fieldValue) {
$fields[] = "$fieldName = :$fieldName";
}
$fields = implode(',', $fields);
$query = $db->prepare("UPDATE $table SET $fields WHERE $idColumnName = :idColumnValue LIMIT 1;");
$data['idColumnValue'] = $id;
$query->execute($data);
return $this->getRow(
$environment,
"SELECT * FROM $table WHERE $idColumnName = :idColumnValue LIMIT 1;",
[ 'idColumnValue' => $id ]
);
}
}