FrxSQLQueryBuilder.inc in Forena Reports 6.2
File
FrxSQLQueryBuilder.inc
View source
<?php
class FrxSQLQueryBuilder {
private $clauses = array();
private $descriptions = array();
private $applied_filters = array();
private $where_clause = '';
private $comparison_operators = array(
'<' => 'is less than',
'>' => 'is greater than',
'=' => 'is equal to',
'<>' => 'is not equal to',
'LIKE' => 'is like',
);
private $data;
public function defineFilter($field, $comparison, $sql, $title = '') {
if (!isset($this->clauses[$field])) {
$this->clauses[$field] = array();
}
$this->clauses[$field][$comparison] = $sql;
$title = $title ? $title : $field . ' is ' . $comparison;
$this->descriptions[$field][$comparison] = $title;
return $this;
}
public function defineColumnFilters($columns, $comparisons = array()) {
$columns = (array) $columns;
if ($columns) {
foreach ($columns as $column) {
if (!$comparisons) {
$comparisons = array_keys($this->comparison_operators);
}
if ($comparisons) {
foreach ($comparisons as $comparison) {
if (array_key_exists($comparison, $this->comparison_operators)) {
$clause = $column . ' ' . $comparison . ' :' . $column;
$this
->defineFilter($column, $comparison, $clause, $this->comparison_operators[$comparison]);
}
}
}
}
}
return $this;
}
public function defineNumericFilters($columns) {
$this
->defineColumnFilters($columns, array(
'<',
'>',
'=',
'<>',
));
return $this;
}
public function query($block, $values = array()) {
$this->applied_filters = array();
$this->block = $block;
$this->data = $values;
return $this;
}
public function execute() {
$where = '';
if ($this->where_clause) {
$where = 'WHERE 1=1' . $this->where_clause;
}
return forena_invoke_data_provider($this->block, $this->data, $where);
}
public function filter($field, $comparison) {
if (@$this->clauses[$field][$comparison]) {
$this->where_clause .= ' AND ' . $this->clauses[$field][$comparison];
}
return $this;
}
}
Classes
Name |
Description |
FrxSQLQueryBuilder |
FrxSQLQuery SQL Builder
This class defines a common query builder that is used to
make SQL safe queries based on named column filteres.
@author metzlerd |