From 7b0eb54b0a3bddadc340b183b9357d412972787f Mon Sep 17 00:00:00 2001
From: ghzhang <zhangguohong@lnxall.com>
Date: Mon, 20 Nov 2023 17:53:09 +0800
Subject: [PATCH] 123

---
 .../ua_history_data_backend_sqlite.c          | 50 ++++++++++++++-----
 1 file changed, 38 insertions(+), 12 deletions(-)

diff --git a/plugins/historydata/ua_history_data_backend_sqlite.c b/plugins/historydata/ua_history_data_backend_sqlite.c
index 023cf4e..725ca64 100755
--- a/plugins/historydata/ua_history_data_backend_sqlite.c
+++ b/plugins/historydata/ua_history_data_backend_sqlite.c
@@ -8,11 +8,14 @@
 #pragma GCC diagnostic ignored "-Wcast-qual"
 #pragma GCC diagnostic ignored "-Wsign-conversion"
 #include <open62541/plugin/historydata/history_data_backend_sqlite.h>
-
+#include "dy_utils/hash_intptr.h"
 #include <limits.h>
 #include <string.h>
 #include <sqlite3.h>
 
+typedef struct hash_intptr hash_intptr_t;
+static hash_intptr_t *insert_stmt_hash;
+
 static void str_replace_chr(char *line)
 {
     for(int i = 0; line[i] != '\0';i++)
@@ -296,20 +299,39 @@ serverSetHistoryData_backend_sqlite(UA_Server *server,
 		return UA_STATUSCODE_GOOD;
 	}
 
-	char sql[512] = {0};
-	char buff[128] = {0};
-	memcpy(buff,nodeId->identifier.string.data,nodeId->identifier.string.length);
-	str_replace_chr(buff);
-	sprintf(sql,"INSERT INTO '%s' VALUES(NULL,%d,'%s',%" PRId64")",buff,GetDataTypeToInt(&value->value),result,(uint64_t)timestamp);
-	char *zErrMsg =NULL;
-    sqlite3_exec(db,sql,NULL,NULL,&zErrMsg);
-	if(zErrMsg )
+	// char sql[512] = {0};
+
+	// sprintf(sql,"INSERT INTO '%s' VALUES(NULL,%d,'%s',%" PRId64")",buff,GetDataTypeToInt(&value->value),result,(uint64_t)timestamp);
+    sqlite3_stmt* insert_stmt = NULL;
+	if (hash_intptr_findptr(insert_stmt_hash, nodeId->identifier.string.data, (unsigned int)nodeId->identifier.string.length, (void **)&insert_stmt) < 0)
 	{
-		printf("serverSetHistoryData_backend_sqlite error:%s\n",zErrMsg);
-		sqlite3_free(zErrMsg);
+		char insertSQL[512] = {0};
+		char buff[128] = {0};
+		memcpy(buff,nodeId->identifier.string.data,nodeId->identifier.string.length);
+		str_replace_chr(buff);
+		sprintf(insertSQL,"INSERT INTO '%s' VALUES(NULL,?,?,?)",buff);
+		int ret = sqlite3_prepare_v2(db, insertSQL, (int)strlen(insertSQL), &insert_stmt, NULL);
+		if (ret != SQLITE_OK) {
+			printf("sqlite3_prepare_v2 error:%d\n",ret);
+			if (insert_stmt)
+				sqlite3_finalize(insert_stmt);
+			return UA_STATUSCODE_GOOD;
+		}
+		ret = hash_intptr_addptr(&insert_stmt_hash, nodeId->identifier.string.data,(unsigned int)nodeId->identifier.string.length, insert_stmt);
+		if (ret < 0)
+		{
+			printf( "hash_intptr_addptr failed, key: %.*s",nodeId->identifier.string.length,nodeId->identifier.string.data);
+			return UA_STATUSCODE_GOOD;
+		}
+
+	}
+	sqlite3_bind_int(insert_stmt, 1, GetDataTypeToInt(&value->value));
+	sqlite3_bind_text(insert_stmt, 2, result, -1, NULL);
+	sqlite3_bind_int64(insert_stmt, 3, timestamp);
+	if (sqlite3_step(insert_stmt) != SQLITE_DONE) {
 		return UA_STATUSCODE_GOOD;
 	}
-	sqlite3_free(zErrMsg);
+	sqlite3_reset(insert_stmt);
 	free(result);
     return UA_STATUSCODE_GOOD;
 }
@@ -526,6 +548,7 @@ getDataValues_backend_sqlite(UA_Server *server,
 				strcpy((char*)ua_result.data, azResult[i*3 +4]);
 				UA_Variant_setScalarCopy(&result[i].value, &ua_result, &UA_TYPES[UA_TYPES_STRING]);
 				UA_String_clear(&ua_result);
+				free(ua_result.data);
 				break;
 			}
 		}
@@ -651,6 +674,7 @@ getDataValue_backend_sqlite(UA_Server *server,
 			strcpy((char*)ua_result.data, azResult[4]);
 			UA_Variant_setScalarCopy(&result->value, &ua_result, &UA_TYPES[UA_TYPES_STRING]);
 			UA_String_clear(&ua_result);
+			free(ua_result.data);
 			break;
 		}
 	}
@@ -923,6 +947,7 @@ static UA_StatusCode getHistoryData_backend_sqlite(UA_Server *server,
 				strcpy((char*)ua_result.data, azResult[i*3 +4]);
 				UA_Variant_setScalarCopy(&resultValue.value, &ua_result, &UA_TYPES[UA_TYPES_STRING]);
 				UA_String_clear(&ua_result);
+				free(ua_result.data);
 				break;
 			}
 		}
@@ -986,6 +1011,7 @@ UA_HistoryDataBackend_Sqlite(char* path) {
 		printf("Can't open database: %s\n", sqlite3_errmsg(db));
 		exit(0);
     }
+
 	char *zErrMsg =NULL;	
 	/* 优化性能 */
 	sqlite3_exec(db,"PRAGMA main.cache_size=100;",NULL,NULL,&zErrMsg);
-- 
2.17.1

