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 |
|
413654
|
13 |
$this->pdo->query('CREATE TABLE IF NOT EXISTS vehicles2 ( |
1a98b6
|
14 |
id INT PRIMARY KEY, |
JK |
15 |
num INT UNIQUE, |
413654
|
16 |
weight INT, |
JK |
17 |
line VARCHAR, |
|
18 |
date INT |
1a98b6
|
19 |
)'); |
413654
|
20 |
try { |
JK |
21 |
$this->beginTransaction(); |
|
22 |
$this->pdo->query('INSERT INTO vehicles2 SELECT id, num, weight, \'?\', \''.time().'\' FROM vehicles'); |
|
23 |
$this->commit(); |
|
24 |
$this->pdo->query('DROP TABLE vehicles'); |
|
25 |
} catch(PDOException $e) { |
|
26 |
$this->rollback(); |
|
27 |
} |
1a98b6
|
28 |
|
413654
|
29 |
$this->addStatement = $this->pdo->prepare('INSERT OR REPLACE INTO vehicles2 (id, num, weight, line, date) VALUES (:id, :num, :weight, :line, :date)'); |
7b7026
|
30 |
|
JK |
31 |
$this->_cacheClear(); |
1a98b6
|
32 |
} |
JK |
33 |
|
64994e
|
34 |
public function beginTransaction() { |
JK |
35 |
$this->pdo->beginTransaction(); |
|
36 |
} |
|
37 |
|
|
38 |
public function commit() { |
|
39 |
$this->pdo->commit(); |
|
40 |
} |
|
41 |
|
|
42 |
public function rollback() { |
|
43 |
$this->pdo->rollback(); |
|
44 |
} |
|
45 |
|
7b7026
|
46 |
protected function _cachePopulate() { |
JK |
47 |
if($this->cacheId === NULL) { |
413654
|
48 |
$st = $this->pdo->prepare('SELECT * FROM vehicles2'); |
7b7026
|
49 |
$st->execute(); |
JK |
50 |
$result = $st->fetchAll(PDO::FETCH_ASSOC); |
|
51 |
|
|
52 |
$this->cacheId = []; |
|
53 |
$this->cacheNum = []; |
|
54 |
foreach($result as $vehicle) { |
|
55 |
$this->_cacheAdd($vehicle); |
|
56 |
} |
|
57 |
} |
|
58 |
} |
|
59 |
|
|
60 |
protected function _cacheAdd($vehicle) { |
|
61 |
$this->_cachePopulate(); |
|
62 |
$this->cacheId[$vehicle['id']] = $vehicle; |
|
63 |
$this->cacheNum[$vehicle['num']] = $vehicle; |
|
64 |
} |
|
65 |
|
|
66 |
protected function _cacheClear() { |
|
67 |
$this->cacheId = NULL; |
|
68 |
$this->cacheNum = NULL; |
|
69 |
} |
|
70 |
|
413654
|
71 |
public function getAllById() { |
7b7026
|
72 |
$this->_cachePopulate(); |
JK |
73 |
return $this->cacheId; |
413654
|
74 |
} |
JK |
75 |
|
|
76 |
public function getAllByNum() { |
|
77 |
$this->_cachePopulate(); |
|
78 |
return $this->cacheNum; |
7b7026
|
79 |
} |
JK |
80 |
|
1a98b6
|
81 |
public function getById($id) { |
7b7026
|
82 |
$this->_cachePopulate(); |
JK |
83 |
return $this->cacheId[$id] ?? NULL; |
1a98b6
|
84 |
} |
JK |
85 |
|
|
86 |
public function getByNum($num) { |
7b7026
|
87 |
$this->_cachePopulate(); |
JK |
88 |
return $this->cacheNum[$num] ?? NULL; |
1a98b6
|
89 |
} |
JK |
90 |
|
|
91 |
public function clear() { |
413654
|
92 |
$this->pdo->query('DELETE FROM vehicles2'); |
7b7026
|
93 |
$this->_cacheClear(); |
1a98b6
|
94 |
} |
JK |
95 |
|
413654
|
96 |
public function add($id, $num, $weight, $line = NULL, $date = NULL) { |
7b7026
|
97 |
$vehicle = [ |
JK |
98 |
'id' => (string)$id, |
|
99 |
'num' => (string)$num, |
413654
|
100 |
'weight' => (string)$weight, |
JK |
101 |
'line' => (string)($line ?? ''), |
|
102 |
'date' => (string)($date ?? time()), |
7b7026
|
103 |
]; |
JK |
104 |
$this->addStatement->execute($vehicle); |
|
105 |
$this->_cacheAdd($vehicle); |
1a98b6
|
106 |
} |
JK |
107 |
|
413654
|
108 |
public function addMapping($vehiclesMapping, Mapper $mapper) { |
64994e
|
109 |
$this->beginTransaction(); |
413654
|
110 |
$weight = count($vehiclesMapping); |
JK |
111 |
foreach($vehiclesMapping as $id => $num) { |
|
112 |
$trip = $mapper->getTTSSVehicleTrip($id); |
|
113 |
$this->add($id, $num, $weight, ($trip['line'] ?? '?') . ' ' . ($trip['direction'] ?? '?')); |
1a98b6
|
114 |
} |
64994e
|
115 |
$this->commit(); |
1a98b6
|
116 |
} |
JK |
117 |
} |