|
@@ -46,21 +46,21 @@ int ProtoParserPrivate::open(const char *pwd)
|
|
|
FS.mount(NODEFS, {root : '.'}, '/data'););
|
|
|
*/
|
|
|
#ifdef DUMP
|
|
|
- MqttLog(MQTT_LOG_INFO, "open database: hcdchat.db");
|
|
|
+ MqttLog(LOG_INFO, LOG_TYPE_DB, "open database: hcdchat.db");
|
|
|
#endif
|
|
|
int ret = sqlite3_open("/data/hcdchat.db", &_db);
|
|
|
if (ret != SQLITE_OK)
|
|
|
ret = sqlite3_open("hcdchat.db", &_db);
|
|
|
if (ret != SQLITE_OK)
|
|
|
{
|
|
|
- MqttLog(MQTT_LOG_ERROR, "open database failed(%d): %s\n", ret, sqlite3_errmsg(_db));
|
|
|
+ MqttLog(LOG_ERROR, LOG_TYPE_DB, "open database failed(%d): %s\n", ret, sqlite3_errmsg(_db));
|
|
|
return ret;
|
|
|
}
|
|
|
sqlite3_key(_db, pwd, strlen(pwd));
|
|
|
ret = check();
|
|
|
if (ret != SQLITE_OK)
|
|
|
{
|
|
|
- MqttLog(MQTT_LOG_ERROR, "check database failed(%d): %s\n", ret, sqlite3_errmsg(_db));
|
|
|
+ MqttLog(LOG_ERROR, LOG_TYPE_DB, "check database failed(%d): %s\n", ret, sqlite3_errmsg(_db));
|
|
|
return ret;
|
|
|
}
|
|
|
sqlite3_busy_timeout(_db, 3000);
|
|
@@ -78,7 +78,7 @@ int ProtoParserPrivate::check()
|
|
|
ret = sqlite3_exec(_db, sql, nullptr, nullptr, nullptr);
|
|
|
if (ret != 0)
|
|
|
{
|
|
|
- MqttLog(MQTT_LOG_ERROR, "create table t_user_messages failed(%d): %s\n", ret, sqlite3_errmsg(_db));
|
|
|
+ MqttLog(LOG_ERROR, LOG_TYPE_DB, "create table t_user_messages failed(%d): %s\n", ret, sqlite3_errmsg(_db));
|
|
|
return ret;
|
|
|
}
|
|
|
|
|
@@ -86,7 +86,7 @@ int ProtoParserPrivate::check()
|
|
|
ret = sqlite3_exec(_db, sql, nullptr, nullptr, nullptr);
|
|
|
if (ret != 0)
|
|
|
{
|
|
|
- MqttLog(MQTT_LOG_ERROR, "create table t_messages failed(%d): %s\n", ret, sqlite3_errmsg(_db));
|
|
|
+ MqttLog(LOG_ERROR, LOG_TYPE_DB, "create table t_messages failed(%d): %s\n", ret, sqlite3_errmsg(_db));
|
|
|
return ret;
|
|
|
}
|
|
|
|
|
@@ -294,7 +294,7 @@ void ProtoParserPrivate::recallMessage(uint64_t mid)
|
|
|
// TODO:
|
|
|
#else
|
|
|
bool found = false;
|
|
|
- for (auto it = _messages.begin(); it != _messages.end() ; it++)
|
|
|
+ for (auto it = _messages.begin(); it != _messages.end(); it++)
|
|
|
{
|
|
|
if (it->message_id() == mid)
|
|
|
{
|
|
@@ -311,7 +311,7 @@ void ProtoParserPrivate::recallMessage(uint64_t mid)
|
|
|
{
|
|
|
MqttLog(LOG_WARN, LOG_TYPE_FUNC, "recall can't find message with id: %lld", mid);
|
|
|
}
|
|
|
- #endif
|
|
|
+#endif
|
|
|
}
|
|
|
|
|
|
void ProtoParserPrivate::sendMessage(Message &msg, uint32_t msgId)
|
|
@@ -334,6 +334,8 @@ void ProtoParserPrivate::sendMessage(Message &msg, uint32_t msgId)
|
|
|
int ProtoParserPrivate::getMessage(uint64_t id, std::function<bool(const Message &msg)> cb)
|
|
|
{
|
|
|
#ifdef USE_SQLITE3
|
|
|
+ // TODO:
|
|
|
+ return 0;
|
|
|
#else
|
|
|
for (auto &msg : _messages)
|
|
|
{
|
|
@@ -354,7 +356,7 @@ int ProtoParserPrivate::getMessage(bool before, uint64_t idx, int count, int typ
|
|
|
int cnt = 0;
|
|
|
#ifdef USE_SQLITE3
|
|
|
#if defined DUMP || defined DUMP_MESSAGE
|
|
|
- MqttLog(MQTT_LOG_INFO, "getMessage, before: %d, idx: %lld, count: %d, type: %d, line: %d, target: %s", before, idx, count, type, line, target);
|
|
|
+ MqttLog(LOG_INFO, LOG_TYPE_FUNC, "getMessage, before: %d, idx: %lld, count: %d, type: %d, line: %d, target: %s", before, idx, count, type, line, target);
|
|
|
#endif
|
|
|
std::string sql = "select _mid, _from, _type, _target, _line, _data, _searchable_key, _dt, _content_type, _to from t_messages where _type = ? and _line = ? and _target = ?";
|
|
|
if (idx == 0)
|
|
@@ -375,7 +377,7 @@ int ProtoParserPrivate::getMessage(bool before, uint64_t idx, int count, int typ
|
|
|
Message msg;
|
|
|
toMessage(stmt, msg);
|
|
|
#if defined DUMP || defined DUMP_MESSAGE
|
|
|
- MqttLog(MQTT_LOG_INFO, "getMessage: %lld", msg.message_id());
|
|
|
+ MqttLog(LOG_INFO, LOG_TYPE_DB, "getMessage: %lld", msg.message_id());
|
|
|
#endif
|
|
|
|
|
|
if(!cb(msg))
|
|
@@ -503,7 +505,7 @@ int ProtoParserPrivate::getConversation(const char *keyword, const char *types,
|
|
|
str2[str2.length() - 1] = ')';
|
|
|
|
|
|
#if defined DUMP || defined DUMP_MESSAGE
|
|
|
- MqttLog(MQTT_LOG_INFO, "getConversation, types: %s, lines: %s, keyword: %s", types, lines, keyword == nullptr ? "" : keyword);
|
|
|
+ MqttLog(LOG_INFO, LOG_TYPE_DB, "getConversation, types: %s, lines: %s, keyword: %s", types, lines, keyword == nullptr ? "" : keyword);
|
|
|
#endif
|
|
|
std::string sql = "select _mid, _from, _type, _target, _line, _data, _searchable_key, _dt, _content_type, _to from (select * from t_messages where _type in " + str1 + " and _line in " + str2 + " ";
|
|
|
if (keyword != nullptr && strlen(keyword) > 0)
|
|
@@ -517,7 +519,7 @@ int ProtoParserPrivate::getConversation(const char *keyword, const char *types,
|
|
|
toMessage(stmt, msg);
|
|
|
|
|
|
#if defined DUMP || defined DUMP_MESSAGE
|
|
|
- MqttLog(MQTT_LOG_INFO, "getConversation: %lld", msg.message_id());
|
|
|
+ MqttLog(LOG_INFO, LOG_TYPE_DB ,"getConversation: %lld", msg.message_id());
|
|
|
#endif
|
|
|
|
|
|
if (!cb(msg))
|
|
@@ -588,7 +590,7 @@ int ProtoParserPrivate::exec(const char *sql, std::function<bool(struct sqlite3_
|
|
|
int ret = sqlite3_prepare(_db, sql, strlen(sql), &stmt, nullptr);
|
|
|
if (ret != SQLITE_OK)
|
|
|
{
|
|
|
- MqttLog(MQTT_LOG_ERROR, "sqlite3_prepare(%s) failed(%d): %s\n", sql, ret, sqlite3_errmsg(_db));
|
|
|
+ MqttLog(LOG_ERROR, LOG_TYPE_DB, "sqlite3_prepare(%s) failed(%d): %s\n", sql, ret, sqlite3_errmsg(_db));
|
|
|
return ret;
|
|
|
}
|
|
|
if (pars != nullptr)
|
|
@@ -607,7 +609,7 @@ int ProtoParserPrivate::exec(const char *sql, std::function<bool(struct sqlite3_
|
|
|
}
|
|
|
if (ret != SQLITE_OK)
|
|
|
{
|
|
|
- MqttLog(MQTT_LOG_ERROR, "exec(%s) failed(%d): %s\n", sql, ret, sqlite3_errmsg(_db));
|
|
|
+ MqttLog(LOG_ERROR, LOG_TYPE_DB, "exec(%s) failed(%d): %s\n", sql, ret, sqlite3_errmsg(_db));
|
|
|
return ret;
|
|
|
}
|
|
|
return ret;
|
|
@@ -649,7 +651,7 @@ int ProtoParserPrivate::insert(const char *sql, std::function<bool(struct sqlite
|
|
|
auto ret = sqlite3_prepare_v2(_db, sql, strlen(sql), &stmt, nullptr);
|
|
|
if (ret != SQLITE_OK)
|
|
|
{
|
|
|
- MqttLog(MQTT_LOG_ERROR, "sqlite3_prepare_v2 failed(%d), sql: (%s), errmsg: %s\n", ret, sql, sqlite3_errmsg(_db));
|
|
|
+ MqttLog(LOG_ERROR, LOG_TYPE_DB, "sqlite3_prepare_v2 failed(%d), sql: (%s), errmsg: %s\n", ret, sql, sqlite3_errmsg(_db));
|
|
|
return ret;
|
|
|
}
|
|
|
while (cb(stmt))
|
|
@@ -659,7 +661,7 @@ int ProtoParserPrivate::insert(const char *sql, std::function<bool(struct sqlite
|
|
|
ret = SQLITE_OK;
|
|
|
if (ret != SQLITE_OK)
|
|
|
{
|
|
|
- MqttLog(MQTT_LOG_ERROR, "sqlite3_step failed(%d), errmsg: %s\n", ret, sqlite3_errmsg(_db));
|
|
|
+ MqttLog(LOG_ERROR, LOG_TYPE_DB, "sqlite3_step failed(%d), errmsg: %s\n", ret, sqlite3_errmsg(_db));
|
|
|
break;
|
|
|
}
|
|
|
}
|
|
@@ -668,12 +670,12 @@ int ProtoParserPrivate::insert(const char *sql, std::function<bool(struct sqlite
|
|
|
ret = sqlite3_finalize(stmt);
|
|
|
if (ret != SQLITE_OK)
|
|
|
{
|
|
|
- MqttLog(MQTT_LOG_ERROR, "sqlite3_finalize failed(%d), errmsg: %s\n", ret, sqlite3_errmsg(_db));
|
|
|
+ MqttLog(LOG_ERROR, LOG_TYPE_DB, "sqlite3_finalize failed(%d), errmsg: %s\n", ret, sqlite3_errmsg(_db));
|
|
|
}
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
- MqttLog(MQTT_LOG_WARN, "not invoke sqlite3_finalize");
|
|
|
+ MqttLog(LOG_WARN, LOG_TYPE_DB, "not invoke sqlite3_finalize");
|
|
|
}
|
|
|
return ret;
|
|
|
}
|
|
@@ -1002,7 +1004,7 @@ int ProtoParserPrivate::update(PullMessageResult &result, uint64_t &head)
|
|
|
auto ret = sqlite3_exec(_db, sql.c_str(), nullptr, nullptr, nullptr);
|
|
|
if (ret != SQLITE_OK)
|
|
|
{
|
|
|
- MqttLog(MQTT_LOG_ERROR, "failed exec: %s: %d, %s", sql.c_str(), ret, sqlite3_errmsg(_db));
|
|
|
+ MqttLog(LOG_ERROR, LOG_TYPE_DB, "failed exec: %s: %d, %s", sql.c_str(), ret, sqlite3_errmsg(_db));
|
|
|
}
|
|
|
sql = "insert into t_messages(_mid, _from, _type, _target, _line, _data, _searchable_key, _dt, _content_type, _to) values (?,?,?,?,?,?,?,?,?,?);";
|
|
|
|
|
@@ -1026,14 +1028,14 @@ int ProtoParserPrivate::update(PullMessageResult &result, uint64_t &head)
|
|
|
bindText(stmt, col++, entry.to_user().c_str());
|
|
|
i++;
|
|
|
#if defined DUMP || defined DUMP_MESSAGE
|
|
|
- MqttLog(MQTT_LOG_INFO, "insert message %lld, type: %d, line: %d, target: %s", entry.message_id(), entry.conversation().type(), entry.conversation().line(), entry.conversation().target().c_str());
|
|
|
+ MqttLog(LOG_INFO, LOG_TYPE_DB, "insert message %lld, type: %d, line: %d, target: %s", entry.message_id(), entry.conversation().type(), entry.conversation().line(), entry.conversation().target().c_str());
|
|
|
#endif
|
|
|
return true;
|
|
|
});
|
|
|
#if defined DUMP || defined DUMP_MESSAGE
|
|
|
auto err = exec("select count(*) from t_messages", [&](struct sqlite3_stmt *stmt) {
|
|
|
int c = sqlite3_column_int64(stmt, 0);
|
|
|
- MqttLog(MQTT_LOG_INFO, "t_messages count: %d", c);
|
|
|
+ MqttLog(LOG_INFO, LOG_TYPE_DB, "t_messages count: %d", c);
|
|
|
return true;
|
|
|
});
|
|
|
|
|
@@ -1041,7 +1043,7 @@ int ProtoParserPrivate::update(PullMessageResult &result, uint64_t &head)
|
|
|
Message msg;
|
|
|
toMessage(stmt, msg);
|
|
|
|
|
|
- MqttLog(MQTT_LOG_INFO, "getMessage: %lld, type: %d, line: %d, target: %s", msg.message_id(), msg.conversation().type(), msg.conversation().line(), msg.conversation().target().c_str());
|
|
|
+ MqttLog(LOG_INFO, LOG_TYPE_DB, "getMessage: %lld, type: %d, line: %d, target: %s", msg.message_id(), msg.conversation().type(), msg.conversation().line(), msg.conversation().target().c_str());
|
|
|
|
|
|
return true;
|
|
|
});
|
|
@@ -1104,7 +1106,7 @@ void ProtoParserPrivate::test()
|
|
|
bindText(stmt, col++, entry.to_user().c_str());
|
|
|
i++;
|
|
|
#if defined DUMP || defined DUMP_MESSAGE
|
|
|
- MqttLog(MQTT_LOG_INFO, "insert message %lld, type: %d, line: %d, target: %s", entry.message_id(), entry.conversation().type(), entry.conversation().line(), entry.conversation().target().c_str());
|
|
|
+ MqttLog(LOG_INFO, LOG_TYPE_DB, "insert message %lld, type: %d, line: %d, target: %s", entry.message_id(), entry.conversation().type(), entry.conversation().line(), entry.conversation().target().c_str());
|
|
|
#endif
|
|
|
return true;
|
|
|
});
|