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]);
76 if(!$operator) $operator =
'<=';
78 if(($ref ===
null) || ($ref ==
'null'))
$where[] =
"`$column` is null";
79 elseif($ref ==
'""')
$where[] =
"(`$column` is null or `$column` = '')";
81 if(!$operator) $operator =
'=';
83 $where[] =
"`$column` " . (is_array($ref) ?
"in (:$key)" :
"$operator :$key");
88 foreach((array)$ref as $date)
if($date = \
Rsi\Fred\DateTime\Date::create($date)){
89 $args[$key_from =
'd' . count(
$args)] = (string)$date;
90 $args[$key_to =
'd' . count(
$args)] = (string)$date->add();
91 $dates[] =
"`$column` >= :$key_from and `$column` < :$key_to";
96 $likes = $loose_likes = [];
97 foreach((array)$ref as $like)
if($like){
98 if(preg_match(
'/^\\/.+\\/$/',$like))
try{
100 $args[$key =
'r' . count(
$args)] = substr($like,1,-1);
101 $likes[] =
"`$column` regexp :$key";
104 $this->_controller->component(
'log')->debug(
"Invalid reg-ex '$like': " . str_replace(
'preg_match(): Compilation failed: ',
'',$e->getMessage()),__FILE__,__LINE__);
107 elseif(substr($like = strtr($like,[
'?' =>
'_',
'*' =>
'%']),0,1) ==
'-'){
108 $args[$key =
'x' . count(
$args)] =
'%' . substr($like,1) .
'%';
109 $likes[] =
"(`$column` is null or `$column` not like :$key)";
111 elseif($like ==
'null') $likes[] =
"`$column` is null";
112 elseif($like ==
'""') $likes[] =
"(`$column` is null or `$column` = '')";
114 $args[$key =
'l' . count(
$args)] = $like .
'%';
115 $likes[] =
"`$column` like :$key";
116 if(substr($like,0,1) !=
'%'){
117 $args[$key =
'l' . count(
$args)] =
'%' . $like .
'%';
118 $loose_likes[] =
"`$column` like :$key";
124 if($loose_likes) $loose[$column] = $this->
implodeMulti($loose_likes);
131 $where =
'where ' . ($this->
where ?
'(' . $this->
where .
')' . ($strict ?
"\nand " :
'') :
'');
132 $all = $this->
where || $strict ?
$where . implode(
' and ',$loose + $strict) :
null;
133 $loose = $loose ?
$where .
'not (' . implode(
' and ',$strict) .
")\nand " . implode(
' and ',array_merge($strict,$loose)) :
null;
134 $strict = $this->
where || $strict ?
$where . implode(
' and ',$strict) :
null;
143 foreach(
$order + $this->
order as $column => $type){
144 $column =
"`$column`";
145 if($type & SORT_DESC) $column .=
' desc';
146 $columns[] = $column;
148 return $columns ?
'order by ' . implode(
',',$columns) :
null;
152 $args = array_merge($params ?: [],$this->args);
155 session_write_close();
156 $select =
"select {$this->select}\nfrom {$this->table}";
157 $sql =
"$select\n$strict\n$order";
158 if($limit) $sql = $this->db->limit($sql,$limit,$offset);
159 $records = $this->db->all($sql,
$args);
160 if($loose && (!$limit || (($count = count($records)) < $limit))){
161 $sql =
"$select\n$loose\n$order";
167 elseif($offset) $offset -= $this->db->single(
"select count(*) from {$this->table}\n$strict",
$args);
168 $sql = $this->db->limit($sql,$limit,$offset);
170 $records = array_merge($records,$this->db->all($sql,
$args));
177 if(!$totals)
return [];
178 $args = array_merge($params ?: [],$this->args);
180 session_write_close();
182 foreach($totals as $column => &$types)
foreach(($types = (array)$types) as $type){
183 $alias = $column . self::COLUMN_TYPE_GLUE . $type;
189 default: $columns[] =
"$type(`$column`) as `$alias`";
193 $select =
'select ' . implode(
',',$columns) .
"\nfrom {$this->table}\n";
194 $alias_totals = $this->db->single(
$select . $all,
$args,
false);
197 foreach($totals as $column => $types){
198 $result[$column] = [];
199 foreach($types as $type) $result[$column][$type] =
206 $args = array_merge($params ?: [],$this->args);
210 default: $total =
"$total(`$column`)";
212 session_write_close();
213 $sql =
"select `$group`,$total from {$this->table}\n$all\ngroup by `$group`\norder by ";
214 $result = $this->db->record($limit ? $this->db->limit($sql . $total . ($limit > 0 ?
'' :
' desc'),abs($limit)) : $sql .
"`$group`",
$args);
224 if($this->_db ===
null) $this->_db = $this->_controller->component(
'db');
229 if(!$this->_id) $this->_id = md5($this->select . $this->table . $this->
where);