From 2b6f863cf35836b0a31982443a9d14ba532874d1 Mon Sep 17 00:00:00 2001
From: ghzhang <zhangguohong@lnxall.com>
Date: Wed, 19 Jul 2023 19:14:07 +0800
Subject: [PATCH] fixbug

---
 .../ua_history_data_backend_sqlite.c          | 58 ++++++++++++-------
 1 file changed, 37 insertions(+), 21 deletions(-)

diff --git a/plugins/historydata/ua_history_data_backend_sqlite.c b/plugins/historydata/ua_history_data_backend_sqlite.c
index a1ff341..9203fad 100755
--- a/plugins/historydata/ua_history_data_backend_sqlite.c
+++ b/plugins/historydata/ua_history_data_backend_sqlite.c
@@ -40,7 +40,9 @@ resultSize_backend_sqlite(UA_Server *server,
 	char *zErrMsg =NULL;
 	char **azResult=NULL; //二维数组存放结果
 	char sql[512] = {0};
-	sprintf(sql,"select count(*) from 'variable' where name = '%s'",nodeId->identifier.string.data);
+	char buff[128] = {0};
+	memcpy(buff,nodeId->identifier.string.data,nodeId->identifier.string.length);
+	sprintf(sql,"select count(*) from 'variable' where name = '%s'",buff);
 	sqlite3_get_table( db , sql , &azResult , &nrow , &ncolumn , &zErrMsg );
 	if(zErrMsg )
 	{
@@ -86,7 +88,9 @@ getDateTimeMatch_backend_sqlite(UA_Server *server,
 	char *zErrMsg =NULL;
 	char **azResult=NULL; //二维数组存放结果
 	char sql[512] = {0};
-	sprintf(sql,"select count(*) from 'variable' where name = '%s' and time <= %llu",nodeId->identifier.string.data,(uint64_t)timestamp);
+	char buff[128] = {0};
+	memcpy(buff,nodeId->identifier.string.data,nodeId->identifier.string.length);
+	sprintf(sql,"select count(*) from 'variable' where name = '%s' and time <= %llu",buff,(uint64_t)timestamp);
 	sqlite3_get_table( db , sql , &azResult , &nrow , &ncolumn , &zErrMsg );
 	if(zErrMsg )
 	{
@@ -283,7 +287,9 @@ serverSetHistoryData_backend_sqlite(UA_Server *server,
 	}
 
 	char sql[512] = {0};
-	sprintf(sql,"INSERT INTO 'variable' VALUES(NULL,'%s',%d,'%s',%llu)",nodeId->identifier.string.data,GetDataTypeToInt(&value->value),result,(uint64_t)timestamp);
+	char buff[128] = {0};
+	memcpy(buff,nodeId->identifier.string.data,nodeId->identifier.string.length);
+	sprintf(sql,"INSERT INTO 'variable' VALUES(NULL,'%s',%d,'%s',%llu)",buff,GetDataTypeToInt(&value->value),result,(uint64_t)timestamp);
 	char *zErrMsg =NULL;
     sqlite3_exec(db,sql,NULL,NULL,&zErrMsg);
 	if(zErrMsg )
@@ -314,7 +320,9 @@ getEnd_backend_sqlite(UA_Server *server,
 	char *zErrMsg =NULL;
 	char **azResult=NULL; //二维数组存放结果
 	char sql[512] = {0};
-	sprintf(sql,"select count(*) from 'variable' where name = '%s'",nodeId->identifier.string.data);
+	char buff[128] = {0};
+	memcpy(buff,nodeId->identifier.string.data,nodeId->identifier.string.length);
+	sprintf(sql,"select count(*) from 'variable' where name = '%s'",buff);
 	sqlite3_get_table( db , sql , &azResult , &nrow , &ncolumn , &zErrMsg );
 	if(zErrMsg )
 	{
@@ -350,7 +358,9 @@ lastIndex_backend_sqlite(UA_Server *server,
 	char *zErrMsg =NULL;
 	char **azResult=NULL; //二维数组存放结果
 	char sql[512] = {0};
-	sprintf(sql,"select count(*) from 'variable' where name = '%s'",nodeId->identifier.string.data);
+	char buff[128] = {0};
+	memcpy(buff,nodeId->identifier.string.data,nodeId->identifier.string.length);
+	sprintf(sql,"select count(*) from 'variable' where name = '%s'",buff);
 	sqlite3_get_table( db , sql , &azResult , &nrow , &ncolumn , &zErrMsg );
 	if(zErrMsg )
 	{
@@ -414,13 +424,15 @@ getDataValues_backend_sqlite(UA_Server *server,
 	char *zErrMsg =NULL;
 	char **azResult=NULL; //二维数组存放结果
 	char sql[512] = {0};
+	char buff[128] = {0};
+	memcpy(buff,nodeId->identifier.string.data,nodeId->identifier.string.length);
 	if(reverse)
 	{
-		sprintf(sql,"select datatype,result,time from 'variable' where name = '%s' order by time desc limit %d offset %d",nodeId->identifier.string.data,(int)total,(int)index);
+		sprintf(sql,"select datatype,result,time from 'variable' where name = '%s' order by time desc limit %d offset %d",buff,(int)total,(int)index);
 	}
 	else
 	{
-		sprintf(sql,"select datatype,result,time from 'variable' where name = '%s' order by time limit %d offset %d",nodeId->identifier.string.data,(int)total,(int)index);
+		sprintf(sql,"select datatype,result,time from 'variable' where name = '%s' order by time limit %d offset %d",buff,(int)total,(int)index);
 	}
 	printf("sql:%s\n",sql);
 	sqlite3_get_table( db , sql , &azResult , &nrow , &ncolumn , &zErrMsg );
@@ -541,8 +553,10 @@ getDataValue_backend_sqlite(UA_Server *server,
 	char *zErrMsg =NULL;
 	char **azResult=NULL; //二维数组存放结果
 	char sql[512] = {0};
+	char buff[128] = {0};
+	memcpy(buff,nodeId->identifier.string.data,nodeId->identifier.string.length);
 	
-	sprintf(sql,"select datatype,result,time from 'variable' where name = '%s' order by time limit 1 offset %d",nodeId->identifier.string.data,(int)index);
+	sprintf(sql,"select datatype,result,time from 'variable' where name = '%s' order by time limit 1 offset %d",buff,(int)index);
 	sqlite3_get_table( db , sql , &azResult , &nrow , &ncolumn , &zErrMsg );
 	//printf("sql:%s\n",sql);
 	//printf("nrow:%d ncolumn:%d\n",nrow,ncolumn);
@@ -746,6 +760,8 @@ static UA_StatusCode getHistoryData_backend_sqlite(UA_Server *server,
 	char *zErrMsg =NULL;
 	char **azResult=NULL; //二维数组存放结果
 	char sql[512] = {0};
+	char buff[128] = {0};
+	memcpy(buff,nodeId->identifier.string.data,nodeId->identifier.string.length);
 	char sqlCount[512] = {0};
 	int maxSize = maxSizePerResponse ;
 	if(numValuesPerNode > 0 && maxSizePerResponse > numValuesPerNode)
@@ -756,34 +772,34 @@ static UA_StatusCode getHistoryData_backend_sqlite(UA_Server *server,
 	{
 		if(start < end)
 		{
-			sprintf(sql,"select datatype,result,time from 'variable' where name = '%s' and time >= %llu and time <=%llu order by time limit %d offset %d",nodeId->identifier.string.data,start,end,maxSize,(int)skip);
-			sprintf(sqlCount,"select count(*) from 'variable' where name = '%s' and time >= %llu and time <=%llu",nodeId->identifier.string.data,start,end);
+			sprintf(sql,"select datatype,result,time from 'variable' where name = '%s' and time >= %llu and time <=%llu order by time limit %d offset %d",buff,start,end,maxSize,(int)skip);
+			sprintf(sqlCount,"select count(*) from 'variable' where name = '%s' and time >= %llu and time <=%llu",buff,start,end);
 		}
 		else if(end == 0)
 		{
-			sprintf(sql,"select datatype,result,time from 'variable' where name = '%s' and time >= %llu order by time limit %d  offset %d",nodeId->identifier.string.data,start,maxSize,(int)skip);
-			sprintf(sqlCount,"select count(*) from 'variable' where name = '%s' and time >= %llu",nodeId->identifier.string.data,start);
+			sprintf(sql,"select datatype,result,time from 'variable' where name = '%s' and time >= %llu order by time limit %d  offset %d",buff,start,maxSize,(int)skip);
+			sprintf(sqlCount,"select count(*) from 'variable' where name = '%s' and time >= %llu",buff,start);
 		}
 		else 
 		{
-			sprintf(sql,"select datatype,result,time from 'variable' where name = '%s' and time >= %llu and time <=%llu order by time limit %d offset %d",nodeId->identifier.string.data,end,start,maxSize,(int)skip);
-			sprintf(sqlCount,"select count(*) from 'variable' where name = '%s' and time >= %llu and time <=%llu",nodeId->identifier.string.data,end,start);
+			sprintf(sql,"select datatype,result,time from 'variable' where name = '%s' and time >= %llu and time <=%llu order by time limit %d offset %d",buff,end,start,maxSize,(int)skip);
+			sprintf(sqlCount,"select count(*) from 'variable' where name = '%s' and time >= %llu and time <=%llu",buff,end,start);
 		}
 	} else {
 		if(start < end)
 		{
-			sprintf(sql,"select datatype,result,time from 'variable' where name = '%s' and time > %llu and time <%llu order by time limit %d offset %d",nodeId->identifier.string.data,start,end,maxSize,(int)skip);
-			sprintf(sqlCount,"select count(*) from 'variable' where name = '%s' and time > %llu and time <%llu",nodeId->identifier.string.data,start,end);
+			sprintf(sql,"select datatype,result,time from 'variable' where name = '%s' and time > %llu and time <%llu order by time limit %d offset %d",buff,start,end,maxSize,(int)skip);
+			sprintf(sqlCount,"select count(*) from 'variable' where name = '%s' and time > %llu and time <%llu",buff,start,end);
 		}
 		else if(end == 0)
 		{
-			sprintf(sql,"select datatype,result,time from 'variable' where name = '%s' and time > %llu order by time limit %d offset %d",nodeId->identifier.string.data,start,maxSize,(int)skip);
-			sprintf(sqlCount,"select count(*) from 'variable' where name = '%s' and time > %llu",nodeId->identifier.string.data,start);
+			sprintf(sql,"select datatype,result,time from 'variable' where name = '%s' and time > %llu order by time limit %d offset %d",buff,start,maxSize,(int)skip);
+			sprintf(sqlCount,"select count(*) from 'variable' where name = '%s' and time > %llu",buff,start);
 		}
 		else 
 		{
-			sprintf(sql,"select datatype,result,time from 'variable' where name = '%s' and time > %llu and time <%llu order by time limit %d offset %d",nodeId->identifier.string.data,end,start,maxSize,(int)skip);
-			sprintf(sqlCount,"select count(*) from 'variable' where name = '%s' and time > %llu and time <%llu",nodeId->identifier.string.data,end,start);
+			sprintf(sql,"select datatype,result,time from 'variable' where name = '%s' and time > %llu and time <%llu order by time limit %d offset %d",buff,end,start,maxSize,(int)skip);
+			sprintf(sqlCount,"select count(*) from 'variable' where name = '%s' and time > %llu and time <%llu",buff,end,start);
 		}
 	}
 	sqlite3_get_table( db , sqlCount , &azResult , &nrow , &ncolumn , &zErrMsg );
@@ -795,7 +811,7 @@ static UA_StatusCode getHistoryData_backend_sqlite(UA_Server *server,
 		return UA_STATUSCODE_BADOUTOFMEMORY;
 	}
 	int totalCount = (int)atoi(azResult[1]);
-	//printf("totalCount:%d\n",totalCount);
+	// printf("totalCount:%d %s\n",totalCount,sqlCount);
 	sqlite3_free_table(azResult);
 	sqlite3_free(zErrMsg);
 	//printf("sql:%s\n",sql);
-- 
2.17.1

