-
Notifications
You must be signed in to change notification settings - Fork 1
/
classDatabase.php
281 lines (241 loc) · 7.09 KB
/
classDatabase.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
<?php
/**
* class Database
* parent class for database connections
* uses PDO
*/
namespace Cat;
use PDO;
use PDOException;
require_once 'config.php';
class Database
{
public $isConnected;//are we connected?
protected $databaseData;//protected only use in this class
protected $recordID;//record id in database, used for deleting
private static $factory = false;
private $db;
/**
* database constructor.
* @param array $options
*/
public function __construct(/*$options=[]*/)
{
/*$this->isConnected = TRUE;//if true then we connected to db
//connect to db
try {
$this->databaseData = new PDO(DB_TYPE.':host='.DB_HOST.';dbname='.DB_NAME, DB_USER, DB_PASS);
$this->databaseData->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
return "connection failed: " . $e->getMessage() . "<br /><br />";
}*/
}
/**
* Get Factory
* @return bool|Database
*/
public static function getFactory()
{
if (!self::$factory)
self::$factory = new Database();
return self::$factory;
}
/**
* Get Connection
* @return PDO
*/
public function getConnection()
{
if (!$this->db) {
$this->db = new PDO(DB_TYPE.':host='.DB_HOST.';dbname='.DB_NAME, DB_USER, DB_PASS);
}
return $this->db;
}
/**
* Disconnect from Database
*/
public function disconnect()
{
$this->databaseData = NULL;
$this->isConnected = FALSE;
}
/**
* Get Row
* @param $id
* @deprecated - moved to \Cat\getSingleCatByID
*/
public function getRow($id)
{
/*try
{
$stmt = $this->databaseData->prepare("SELECT * FROM users WHERE id=$id");
$stmt->execute();
return $stmt->fetch();
} catch (\PDOException $e) {
return $e->getMessage();
}*/
}
/**
* Get All Rows
* @return array
*/
public function getAllRows()
{
try
{
$stmt = $this->db->prepare("SELECT * FROM users");
$stmt->execute();
return $stmt->fetchAll();
} catch (\PDOException $e) {
echo $e->getMessage();
}
}
/**
* Return Column Count
* @param $columnName
*/
public function returnColumnCount($columnName) {
try
{
$stmt = $this->databaseData->prepare("SELECT COUNT(id) FROM users");
$stmt->execute();
$result = $stmt->fetchColumn($columnName);
echo 'There are '. $result . ' number of records';
} catch (\PDOException $e) {
echo 'There was an issue excecuting your query:' . $e->getMessage();
}
}
/**
* Get Rows By Category
* @param $columnName
* @param $columnValue
*/
//pipe in certain search query to output all rows by
//i.e., output all rows where Coloring is Brown
//@TODO:extend to show more than one column
public function getRowsByCategory($columnName, $columnValue) {
/*
* in the cat class, getcatsbyategory
* also in catclass, valid categories in the cat class
*/
try
{
$stmt = $this->databaseData->prepare("SELECT * FROM users WHERE $columnName = '$columnValue'");
$stmt->execute();
//$result = $stmt->fetchAll(PDO::FETCH_CLASS, '\\Cat\\Cat');
$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
print_r($result);
} catch (\PDOException $e) {
echo $e->getMessage();
}
}
/**
* Insert New Record
* @param $data
* @return string
*/
public function insertRow($data) {
//@TODO: check if not empty data, etc
$db = self::getConnection();
try
{
$stmt = $db->prepare("INSERT INTO users (" . implode(', ', array_keys($data)) . ") VALUES (:" . implode(', :', array_keys($data)) . ")");
foreach($data as $key => $value) {
$stmt->bindValue($key, $value, PDO::PARAM_STR);
}
$stmt->execute();
return 'record created successfully';
} catch (\PDOException $e) {
return 'Adding new record failed' . $e->getMessage();
}
}
/**
* Update Record
* @param $data
* @param $where
* @return string
*/
public function updateRow($data, $where)
{
$db = self::getConnection();
//var_dump($data);
//echo '<p></p>';
//var_dump($where);
//echo '<p></p>';
try
{
$values = [];
$qry = "UPDATE users SET";
foreach($data as $key => $value) {
$qry .= " {$key}=:{$key},";
$values[":{$key}"] = $value;
}
$qry = rtrim($qry, ',');
if(!empty($where)){
$qry .= " WHERE ";
foreach($where as $key => $value) {
$qry .= " {$key}=:{$key} AND";
//$qry .= " {$key}=:{$key}";
$values[":{$key}"] = $value;
}
$qry = substr($qry, 0, -4);
}
$stmt = $db->prepare($qry);
$stmt->execute($values);
//print_r($stmt);
//echo '<p></p>';
//var_dump($stmt->errorInfo());
} catch (\PDOException $e) {
//return 'Adding new record failed' . $e->getMessage();
//@TODO:write to error log
return $e;
}
}
/**
* Update Record Row
* @param $data
* @param $catID
* @return string
*/
/*public function updateRow($data, $catID)
{
$db = self::getConnection();
try
{
foreach($data as $key => $value) {
$columns[] = '' . $key . '=' . $value;
}
$stmt = $db->prepare("UPDATE users SET " . implode(', ', $columns). " WHERE id=:catID");
print_r($stmt);
foreach($data as $key => $value) {
$stmt->bindValue($key, $value, PDO::PARAM_STR);
}
$stmt->bindValue(':catID', $catID, PDO::PARAM_INT);
$stmt->execute();
} catch (\PDOException $e) {
return 'Adding new record failed' . $e->getMessage();
}
}*/
/**
* Delete Database Row
* @param $query
*/
public function deleteRow($id)
{
//@TODO:using wrong way to pass in variable, refer to updateRow()
$this->recordID = $id;
if ($id == '') {
return 'invalid delete: record is blank';
} else {
try
{
$stmt = $this->databaseData->prepare("DELETE FROM users WHERE id = '{$id}'");
//@TODO: check if record exists first, create if/else around this
$stmt->execute();
return 'record deleted successfully';
} catch (\PDOException $e) {
return $e->getMessage();
}
}
}
}