dong_dl 1 year ago
parent
commit
a44cd0eae3

+ 2 - 2
src/proto/ProtoParser.cpp

@@ -1590,13 +1590,13 @@ int ProtoParser::connectVal(std::string requestJson)
 
 EM_BOOL ProtoParser::onWebSocketClose(int eventType, const EmscriptenWebSocketCloseEvent *websocketEvent, void *userData)
 {
-    MqttLog(MQTT_LOG_ERROR, "ProtoParser::onWebSocketClose");
+    MqttLog(MQTT_LOG_ERROR, "web socket closed");
     onDisconnected();
     return EM_TRUE;
 }
 EM_BOOL ProtoParser::onWebSocketError(int eventType, const EmscriptenWebSocketErrorEvent *websocketEvent, void *userData)
 {
-    MqttLog(MQTT_LOG_ERROR, "ProtoParser::onWebSocketError");
+    MqttLog(MQTT_LOG_ERROR, "web socket error");
     return EM_TRUE;
 }
 EM_BOOL ProtoParser::onWebSocketOpen(int eventType, const EmscriptenWebSocketOpenEvent *websocketEvent, void *userData)

+ 40 - 21
src/proto/ProtoParserPrivate.cpp

@@ -263,10 +263,11 @@ int ProtoParserPrivate::getMessage(bool before, uint64_t idx, int count, int typ
             return false;
         return true; }, [&](auto stmt) { 
             sqlite3_bind_int(stmt, 1, type);
-         sqlite3_bind_int(stmt, 2, line);
-         sqlite3_bind_text(stmt, 3, target, strlen(target), nullptr); 
-        if (idx != 0)
-         sqlite3_bind_int64(stmt, 4, idx); });
+            sqlite3_bind_int(stmt, 2, line);
+            bindText(stmt, 3, target); 
+            if (idx != 0)
+                sqlite3_bind_int64(stmt, 4, idx);
+        });
 #else
     if (before)
     {
@@ -334,7 +335,7 @@ int ProtoParserPrivate::getConversation(const char *keyword, const char *types,
                 return false;
             return true; }, [&](auto stmt) {
             if (keyword != nullptr && strlen(keyword) > 0)
-                sqlite3_bind_text(stmt, 1, keyword, strlen(keyword), nullptr); });
+                bindText(stmt, 1, keyword); });
 #else
     std::set<int> intTypes, intLines;
     parseIntArray(types, intTypes);
@@ -434,6 +435,24 @@ int64_t ProtoParserPrivate::getMaxId(const char *sql)
     return ret;
 }
 
+void ProtoParserPrivate::bindText(struct sqlite3_stmt *stmt, int col, const char *str)
+{
+    if (str == nullptr)
+    {
+        sqlite3_bind_null(stmt, col);
+    }
+    else
+    {
+        char *s = strdup(str);
+        sqlite3_bind_text(stmt, col, s, strlen(s), freeStr);
+    }
+}
+
+void ProtoParserPrivate::freeStr(void *ptr)
+{
+    free(ptr);
+}
+
 int ProtoParserPrivate::insert(const char *sql, std::function<bool(struct sqlite3_stmt *stmt)> cb)
 {
     struct sqlite3_stmt *stmt = nullptr;
@@ -534,12 +553,12 @@ int ProtoParserPrivate::update(GetFriendsResult &result)
             return false;
         auto entry = result.entry(i);
         auto err   = sqlite3_reset(stmt);
-        sqlite3_bind_text(stmt, 0, entry.uid().c_str(), entry.uid().length(), nullptr);
+        bindText(stmt, 0, entry.uid().c_str());
         sqlite3_bind_int(stmt, 1, entry.state());
         sqlite3_bind_int64(stmt, 2, entry.update_dt());
-        sqlite3_bind_text(stmt, 3, entry.alias().c_str(), entry.alias().length(), nullptr);
+        bindText(stmt, 3, entry.alias().c_str());
         sqlite3_bind_int(stmt, 4, entry.blacked());
-        sqlite3_bind_text(stmt, 5, entry.extra().c_str(), entry.extra().length(), nullptr);
+        bindText(stmt, 5, entry.extra().c_str());
         i++;
         return true;
     });
