已合并
hash key 偶现db存储乱码 #412
wangzixuan创建于 18 天前
hash key 偶现db存储乱码 #412
已合并
共 2 个文件变更+65-92
| @@ -15,8 +15,10 @@ | |||
| 15 | * -------------------------------------------------------------------------*/ | 15 | * -------------------------------------------------------------------------*/ |
| 16 | 16 | ||
| 17 | 17 | ||
| 18 | -namespace Analysis { | 18 | +namespace Analysis |
| 19 | -namespace Domain { | 19 | +{ |
| 20 | +namespace Domain | ||
| 21 | +{ | ||
| 20 | HashDBDumper::HashDBDumper(const std::string &hostFilePath) : BaseDumper<HashDBDumper>(hostFilePath, "GeHashInfo") | 22 | HashDBDumper::HashDBDumper(const std::string &hostFilePath) : BaseDumper<HashDBDumper>(hostFilePath, "GeHashInfo") |
| 21 | { | 23 | { |
| 22 | MAKE_SHARED0_NO_OPERATION(database_, HashDB); | 24 | MAKE_SHARED0_NO_OPERATION(database_, HashDB); |
| @@ -25,14 +27,16 @@ HashDBDumper::HashDBDumper(const std::string &hostFilePath) : BaseDumper<HashDBD | |||
| 25 | std::vector<std::tuple<std::string, std::string>> HashDBDumper::GenerateData(const HashDataMap &hashData) | 27 | std::vector<std::tuple<std::string, std::string>> HashDBDumper::GenerateData(const HashDataMap &hashData) |
| 26 | { | 28 | { |
| 27 | std::vector<std::tuple<std::string, std::string>> data; | 29 | std::vector<std::tuple<std::string, std::string>> data; |
| 28 | - if (!Utils::Reserve(data, hashData.size())) { | 30 | + if (!Utils::Reserve(data, hashData.size())) |
| 31 | + { | ||
| 29 | ERROR("Can't reserve vector"); | 32 | ERROR("Can't reserve vector"); |
| 30 | return data; | 33 | return data; |
| 31 | } | 34 | } |
| 32 | - for (auto &pair: hashData) { | 35 | + for (auto &pair : hashData) |
| 36 | + { | ||
| 33 | data.emplace_back(std::to_string(pair.first), pair.second); | 37 | data.emplace_back(std::to_string(pair.first), pair.second); |
| 34 | } | 38 | } |
| 35 | return data; | 39 | return data; |
| 36 | } | 40 | } |
| 37 | -} // Domain | 41 | +} // namespace Domain |
| 38 | -} // Analysis | 42 | +} // namespace Analysis |
| @@ -17,32 +17,40 @@ | |||
| 17 | 17 | ||
| 18 | 18 | ||
| 19 | 19 | ||
| 20 | + | ||
| 20 | 21 | ||
| 22 | + | ||
| 21 | 23 | ||
| 22 | 24 | ||
| 23 | -namespace Analysis { | 25 | +namespace Analysis |
| 24 | -namespace Infra { | 26 | +{ |
| 27 | +namespace Infra | ||
| 28 | +{ | ||
| 25 | using namespace Analysis::Utils; | 29 | using namespace Analysis::Utils; |
| 26 | using namespace Analysis; | 30 | using namespace Analysis; |
| 27 | 31 | ||
| 28 | -namespace { | 32 | +namespace |
| 33 | +{ | ||
| 29 | const std::string CREATE_TABLE = "create table"; | 34 | const std::string CREATE_TABLE = "create table"; |
| 30 | const std::string CREATE_INDEX = "create index"; | 35 | const std::string CREATE_INDEX = "create index"; |
| 31 | const std::string DROP_TABLE = "drop table"; | 36 | const std::string DROP_TABLE = "drop table"; |
| 32 | const std::string DELETE = "delete"; | 37 | const std::string DELETE = "delete"; |
| 33 | const std::string UPDATE = "update"; | 38 | const std::string UPDATE = "update"; |
| 34 | const std::string CHECK = "check"; | 39 | const std::string CHECK = "check"; |
| 35 | -} | 40 | +} // namespace |
| 36 | 41 | ||
| 37 | Connection::Connection(const std::string &path) | 42 | Connection::Connection(const std::string &path) |
| 38 | { | 43 | { |
| 39 | - int rc = sqlite3_open(path.c_str(), &db_); // 连接数据库 | 44 | + int rc = sqlite3_open(path.c_str(), &db_); // 连接数据库 |
| 40 | - if (rc != SQLITE_OK) { | 45 | + if (rc != SQLITE_OK) |
| 46 | + { | ||
| 41 | std::string errorMsg = "Failed to open database: " + std::string(sqlite3_errmsg(db_)); | 47 | std::string errorMsg = "Failed to open database: " + std::string(sqlite3_errmsg(db_)); |
| 42 | ERROR("sqlite3_exec return %, %", rc, errorMsg); | 48 | ERROR("sqlite3_exec return %, %", rc, errorMsg); |
| 43 | sqlite3_close_v2(db_); | 49 | sqlite3_close_v2(db_); |
| 44 | db_ = nullptr; | 50 | db_ = nullptr; |
| 45 | - } else { | 51 | + } |
| 52 | + else | ||
| 53 | + { | ||
| 46 | sqlite3_exec(db_, "PRAGMA synchronous = OFF;", nullptr, nullptr, nullptr); | 54 | sqlite3_exec(db_, "PRAGMA synchronous = OFF;", nullptr, nullptr, nullptr); |
| 47 | } | 55 | } |
| 48 | } | 56 | } |
| @@ -51,9 +59,11 @@ Connection::~Connection() | |||
| 51 | { | 59 | { |
| 52 | // 先释放sqlite3_stmt,再释放sqlite3 | 60 | // 先释放sqlite3_stmt,再释放sqlite3 |
| 53 | FinalizeStmt(); | 61 | FinalizeStmt(); |
| 54 | - if (db_) { | 62 | + if (db_) |
| 63 | + { | ||
| 55 | int rc = sqlite3_close(db_); | 64 | int rc = sqlite3_close(db_); |
| 56 | - if (rc != SQLITE_OK) { | 65 | + if (rc != SQLITE_OK) |
| 66 | + { | ||
| 57 | ERROR("First close failed, err: % !", sqlite3_errmsg(db_)); | 67 | ERROR("First close failed, err: % !", sqlite3_errmsg(db_)); |
| 58 | sqlite3_close_v2(db_); | 68 | sqlite3_close_v2(db_); |
| 59 | } | 69 | } |
| @@ -66,8 +76,9 @@ bool Connection::ExecuteSql(const std::string &sql, const std::string &sqlType) | |||
| 66 | CHAR_PTR errMsg = nullptr; | 76 | CHAR_PTR errMsg = nullptr; |
| 67 | sqlite3_busy_timeout(db_, TIMEOUT); | 77 | sqlite3_busy_timeout(db_, TIMEOUT); |
| 68 | int rc = sqlite3_exec(db_, sql.c_str(), nullptr, nullptr, &errMsg); | 78 | int rc = sqlite3_exec(db_, sql.c_str(), nullptr, nullptr, &errMsg); |
| 69 | - if (rc != SQLITE_OK) { | 79 | + if (rc != SQLITE_OK) |
| 70 | - std::string errorMsg = "Failed to " + sqlType +": " + std::string(errMsg); | 80 | + { |
| 81 | + std::string errorMsg = "Failed to " + sqlType + ": " + std::string(errMsg); | ||
| 71 | ERROR("sqlite3_exec return %, %", rc, errorMsg); | 82 | ERROR("sqlite3_exec return %, %", rc, errorMsg); |
| 72 | sqlite3_free(errMsg); | 83 | sqlite3_free(errMsg); |
| 73 | return false; | 84 | return false; |
| @@ -82,45 +93,32 @@ bool Connection::CheckTableExists(const std::string &tableName) | |||
| 82 | return ExecuteQuery(sql, result) && !result.empty(); | 93 | return ExecuteQuery(sql, result) && !result.empty(); |
| 83 | } | 94 | } |
| 84 | 95 | ||
| 85 | -bool Connection::ExecuteCreateTable(const std::string &sql) | 96 | +bool Connection::ExecuteCreateTable(const std::string &sql) { return ExecuteSql(sql, CREATE_TABLE); } |
| 86 | -{ | ||
| 87 | - return ExecuteSql(sql, CREATE_TABLE); | ||
| 88 | -} | ||
| 89 | 97 | ||
| 90 | -bool Connection::ExecuteCreateIndex(const std::string &sql) | 98 | +bool Connection::ExecuteCreateIndex(const std::string &sql) { return ExecuteSql(sql, CREATE_INDEX); } |
| 91 | -{ | ||
| 92 | - return ExecuteSql(sql, CREATE_INDEX); | ||
| 93 | -} | ||
| 94 | 99 | ||
| 95 | -bool Connection::ExecuteDropTable(const std::string &sql) | 100 | +bool Connection::ExecuteDropTable(const std::string &sql) { return ExecuteSql(sql, DROP_TABLE); } |
| 96 | -{ | ||
| 97 | - return ExecuteSql(sql, DROP_TABLE); | ||
| 98 | -} | ||
| 99 | 101 | ||
| 100 | -bool Connection::ExecuteDelete(const std::string &sql) | 102 | +bool Connection::ExecuteDelete(const std::string &sql) { return ExecuteSql(sql, DELETE); } |
| 101 | -{ | ||
| 102 | - return ExecuteSql(sql, DELETE); | ||
| 103 | -} | ||
| 104 | 103 | ||
| 105 | -bool Connection::ExecuteUpdate(const std::string &sql) | 104 | +bool Connection::ExecuteUpdate(const std::string &sql) { return ExecuteSql(sql, UPDATE); } |
| 106 | -{ | ||
| 107 | - return ExecuteSql(sql, UPDATE); | ||
| 108 | -} | ||
| 109 | 105 | ||
| 110 | std::vector<TableColumn> Connection::ExecuteGetTableColumns(const std::string &tableName) | 106 | std::vector<TableColumn> Connection::ExecuteGetTableColumns(const std::string &tableName) |
| 111 | { | 107 | { |
| 112 | - std::vector <TableColumn> cols; | 108 | + std::vector<TableColumn> cols; |
| 113 | std::string sql = "PRAGMA table_info(" + tableName + ");"; | 109 | std::string sql = "PRAGMA table_info(" + tableName + ");"; |
| 114 | sqlite3_busy_timeout(db_, TIMEOUT); | 110 | sqlite3_busy_timeout(db_, TIMEOUT); |
| 115 | // prepare前需要保证stmt是nullptr | 111 | // prepare前需要保证stmt是nullptr |
| 116 | FinalizeStmt(); | 112 | FinalizeStmt(); |
| 117 | bool rc = sqlite3_prepare_v2(db_, sql.c_str(), -1, &stmt_, nullptr); | 113 | bool rc = sqlite3_prepare_v2(db_, sql.c_str(), -1, &stmt_, nullptr); |
| 118 | - if (rc != SQLITE_OK) { | 114 | + if (rc != SQLITE_OK) |
| 115 | + { | ||
| 119 | std::string errorMsg = std::string(sqlite3_errmsg(db_)); | 116 | std::string errorMsg = std::string(sqlite3_errmsg(db_)); |
| 120 | ERROR("% failed", sql); | 117 | ERROR("% failed", sql); |
| 121 | return cols; | 118 | return cols; |
| 122 | } | 119 | } |
| 123 | - while (sqlite3_step(stmt_) == SQLITE_ROW) { | 120 | + while (sqlite3_step(stmt_) == SQLITE_ROW) |
| 121 | + { | ||
| 124 | std::string name; | 122 | std::string name; |
| 125 | std::string type; | 123 | std::string type; |
| 126 | GetColumn(name); | 124 | GetColumn(name); |
| @@ -134,7 +132,8 @@ std::vector<TableColumn> Connection::ExecuteGetTableColumns(const std::string &t | |||
| 134 | bool Connection::InsertCmd(const std::string &tableName, const uint32_t &colNum) | 132 | bool Connection::InsertCmd(const std::string &tableName, const uint32_t &colNum) |
| 135 | { | 133 | { |
| 136 | std::string valueStr; | 134 | std::string valueStr; |
| 137 | - for (uint32_t i = 0; i < colNum - 1; ++i) { | 135 | + for (uint32_t i = 0; i < colNum - 1; ++i) |
| 136 | + { | ||
| 138 | valueStr += "?,"; | 137 | valueStr += "?,"; |
| 139 | } | 138 | } |
| 140 | valueStr = "(" + valueStr + "?)"; | 139 | valueStr = "(" + valueStr + "?)"; |
| @@ -143,7 +142,8 @@ bool Connection::InsertCmd(const std::string &tableName, const uint32_t &colNum) | |||
| 143 | // prepare前需要保证stmt是nullptr | 142 | // prepare前需要保证stmt是nullptr |
| 144 | FinalizeStmt(); | 143 | FinalizeStmt(); |
| 145 | bool rc = sqlite3_prepare_v2(db_, sql.c_str(), -1, &stmt_, nullptr); | 144 | bool rc = sqlite3_prepare_v2(db_, sql.c_str(), -1, &stmt_, nullptr); |
| 146 | - if (rc != SQLITE_OK) { | 145 | + if (rc != SQLITE_OK) |
| 146 | + { | ||
| 147 | std::string errorMsg = std::string(sqlite3_errmsg(db_)); | 147 | std::string errorMsg = std::string(sqlite3_errmsg(db_)); |
| 148 | ERROR("sqlite3_prepare_v2 run failed: %", errorMsg); | 148 | ERROR("sqlite3_prepare_v2 run failed: %", errorMsg); |
| 149 | return false; | 149 | return false; |
| @@ -156,7 +156,8 @@ bool Connection::QueryCmd(const std::string &sql) | |||
| 156 | // prepare前需要保证stmt是nullptr | 156 | // prepare前需要保证stmt是nullptr |
| 157 | FinalizeStmt(); | 157 | FinalizeStmt(); |
| 158 | int rc = sqlite3_prepare_v2(db_, sql.c_str(), -1, &stmt_, nullptr); | 158 | int rc = sqlite3_prepare_v2(db_, sql.c_str(), -1, &stmt_, nullptr); |
| 159 | - if (rc != SQLITE_OK) { | 159 | + if (rc != SQLITE_OK) |
| 160 | + { | ||
| 160 | std::string errorMsg = "Failed to bind parameter: " + std::string(sqlite3_errmsg(db_)); | 161 | std::string errorMsg = "Failed to bind parameter: " + std::string(sqlite3_errmsg(db_)); |
| 161 | ERROR("sqlite3_prepare_v2 run failed: %", errorMsg); | 162 | ERROR("sqlite3_prepare_v2 run failed: %", errorMsg); |
| 162 | return false; | 163 | return false; |
| @@ -166,76 +167,44 @@ bool Connection::QueryCmd(const std::string &sql) | |||
| 166 | 167 | ||
| 167 | void Connection::FinalizeStmt() | 168 | void Connection::FinalizeStmt() |
| 168 | { | 169 | { |
| 169 | - if (stmt_) { | 170 | + if (stmt_) |
| 171 | + { | ||
| 170 | sqlite3_finalize(stmt_); | 172 | sqlite3_finalize(stmt_); |
| 171 | stmt_ = nullptr; | 173 | stmt_ = nullptr; |
| 172 | } | 174 | } |
| 173 | } | 175 | } |
| 174 | 176 | ||
| 175 | -void Connection::BindParameters(int64_t value) | 177 | +void Connection::BindParameters(int64_t value) { sqlite3_bind_int64(stmt_, ++index_, value); } |
| 176 | -{ | ||
| 177 | - sqlite3_bind_int64(stmt_, ++index_, value); | ||
| 178 | -} | ||
| 179 | 178 | ||
| 180 | -void Connection::BindParameters(uint64_t value) | 179 | +void Connection::BindParameters(uint64_t value) { sqlite3_bind_int64(stmt_, ++index_, value); } |
| 181 | -{ | ||
| 182 | - sqlite3_bind_int64(stmt_, ++index_, value); | ||
| 183 | -} | ||
| 184 | 180 | ||
| 185 | -void Connection::BindParameters(int32_t value) | 181 | +void Connection::BindParameters(int32_t value) { sqlite3_bind_int(stmt_, ++index_, value); } |
| 186 | -{ | ||
| 187 | - sqlite3_bind_int(stmt_, ++index_, value); | ||
| 188 | -} | ||
| 189 | 182 | ||
| 190 | -void Connection::BindParameters(uint32_t value) | 183 | +void Connection::BindParameters(uint32_t value) { sqlite3_bind_int64(stmt_, ++index_, value); } |
| 191 | -{ | ||
| 192 | - sqlite3_bind_int64(stmt_, ++index_, value); | ||
| 193 | -} | ||
| 194 | 184 | ||
| 195 | -void Connection::BindParameters(double value) | 185 | +void Connection::BindParameters(double value) { sqlite3_bind_double(stmt_, ++index_, value); } |
| 196 | -{ | ||
| 197 | - sqlite3_bind_double(stmt_, ++index_, value); | ||
| 198 | -} | ||
| 199 | 186 | ||
| 200 | void Connection::BindParameters(std::string value) | 187 | void Connection::BindParameters(std::string value) |
| 201 | { | 188 | { |
| 202 | - sqlite3_bind_text(stmt_, ++index_, value.c_str(), -1, nullptr); | 189 | + sqlite3_bind_text(stmt_, ++index_, value.c_str(), -1, SQLITE_TRANSIENT); |
| 203 | } | 190 | } |
| 204 | 191 | ||
| 205 | -void Connection::GetColumn(int64_t &value) | 192 | +void Connection::GetColumn(int64_t &value) { value = sqlite3_column_int64(stmt_, ++index_); } |
| 206 | -{ | ||
| 207 | - value = sqlite3_column_int64(stmt_, ++index_); | ||
| 208 | -} | ||
| 209 | 193 | ||
| 210 | -void Connection::GetColumn(uint64_t &value) | 194 | +void Connection::GetColumn(uint64_t &value) { value = static_cast<uint64_t>(sqlite3_column_int64(stmt_, ++index_)); } |
| 211 | -{ | ||
| 212 | - value = static_cast<uint64_t>(sqlite3_column_int64(stmt_, ++index_)); | ||
| 213 | -} | ||
| 214 | 195 | ||
| 215 | -void Connection::GetColumn(int32_t &value) | 196 | +void Connection::GetColumn(int32_t &value) { value = sqlite3_column_int(stmt_, ++index_); } |
| 216 | -{ | ||
| 217 | - value = sqlite3_column_int(stmt_, ++index_); | ||
| 218 | -} | ||
| 219 | 197 | ||
| 220 | -void Connection::GetColumn(uint32_t &value) | 198 | +void Connection::GetColumn(uint32_t &value) { value = static_cast<uint32_t>(sqlite3_column_int64(stmt_, ++index_)); } |
| 221 | -{ | ||
| 222 | - value = static_cast<uint32_t>(sqlite3_column_int64(stmt_, ++index_)); | ||
| 223 | -} | ||
| 224 | 199 | ||
| 225 | -void Connection::GetColumn(double &value) | 200 | +void Connection::GetColumn(double &value) { value = sqlite3_column_double(stmt_, ++index_); } |
| 226 | -{ | ||
| 227 | - value = sqlite3_column_double(stmt_, ++index_); | ||
| 228 | -} | ||
| 229 | 201 | ||
| 230 | void Connection::GetColumn(std::string &value) | 202 | void Connection::GetColumn(std::string &value) |
| 231 | { | 203 | { |
| 232 | - auto valueStr = const_cast<unsigned char*>(sqlite3_column_text(stmt_, ++index_)); | 204 | + auto valueStr = const_cast<unsigned char *>(sqlite3_column_text(stmt_, ++index_)); |
| 233 | value = std::string(ReinterpretConvert<CHAR_PTR>(valueStr)); | 205 | value = std::string(ReinterpretConvert<CHAR_PTR>(valueStr)); |
| 234 | } | 206 | } |
| 235 | 207 | ||
| 236 | -void Connection::GetColumn(uint16_t &value) | 208 | +void Connection::GetColumn(uint16_t &value) { value = sqlite3_column_int(stmt_, ++index_); } |
| 237 | -{ | 209 | +} // namespace Infra |
| 238 | - value = sqlite3_column_int(stmt_, ++index_); | 210 | +} // namespace Analysis |
| 239 | -} | ||
| 240 | -} // Infra | ||
| 241 | -} // Analysis | ||