2019-08-11 23:06:30 +08:00
|
|
|
/*
|
|
|
|
* SQLiteBaseInfoDriver.cpp
|
|
|
|
*
|
|
|
|
* Copyright (c) 2019 fasiondog
|
|
|
|
*
|
|
|
|
* Created on: 2019-8-11
|
|
|
|
* Author: fasiondog
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <thread>
|
|
|
|
#include "../../../StockManager.h"
|
|
|
|
#include "SQLiteBaseInfoDriverV2.h"
|
|
|
|
#include "../table/MarketInfoTable.h"
|
2019-08-13 23:29:15 +08:00
|
|
|
#include "../table/StockTypeInfoTable.h"
|
|
|
|
#include "../table/StockWeightoTable.h"
|
2019-08-11 23:06:30 +08:00
|
|
|
|
|
|
|
namespace hku {
|
|
|
|
|
|
|
|
SQLiteBaseInfoDriverV2::SQLiteBaseInfoDriverV2()
|
|
|
|
: BaseInfoDriver("sqlite3"), m_pool(nullptr) {
|
|
|
|
}
|
|
|
|
|
|
|
|
SQLiteBaseInfoDriverV2::~SQLiteBaseInfoDriverV2() {
|
|
|
|
if (m_pool) {
|
|
|
|
delete m_pool;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SQLiteBaseInfoDriverV2::_init() {
|
|
|
|
string dbname;
|
|
|
|
try {
|
|
|
|
dbname = getParam<string>("db");
|
|
|
|
HKU_TRACE("SQLITE3: {}", dbname);
|
|
|
|
} catch(...) {
|
|
|
|
HKU_ERROR("Can't get Sqlite3 filename!");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
int cpu_num = std::thread::hardware_concurrency();
|
|
|
|
if (cpu_num > 1) {
|
|
|
|
cpu_num--;
|
|
|
|
}
|
|
|
|
m_pool = new DBConnectPool(m_params, cpu_num);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SQLiteBaseInfoDriverV2::_loadMarketInfo() {
|
|
|
|
if (!m_pool) {
|
2019-08-13 23:29:15 +08:00
|
|
|
HKU_ERROR("Connect pool ptr is null!");
|
2019-08-11 23:06:30 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
DBConnectGuard dbGuard(m_pool);
|
|
|
|
auto con = dbGuard.getConnect();
|
|
|
|
|
|
|
|
vector<MarketInfoTable> infoTables;
|
|
|
|
try {
|
|
|
|
con->batchLoad(infoTables);
|
|
|
|
} catch (std::exception& e) {
|
|
|
|
HKU_FATAL("load Market table failed! {}", e.what());
|
|
|
|
return false;
|
|
|
|
} catch (...) {
|
|
|
|
HKU_FATAL("load Market table failed!");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
StockManager& sm = StockManager::instance();
|
|
|
|
for (auto& info: infoTables) {
|
|
|
|
Datetime lastDate;
|
|
|
|
try {
|
|
|
|
lastDate = Datetime(info.lastDate() * 10000);
|
|
|
|
} catch (...) {
|
|
|
|
lastDate = Null<Datetime>();
|
|
|
|
HKU_WARN("lastDate of market({}) is invalid! ", info.market());
|
|
|
|
}
|
2019-08-13 23:29:15 +08:00
|
|
|
sm.loadMarketInfo(
|
2019-08-11 23:06:30 +08:00
|
|
|
MarketInfo(
|
|
|
|
info.market(),
|
|
|
|
info.name(),
|
|
|
|
info.description(),
|
|
|
|
info.code(),
|
|
|
|
lastDate
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-08-13 23:29:15 +08:00
|
|
|
bool SQLiteBaseInfoDriverV2::_loadStockTypeInfo() {
|
|
|
|
if (!m_pool) {
|
|
|
|
HKU_ERROR("Connect pool ptr is null!");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
DBConnectGuard dbGuard(m_pool);
|
|
|
|
auto con = dbGuard.getConnect();
|
|
|
|
|
|
|
|
vector<StockTypeInfoTable> infoTables;
|
|
|
|
try {
|
|
|
|
con->batchLoad(infoTables);
|
|
|
|
} catch (std::exception& e) {
|
|
|
|
HKU_FATAL("load StockTypeInfo table failed! {}", e.what());
|
|
|
|
return false;
|
|
|
|
} catch (...) {
|
|
|
|
HKU_FATAL("load StockTypeInfo table failed!");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
StockManager& sm = StockManager::instance();
|
|
|
|
for (auto& info: infoTables) {
|
|
|
|
sm.loadStockTypeInfo(
|
|
|
|
StockTypeInfo(
|
|
|
|
info.type(),
|
|
|
|
info.description(),
|
|
|
|
info.tick(),
|
|
|
|
info.tickValue(),
|
|
|
|
info.precision(),
|
|
|
|
info.minTradeNumber(),
|
|
|
|
info.maxTradeNumber()
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SQLiteBaseInfoDriverV2::_loadStock() {
|
|
|
|
if (!m_pool) {
|
|
|
|
HKU_ERROR("Connect pool ptr is null!");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
DBConnectGuard dbGuard(m_pool);
|
|
|
|
auto con = dbGuard.getConnect();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
StockWeightList SQLiteBaseInfoDriverV2::_getStockWeightList(uint64 stockid) {
|
|
|
|
StockWeightList result;
|
|
|
|
if (!m_pool) {
|
|
|
|
HKU_ERROR("Connect pool ptr is null!");
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
DBConnectGuard dbGuard(m_pool);
|
|
|
|
auto con = dbGuard.getConnect();
|
|
|
|
|
|
|
|
vector<StockWeightTable> table;
|
|
|
|
try {
|
|
|
|
con->batchLoad(table, format("stockid={}", stockid));
|
|
|
|
} catch (std::exception& e) {
|
|
|
|
HKU_FATAL("load StockWeight table failed! {}", e.what());
|
|
|
|
return result;
|
|
|
|
} catch (...) {
|
|
|
|
HKU_FATAL("load StockWeight table failed!");
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
for (auto& weight: table) {
|
|
|
|
result.push_back(StockWeight());
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2019-08-11 23:06:30 +08:00
|
|
|
} /* namespace */
|