hikyuu2/hikyuu_cpp/hikyuu/data_driver/base_info/sqlite/SQLiteBaseInfoDriver.cpp

301 lines
11 KiB
C++
Raw Normal View History

2015-01-07 01:26:14 +08:00
/*
* SQLiteBaseInfoDriver.cpp
*
2019-08-16 01:17:24 +08:00
* Copyright (c) 2019 fasiondog
2019-11-10 23:31:41 +08:00
*
2019-08-16 01:17:24 +08:00
* Created on: 2019-8-11
2015-01-07 01:26:14 +08:00
* Author: fasiondog
*/
2019-08-16 01:17:24 +08:00
#include <thread>
2016-04-03 00:08:31 +08:00
#include "../../../StockManager.h"
2019-08-16 01:17:24 +08:00
#include "SQLiteBaseInfoDriver.h"
#include "../table/MarketInfoTable.h"
#include "../table/StockTypeInfoTable.h"
#include "../table/StockWeightTable.h"
#include "../table/StockTable.h"
#include "../table/HolidayTable.h"
2015-01-07 01:26:14 +08:00
namespace hku {
2019-11-10 23:31:41 +08:00
SQLiteBaseInfoDriver::SQLiteBaseInfoDriver() : BaseInfoDriver("sqlite3"), m_pool(nullptr) {}
2015-01-07 01:26:14 +08:00
2019-08-16 01:17:24 +08:00
SQLiteBaseInfoDriver::~SQLiteBaseInfoDriver() {
if (m_pool) {
delete m_pool;
}
}
2015-01-07 01:26:14 +08:00
bool SQLiteBaseInfoDriver::_init() {
2020-11-22 18:34:37 +08:00
string dbname = tryGetParam<string>("db", "");
HKU_ERROR_IF_RETURN(dbname == "", false, "Can't get Sqlite3 filename!");
HKU_TRACE("SQLITE3: {}", dbname);
2019-11-16 02:10:21 +08:00
m_pool = new ConnectPool<SQLiteConnect>(m_params);
HKU_CHECK(m_pool, "Failed malloc ConnectPool!");
return true;
2015-01-07 01:26:14 +08:00
}
2021-01-27 00:37:59 +08:00
vector<MarketInfo> SQLiteBaseInfoDriver::getAllMarketInfo() {
vector<MarketInfo> result;
HKU_ERROR_IF_RETURN(!m_pool, result, "Connect pool ptr is null!");
2015-01-07 01:26:14 +08:00
2019-08-16 01:17:24 +08:00
try {
2020-10-31 00:07:18 +08:00
auto con = m_pool->getConnect();
vector<MarketInfoTable> infoTables;
2019-08-16 01:17:24 +08:00
con->batchLoad(infoTables);
2020-10-31 00:07:18 +08:00
for (auto& info : infoTables) {
try {
2021-01-27 00:37:59 +08:00
result.emplace_back(info.market(), info.name(), info.description(), info.code(),
info.lastDate(), info.openTime1(), info.closeTime1(),
info.openTime2(), info.closeTime2());
2020-10-31 00:07:18 +08:00
} catch (std::exception& e) {
HKU_ERROR("Failed load market, {}", e.what());
} catch (...) {
2020-12-25 23:51:36 +08:00
HKU_ERROR_UNKNOWN;
2020-10-31 00:07:18 +08:00
}
}
2019-08-16 01:17:24 +08:00
} catch (std::exception& e) {
HKU_FATAL("load Market table failed! {}", e.what());
} catch (...) {
HKU_FATAL("load Market table failed!");
2015-01-07 01:26:14 +08:00
}
2021-01-27 00:37:59 +08:00
return result;
2015-01-07 01:26:14 +08:00
}
2021-01-27 00:37:59 +08:00
vector<StockTypeInfo> SQLiteBaseInfoDriver::getAllStockTypeInfo() {
vector<StockTypeInfo> result;
HKU_ERROR_IF_RETURN(!m_pool, result, "Connect pool ptr is null!");
2019-08-16 01:17:24 +08:00
try {
2021-01-27 00:37:59 +08:00
auto con = m_pool->getConnect();
vector<StockTypeInfoTable> infoTables;
2019-08-16 01:17:24 +08:00
con->batchLoad(infoTables);
2021-01-27 00:37:59 +08:00
for (auto& info : infoTables) {
result.emplace_back(info.type(), info.description(), info.tick(), info.tickValue(),
info.precision(), info.minTradeNumber(), info.maxTradeNumber());
}
2019-08-16 01:17:24 +08:00
} catch (std::exception& e) {
HKU_FATAL("load StockTypeInfo table failed! {}", e.what());
} catch (...) {
HKU_FATAL("load StockTypeInfo table failed!");
}
2021-01-27 00:37:59 +08:00
return result;
2015-01-07 01:26:14 +08:00
}
2021-01-28 00:43:53 +08:00
vector<StockInfo> SQLiteBaseInfoDriver::getAllStockInfo() {
vector<StockInfo> result;
HKU_ERROR_IF_RETURN(!m_pool, result, "Connect pool ptr is null!");
2019-08-16 01:17:24 +08:00
try {
2021-01-28 00:43:53 +08:00
auto con = m_pool->getConnect();
con->batchLoad(result);
2019-08-16 01:17:24 +08:00
} catch (...) {
}
return result;
}
StockInfo SQLiteBaseInfoDriver::getStockInfo(string market, const string& code) {
HKU_ASSERT(m_pool);
StockInfo result;
try {
to_upper(market);
auto con = m_pool->getConnect();
string sql =
2021-02-20 00:43:06 +08:00
format("{} and a.code='{}' and c.market='{}'", StockInfo::getSelectSQL(), code, market);
SQLStatementPtr st = con->getStatement(sql);
st->exec();
if (st->moveNext()) {
result.load(st);
}
} catch (...) {
2015-01-07 01:26:14 +08:00
}
2021-01-28 00:43:53 +08:00
return result;
2015-01-07 01:26:14 +08:00
}
StockWeightList SQLiteBaseInfoDriver::getStockWeightList(const string& market, const string& code,
Datetime start, Datetime end) {
HKU_ASSERT(m_pool);
2019-08-16 01:17:24 +08:00
StockWeightList result;
2015-01-07 01:26:14 +08:00
try {
auto con = m_pool->getConnect();
HKU_CHECK(con, "Failed fetch connect!");
vector<StockWeightTable> table;
Datetime new_end = end.isNull() ? Datetime::max() : end;
con->batchLoad(
table,
format(
"stockid=(select stockid from stock where marketid=(select marketid from "
"market where market='{}') and code='{}') and date>={} and date<{} order by date asc",
market, code, start.year() * 10000 + start.month() * 100 + start.day(),
new_end.year() * 10000 + new_end.month() * 100 + new_end.day()));
for (auto& w : table) {
try {
result.push_back(StockWeight(Datetime(w.date * 10000), w.countAsGift * 0.0001,
w.countForSell * 0.0001, w.priceForSell * 0.001,
w.bonus * 0.001, w.countOfIncreasement * 0.0001,
w.totalCount, w.freeCount));
} catch (std::out_of_range& e) {
HKU_WARN("Date of id({}) is invalid! {}", w.id(), e.what());
} catch (std::exception& e) {
HKU_WARN("Error StockWeight Record id({}) {}", w.id(), e.what());
} catch (...) {
2020-12-25 23:51:36 +08:00
HKU_WARN("Error StockWeight Record id({})! Unknown reason!", w.id());
}
}
2019-08-16 01:17:24 +08:00
} catch (std::exception& e) {
HKU_FATAL("load StockWeight table failed! {}", e.what());
} catch (...) {
2019-08-16 01:17:24 +08:00
HKU_FATAL("load StockWeight table failed!");
2015-01-07 01:26:14 +08:00
}
return result;
}
2019-11-10 23:31:41 +08:00
Parameter SQLiteBaseInfoDriver ::getFinanceInfo(const string& market, const string& code) {
Parameter result;
2020-11-22 18:34:37 +08:00
HKU_IF_RETURN(!m_pool, result);
2019-11-10 23:31:41 +08:00
std::stringstream buf;
2019-08-16 01:17:24 +08:00
buf << "select f.updated_date, f.ipo_date, f.province,"
<< "f.industry, f.zongguben, f.liutongguben, f.guojiagu, f.faqirenfarengu,"
<< "f.farengu, f.bgu, f.hgu, f.zhigonggu, f.zongzichan, f.liudongzichan,"
<< "f.gudingzichan, f.wuxingzichan, f.gudongrenshu, f.liudongfuzhai,"
<< "f.changqifuzhai, f.zibengongjijin, f.jingzichan, f.zhuyingshouru,"
<< "f.zhuyinglirun, f.yingshouzhangkuan, f.yingyelirun, f.touzishouyu,"
<< "f.jingyingxianjinliu, f.zongxianjinliu, f.cunhuo, f.lirunzonghe,"
<< "f.shuihoulirun, f.jinglirun, f.weifenpeilirun, f.meigujingzichan,"
<< "f.baoliu2 from stkfinance f, stock s, market m "
<< "where m.market='" << market << "'"
<< " and s.code = '" << code << "'"
<< " and s.marketid = m.marketid"
<< " and f.stockid = s.stockid"
<< " order by updated_date DESC limit 1";
2019-11-16 02:10:21 +08:00
auto con = m_pool->getConnect();
2019-08-16 01:17:24 +08:00
auto st = con->getStatement(buf.str());
st->exec();
2019-11-10 23:31:41 +08:00
if (!st->moveNext()) {
2019-08-16 01:17:24 +08:00
return result;
}
2019-08-16 01:17:24 +08:00
2019-08-16 23:18:22 +08:00
int updated_date(0), ipo_date(0);
price_t province(0), industry(0), zongguben(0), liutongguben(0), guojiagu(0), faqirenfarengu(0);
2019-11-10 23:31:41 +08:00
price_t farengu(0), bgu(0), hgu(0), zhigonggu(0), zongzichan(0), liudongzichan(0),
gudingzichan(0);
2019-08-16 23:18:22 +08:00
price_t wuxingzichan(0), gudongrenshu(0), liudongfuzhai(0), changqifuzhai(0), zibengongjijin(0);
price_t jingzichan(0), zhuyingshouru(0), zhuyinglirun(0), yingshouzhangkuan(0), yingyelirun(0);
price_t touzishouyi(0), jingyingxianjinliu(0), zongxianjinliu(0), cunhuo(0), lirunzonghe(0);
price_t shuihoulirun(0), jinglirun(0), weifenpeilirun(0), meigujingzichan(0), baoliu2(0);
2019-08-16 01:17:24 +08:00
2019-11-10 23:31:41 +08:00
st->getColumn(0, updated_date, ipo_date, province, industry, zongguben, liutongguben, guojiagu,
faqirenfarengu, farengu, bgu, hgu, zhigonggu, zongzichan, liudongzichan,
gudingzichan, wuxingzichan, gudongrenshu, liudongfuzhai, changqifuzhai,
zibengongjijin, jingzichan, zhuyingshouru, zhuyinglirun, yingshouzhangkuan,
yingyelirun, touzishouyi, jingyingxianjinliu, zongxianjinliu, cunhuo, lirunzonghe,
shuihoulirun, jinglirun, weifenpeilirun, meigujingzichan, baoliu2);
2019-08-16 01:17:24 +08:00
result.set<string>("market", market);
result.set<string>("code", code);
result.set<int>("updated_date", updated_date);
result.set<int>("ipo_date", ipo_date);
result.set<price_t>("province", province);
result.set<price_t>("industry", industry);
result.set<price_t>("zongguben", zongguben);
result.set<price_t>("liutongguben", liutongguben);
result.set<price_t>("guojiagu", guojiagu);
result.set<price_t>("faqirenfarengu", faqirenfarengu);
result.set<price_t>("farengu", farengu);
result.set<price_t>("bgu", bgu);
result.set<price_t>("hgu", hgu);
result.set<price_t>("zhigonggu", zhigonggu);
result.set<price_t>("zongzichan", zongzichan);
result.set<price_t>("liudongzichan", liudongzichan);
result.set<price_t>("gudingzichan", gudingzichan);
result.set<price_t>("wuxingzichan", wuxingzichan);
result.set<price_t>("gudongrenshu", gudongrenshu);
result.set<price_t>("liudongfuzhai", liudongfuzhai);
result.set<price_t>("changqifuzhai", changqifuzhai);
result.set<price_t>("zibengongjijin", zibengongjijin);
result.set<price_t>("jingzichan", jingzichan);
result.set<price_t>("zhuyingshouru", zhuyingshouru);
result.set<price_t>("zhuyinglirun", zhuyinglirun);
result.set<price_t>("yingshouzhangkuan", yingshouzhangkuan);
result.set<price_t>("yingyelirun", yingyelirun);
result.set<price_t>("touzishouyi", touzishouyi);
result.set<price_t>("jingyingxianjinliu", jingyingxianjinliu);
result.set<price_t>("zongxianjinliu", zongxianjinliu);
result.set<price_t>("cunhuo", cunhuo);
result.set<price_t>("lirunzonghe", lirunzonghe);
result.set<price_t>("shuihoulirun", shuihoulirun);
result.set<price_t>("jinglirun", jinglirun);
result.set<price_t>("weifenpeilirun", weifenpeilirun);
result.set<price_t>("meigujingzichan", meigujingzichan);
result.set<price_t>("baoliu2", baoliu2);
return result;
}
MarketInfo SQLiteBaseInfoDriver::getMarketInfo(const string& market) {
MarketInfo result;
HKU_ERROR_IF_RETURN(!m_pool, result, "Connect pool ptr is null!");
auto con = m_pool->getConnect();
try {
MarketInfoTable info;
2021-01-26 00:06:24 +08:00
string new_market(market);
to_upper(new_market);
con->load(info, format("market=\"{}\"", new_market));
if (!info.market().empty()) {
result = MarketInfo(info.market(), info.name(), info.description(), info.code(),
info.lastDate(), info.openTime1(), info.closeTime1(),
info.openTime2(), info.closeTime2());
}
} catch (...) {
}
return result;
}
StockTypeInfo SQLiteBaseInfoDriver::getStockTypeInfo(uint32_t type) {
StockTypeInfo result;
HKU_ERROR_IF_RETURN(!m_pool, result, "Connect pool ptr is null!");
auto con = m_pool->getConnect();
try {
StockTypeInfoTable info;
2021-01-26 00:06:24 +08:00
con->load(info, format("type={}", type));
if (info.type() != Null<uint32_t>()) {
result = StockTypeInfo(info.type(), info.description(), info.tick(), info.tickValue(),
info.precision(), info.minTradeNumber(), info.maxTradeNumber());
}
} catch (...) {
}
return result;
}
std::unordered_set<Datetime> SQLiteBaseInfoDriver::getAllHolidays() {
HKU_ASSERT(m_pool);
std::unordered_set<Datetime> result;
try {
auto con = m_pool->getConnect();
std::vector<HolidayTable> holidays;
con->batchLoad(holidays);
for (auto& holiday : holidays) {
try {
result.insert(holiday.datetime());
} catch (std::exception& e) {
HKU_WARN(e.what());
} catch (...) {
HKU_ERROR_UNKNOWN;
}
}
} catch (...) {
}
return result;
}
2019-11-10 23:31:41 +08:00
} // namespace hku