-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathSqlDBStore.php
More file actions
153 lines (124 loc) · 4.96 KB
/
Copy pathSqlDBStore.php
File metadata and controls
153 lines (124 loc) · 4.96 KB
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
<?php
/**
* @file
* Class SqlDBStore
*/
namespace Roomify\Bat\Store;
use Roomify\Bat\Event\Event;
use Roomify\Bat\Event\EventItemizer;
use Roomify\Bat\Unit\Unit;
use Roomify\Bat\Store\SqlDBStore;
/**
* This is a generic Sql implementation of the Store.
*
*/
abstract class SqlDBStore extends Store {
// There are two types of stores - for event ids and status
const BAT_EVENT = 'event';
const BAT_STATE = 'state';
/**
* The table that holds day data.
* @var
*/
public $day_table;
/**
* The table that holds hour data.
* @var
*/
public $hour_table;
/**
* The table that holds minute data.
* @var
*/
public $minute_table;
/**
* The table that holds day data without prefix.
* @var
*/
public $day_table_no_prefix;
/**
* The table that holds hour data without prefix.
* @var
*/
public $hour_table_no_prefix;
/**
* The table that holds minute data without prefix.
* @var
*/
public $minute_table_no_prefix;
/**
* The event type we are dealing with.
* @var
*/
public $event_type;
/**
* SqlDBStore constructor.
*
* Provided with the event type it will determine the appropriate table names to
* store data in. This assumes standard behaviour from Bat_Event
* @param $event_type
* @param string $event_data
*/
public function __construct($event_type, $event_data = 'state', $prefix = '') {
$this->event_type = $event_type;
if ($event_data == SqlDBStore::BAT_STATE) {
$this->day_table = $prefix . 'bat_event_' . $event_type . '_day_' . SqlDBStore::BAT_STATE;
$this->hour_table = $prefix . 'bat_event_' . $event_type . '_hour_' . SqlDBStore::BAT_STATE;
$this->minute_table = $prefix . 'bat_event_' . $event_type . '_minute_' . SqlDBStore::BAT_STATE;
$this->day_table_no_prefix = 'bat_event_' . $event_type . '_day_' . SqlDBStore::BAT_STATE;
$this->hour_table_no_prefix = 'bat_event_' . $event_type . '_hour_' . SqlDBStore::BAT_STATE;
$this->minute_table_no_prefix = 'bat_event_' . $event_type . '_minute_' . SqlDBStore::BAT_STATE;
}
if ($event_data == SqlDBStore::BAT_EVENT) {
$this->day_table = $prefix . 'bat_event_' . $event_type . '_day_' . SqlDBStore::BAT_EVENT;
$this->hour_table = $prefix . 'bat_event_' . $event_type . '_hour_' . SqlDBStore::BAT_EVENT;
$this->minute_table = $prefix . 'bat_event_' . $event_type . '_minute_' . SqlDBStore::BAT_EVENT;
$this->day_table_no_prefix = 'bat_event_' . $event_type . '_day_' . SqlDBStore::BAT_EVENT;
$this->hour_table_no_prefix = 'bat_event_' . $event_type . '_hour_' . SqlDBStore::BAT_EVENT;
$this->minute_table_no_prefix = 'bat_event_' . $event_type . '_minute_' . SqlDBStore::BAT_EVENT;
}
}
/**
* @param \DateTime $start_date
* @param \DateTime $end_date
* @param $unit_ids
*
* @return array
*/
public function buildQueries(\DateTime $start_date, \DateTime $end_date, $unit_ids) {
$queries = array();
$queries[Event::BAT_DAY] = 'SELECT * FROM ' . $this->day_table;
$queries[Event::BAT_HOUR] = 'SELECT * FROM ' . $this->hour_table;
$queries[Event::BAT_MINUTE] = 'SELECT * FROM ' . $this->minute_table;
// Create a mock event which we will use to determine how to query the database
$mock_event = new Event($start_date, $end_date, new Unit(0, 0, array()), -10);
// We don't need a granular event even if we are retrieving granular data - since we don't
// know what the event break-down is going to be we need to get the full range of data from
// days, hours and minutes.
$itemized = $mock_event->itemize(new EventItemizer($mock_event, Event::BAT_DAILY));
$year_count = 0;
$query_parameters = '';
foreach ($itemized[Event::BAT_DAY] as $year => $months) {
if ($year_count > 0) {
// We are dealing with multiple years so add an OR
$query_parameters .= ' OR ';
}
$query_parameters .= 'year IN (' . $year . ') ';
$query_parameters .= 'AND month IN (' . implode(",", array_keys($months)) . ') ';
if (count($unit_ids) > 0) {
// Unit ids are defined so add this as a filter
$query_parameters .= 'AND unit_id in (' . implode("," , $unit_ids) . ') ';
}
$year_count++;
}
// Add parameters to each query
$queries[Event::BAT_DAY] = !empty($query_parameters) ? $queries[Event::BAT_DAY] . ' WHERE ' . $query_parameters : $queries[Event::BAT_DAY];
$queries[Event::BAT_HOUR] = !empty($query_parameters) ? $queries[Event::BAT_HOUR] . ' WHERE ' . $query_parameters : $queries[Event::BAT_HOUR];
$queries[Event::BAT_MINUTE] = !empty($query_parameters) ? $queries[Event::BAT_MINUTE] . ' WHERE ' . $query_parameters : $queries[Event::BAT_MINUTE];
// Clean up and add ordering information
$queries[Event::BAT_DAY] .= ' ORDER BY unit_id, year, month';
$queries[Event::BAT_HOUR] .= ' ORDER BY unit_id, year, month, day';
$queries[Event::BAT_MINUTE] .= ' ORDER BY unit_id, year, month, day, hour';
return $queries;
}
}