@@ -594,11 +613,11 @@ int ProtoParserPrivate::update(GetUserSettingResult &result)
     return insert(sql, [&](auto stmt) {
         if (i >= n)
             return false;
-        auto entry = result.entry(i);
+        auto& entry = result.entry(i);
         auto err   = sqlite3_reset(stmt);
         sqlite3_bind_int(stmt, 0, entry.scope());
-        sqlite3_bind_text(stmt, 1, entry.key().c_str(), entry.key().length(), nullptr);
-        sqlite3_bind_text(stmt, 2, entry.value().c_str(), entry.value().length(), nullptr);
+        bindText(stmt, 1, entry.key().c_str());
+        bindText(stmt, 2, entry.value().c_str());
         sqlite3_bind_int64(stmt, 0, entry.update_dt());
         i++;
         return true;
@@ -757,20 +776,20 @@ int ProtoParserPrivate::update(PullMessageResult &result, uint64_t &head)
     ret   = insert(sql.c_str(), [&](auto stmt) {
         if (i >= n)
             return false;
-        auto entry = result.message(i);
+        auto& entry = result.message(i);
         auto err   = sqlite3_reset(stmt);
 
         int col = 1;
         sqlite3_bind_int64(stmt, col++, entry.message_id());
-        sqlite3_bind_text(stmt, col++, entry.from_user().c_str(), entry.from_user().length(), nullptr);
+        bindText(stmt, col++, entry.from_user().c_str());
         sqlite3_bind_int(stmt, col++, entry.conversation().type());
-        sqlite3_bind_text(stmt, col++, entry.conversation().target().c_str(), entry.conversation().target().length(), nullptr);
+        bindText(stmt, col++, entry.conversation().target().c_str());
         sqlite3_bind_int(stmt, col++, entry.conversation().line());
         sqlite3_bind_blob(stmt, col++, entry.content().data().c_str(), entry.content().data().length(), nullptr);
-        sqlite3_bind_text(stmt, col++, entry.content().searchable_content().c_str(), entry.content().searchable_content().length(), nullptr);
+        bindText(stmt, col++, entry.content().searchable_content().c_str());
         sqlite3_bind_int64(stmt, col++, entry.server_timestamp());
         sqlite3_bind_int(stmt, col++, entry.content().type());
-        sqlite3_bind_text(stmt, col++, entry.to_user().c_str(), entry.to_user().length(), nullptr);
+        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());
@@ -788,7 +807,7 @@ int ProtoParserPrivate::update(PullMessageResult &result, uint64_t &head)
         Message msg;
         toMessage(stmt, msg);
 
-        MqttLog(MQTT_LOG_INFO, "getMessage: %lld, type: %d, line: %d", msg.message_id(), msg.conversation().type(), msg.conversation().line());
+        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());
 
         return true;
     });
@@ -845,15 +864,15 @@ void ProtoParserPrivate::test()
 
         int col = 1;
         sqlite3_bind_int64(stmt, col++, entry.message_id());
-        sqlite3_bind_text(stmt, col++, entry.from_user().c_str(), entry.from_user().length(), nullptr);
+        bindText(stmt, col++, entry.from_user().c_str());
         sqlite3_bind_int(stmt, col++, entry.conversation().type());
-        sqlite3_bind_text(stmt, col++, entry.conversation().target().c_str(), entry.conversation().target().length(), nullptr);
+        bindText(stmt, col++, entry.conversation().target().c_str());
         sqlite3_bind_int(stmt, col++, entry.conversation().line());
         sqlite3_bind_blob(stmt, col++, entry.content().data().c_str(), entry.content().data().length(), nullptr);
-        sqlite3_bind_text(stmt, col++, entry.content().searchable_content().c_str(), entry.content().searchable_content().length(), nullptr);
+        bindText(stmt, col++, entry.content().searchable_content().c_str());
         sqlite3_bind_int64(stmt, col++, entry.server_timestamp());
         sqlite3_bind_int(stmt, col++, entry.content().type());
-        sqlite3_bind_text(stmt, col++, entry.to_user().c_str(), entry.to_user().length(), nullptr);
+        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());

+ 2 - 0
src/proto/ProtoParserPrivate.h

@@ -41,6 +41,8 @@ protected:
     int         insert(const char *sql, std::function<bool(struct sqlite3_stmt *stmt)> cb);
     int64_t     getMaxId(const char *sql);
     static void toMessage(struct sqlite3_stmt *stmt, Message &msg);
+    void        bindText(struct sqlite3_stmt *stmt, int col, const char* str);
+    static void freeStr(void *);
 #endif
 private:
 #ifndef USE_ALL_SQLITE3

+ 1 - 1
src/sqlcipher/include/sqlite3.h

@@ -125,7 +125,7 @@ extern "C" {
 */
 #define SQLITE_VERSION        "3.34.1"
 #define SQLITE_VERSION_NUMBER 3034001
-#define SQLITE_SOURCE_ID      "20230314"
+#define SQLITE_SOURCE_ID      "20230316"
 
 /*
 ** CAPI3REF: Run-Time Library Version Numbers