/adei/ui

To get this branch, use:
bzr branch http://darksoft.org/webbzr/adei/ui

« back to all changes in this revision

Viewing changes to classes/readers/dbreader.php

  • Committer: Suren A. Chilingaryan
  • Date: 2008-04-02 10:23:22 UTC
  • Revision ID: csa@dside.dyndns.org-20080402102322-okib92sicg2dx3o3
Initial import

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
 
 
3
class DBLogGroup extends LOGGROUP {
 
4
 var $table;
 
5
 
 
6
 function __construct(array &$info, DBReader &$rdr, $flags = 0) {
 
7
    parent::__construct($info);
 
8
 
 
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);
 
13
                return;
 
14
            }
 
15
        }
 
16
    }   
 
17
    $this->table = &$this->gid;
 
18
 }
 
19
}
 
20
 
 
21
class DBReader extends READER {
 
22
 var $db;
 
23
 
 
24
 var $groups;
 
25
 var $tables;
 
26
 var $columns;
 
27
 
 
28
 var $data_request;
 
29
 var $time_sort;
 
30
 
 
31
 function __construct(&$props) {
 
32
    parent::__construct($props);
 
33
    $this->db = new DATABASE($this->server);
 
34
    
 
35
    $this->groups = $this->opts->Get('groups');
 
36
    if ($this->groups) $this->group_class = "DBLogGroup";
 
37
    
 
38
    $this->tables = $this->opts->Get('tables');
 
39
    $this->columns = $this->opts->Get('columns');
 
40
    $this->data_request = false;
 
41
 
 
42
    $time_format = $this->opts->Get("timeformat");
 
43
    if ($time_format) $this->time_format = $time_format;
 
44
    else $this->time_format = $this->db->GetTimeFormat();
 
45
 
 
46
    $time_zone = $this->opts->Get("timezone");
 
47
    if ($time_zone) $this->time_zone = new DateTimeZone($time_zone);
 
48
    
 
49
    $time_sort = $this->opts->Get("timesort");
 
50
    if ($time_sort) {
 
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 = "";
 
55
 }
 
56
 
 
57
 function GetGroupInfo(LOGGROUP &$grp = NULL, $flags = 0) {
 
58
    $groups = array();
 
59
 
 
60
    $res = $this->db->ShowTables();
 
61
    foreach ($res as $row) {
 
62
        $gid = $row[0];
 
63
 
 
64
        if (is_array($this->tables)) {
 
65
            $found = false;
 
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);
 
72
                        } else {
 
73
                            $gid = preg_replace($re, $info['gid'], $gid);
 
74
                            $name = $gid;
 
75
                        }
 
76
                    } else {
 
77
                        $gid = preg_replace($re, $info, $gid);
 
78
                        $name = $gid;
 
79
                    }
 
80
                    $found = true;
 
81
                    break;
 
82
                }
 
83
            }
 
84
            if (!$found) continue;
 
85
            if (($grp)&&($grp->gid != $gid)) continue;
 
86
        } else {
 
87
            if (($grp)&&($grp->gid != $gid)) continue;
 
88
            if (!preg_match($this->tables, $gid)) continue;
 
89
            $name = $gid;
 
90
        }
 
91
 
 
92
        $groups[$gid] = $row;
 
93
        $groups[$gid]['gid'] = $gid;
 
94
        $groups[$gid]['name'] = $name;
 
95
 
 
96
        if ($flags&READER::NEED_INFO) {
 
97
            if ($grp) $grzeus = &$grp;
 
98
            else {
 
99
                $ginfo = array("db_group" => $gid);
 
100
                $grzeus = $this->CreateGroup($ginfo);
 
101
            }
 
102
            
 
103
            $tc = $this->columns['time'];
 
104
            $req = "MIN($tc), MAX($tc)";
 
105
            if ($flags&READER::NEED_COUNT) 
 
106
                $req .= ", COUNT($tc)";
 
107
 
 
108
            $valres = $this->db->Query("SELECT $req FROM " . $grzeus->table);
 
109
            $vals = $valres->fetch(PDO::FETCH_NUM);
 
110
            $valres = NULL;
 
111
 
 
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;
 
115
            }
 
116
            
 
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;
 
120
            }
 
121
 
 
122
            if ($flags&READER::NEED_COUNT) 
 
123
                $groups[$gid]['records'] = $vals[2];
 
124
 
 
125
 
 
126
            if ($flags&READER::NEED_ITEMINFO) {
 
127
                $groups[$gid]['items'] = $this->GetItemList($grzeus);
 
128
            }
 
129
        }
 
130
    }
 
131
    
 
132
    return $grp?$groups[$grp->gid]:$groups;
 
133
 }
 
134
 
 
135
 function GetItemList(LOGGROUP &$grp = NULL, MASK &$mask = NULL, $flags = 0) {
 
136
    $grp = $this->CheckGroup($grp);
 
137
    if (!$mask) $mask = $this->CreateMask($grp);
 
138
 
 
139
    $items = array();
 
140
 
 
141
    $resp = $this->db->ShowColumns($grp->table);
 
142
 
 
143
    $pos = 0; $rpos = 0;
 
144
    foreach ($resp as $row) {
 
145
        $name = $row[0];
 
146
        if (!preg_match($this->columns['data'], $name)) continue;
 
147
 
 
148
        if (!$mask->Check($pos++)) continue;
 
149
    
 
150
        $items[$rpos] = array(
 
151
            "id" => $pos - 1,
 
152
            "name" =>  $name,
 
153
            "column" => $name
 
154
        );
 
155
        $rpos++;
 
156
    }
 
157
    
 
158
    if (!$name)
 
159
        throw new ADEIException(translate("DBReader can't find any column in table (%s)", $grp->table));
 
160
 
 
161
    if (!$pos)
 
162
        throw new ADEIException(translate("DBReader is not able to find any column matching filter (%s) in table (%s)", $this->columns['data'], $grp->table));
 
163
    
 
164
    return $items;
 
165
 }
 
166
 
 
167
 function FindColumns(&$grp) {
 
168
    if (!$this->data_request[$grp->gid]) {
 
169
        if ($this->time_module)
 
170
            $time_column = $this->columns['time'];
 
171
        else
 
172
            $time_column = $this->db->GetTimeRequest($this->columns['time']);
 
173
            
 
174
        $data_columns = "";
 
175
 
 
176
        $items = $this->GetItemList($grp);
 
177
        foreach ($items as $item) {
 
178
            $data_columns .=  "\"" . $item['column'] . "\", ";
 
179
        }
 
180
        
 
181
        $this->data_request[$grp->gid] = "$data_columns $time_column";
 
182
        
 
183
    }
 
184
 }    
 
185
 
 
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);
 
191
        
 
192
        $from = $ivl->GetWindowStart();
 
193
        $to = $ivl->GetWindowEnd();
 
194
    }
 
195
 
 
196
    $this->FindColumns($grp);    
 
197
 
 
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);
 
201
 }
 
202
 
 
203
 function HaveData(LOGGROUP &$grp = NULL, $from = 0, $to = 0) {
 
204
    $grp = $this->CheckGroup($grp);
 
205
    if (!$to) $to = time();
 
206
 
 
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) . '\'',
 
209
        "limit" => 1)));
 
210
    if ($res->fetch(PDO::FETCH_NUM)) return true;
 
211
    return false;
 
212
 }
 
213
 
 
214
}
 
215
 
 
216
?>
 
 
b'\\ No newline at end of file'