00001 <?php
00013 class RecordSelector
00014 {
00015 var $model = null;
00016
00017 var $sql_where = null;
00018 var $sql_order = null;
00019 var $sql_limit = null;
00020
00021 var $it_ptr;
00022 var $id_cache;
00023
00033 function RecordSelector($where, $args, &$model)
00034 {
00035 $this->_reset_state();
00036 $this->model = $model;
00037 if($where) {
00038 $this->sql_where = "WHERE (".$this->model->db->query($where, $args).")";
00039 }
00040 }
00041
00050 function where()
00051 {
00052 $this->_reset_state();
00053 $args = func_get_args();
00054 $q = array_shift($args);
00055 if(empty($q)) return $this;
00056
00057 if($this->sql_where) {
00058 $this->sql_where .= " AND (".$this->model->db->query($q, $args).")";
00059 } else {
00060 $this->sql_where = "WHERE (".$this->model->db->query($q, $args).")";
00061 }
00062 return $this;
00063 }
00064
00072 function eq($col, $val)
00073 {
00074 return $this->where("$col='%s'", $val);
00075 }
00076
00084 function match($record)
00085 {
00086 $t = $this;
00087 foreach($record as $k=>$v) {
00088 $t = $this->eq($k, $v);
00089 }
00090 return $t;
00091 }
00092
00099 function order($orderby)
00100 {
00101 $this->_reset_state();
00102 if($this->sql_order) {
00103 $this->sql_order .= ",$orderby";
00104 } else {
00105 $this->sql_order = "ORDER BY $orderby";
00106 }
00107 return $this;
00108 }
00109
00117 function limit($num, $offset=0)
00118 {
00119 $this->_reset_state();
00120 $this->sql_limit = "LIMIT $num OFFSET $offset";
00121 return $this;
00122 }
00123
00131 function fetch()
00132 {
00133 $q = "SELECT * FROM {$this->model->table}".$this->_build_clause();
00134 $ret = $this->model->db->get_all($q);
00135 switch(count($ret)) {
00136 case 0: return false;
00137 case 1: return $ret[0];
00138 default: return $ret;
00139 }
00140 }
00141
00147 function count()
00148 {
00149 $q = "SELECT COUNT (*) FROM {$this->model->table}".$this->_build_clause();
00150 return $this->model->db->get_value($q);
00151 }
00152
00164 function load($ret2d=false)
00165 {
00166
00167 $ids = $this->_get_ids();
00168 $ret = array();
00169 foreach($ids as $id) $ret[] = $this->model->load($id);
00170 switch(count($ret)) {
00171 case 0: return $ret2d ? array() : false;
00172 case 1: return $ret2d ? $ret : $ret[0];
00173 default: return $ret;
00174 }
00175 }
00176
00185 function load_all()
00186 {
00187 return $this->load(true);
00188 }
00189
00198 function load_one()
00199 {
00200 $ids = $this->_get_ids();
00201 if(!isset($ids[$this->it_ptr])) return false;
00202 return $this->model->load($ids[$this->it_ptr++]);
00203 }
00204
00208 function one()
00209 {
00210 return $this->load_one();
00211 }
00212
00216 function delete()
00217 {
00218
00219 $ids = $this->_get_ids();
00220 foreach($ids as $id) $this->model->delete($id);
00221 }
00222
00231 function set($key, $val=null)
00232 {
00233 if(is_array($key)) {
00234 $kp = $args = array();
00235 foreach($key as $k=>$v) {
00236 $kp[] = "\"$k\"='%s'";
00237 $args[] = $v;
00238 }
00239 $q = $this->model->db->query("UPDATE {$this->model->table} SET ".implode(',',$kp).$this->_build_clause(), $args);
00240 } else {
00241 $q = $this->model->db->query("UPDATE {$this->model->table} SET \"$key\"='%s'".$this->_build_clause(), array($val));
00242 }
00243
00244 if($this->model->enable_cache && $this->model->cache) {
00245 $ids = $this->_get_ids();
00246 foreach($ids as $id) $this->model->invalidate($id);
00247 }
00248 return $this->model->db->execute($q);
00249 }
00250
00264 function get()
00265 {
00266 $cols = array();
00267 foreach(func_get_args() as $a) $cols[] = "\"$a\"";
00268 $q = "SELECT ".implode(',',$cols)." FROM {$this->model->table}".$this->_build_clause();
00269 $rows = $this->model->db->get_all($q);
00270
00271 if(empty($rows)) return false;
00272
00273 $cols = func_get_args();
00274 $ret = array();
00275 if(count($cols) > 1) {
00276 foreach($rows as $row) {
00277 $rec = array();
00278 foreach($cols as $col) $rec[$col] = $row[$col];
00279 $ret[] = $rec;
00280 }
00281 } else {
00282 foreach($rows as $row) $ret[] = $row[$cols[0]];
00283 }
00284
00285 if(count($rows) == 1) {
00286 return $ret[0];
00287 }
00288 return $ret;
00289 }
00290
00300 function pair()
00301 {
00302 if(func_num_args() < 2) return false;
00303 $args = func_get_args();
00304 $rows = call_user_func_array(array($this,'get'), $args);
00305 if(!isset($rows[0])) $rows = array($rows);
00306
00307 $ret = array();
00308 foreach($rows as $row) {
00309 if(count($row) > 2) {
00310 $ret[$row[$args[0]]] = array();
00311 for($i = 1; $i < count($args); $i++) $ret[$row[$args[0]]][] = $row[$args[$i]];
00312 } else {
00313 $ret[$row[$args[0]]] = $row[$args[1]];
00314 }
00315 }
00316 return $ret;
00317 }
00318
00319 function _get_ids()
00320 {
00321 if(!empty($this->id_cache)) {
00322 return $this->id_cache;
00323 }
00324 $ids = $this->get('id');
00325 assert_type($ids, 'array');
00326 $this->id_cache = $ids;
00327 return $ids;
00328 }
00329
00330 function _reset_state()
00331 {
00332 $this->it_ptr = 0;
00333 $this->id_cache = array();
00334 }
00335
00336 function _build_clause()
00337 {
00338 $sql = '';
00339 if($this->sql_where) $sql .= ' '.$this->sql_where;
00340 if($this->sql_order) $sql .= ' '.$this->sql_order;
00341 if($this->sql_limit) $sql .= ' '.$this->sql_limit;
00342 return $sql;
00343 }
00344 }
00345
00346 ?>