/adei/trunk

To get this branch, use:
bzr branch http://darksoft.org/webbzr/adei/trunk
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
<?php

/* 
    $limit > 0 - first <limit> elements
    $limit < 0 - last <limit> elements
*/

class RAWPoint implements Iterator {
 var $cache;
 var $ids;
 var $ivl;
 var $limit;
 var $sampling;

 var $res;
 var $key, $row;
 
 var $use_subseconds;
 var $postfix;
    
 function __construct(CACHEDB $cache, MASK $mask, INTERVAL $ivl, $limit, $sampling = 0) {
    $this->cache = &$cache;

    $this->ids = &$mask->ids;
    if (!is_array($this->ids)) throw new ADEIException("Internal application error (MASK should be created using CACHE call)");

    $this->ivl = $ivl;
    $this->limit = $limit;

    $this->sampling = $sampling;

    $this->use_subseconds = $cache->use_subseconds;
    $this->postfix = false;
 }

 function __destruct() {
    $this->Clear();
 }

 function SetOption($option, $value) {
    $this->$option = $value;
 } 

 function SetOptions($options) {
    foreach ($options as $opt => &$value) {
	$this->$opt = $value;
    }
 }
 
 function Clear() {
    if ($this->res) {
	@mysql_free_result($this->res);
	unset($this->res);
    }
 }
 
 function rewind() {
    global $MYSQL_FORCE_INDEXES;
    
    $this->Clear();
    
    $list = "";

    foreach ($this->ids as $id) {
	$list .= "v$id AS v$id, ";
    }	

    $table = $this->cache->GetTableName(0, $this->postfix);
    $sql = $this->ivl->GetSQL($this->cache, $table, $this->limit, $this->use_subseconds, isset($this->sequence)?$this->sequence:false, $this->sampling);

    if ($MYSQL_FORCE_INDEXES) {
	if ($sql['index']) sprintf($idx_fix, "FORCE INDEX (%s)", $sql['index']);
	else $idx_fix = "FORCE INDEX (PRIMARY)";
    } else $idx_fix = "";

    $list .= $sql['list'];
    $cond = &$sql['cond'];
    $sort = &$sql['sort'];
    $limit = &$sql['limit'];
    $join = &$sql['join'];

    $this->res = mysql_unbuffered_query("SELECT $list FROM `$table` $idx_fix $join $cond $sort $limit", $this->cache->dbh);

    if (!$this->res)
	throw new ADEIException(translate("SELECT request '%s'  on CACHE table '%s' is failed. MySQL error: %s", "SELECT $list FROM `$table` $idx_fix $join $cond $sort $limit", $table, mysql_error($this->cache->dbh)));

    $this->next();
 }
 
 function current() {
    return $this->row;
 }
 
 function key() {
    return $this->key;
 }
 
 function next() {
    $this->row = mysql_fetch_row($this->res);
    if ($this->row) {
	$lastkey = sizeof($this->row) - 1;
	if ($this->use_subseconds) {
	    $ns = $this->row[$lastkey];
	    
	    $extra = (9 - strlen($ns));
	    if ($extra) $ns = str_repeat('0', $extra) . $ns;
		
	    $this->key = $this->row[$lastkey-1] . ".$ns";
	    unset($this->row[$lastkey]);
	    unset($this->row[$lastkey-1]);
	} else {
	    $this->key = $this->row[$lastkey];
	    unset($this->row[$lastkey]);
	}
    } else {
	$this->key = false;
	mysql_free_result($this->res);
    }
 }

 function valid() {
    return $this->key?true:false;
 }
}

?>