5
5
* Author: fasiondog
6
6
*/
7
7
8
+ #include " hikyuu/utilities/db_connect/DBConnect.h"
8
9
#include " hikyuu/utilities/db_connect/sqlite/SQLiteConnect.h"
9
- #include " hikyuu/utilities/db_connect/TableMacro.h"
10
10
#include " SQLiteBlockInfoDriver.h"
11
11
12
12
namespace hku {
13
13
14
- struct SQLiteBlockTable {
15
- TABLE_BIND4 (SQLiteBlockTable , block, category, name, market_code, index_code)
14
+ struct SQLiteBlockView {
15
+ TABLE_BIND4 (SQLiteBlockView , block, category, name, market_code, index_code)
16
16
string category;
17
17
string name;
18
18
string market_code;
19
19
string index_code;
20
20
};
21
21
22
+ struct SQLiteBlockTable {
23
+ TABLE_BIND3 (SQLiteBlockTable, block, category, name, market_code)
24
+ string category;
25
+ string name;
26
+ string market_code;
27
+ };
28
+
29
+ struct SQLiteBlockIndexTable {
30
+ TABLE_BIND3 (SQLiteBlockIndexTable, BlockIndex, category, name, market_code)
31
+ string category;
32
+ string name;
33
+ string market_code;
34
+ };
35
+
22
36
SQLiteBlockInfoDriver::~SQLiteBlockInfoDriver () {}
23
37
24
38
bool SQLiteBlockInfoDriver::_init () {
25
39
string dbname = tryGetParam<string>(" db" , " " );
26
40
return !(dbname == " " );
27
41
}
28
42
29
- void SQLiteBlockInfoDriver::load () {
43
+ DBConnectPtr SQLiteBlockInfoDriver::getConnect () {
30
44
string dbname = tryGetParam<string>(" db" , " " );
31
- HKU_ERROR_IF_RETURN ( dbname == " " , void (), " Can't get Sqlite3 filename!" );
45
+ HKU_CHECK (! dbname. empty (), " Can't get Sqlite3 filename!" );
32
46
HKU_TRACE (" SQLITE3: {}" , dbname);
47
+ return std::make_shared<SQLiteConnect>(m_params);
48
+ }
33
49
34
- SQLiteConnect connect (m_params);
35
- vector<SQLiteBlockTable> records;
36
- connect.batchLoadView (records,
37
- " select a.id, a.category, a.name, a.market_code, b.market_code as "
38
- " index_code from block a left "
39
- " join BlockIndex b on a.category=b.category and a.name = b.name" );
40
-
50
+ void SQLiteBlockInfoDriver::load () {
51
+ vector<SQLiteBlockView> records;
52
+ auto connect = getConnect ();
53
+ connect->batchLoadView (records,
54
+ " select a.id, a.category, a.name, a.market_code, b.market_code as "
55
+ " index_code from block a left "
56
+ " join BlockIndex b on a.category=b.category and a.name = b.name" );
57
+
58
+ std::unique_lock<std::shared_mutex> lock (m_buffer_mutex);
41
59
for (auto & record : records) {
42
60
auto category_iter = m_buffer.find (record.category );
43
61
if (category_iter == m_buffer.end ()) {
@@ -54,6 +72,7 @@ void SQLiteBlockInfoDriver::load() {
54
72
55
73
Block SQLiteBlockInfoDriver::getBlock (const string& category, const string& name) {
56
74
Block ret;
75
+ std::shared_lock<std::shared_mutex> lock (m_buffer_mutex);
57
76
auto category_iter = m_buffer.find (category);
58
77
HKU_IF_RETURN (category_iter == m_buffer.end (), ret);
59
78
@@ -66,6 +85,7 @@ Block SQLiteBlockInfoDriver::getBlock(const string& category, const string& name
66
85
67
86
BlockList SQLiteBlockInfoDriver::getBlockList (const string& category) {
68
87
BlockList ret;
88
+ std::shared_lock<std::shared_mutex> lock (m_buffer_mutex);
69
89
auto category_iter = m_buffer.find (category);
70
90
HKU_IF_RETURN (category_iter == m_buffer.end (), ret);
71
91
@@ -79,6 +99,7 @@ BlockList SQLiteBlockInfoDriver::getBlockList(const string& category) {
79
99
80
100
BlockList SQLiteBlockInfoDriver::getBlockList () {
81
101
BlockList ret;
102
+ std::shared_lock<std::shared_mutex> lock (m_buffer_mutex);
82
103
for (auto category_iter = m_buffer.begin (); category_iter != m_buffer.end (); ++category_iter) {
83
104
const auto & category_blocks = category_iter->second ;
84
105
for (auto iter = category_blocks.begin (); iter != category_blocks.end (); ++iter) {
@@ -89,11 +110,55 @@ BlockList SQLiteBlockInfoDriver::getBlockList() {
89
110
}
90
111
91
112
void SQLiteBlockInfoDriver::save (const Block& block) {
92
- HKU_THROW (" Not support save block info!" );
113
+ std::unique_lock<std::shared_mutex> lock (m_buffer_mutex);
114
+ auto category_iter = m_buffer.find (block.category ());
115
+ if (category_iter == m_buffer.end ()) {
116
+ m_buffer.emplace (block.category (), unordered_map<string, Block>{{block.name (), block}});
117
+ } else {
118
+ category_iter->second .emplace (block.name (), block);
119
+ }
120
+
121
+ auto connect = getConnect ();
122
+ AutoTransAction trans (connect);
123
+ auto condition = (Field (" category" ) == block.category ()) & (Field (" name" ) == block.name ());
124
+ connect->remove (SQLiteBlockView::getTableName (), condition, false );
125
+ connect->remove (SQLiteBlockIndexTable::getTableName (), condition, false );
126
+
127
+ if (!block.getIndexStock ().isNull ()) {
128
+ SQLiteBlockIndexTable index ;
129
+ index .category = block.category ();
130
+ index .name = block.name ();
131
+ index .market_code = block.getIndexStock ().market_code ();
132
+ connect->save (index , false );
133
+ }
134
+
135
+ for (auto iter = block.begin (); iter != block.end (); ++iter) {
136
+ SQLiteBlockTable record;
137
+ record.category = block.category ();
138
+ record.name = block.name ();
139
+ record.market_code = iter->market_code ();
140
+ connect->save (record, false );
141
+ }
93
142
}
94
143
95
144
void SQLiteBlockInfoDriver::remove (const string& category, const string& name) {
96
- HKU_THROW (" Not support save block info!" );
145
+ {
146
+ auto connect = getConnect ();
147
+ AutoTransAction trans (connect);
148
+ auto condition = (Field (" category" ) == category) & (Field (" name" ) == name);
149
+ connect->remove (SQLiteBlockTable::getTableName (), condition, false );
150
+ connect->remove (SQLiteBlockIndexTable::getTableName (), condition, false );
151
+ }
152
+
153
+ std::unique_lock<std::shared_mutex> lock (m_buffer_mutex);
154
+ auto category_iter = m_buffer.find (category);
155
+ HKU_IF_RETURN (category_iter == m_buffer.end (), void ());
156
+
157
+ auto block_iter = category_iter->second .find (name);
158
+ HKU_IF_RETURN (block_iter == category_iter->second .end (), void ());
159
+
160
+ category_iter->second .erase (block_iter);
161
+ m_buffer.erase (category_iter);
97
162
}
98
163
99
164
} // namespace hku
0 commit comments