`
chennanfei
  • 浏览: 40924 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

延缓查询

 
阅读更多

 

<?php
class SelectQuery {
    private $dbConn;
    private $select;
    
    public function __construct($dbConn) {
        $this->dbConn = $dbConn;
        $this->select = $dbConn->select();
    }
    
    public function addField($col, $value, $op = '=', $join = 'and') {};
    
    public function getLazyEntities() {
        return new LazyQuery($this, 'findAll');
    };
    
    public function getLazyEntity() {
        return new LazyQuery($this, 'find');
    };
    
    public function findAll() {
        return $this->dbConn->fetchAll($this->select);
    }
    
    public function find() {
        return $this->dbConn->fetchRow($this->select);
    }
}

class LazyQuery {
    private $query;
    private $method;
    
    public function __construct($query, $method) {
        $this->query = $query;
        $this->method = $method;
    }
    
    public function execute() {
        $query = $this->query;
        $method = $this->method;
        if ($query && method_exists($query, $method)) {
            return $query->$method();
        }

        throw new Exception("Query failed to call method.");
    }
}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics