3
class DBLogGroup extends LOGGROUP {
6
function __construct(array &$info, DBReader &$rdr, $flags = 0) {
7
parent::__construct($info);
9
if (is_array($rdr->groups)) {
10
foreach($rdr->groups as $re => &$table) {
11
if (preg_match($re, $this->gid)) {
12
$this->table = preg_replace($re, $table, $this->gid);
17
$this->table = &$this->gid;
21
class DBReader extends READER {
31
function __construct(&$props) {
32
parent::__construct($props);
33
$this->db = new DATABASE($this->server);
35
$this->groups = $this->opts->Get('groups');
36
if ($this->groups) $this->group_class = "DBLogGroup";
38
$this->tables = $this->opts->Get('tables');
39
$this->columns = $this->opts->Get('columns');
40
$this->data_request = false;
42
$time_format = $this->opts->Get("timeformat");
43
if ($time_format) $this->time_format = $time_format;
44
else $this->time_format = $this->db->GetTimeFormat();
46
$time_zone = $this->opts->Get("timezone");
47
if ($time_zone) $this->time_zone = new DateTimeZone($time_zone);
49
$time_sort = $this->opts->Get("timesort");
51
if (is_string($time_sort)) $this->time_sort = "ORDER BY " . $time_sort;
52
else if ($time_sort>0) $this->time_sort = "ORDER BY " . $this->columns['time'] . " ASC";
53
else $this->time_sort = "ORDER BY " . $this->columns['time'] . " DESC";
54
} else $this->time_sort = "";
57
function GetGroupInfo(LOGGROUP &$grp = NULL, $flags = 0) {
60
$res = $this->db->ShowTables();
61
foreach ($res as $row) {
64
if (is_array($this->tables)) {
66
foreach ($this->tables as $re => &$info) {
67
if (preg_match($re, $gid)) {
68
if (is_array($info)) {
69
if (isset($info['title'])) {
70
$name = preg_replace($re, $info['title'], $gid);
71
$gid = preg_replace($re, $info['gid'], $gid);
73
$gid = preg_replace($re, $info['gid'], $gid);
77
$gid = preg_replace($re, $info, $gid);
84
if (!$found) continue;
85
if (($grp)&&($grp->gid != $gid)) continue;
87
if (($grp)&&($grp->gid != $gid)) continue;
88
if (!preg_match($this->tables, $gid)) continue;
93
$groups[$gid]['gid'] = $gid;
94
$groups[$gid]['name'] = $name;
96
if ($flags&READER::NEED_INFO) {
97
if ($grp) $grzeus = &$grp;
99
$ginfo = array("db_group" => $gid);
100
$grzeus = $this->CreateGroup($ginfo);
103
$tc = $this->columns['time'];
104
$req = "MIN($tc), MAX($tc)";
105
if ($flags&READER::NEED_COUNT)
106
$req .= ", COUNT($tc)";
108
$valres = $this->db->Query("SELECT $req FROM " . $grzeus->table);
109
$vals = $valres->fetch(PDO::FETCH_NUM);
112
$groups[$gid]['first'] = $this->ExportUnixTime($vals[0]);
113
if (($this->start_date)&&($this->start_date > $groups[$gid]['first'])) {
114
$groups[$gid]['first'] = $this->start_date;
117
$groups[$gid]['last'] = $this->ExportUnixTime($vals[1]);
118
if (($this->end_date)&&($this->end_date < $groups[$gid]['last'])) {
119
$groups[$gid]['last'] = $this->end_date;
122
if ($flags&READER::NEED_COUNT)
123
$groups[$gid]['records'] = $vals[2];
126
if ($flags&READER::NEED_ITEMINFO) {
127
$groups[$gid]['items'] = $this->GetItemList($grzeus);
132
return $grp?$groups[$grp->gid]:$groups;
135
function GetItemList(LOGGROUP &$grp = NULL, MASK &$mask = NULL, $flags = 0) {
136
$grp = $this->CheckGroup($grp);
137
if (!$mask) $mask = $this->CreateMask($grp);
141
$resp = $this->db->ShowColumns($grp->table);
144
foreach ($resp as $row) {
146
if (!preg_match($this->columns['data'], $name)) continue;
148
if (!$mask->Check($pos++)) continue;
150
$items[$rpos] = array(
159
throw new ADEIException(translate("DBReader can't find any column in table (%s)", $grp->table));
162
throw new ADEIException(translate("DBReader is not able to find any column matching filter (%s) in table (%s)", $this->columns['data'], $grp->table));
167
function FindColumns(&$grp) {
168
if (!$this->data_request[$grp->gid]) {
169
if ($this->time_module)
170
$time_column = $this->columns['time'];
172
$time_column = $this->db->GetTimeRequest($this->columns['time']);
176
$items = $this->GetItemList($grp);
177
foreach ($items as $item) {
178
$data_columns .= "\"" . $item['column'] . "\", ";
181
$this->data_request[$grp->gid] = "$data_columns $time_column";
186
function GetData(LOGGROUP &$grp = NULL, $from = 0, $to = 0) {
187
$grp = $this->CheckGroup($grp);
188
if ((!$from)||(!$to)) {
189
$ivl = $this->CreateInterval($grp);
190
$ivl->Limit($from, $to);
192
$from = $ivl->GetWindowStart();
193
$to = $ivl->GetWindowEnd();
196
$this->FindColumns($grp);
198
// echo("SELECT " . $this->data_request[$grp->gid] . " FROM " . $grp->table . " WHERE \"" . $this->columns['time'] . "\" BETWEEN '" . $this->ImportUnixTime($from) . "' and '" . $this->ImportUnixTime($to) . '\'' . "\n\n");
199
$stmt = $this->db->Prepare("SELECT " . $this->data_request[$grp->gid] . " FROM " . $grp->table . " WHERE \"" . $this->columns['time'] . "\" BETWEEN '" . $this->ImportUnixTime($from) . "' and '" . $this->ImportUnixTime($to) . '\' ' . $this->time_sort);
200
return new DATABASEData($this, $stmt);
203
function HaveData(LOGGROUP &$grp = NULL, $from = 0, $to = 0) {
204
$grp = $this->CheckGroup($grp);
205
if (!$to) $to = time();
207
$res = $this->db->Query($this->db->SelectRequest($grp->table, array($this->columns['time']), array(
208
"condition" => "\"" . $this->columns['time'] . "\" BETWEEN '" . $this->ImportUnixTime($from) . "' and '" . $this->ImportUnixTime($to) . '\'',
210
if ($res->fetch(PDO::FETCH_NUM)) return true;
b'\\ No newline at end of file'