changeset 8 | 54ea0099329c |
parent 7 | 3ed6f90e97d9 |
child 13 | 4690a1ccb7cc |
7:3ed6f90e97d9 | 8:54ea0099329c |
---|---|
10 use Logger; |
10 use Logger; |
11 use PDO; |
11 use PDO; |
12 use PDOException; |
12 use PDOException; |
13 |
13 |
14 class Database implements DBInterface { |
14 class Database implements DBInterface { |
15 private static $logger = null; |
|
16 private static $handle = null; |
15 private static $handle = null; |
17 |
16 private $logger; |
18 /** |
17 /** |
19 * @var PDO |
18 * @var PDO |
20 */ |
19 */ |
21 private $pdo; |
20 private $pdo; |
22 |
21 |
23 private function __construct() { |
22 private function __construct() { |
24 self::$logger = Logger::getLogger('__CLASS__'); |
23 $this->logger = Logger::getLogger(__CLASS__); |
25 |
24 |
26 $dsn = 'mysql:host=localhost;dbname=ticketsystem'; |
25 $dsn = 'mysql:host=localhost;dbname=ticketsystem'; |
27 $config = array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"); |
26 $config = array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"); |
28 $this->pdo = new PDO($dsn, 'ticketsystem', 'ticketsystem', $config); |
27 $this->pdo = new PDO($dsn, 'ticketsystem', 'ticketsystem', $config); |
29 } |
28 } |
57 * |
56 * |
58 * @return bool |
57 * @return bool |
59 */ |
58 */ |
60 public function cleanup() { |
59 public function cleanup() { |
61 try { |
60 try { |
62 $status = $this->pdo->exec("DELETE FROM `t_ticket` WHERE `id` > 1;"); |
61 $this->pdo->exec("DELETE FROM `t_user` WHERE `id` > 2;"); |
63 $status = $this->pdo->exec("DELETE FROM `t_history` WHERE `id` > 1;"); |
62 $this->pdo->exec("DELETE FROM `t_group` WHERE `id` > 5;"); |
64 $status = $this->pdo->exec("DELETE FROM `t_user` WHERE `id` > 2;"); |
63 |
65 $status = $this->pdo->exec("ALTER TABLE `t_history` AUTO_INCREMENT = 1;"); |
64 $this->pdo->exec("ALTER TABLE `t_user` AUTO_INCREMENT = 2;"); |
66 $status = $this->pdo->exec("ALTER TABLE `t_ticket` AUTO_INCREMENT = 1;"); |
65 $this->pdo->exec("ALTER TABLE `t_group` AUTO_INCREMENT = 5;"); |
67 $status = $this->pdo->exec("ALTER TABLE `t_user` AUTO_INCREMENT = 2;"); |
|
68 } catch (PDOException $pdoe) { |
66 } catch (PDOException $pdoe) { |
69 return false; |
67 return false; |
70 } |
68 } |
71 |
69 |
72 return true; |
70 return true; |
96 } |
94 } |
97 |
95 |
98 fclose($handle); |
96 fclose($handle); |
99 } |
97 } |
100 |
98 |
99 /** |
|
100 * @param $table |
|
101 * @param $cond |
|
102 * @return array|null |
|
103 */ |
|
101 public function fetchAll($table, $cond) { |
104 public function fetchAll($table, $cond) { |
105 $this->logger->info(sprintf('%s(%s, %s) ', __METHOD__, $table, $cond)); |
|
106 |
|
102 return $this->fetch($table, $cond); |
107 return $this->fetch($table, $cond); |
103 } |
108 } |
104 |
109 |
105 /** |
110 /** |
106 * @param $table |
111 * @param $table |
107 * @param $cond |
112 * @param $cond |
108 * @return array|null |
113 * @return array|null |
109 */ |
114 */ |
110 public function fetch($table, $cond) { |
115 public function fetch($table, $cond) { |
116 $this->logger->info(sprintf('%s(%s, %s) ', __METHOD__, $table, $cond)); |
|
117 |
|
111 $stmt = $this->pdo->prepare(sprintf(' |
118 $stmt = $this->pdo->prepare(sprintf(' |
112 SELECT |
119 SELECT |
113 * |
120 * |
114 FROM |
121 FROM |
115 `%s` |
122 `%s` |
156 } |
163 } |
157 |
164 |
158 fclose($handle); |
165 fclose($handle); |
159 } |
166 } |
160 |
167 |
168 /** |
|
169 * @param $table |
|
170 * @param $id |
|
171 * @return mixed|null |
|
172 */ |
|
161 public function find($table, $id) { |
173 public function find($table, $id) { |
174 $this->logger->info(sprintf('%s(%s, %d) ', __METHOD__, $table, $id)); |
|
175 |
|
162 $stmt = $this->pdo->prepare( |
176 $stmt = $this->pdo->prepare( |
163 sprintf(' |
177 sprintf(' |
164 SELECT |
178 SELECT |
165 * |
179 * |
166 FROM |
180 FROM |
178 } |
192 } |
179 |
193 |
180 return null; |
194 return null; |
181 } |
195 } |
182 |
196 |
197 /** |
|
198 * @param $table |
|
199 * @param $sys |
|
200 * @return array|null |
|
201 */ |
|
183 public function findAll($table, $sys) { |
202 public function findAll($table, $sys) { |
203 $this->logger->info(sprintf('%s(%s, %d) ', __METHOD__, $table, $sys)); |
|
204 |
|
184 $stmt = $this->pdo->prepare( |
205 $stmt = $this->pdo->prepare( |
185 sprintf(' |
206 sprintf(' |
186 SELECT |
207 SELECT |
187 * |
208 * |
188 FROM |
209 FROM |
200 } |
221 } |
201 |
222 |
202 return null; |
223 return null; |
203 } |
224 } |
204 |
225 |
226 /** |
|
227 * @param $table |
|
228 * @param $field |
|
229 * @param $value |
|
230 * @return mixed|null |
|
231 */ |
|
205 public function findByField($table, $field, $value) { |
232 public function findByField($table, $field, $value) { |
233 $this->logger->info(sprintf('%s(%s, %s, %s) ', __METHOD__, $table, $field, $value)); |
|
234 |
|
206 $stmt = $this->pdo->prepare( |
235 $stmt = $this->pdo->prepare( |
207 sprintf(' |
236 sprintf(' |
208 SELECT |
237 SELECT |
209 * |
238 * |
210 FROM |
239 FROM |
221 } |
250 } |
222 |
251 |
223 return null; |
252 return null; |
224 } |
253 } |
225 |
254 |
255 /** |
|
256 * @param $table |
|
257 * @param $field |
|
258 * @param $value |
|
259 * @return array|null |
|
260 */ |
|
226 function findAllByField($table, $field, $value) { |
261 function findAllByField($table, $field, $value) { |
262 $this->logger->info(sprintf('%s(%s, %s, %s) ', __METHOD__, $table, $field, $value)); |
|
263 |
|
227 $stmt = $this->pdo->prepare( |
264 $stmt = $this->pdo->prepare( |
228 sprintf(' |
265 sprintf(' |
229 SELECT |
266 SELECT |
230 * |
267 * |
231 FROM |
268 FROM |
242 } |
279 } |
243 |
280 |
244 return null; |
281 return null; |
245 } |
282 } |
246 |
283 |
284 /** |
|
285 * @param $sql |
|
286 * @return array|null |
|
287 */ |
|
247 public function query($sql) { |
288 public function query($sql) { |
289 $this->logger->info(sprintf('%s(%s, %s) ', __METHOD__, $sql)); |
|
290 |
|
248 $stmt = $this->pdo->query($sql); |
291 $stmt = $this->pdo->query($sql); |
249 |
292 |
250 if ($stmt) { |
293 if ($stmt) { |
251 return $stmt->fetchAll(PDO::FETCH_ASSOC); |
294 return $stmt->fetchAll(PDO::FETCH_ASSOC); |
252 } |
295 } |
253 |
296 |
254 return null; |
297 return null; |
255 } |
298 } |
256 |
299 |
300 /** |
|
301 * @param $table |
|
302 * @param $array |
|
303 * @return int |
|
304 */ |
|
257 public function persist($table, $array) { |
305 public function persist($table, $array) { |
258 $this->logger->info(sprintf('%s(%s, %s) ', __METHOD__, $table, print_r($array, true))); |
306 $this->logger->info(sprintf('%s(%s, %s) ', __METHOD__, $table, print_r($array, true))); |
259 |
307 |
260 $keys = array(); |
308 $keys = array(); |
261 foreach (array_keys($array) as $key) { |
309 foreach (array_keys($array) as $key) { |
281 ", $table, $fieldList, $fields); |
329 ", $table, $fieldList, $fields); |
282 |
330 |
283 return $this->pdo->exec($sql); |
331 return $this->pdo->exec($sql); |
284 } |
332 } |
285 |
333 |
334 /** |
|
335 * @param $table |
|
336 * @param $id |
|
337 * @param $array |
|
338 * @return int |
|
339 */ |
|
286 public function store($table, $id, $array) { |
340 public function store($table, $id, $array) { |
287 $this->logger->info(sprintf('%s(%s, %d, %s) ', __METHOD__, $table, $id, print_r($array, true))); |
341 $this->logger->info(sprintf('%s(%s, %d, %s) ', __METHOD__, $table, $id, print_r($array, true))); |
288 |
342 |
289 $list = array(); |
343 $list = array(); |
290 foreach ($array as $key => $value) { |
344 foreach ($array as $key => $value) { |
302 ", $table, $listItems, $id); |
356 ", $table, $listItems, $id); |
303 |
357 |
304 return $this->pdo->exec($sql); |
358 return $this->pdo->exec($sql); |
305 } |
359 } |
306 |
360 |
361 /** |
|
362 * @param $table |
|
363 * @param $id |
|
364 * @return int |
|
365 */ |
|
307 public function delete($table, $id) { |
366 public function delete($table, $id) { |
308 $this->logger->info(sprintf('%s(%s, %s) ', __METHOD__, $table, $id)); |
367 $this->logger->info(sprintf('%s(%s, %s) ', __METHOD__, $table, $id)); |
309 |
368 |
310 $sql = sprintf(" |
369 $sql = sprintf(" |
311 DELETE FROM `%s` |
370 DELETE FROM `%s` |