Jacek Kowalski
2019-06-30 d0b22a371bdd4e8f09df8ba63c6206208c244c86
commit | author | age
1a98b6 1 <?php
JK 2 class Database {
3     private $pdo;
4     private $addStatement;
7b7026 5     
JK 6     private $cacheId;
7     private $cacheNum;
1a98b6 8     
JK 9     public function __construct($file) {
10         $this->pdo = new PDO('sqlite:'.$file);
11         $this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
12         
13         $this->pdo->query('CREATE TABLE IF NOT EXISTS vehicles (
14             id INT PRIMARY KEY,
15             num INT UNIQUE,
16             weight INT
17         )');
18         
7b7026 19         $this->addStatement = $this->pdo->prepare('INSERT OR REPLACE INTO vehicles (id, num, weight) VALUES (:id, :num, :weight)');
JK 20         
21         $this->_cacheClear();
1a98b6 22     }
JK 23     
64994e 24     public function beginTransaction() {
JK 25         $this->pdo->beginTransaction();
26     }
27     
28     public function commit() {
29         $this->pdo->commit();
30     }
31     
32     public function rollback() {
33         $this->pdo->rollback();
34     }
35     
7b7026 36     protected function _cachePopulate() {
JK 37         if($this->cacheId === NULL) {
38             $st = $this->pdo->prepare('SELECT * FROM vehicles');
39             $st->execute();
40             $result = $st->fetchAll(PDO::FETCH_ASSOC);
41             
42             $this->cacheId = [];
43             $this->cacheNum = [];
44             foreach($result as $vehicle) {
45                 $this->_cacheAdd($vehicle);
46             }
47         }
48     }
49     
50     protected function _cacheAdd($vehicle) {
51         $this->_cachePopulate();
52         $this->cacheId[$vehicle['id']] = $vehicle;
53         $this->cacheNum[$vehicle['num']] = $vehicle;
54     }
55     
56     protected function _cacheClear() {
57         $this->cacheId = NULL;
58         $this->cacheNum = NULL;
59     }
60     
61     public function getAll() {
62         $this->_cachePopulate();
63         return $this->cacheId;
64     }
65     
1a98b6 66     public function getById($id) {
7b7026 67         $this->_cachePopulate();
JK 68         return $this->cacheId[$id] ?? NULL;
1a98b6 69     }
JK 70     
71     public function getByNum($num) {
7b7026 72         $this->_cachePopulate();
JK 73         return $this->cacheNum[$num] ?? NULL;
1a98b6 74     }
JK 75     
76     public function clear() {
77         $this->pdo->query('DELETE FROM vehicles');
7b7026 78         $this->_cacheClear();
1a98b6 79     }
JK 80     
81     public function add($id, $num, $weight) {
7b7026 82         $vehicle = [
JK 83             'id' => (string)$id,
84             'num' => (string)$num,
85             'weight' => (string)$weight
86         ];
87         $this->addStatement->execute($vehicle);
88         $this->_cacheAdd($vehicle);
1a98b6 89     }
JK 90     
91     public function addMapping($mapping) {
64994e 92         $this->beginTransaction();
1a98b6 93         $weight = count($mapping);
7b7026 94         foreach($mapping as $id => $num) {
JK 95             $this->add($id, $num, $weight);
1a98b6 96         }
64994e 97         $this->commit();
1a98b6 98     }
JK 99 }