5 use \Rsi\Fred\Controller;
7 class Db extends \Rsi\Fred\Controller\Provider{
29 return \Rsi::nothing($value) ? null : $this->db->single($this->db->limit(
" 30 select $select from {$this->table} 31 where `$key` = :this" . ($this->where ?
' 32 and (' . $this->where .
')' :
''),1),
33 array_merge($params ?: [],$this->args,[
'this' => $value])
37 public function trans($column,$key,$value,$params = null){
38 return $column == $key ? $value : $this->
single(
"`$column`",$key,$value,$params);
41 public function exists($key,$value,$params = null){
42 return (
bool)$this->
single(1,$key,$value,$params);
47 if($enclose)
foreach(
$where as &$arg) $arg =
"($arg)";
49 return '(' . implode(
' or ',
$where) .
')';
61 $strict = $loose = [];
63 foreach(
$search as $column => $values){
65 foreach($values as $type => $ref){
67 if(array_key_exists($column,$this->
search)){
69 $args[$key_count =
'c' . count(
$args)] = count($ref);
70 $where[] = strtr($this->
search[$column],[
':ref' =>
':' . $key,
':count' =>
':' . $key_count]);
73 case self::SEARCH_FROM:
76 if(!$operator) $operator =
'<=';
77 case self::SEARCH_EXACT:
78 if($ref === null)
$where[] =
"`$column` is null";
80 if(!$operator) $operator =
'=';
82 $where[] =
"`$column` " . (is_array($ref) ?
"in (:$key)" :
"$operator :$key");
85 case self::SEARCH_DATE:
87 foreach((array)$ref as $date)
if($date = \
Rsi\
Fred\DateTime\Date::create($date)){
88 $args[$key_from =
'd' . count(
$args)] = (string)$date;
89 $args[$key_to =
'd' . count(
$args)] = (string)$date->add();
90 $dates[] =
"`$column` >= :$key_from and `$column` < :$key_to";
95 $likes = $loose_likes = [];
96 foreach((array)$ref as $like)
if($like){
97 if(preg_match(
'/^\\/.+\\/$/',$like))
try{
99 $args[$key =
'r' . count(
$args)] = substr($like,1,-1);
100 $likes[] =
"`$column` regexp :$key";
103 $this->_controller->component(
'log')->debug(
"Invalid reg-ex '$like': " . str_replace(
'preg_match(): Compilation failed: ',
'',$e->getMessage()),__FILE__,__LINE__);
106 elseif(substr($like = strtr($like,[
'?' =>
'_',
'*' =>
'%']),0,1) ==
'-'){
107 $args[$key =
'x' . count(
$args)] =
'%' . substr($like,1) .
'%';
108 $likes[] =
"(`$column` is null or `$column` not like :$key)";
111 $args[$key =
'l' . count(
$args)] = $like .
'%';
112 $likes[] =
"`$column` like :$key";
113 if(substr($like,0,1) !=
'%'){
114 $args[$key =
'l' . count(
$args)] =
'%' . $like .
'%';
115 $loose_likes[] =
"`$column` like :$key";
121 if($loose_likes) $loose[$column] = $this->
implodeMulti($loose_likes);
128 $where =
'where ' . ($this->
where ?
'(' . $this->
where .
')' . ($strict ?
"\nand " :
'') :
'');
129 $all = $this->
where || $strict ?
$where . implode(
' and ',$loose + $strict) : null;
130 $loose = $loose ?
$where .
'not (' . implode(
' and ',$strict) .
")\nand " . implode(
' and ',array_merge($strict,$loose)) : null;
131 $strict = $this->
where || $strict ?
$where . implode(
' and ',$strict) : null;
140 foreach(
$order + $this->
order as $column => $type){
141 $column =
"`$column`";
142 if($type & SORT_DESC) $column .=
' desc';
143 $columns[] = $column;
145 return $columns ?
'order by ' . implode(
',',$columns) : null;
149 $args = array_merge($params ?: [],$this->args);
152 session_write_close();
153 $select =
"select {$this->select}\nfrom {$this->table}";
154 $sql =
"$select\n$strict\n$order";
155 if($limit) $sql = $this->db->limit($sql,$limit,$offset);
156 $records = $this->db->all($sql,
$args);
157 if($loose && (!$limit || (($count = count($records)) < $limit))){
158 $sql =
"$select\n$loose\n$order";
164 elseif($offset) $offset -= $this->db->single(
"select count(*) from {$this->table}\n$strict",
$args);
165 $sql = $this->db->limit($sql,$limit,$offset);
167 $records = array_merge($records,$this->db->all($sql,
$args));
174 if(!$totals)
return [];
175 $args = array_merge($params ?: [],$this->args);
177 session_write_close();
179 foreach($totals as $column => &$types)
foreach(($types = (array)$types) as $type){
180 $alias = $column . self::COLUMN_TYPE_GLUE . $type;
186 default: $columns[] =
"$type(`$column`) as `$alias`";
190 $select =
'select ' . implode(
',',$columns) .
"\nfrom {$this->table}\n";
191 $alias_totals = $this->db->single(
$select . $all,
$args,
false);
194 foreach($totals as $column => $types){
195 $result[$column] = [];
196 foreach($types as $type) $result[$column][$type] =
203 $args = array_merge($params ?: [],$this->args);
207 default: $total =
"$total(`$column`)";
209 session_write_close();
210 $sql =
"select `$group`,$total from {$this->table}\n$all\ngroup by `$group`\norder by ";
211 $result = $this->db->record($limit ? $this->db->limit($sql . $total . ($limit > 0 ?
'' :
' desc'),abs($limit)) : $sql .
"`$group`",
$args);
221 if($this->_db === null) $this->_db = $this->_controller->component(
'db');
226 if(!$this->_id) $this->_id = md5($this->select . $this->table . $this->
where);
single($select, $key, $value, $params=null)
Return a single row from the dataset.
_group($search, $group, $column, $total, $limit, $params)
where($search, &$args, &$strict, &$loose=null, &$all=null)
Build a where statement for use in an SQL query.
$table
Table(s) to select from (including joins and unions).
$where
Basic where clause (without the 'where' keyword; empty = all rows).
order($order)
Build an order statement for an SQL query.
$search
Custom search (key = column, value = SQL - :ref and :count can be used as placeholders for the...
_search($search, $order, $params, $offset, $limit)
$select
Columns to select.
trans($column, $key, $value, $params=null)
$args
Fixed parameters (in contrast to those provided during actions).
Framework for Rapid and Easy Development.
implodeMulti($where, $enclose=false)
$order
Basic/default order (same format as search function).
search($search, $order=null, $params=null, $offset=0, $limit=null)
Search for records in the dataset.
exists($key, $value, $params=null)
_totals($search, $totals, $params)