/** * 获取当前实例 * @return XF_Db_Table_Select_Paginator */ public static function getInstance() { if (self::$_instance === null) { self::$_instance = new self(); } return self::$_instance; }
/** * 查询所有 * @param mixed $options 分页参数 * @param bool $emptyObject 当没有查询到任何资料时,是否返回空对象?默认为FALSE * @return XF_Db_Table_Abstract | XF_Db_Table_Abstract Array | NULL */ public function fetchAll($options = array(), $emptyObject = FALSE) { $size = isset($this->_adv_limit[1]) ? $this->_adv_limit[1] : 20; $offset = isset($this->_adv_limit[0]) ? $this->_adv_limit[0] : 0; //如果是自定义查询 该sql主要用于获取数据 $sql = isset($options['sql']) ? $options['sql'] : FALSE; //如果是自定义查询时,是否需要将查询到的结果尝试包装成相对应的对象 $sqlPacking = isset($options['sqlPacking']) ? $options['sqlPacking'] : false; //如果是自定义查询,该sql主要用于统计总记录数 $pageSql = isset($options['pageSql']) ? $options['pageSql'] : FALSE; //是否自动分页,true时URL存在分页参数时自动分页数 否则始终为第一页的数据 $autoPage = isset($options['autoPage']) ? $options['autoPage'] : FALSE; //是否需要将查到的数据包装相对应的对象 $packing = isset($options['packing']) ? $options['packing'] : TRUE; $this->_allotDatabaseServer(); //查询的结果 $result = false; //如果等于false 则不分页 if ($size === false || $offset === false) { if ($sql == FALSE) { //是否有缓存 $result = $this->_getDataCache(); if ($result == XF_CACHE_EMPTY) { $this->_db_drive_connect->showQuery($this->_show_query); $result = $this->_db_drive_connect->select($this->_db_table->getTableName(), $this->_adv_find, $this->_adv_where, $this->_adv_order, $this->_adv_group, false, false); $this->_setDataCache($result); } //返回包装后的结果 if ($packing == true) { $result = $this->_packing($result, $emptyObject); } $this->_clearProperty(); return $result; } else { //是否有缓存 $result = $this->_getDataCache($sql); if ($result == XF_CACHE_EMPTY) { $result = $this->_db_drive_connect->execute($sql); $this->_setDataCache($result, $sql); } if ($sqlPacking == true) { $result = $this->_packing($result, $emptyObject); } $this->_clearProperty(); return $result; } } elseif (is_numeric($size) && is_numeric($offset) && $autoPage == false) { if ($sql == false) { //是否有缓存 $result = $this->_getDataCache(); if ($result == XF_CACHE_EMPTY) { $this->_db_drive_connect->showQuery($this->_show_query); $result = $this->_db_drive_connect->select($this->_db_table->getTableName(), $this->_adv_find, $this->_adv_where, $this->_adv_order, $this->_adv_group, $size, $offset); $this->_setDataCache($result); } //返回包装后的结果 if ($packing == true) { $result = $this->_packing($result, $emptyObject); } $this->_clearProperty(); return $result; } else { //是否有缓存 $result = $this->_getDataCache($sql); if ($result == XF_CACHE_EMPTY) { $result = $this->_db_drive_connect->execute($sql, true); $this->_setDataCache($result, $sql); } //返回包装后的结果 if ($packing == true) { $result = $this->_packing($result, $emptyObject); } $this->_clearProperty(); return $result; } } //////////////////// 启用分页 ////////////////////// if ($autoPage == true) { //总记录数 $dataCount = 0; //实例化Page对象 $paginator = XF_Db_Table_Select_Paginator::getInstance(); //当前页码 $p = 1; if ($sql == FALSE) { $dataCount = $this->fetchCount(); } elseif ($pageSql != false) { $result = $this->_db_drive_connect->execute($pageSql, true); if (isset($result[0])) { $key = array_keys($result[0]); $dataCount = $result[0][$key[0]]; } else { $dataCount = 0; } } else { throw new XF_Db_Table_Select_Exception('Not found!', 404); } //URL是否存在页码 $existsPage = FALSE; $paginatorParamValue = XF_Controller_Request_Http::getInstance()->getParam($paginator->getPaginatorParamName(), null); if ($paginatorParamValue !== null && $paginatorParamValue != '') { $existsPage = TRUE; } //获取当前页码 $paginatorParamValue ? $p = intval($paginatorParamValue) : ($p = 1); if ($autoPage === FALSE) { $p = 1; } //是否为正常的页码 $page_count = $dataCount > 0 ? ceil($dataCount / $size) : 0; if ($existsPage && $page_count < $p && $autoPage === TRUE || $p == 0) { throw new XF_Db_Table_Select_Exception('Not found!', 404); } //当前页数据 $result = FALSE; if ($sql == false) { //读取缓存 $this->_adv_limit[1] = ($p - 1) * $size; $result = $this->_getDataCache(); if ($result == XF_CACHE_EMPTY) { $this->_db_drive_connect->showQuery($this->_show_query); $result = $this->_db_drive_connect->select($this->_db_table->getTableName(), $this->_adv_find, $this->_adv_where, $this->_adv_order, $this->_adv_group, $size, ($p - 1) * $size); $this->_setDataCache($result); } } else { $sql = $sql . ' LIMIT ' . ($p - 1) * $size . ',' . $size; //读取缓存 $result = $this->_getDataCache($sql); if ($result == XF_CACHE_EMPTY) { //获取当前页结果 $this->_db_drive_connect->showQuery($this->_show_query); $result = $this->_db_drive_connect->execute($sql, true); } $this->_setDataCache($result, $sql); if ($packing == true) { $result = $this->_packing($result, $emptyObject); } } //设置分页器内容 $paginator->set($size, $dataCount); $paginator->run(); //返回包装后的结果 if ($packing == true && $sql == false) { $result = $this->_packing($result, $emptyObject); } if ($sql != false && $sqlPacking == true) { $result = $this->_packing($result, $emptyObject); } $this->_clearProperty(); return $result; } else { //返回包装后的结果 if ($packing == true && $sql == false) { $result = $this->_packing($result, $emptyObject); } if ($sql != false && $sqlPacking == true) { $result = $this->_packing($result, $emptyObject); } $this->_clearProperty(); return $result; } }