sql nosql filt

master
vitalif 2011-10-16 16:49:44 +00:00
parent cf81ad42be
commit ab17362c49
1 changed files with 42 additions and 26 deletions

View File

@ -68,9 +68,9 @@ class OLAP
var $where, $where_nosql, $group_fields, $cell;
var $is_html_format = true;
var $add_spanned;
var $decimal = '%.3f';
var $graph_scale = 600; /* пикселей */
var $graph_logarithmic = false;
static $decimal = array(3, '.', '');
function __construct($request)
{
@ -164,25 +164,27 @@ class OLAP
if (!empty($fd['is_time']))
$v = Template::timestamp($v, $this->current_src['fielddescs'][$f]['format']);
$this->where_nosql[] = array($f, $t, $v);
$dn = !empty($fd['sql']) ? $fd['sql'] : $f;
$ve = mysql_ecranize($v);
if ($t == 'eq')
if ($fd['options'])
foreach ($fd['options'] as &$o)
if ("$o[id]" === $v)
$o['selected'] = true;
// NoSQL-поля будет фильтровать sql_filter-функция
if (empty($fd['nosql']))
{
if ($fd['sql_eq'])
$this->where[] = str_replace('$', $ve, $fd['sql_eq']);
else
$this->where[] = "$dn = $ve";
if ($fd['options'])
foreach ($fd['options'] as &$o)
if ("$o[id]" === $v)
$o['selected'] = true;
$dn = !empty($fd['sql']) ? $fd['sql'] : $f;
$ve = mysql_ecranize($v);
if ($t == 'eq')
{
if ($fd['sql_eq'])
$this->where[] = str_replace('$', $ve, $fd['sql_eq']);
else
$this->where[] = "$dn = $ve";
}
elseif ($t == 'ge' && $fd['le_ge'])
$this->where[] = "$dn >= $ve";
elseif ($t == 'le' && $fd['le_ge'])
$this->where[] = "$dn <= $ve";
}
elseif ($t == 'ge' && $fd['le_ge'])
$this->where[] = "$dn >= $ve";
elseif ($t == 'le' && $fd['le_ge'])
$this->where[] = "$dn <= $ve";
else
unset($this->request[$k]);
}
// описание группировки
elseif (substr($k, 0, 11) == 'group-type-')
@ -385,17 +387,32 @@ class OLAP
$code .= "self::aggr_update_$aggr($asgn, ".$this->func_trans_field($field, $func).");\n";
}
if ($gen = $this->current_src['generator'])
$gen = $this->current_src['generator'];
$pipe = $this->current_src['sql_filter'];
if ($gen)
{
// Загрузка данных не из базы, а из произвольной функции
$code = 'while($r = $result->fetch()) { ' . $code . '}';
}
elseif ($pipe = $this->current_src['sql_filter'])
elseif ($pipe)
{
// Поддержка фильтрации данных через дополнительную функцию
$code = 'while($row = mysql_fetch_assoc($result)) { foreach('.
$pipe.'($row) as $r) { '.$code.'} } '.
'foreach('.$pipe.'(NULL) as $r) { '.$code.'}';
$call = $pipe;
if (is_array($pipe))
{
if (is_string($pipe[0]) && class_exists($pipe[0]))
$call = $pipe[0].'::'.$pipe[1];
elseif (is_object($pipe[0]))
$call = '$pipe->'.$pipe[1];
else
die("Invalid sql_filter callback in source '".$this->current_src['in']."'");
}
// Поддержка фильтрации данных через callback
// Важно, что функция потоковая, чтобы память не хавала
// В начале и конце дёргается с NULL'ом вместо строки:
// В начале типа "конструктора", в конце типа "выплюнуть запомненное"
$code = $call.'(NULL, $this); while($row = mysql_fetch_assoc($result)) { foreach('.
$call.'($row, $this) as $r) { '.$code.'} } '.
'foreach('.$call.'(NULL, $this) as $r) { '.$code.'}';
}
else
$code = 'while($r = mysql_fetch_assoc($result)) { ' . $code . '}';
@ -825,8 +842,7 @@ class OLAP
return $o[$value];
elseif (preg_match('/^-?\d+\.\d+$/s', $value))
{
$value = sprintf($this->decimal, $value);
$value = preg_replace('/\.0+$|(\.\d*?)0+$/', '\1', $value);
$value = number_format($value, self::$decimal[0], self::$decimal[1], self::$decimal[2]);
return $value;
}
elseif (($proc = $this->current_src['fielddescs'][$field]['format_func']) &&