Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mysql/sqlite支持自定义板块 #302

Merged
merged 5 commits into from
Dec 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions hikyuu/draw/drawplot/matplotlib_draw.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ def set_mpl_params():
if in_interactive_session():
rcParams['interactive'] = True

if sys.platform == 'darwin':
matplotlib.rcParams['font.sans-serif'] = 'Arial Unicode MS'
return

rcParams['font.family'] = 'sans-serif'
rcParams['axes.unicode_minus'] = False

Expand Down
14 changes: 14 additions & 0 deletions hikyuu_cpp/hikyuu/StockManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,20 @@ Block StockManager::getBlock(const string& category, const string& name) {
return m_blockDriver ? m_blockDriver->getBlock(category, name) : Block();
}

void StockManager::saveBlock(const Block& blk) {
if (m_blockDriver) {
HKU_CHECK(!blk.category().empty(), "block's category can not be empty!");
HKU_CHECK(!blk.name().empty(), "block's name can not be empty!");
HKU_CHECK(!blk.empty(), "Can't save empty block!");
m_blockDriver->save(blk);
}
}
void StockManager::removeBlock(const string& category, const string& name) {
if (m_blockDriver) {
m_blockDriver->remove(category, name);
}
}

BlockList StockManager::getBlockList(const string& category) {
return m_blockDriver ? m_blockDriver->getBlockList(category) : BlockList();
}
Expand Down
10 changes: 10 additions & 0 deletions hikyuu_cpp/hikyuu/StockManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,16 @@ class HKU_API StockManager {
*/
Block getBlock(const string& category, const string& name);

void addBlock(const Block& blk) {
saveBlock(blk);
}

void saveBlock(const Block& blk);
void removeBlock(const string& category, const string& name);
void removeBlock(const Block& blk) {
removeBlock(blk.category(), blk.name());
}

/**
* 获取指定分类的板块列表
* @param category 板块分类
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,40 @@
*/

#include "hikyuu/utilities/db_connect/mysql/MySQLConnect.h"
#include "hikyuu/utilities/db_connect/TableMacro.h"
#include "hikyuu/utilities/db_connect/DBConnect.h"
#include "MySQLBlockInfoDriver.h"

namespace hku {

struct MySQLBlockTable {
TABLE_BIND4(MySQLBlockTable, block, category, name, market_code, index_code)
struct MySQLBlockView {
TABLE_BIND4(MySQLBlockView, block, category, name, market_code, index_code)
string category;
string name;
string market_code;
string index_code;
};

struct MySQLBlockTable {
TABLE_BIND3(MySQLBlockTable, block, category, name, market_code)
string category;
string name;
string market_code;
};

struct MySQLBlockIndexTable {
TABLE_BIND3(MySQLBlockIndexTable, BlockIndex, category, name, market_code)
string category;
string name;
string market_code;
};

MySQLBlockInfoDriver::~MySQLBlockInfoDriver() {}

bool MySQLBlockInfoDriver::_init() {
return true;
}

void MySQLBlockInfoDriver::load() {
DBConnectPtr MySQLBlockInfoDriver::getConnect() {
Parameter connect_param;
connect_param.set<string>("host", getParamFromOther<string>(m_params, "host", "127.0.0.1"));
connect_param.set<string>("usr", getParamFromOther<string>(m_params, "usr", "root"));
Expand All @@ -34,15 +48,19 @@ void MySQLBlockInfoDriver::load() {
string port_str = getParamFromOther<string>(m_params, "port", "3306");
unsigned int port = boost::lexical_cast<unsigned int>(port_str);
connect_param.set<int>("port", port);
MySQLConnect connect(connect_param);
return std::make_shared<MySQLConnect>(connect_param);
}

vector<MySQLBlockTable> records;
connect.batchLoadView(
void MySQLBlockInfoDriver::load() {
auto connect = getConnect();
vector<MySQLBlockView> records;
connect->batchLoadView(
records,
"select a.id, a.category, a.name, a.market_code, b.market_code as "
"index_code from `hku_base`.`block` a left "
"join `hku_base`.`BlockIndex` b on a.category=b.category and a.name = b.name");

std::unique_lock<std::shared_mutex> lock(m_buffer_mutex);
for (auto& record : records) {
auto category_iter = m_buffer.find(record.category);
if (category_iter == m_buffer.end()) {
Expand All @@ -59,6 +77,7 @@ void MySQLBlockInfoDriver::load() {

Block MySQLBlockInfoDriver::getBlock(const string& category, const string& name) {
Block ret;
std::shared_lock<std::shared_mutex> lock(m_buffer_mutex);
auto category_iter = m_buffer.find(category);
HKU_IF_RETURN(category_iter == m_buffer.end(), ret);

Expand All @@ -71,6 +90,7 @@ Block MySQLBlockInfoDriver::getBlock(const string& category, const string& name)

BlockList MySQLBlockInfoDriver::getBlockList(const string& category) {
BlockList ret;
std::shared_lock<std::shared_mutex> lock(m_buffer_mutex);
auto category_iter = m_buffer.find(category);
HKU_IF_RETURN(category_iter == m_buffer.end(), ret);

Expand All @@ -84,6 +104,7 @@ BlockList MySQLBlockInfoDriver::getBlockList(const string& category) {

BlockList MySQLBlockInfoDriver::getBlockList() {
BlockList ret;
std::shared_lock<std::shared_mutex> lock(m_buffer_mutex);
for (auto category_iter = m_buffer.begin(); category_iter != m_buffer.end(); ++category_iter) {
const auto& category_blocks = category_iter->second;
for (auto iter = category_blocks.begin(); iter != category_blocks.end(); ++iter) {
Expand All @@ -93,8 +114,56 @@ BlockList MySQLBlockInfoDriver::getBlockList() {
return ret;
}

void MySQLBlockInfoDriver::save(const Block& block) {}
void MySQLBlockInfoDriver::save(const Block& block) {
std::unique_lock<std::shared_mutex> lock(m_buffer_mutex);
auto category_iter = m_buffer.find(block.category());
if (category_iter == m_buffer.end()) {
m_buffer.emplace(block.category(), unordered_map<string, Block>{{block.name(), block}});
} else {
category_iter->second.emplace(block.name(), block);
}

auto connect = getConnect();
AutoTransAction trans(connect);
auto condition = (Field("category") == block.category()) & (Field("name") == block.name());
connect->remove(MySQLBlockView::getTableName(), condition, false);
connect->remove(MySQLBlockIndexTable::getTableName(), condition, false);

if (!block.getIndexStock().isNull()) {
MySQLBlockIndexTable index;
index.category = block.category();
index.name = block.name();
index.market_code = block.getIndexStock().market_code();
connect->save(index, false);
}

for (auto iter = block.begin(); iter != block.end(); ++iter) {
MySQLBlockTable record;
record.category = block.category();
record.name = block.name();
record.market_code = iter->market_code();
connect->save(record, false);
}
}

void MySQLBlockInfoDriver::remove(const string& category, const string& name) {
{
auto connect = getConnect();
AutoTransAction trans(connect);
auto condition = (Field("category") == category) & (Field("name") == name);
connect->remove(MySQLBlockTable::getTableName(), condition, false);
connect->remove(MySQLBlockIndexTable::getTableName(), condition, false);
}

std::unique_lock<std::shared_mutex> lock(m_buffer_mutex);
auto category_iter = m_buffer.find(category);
HKU_IF_RETURN(category_iter == m_buffer.end(), void());

void MySQLBlockInfoDriver::remove(const string& category, const string& name) {}
auto block_iter = category_iter->second.find(name);
HKU_IF_RETURN(block_iter == category_iter->second.end(), void());

category_iter->second.erase(block_iter);
m_buffer.erase(category_iter);
}

} // namespace hku
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

#pragma once

#include <mutex>
#include "../../BlockInfoDriver.h"

namespace hku {
Expand All @@ -24,8 +25,12 @@ class MySQLBlockInfoDriver : public BlockInfoDriver {
virtual void save(const Block& block) override;
virtual void remove(const string& category, const string& name) override;

private:
DBConnectPtr getConnect();

private:
unordered_map<string, unordered_map<string, Block>> m_buffer;
std::shared_mutex m_buffer_mutex;
};

} // namespace hku
Original file line number Diff line number Diff line change
Expand Up @@ -190,11 +190,11 @@ BlockList QLBlockInfoDriver::getBlockList() {
}

void QLBlockInfoDriver::save(const Block& block) {
HKU_THROW("Not support save block info!");
HKU_THROW("Not support save block info! You can use ini file to do it!");
}

void QLBlockInfoDriver::remove(const string& category, const string& name) {
HKU_THROW("Not support remove block info!");
HKU_THROW("Not support save block info! You can use ini file to do it!");
}

} /* namespace hku */
Original file line number Diff line number Diff line change
Expand Up @@ -5,39 +5,57 @@
* Author: fasiondog
*/

#include "hikyuu/utilities/db_connect/DBConnect.h"
#include "hikyuu/utilities/db_connect/sqlite/SQLiteConnect.h"
#include "hikyuu/utilities/db_connect/TableMacro.h"
#include "SQLiteBlockInfoDriver.h"

namespace hku {

struct SQLiteBlockTable {
TABLE_BIND4(SQLiteBlockTable, block, category, name, market_code, index_code)
struct SQLiteBlockView {
TABLE_BIND4(SQLiteBlockView, block, category, name, market_code, index_code)
string category;
string name;
string market_code;
string index_code;
};

struct SQLiteBlockTable {
TABLE_BIND3(SQLiteBlockTable, block, category, name, market_code)
string category;
string name;
string market_code;
};

struct SQLiteBlockIndexTable {
TABLE_BIND3(SQLiteBlockIndexTable, BlockIndex, category, name, market_code)
string category;
string name;
string market_code;
};

SQLiteBlockInfoDriver::~SQLiteBlockInfoDriver() {}

bool SQLiteBlockInfoDriver::_init() {
string dbname = tryGetParam<string>("db", "");
return !(dbname == "");
}

void SQLiteBlockInfoDriver::load() {
DBConnectPtr SQLiteBlockInfoDriver::getConnect() {
string dbname = tryGetParam<string>("db", "");
HKU_ERROR_IF_RETURN(dbname == "", void(), "Can't get Sqlite3 filename!");
HKU_CHECK(!dbname.empty(), "Can't get Sqlite3 filename!");
HKU_TRACE("SQLITE3: {}", dbname);
return std::make_shared<SQLiteConnect>(m_params);
}

SQLiteConnect connect(m_params);
vector<SQLiteBlockTable> records;
connect.batchLoadView(records,
"select a.id, a.category, a.name, a.market_code, b.market_code as "
"index_code from block a left "
"join BlockIndex b on a.category=b.category and a.name = b.name");

void SQLiteBlockInfoDriver::load() {
vector<SQLiteBlockView> records;
auto connect = getConnect();
connect->batchLoadView(records,
"select a.id, a.category, a.name, a.market_code, b.market_code as "
"index_code from block a left "
"join BlockIndex b on a.category=b.category and a.name = b.name");

std::unique_lock<std::shared_mutex> lock(m_buffer_mutex);
for (auto& record : records) {
auto category_iter = m_buffer.find(record.category);
if (category_iter == m_buffer.end()) {
Expand All @@ -54,6 +72,7 @@ void SQLiteBlockInfoDriver::load() {

Block SQLiteBlockInfoDriver::getBlock(const string& category, const string& name) {
Block ret;
std::shared_lock<std::shared_mutex> lock(m_buffer_mutex);
auto category_iter = m_buffer.find(category);
HKU_IF_RETURN(category_iter == m_buffer.end(), ret);

Expand All @@ -66,6 +85,7 @@ Block SQLiteBlockInfoDriver::getBlock(const string& category, const string& name

BlockList SQLiteBlockInfoDriver::getBlockList(const string& category) {
BlockList ret;
std::shared_lock<std::shared_mutex> lock(m_buffer_mutex);
auto category_iter = m_buffer.find(category);
HKU_IF_RETURN(category_iter == m_buffer.end(), ret);

Expand All @@ -79,6 +99,7 @@ BlockList SQLiteBlockInfoDriver::getBlockList(const string& category) {

BlockList SQLiteBlockInfoDriver::getBlockList() {
BlockList ret;
std::shared_lock<std::shared_mutex> lock(m_buffer_mutex);
for (auto category_iter = m_buffer.begin(); category_iter != m_buffer.end(); ++category_iter) {
const auto& category_blocks = category_iter->second;
for (auto iter = category_blocks.begin(); iter != category_blocks.end(); ++iter) {
Expand All @@ -89,11 +110,55 @@ BlockList SQLiteBlockInfoDriver::getBlockList() {
}

void SQLiteBlockInfoDriver::save(const Block& block) {
HKU_THROW("Not support save block info!");
std::unique_lock<std::shared_mutex> lock(m_buffer_mutex);
auto category_iter = m_buffer.find(block.category());
if (category_iter == m_buffer.end()) {
m_buffer.emplace(block.category(), unordered_map<string, Block>{{block.name(), block}});
} else {
category_iter->second.emplace(block.name(), block);
}

auto connect = getConnect();
AutoTransAction trans(connect);
auto condition = (Field("category") == block.category()) & (Field("name") == block.name());
connect->remove(SQLiteBlockView::getTableName(), condition, false);
connect->remove(SQLiteBlockIndexTable::getTableName(), condition, false);

if (!block.getIndexStock().isNull()) {
SQLiteBlockIndexTable index;
index.category = block.category();
index.name = block.name();
index.market_code = block.getIndexStock().market_code();
connect->save(index, false);
}

for (auto iter = block.begin(); iter != block.end(); ++iter) {
SQLiteBlockTable record;
record.category = block.category();
record.name = block.name();
record.market_code = iter->market_code();
connect->save(record, false);
}
}

void SQLiteBlockInfoDriver::remove(const string& category, const string& name) {
HKU_THROW("Not support save block info!");
{
auto connect = getConnect();
AutoTransAction trans(connect);
auto condition = (Field("category") == category) & (Field("name") == name);
connect->remove(SQLiteBlockTable::getTableName(), condition, false);
connect->remove(SQLiteBlockIndexTable::getTableName(), condition, false);
}

std::unique_lock<std::shared_mutex> lock(m_buffer_mutex);
auto category_iter = m_buffer.find(category);
HKU_IF_RETURN(category_iter == m_buffer.end(), void());

auto block_iter = category_iter->second.find(name);
HKU_IF_RETURN(block_iter == category_iter->second.end(), void());

category_iter->second.erase(block_iter);
m_buffer.erase(category_iter);
}

} // namespace hku
Loading
Loading