1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @file |
5
|
|
|
* Class SqlDBStore |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace Roomify\Bat\Store; |
9
|
|
|
|
10
|
|
|
use Roomify\Bat\Event\Event; |
11
|
|
|
use Roomify\Bat\Store\SqlDBStore; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* This is a generic Sql implementation of the Store. |
15
|
|
|
* |
16
|
|
|
*/ |
17
|
|
|
abstract class SqlDBStore extends Store { |
18
|
|
|
|
19
|
|
|
// There are two types of stores - for event ids and status |
20
|
|
|
const BAT_EVENT = 'event'; |
21
|
|
|
const BAT_STATE = 'state'; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* The table that holds day data. |
25
|
|
|
* @var |
26
|
|
|
*/ |
27
|
|
|
public $day_table; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* The table that holds hour data. |
31
|
|
|
* @var |
32
|
|
|
*/ |
33
|
|
|
public $hour_table; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* The table that holds minute data. |
37
|
|
|
* @var |
38
|
|
|
*/ |
39
|
|
|
public $minute_table; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* The event type we are dealing with. |
43
|
|
|
* @var |
44
|
|
|
*/ |
45
|
|
|
public $event_type; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* SqlDBStore constructor. |
49
|
|
|
* |
50
|
|
|
* Provided with the event type it will determine the appropriate table names to |
51
|
|
|
* store data in. This assumes standard behaviour from Bat_Event |
52
|
|
|
* @param $event_type |
53
|
|
|
* @param string $event_data |
54
|
|
|
*/ |
55
|
|
|
public function __construct($event_type, $event_data = 'state') { |
56
|
|
|
|
57
|
|
|
$this->event_type = $event_type; |
58
|
|
|
|
59
|
|
View Code Duplication |
if ($event_data == SqlDBStore::BAT_STATE) { |
|
|
|
|
60
|
|
|
$this->day_table = 'bat_event_' . $event_type . '_day_' . SqlDBStore::BAT_STATE; |
61
|
|
|
$this->hour_table = 'bat_event_' . $event_type . '_hour_' . SqlDBStore::BAT_STATE; |
62
|
|
|
$this->minute_table = 'bat_event_' . $event_type . '_minute_' . SqlDBStore::BAT_STATE; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
View Code Duplication |
if ($event_data == SqlDBStore::BAT_EVENT) { |
|
|
|
|
66
|
|
|
$this->day_table = 'bat_event_' . $event_type . '_day_' . SqlDBStore::BAT_EVENT; |
67
|
|
|
$this->hour_table = 'bat_event_' . $event_type . '_hour_' . SqlDBStore::BAT_EVENT; |
68
|
|
|
$this->minute_table = 'bat_event_' . $event_type . '_minute_' . SqlDBStore::BAT_EVENT; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @param \DateTime $start_date |
75
|
|
|
* @param \DateTime $end_date |
76
|
|
|
* @param $unit_ids |
77
|
|
|
* |
78
|
|
|
* @return array |
79
|
|
|
*/ |
80
|
|
|
public function buildQueries(\DateTime $start_date, \DateTime $end_date, $unit_ids) { |
81
|
|
|
$queries = array(); |
82
|
|
|
|
83
|
|
|
$queries[Event::BAT_DAY] = 'SELECT * FROM ' . $this->day_table . ' WHERE '; |
84
|
|
|
$queries[Event::BAT_HOUR] = 'SELECT * FROM ' . $this->hour_table . ' WHERE '; |
85
|
|
|
$queries[Event::BAT_MINUTE] = 'SELECT * FROM ' . $this->minute_table . ' WHERE '; |
86
|
|
|
|
87
|
|
|
$hours_query = TRUE; |
|
|
|
|
88
|
|
|
$minutes_query = TRUE; |
|
|
|
|
89
|
|
|
|
90
|
|
|
// Create a mock event which we will use to determine how to query the database |
91
|
|
|
$mock_event = new Event($start_date, $end_date, 0, -10); |
92
|
|
|
// We don't need a granular event even if we are retrieving granular data - since we don't |
93
|
|
|
// know what the event break-down is going to be we need to get the full range of data from |
94
|
|
|
// days, hours and minutes. |
95
|
|
|
$itemized = $mock_event->itemizeEvent(Event::BAT_DAILY); |
96
|
|
|
|
97
|
|
|
$year_count = 0; |
98
|
|
|
$hour_count = 0; |
|
|
|
|
99
|
|
|
$minute_count = 0; |
|
|
|
|
100
|
|
|
|
101
|
|
|
$query_parameters = ''; |
102
|
|
|
|
103
|
|
|
foreach($itemized[Event::BAT_DAY] as $year => $months) { |
104
|
|
|
if ($year_count > 0) { |
105
|
|
|
// We are dealing with multiple years so add an OR |
106
|
|
|
$query_parameters .= ' OR '; |
107
|
|
|
} |
108
|
|
|
$query_parameters .= 'year IN (' . $year . ') '; |
109
|
|
|
$query_parameters .= 'AND month IN (' . implode("," ,array_keys($months)) .') '; |
110
|
|
|
if (count($unit_ids) > 0) { |
111
|
|
|
// Unit ids are defined so add this as a filter |
112
|
|
|
$query_parameters .= 'AND unit_id in (' . implode("," , $unit_ids) .') '; |
113
|
|
|
} |
114
|
|
|
$year_count++; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
// Add parameters to each query |
118
|
|
|
$queries[Event::BAT_DAY] .= $query_parameters; |
119
|
|
|
$queries[Event::BAT_HOUR] .= $query_parameters; |
120
|
|
|
$queries[Event::BAT_MINUTE] .= $query_parameters; |
121
|
|
|
|
122
|
|
|
// Clean up and add ordering information |
123
|
|
|
$queries[Event::BAT_DAY] .= ' ORDER BY unit_id, year, month'; |
124
|
|
|
$queries[Event::BAT_HOUR] .= ' ORDER BY unit_id, year, month, day'; |
125
|
|
|
$queries[Event::BAT_MINUTE] .= ' ORDER BY unit_id, year, month, day, hour'; |
126
|
|
|
|
127
|
|
|
return $queries; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
} |
131
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.