bool Stock::getIndexRange(const KQuery& query, size_t& out_start, size_t& out_end) const{ out_start = 0; out_end = 0; if (!m_data || !m_kdataDriver) return false; if (KQuery::INDEX == query.queryType()) return _getIndexRangeByIndex(query, out_start, out_end); if ((KQuery::DATE != query.queryType()) || (query.kType() >= KQuery::INVALID_KTYPE) || query.startDatetime() >= query.endDatetime()) return false; if (m_data->pKData[query.kType()]) { return _getIndexRangeByDateFromBuffer(query, out_start, out_end); } if (!m_kdataDriver->getIndexRangeByDate(m_data->m_market, m_data->m_code, query, out_start, out_end)) { out_start = 0; out_end = 0; return false; } return true; }
bool Stock:: _getIndexRangeByIndex(const KQuery& query, size_t& out_start, size_t& out_end) const { assert(query.queryType() == KQuery::INDEX); out_start = 0; out_end = 0; size_t total = getCount(query.kType()); if (0 == total) { return false; } hku_int64 startix, endix; startix = query.start(); if(startix < 0) { startix += total; if(startix < 0) startix = 0; } endix = query.end(); if(endix < 0) { endix += total; if(endix < 0) endix = 0; } size_t null_size_t = Null<size_t>(); size_t startpos = 0; size_t endpos = null_size_t; try { startpos = boost::numeric_cast<size_t>(startix); } catch(...) { startpos = null_size_t; } try { endpos = boost::numeric_cast<size_t>(endix); } catch (...) { endpos = null_size_t; } if(endpos > total) { endpos = total; } if(startpos >= endpos) { return false; } out_start = startpos; out_end = endpos; return true; }
bool MySQLKDataDriver:: getIndexRangeByDate(const string& market, const string& code, const KQuery& query, size_t& out_start, size_t& out_end) { string func_name(" [MySQLKDataDriver::getIndexRangeByDate]"); out_start = 0; out_end = 0; if (query.queryType() != KQuery::DATE) { HKU_ERROR("queryType must be KQuery::DATE" << func_name); return false; } if(query.startDatetime() >= query.endDatetime() || query.startDatetime() > (Datetime::max)()) { return false; } MYSQL_RES *mysql_result; MYSQL_ROW row; string table(_getTableName(market, code, query.kType())); std::stringstream buf (std::stringstream::out); buf << "select count(1) from " << table << " where date<" << query.startDatetime().number(); if (!_query(buf.str())) { HKU_ERROR("mysql_query error! " << func_name); return false; } mysql_result = mysql_store_result(m_mysql.get()); if (!mysql_result) { HKU_ERROR("mysql_store_result error!" << func_name); return false; } while ((row = mysql_fetch_row(mysql_result))) { try { out_start = boost::lexical_cast<size_t>(row[0]); } catch (...) { HKU_INFO("Error boost::lexical_cast<size_t>" << table << func_name); out_start = 0; mysql_free_result(mysql_result); return false; } } mysql_free_result(mysql_result); buf.str(""); buf << "select count(1) from " << table << " where date<=" << query.endDatetime().number(); if (!_query(buf.str())) { HKU_ERROR("mysql_query error! " << func_name); return false; } mysql_result = mysql_store_result(m_mysql.get()); if (!mysql_result) { HKU_ERROR("mysql_store_result error!" << func_name); return false; } while ((row = mysql_fetch_row(mysql_result))) { try { out_end = boost::lexical_cast<size_t>(row[0]) - 1; } catch (...) { HKU_INFO("Error boost::lexical_cast<size_t>" << table << func_name); out_end = 0; mysql_free_result(mysql_result); return false; } } mysql_free_result(mysql_result); return true; }