From 159fca698374a697ab8b440f456ee82373c05494 Mon Sep 17 00:00:00 2001
From: ghzhang <zhangguohong@lnxall.com>
Date: Fri, 4 Aug 2023 17:49:33 +0800
Subject: [PATCH] 1

---
 .../ua_history_data_backend_sqlite.c          | 70 ++++++++++++-------
 1 file changed, 43 insertions(+), 27 deletions(-)

diff --git a/plugins/historydata/ua_history_data_backend_sqlite.c b/plugins/historydata/ua_history_data_backend_sqlite.c
index 51253ad..f218316 100755
--- a/plugins/historydata/ua_history_data_backend_sqlite.c
+++ b/plugins/historydata/ua_history_data_backend_sqlite.c
@@ -13,6 +13,16 @@
 #include <string.h>
 #include <sqlite3.h>
 
+static char* str_replace_chr(char *line)
+{
+    for(int i = 0; line[i] != '\0';i++)
+    {
+        if(!( (line[i] >= '0' && line[i] <= '9') || (line[i] >= 'a' && line[i] <= 'z') || (line[i] >= 'A' && line[i] <= 'Z') || line[i] == '\0'|| line[i] == '_'))
+        {
+            line[i] = '_';
+        }
+    }
+}
 
 static size_t
 getEnd_backend_sqlite(UA_Server *server,
@@ -289,7 +299,8 @@ serverSetHistoryData_backend_sqlite(UA_Server *server,
 	char sql[512] = {0};
 	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);
+	str_replace_chr(buff);
+	sprintf(sql,"INSERT INTO '%s' VALUES(NULL,%d,'%s',%llu)",buff,GetDataTypeToInt(&value->value),result,(uint64_t)timestamp);
 	char *zErrMsg =NULL;
     sqlite3_exec(db,sql,NULL,NULL,&zErrMsg);
 	if(zErrMsg )
@@ -762,6 +773,7 @@ static UA_StatusCode getHistoryData_backend_sqlite(UA_Server *server,
 	char sql[512] = {0};
 	char buff[128] = {0};
 	memcpy(buff,nodeId->identifier.string.data,nodeId->identifier.string.length);
+	str_replace_chr(buff);
 	char sqlCount[512] = {0};
 	int maxSize = maxSizePerResponse ;
 	if(numValuesPerNode > 0 && maxSizePerResponse > numValuesPerNode)
@@ -772,34 +784,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",buff,start,end,maxSize,(int)skip);
-			sprintf(sqlCount,"select count(*) from 'variable' where name = '%s' and time >= %llu and time <=%llu",buff,start,end);
+			sprintf(sql,"select datatype,result,time from '%s' where  time >= %llu and time <=%llu order by time limit %d offset %d",buff,start,end,maxSize,(int)skip);
+			sprintf(sqlCount,"select count(*) from '%s' where  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",buff,start,maxSize,(int)skip);
-			sprintf(sqlCount,"select count(*) from 'variable' where name = '%s' and time >= %llu",buff,start);
+			sprintf(sql,"select datatype,result,time from '%s' where  time >= %llu order by time limit %d  offset %d",buff,start,maxSize,(int)skip);
+			sprintf(sqlCount,"select count(*) from '%s' where  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",buff,end,start,maxSize,(int)skip);
-			sprintf(sqlCount,"select count(*) from 'variable' where name = '%s' and time >= %llu and time <=%llu",buff,end,start);
+			sprintf(sql,"select datatype,result,time from '%s' where  time >= %llu and time <=%llu order by time limit %d offset %d",buff,end,start,maxSize,(int)skip);
+			sprintf(sqlCount,"select count(*) from '%s' where  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",buff,start,end,maxSize,(int)skip);
-			sprintf(sqlCount,"select count(*) from 'variable' where name = '%s' and time > %llu and time <%llu",buff,start,end);
+			sprintf(sql,"select datatype,result,time from '%s' where  time > %llu and time <%llu order by time limit %d offset %d",buff,start,end,maxSize,(int)skip);
+			sprintf(sqlCount,"select count(*) from '%s' where 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",buff,start,maxSize,(int)skip);
-			sprintf(sqlCount,"select count(*) from 'variable' where name = '%s' and time > %llu",buff,start);
+			sprintf(sql,"select datatype,result,time from '%s' where  time > %llu order by time limit %d offset %d",buff,start,maxSize,(int)skip);
+			sprintf(sqlCount,"select count(*) from '%s' where  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",buff,end,start,maxSize,(int)skip);
-			sprintf(sqlCount,"select count(*) from 'variable' where name = '%s' and time > %llu and time <%llu",buff,end,start);
+			sprintf(sql,"select datatype,result,time from '%s' where time > %llu and time <%llu order by time limit %d offset %d",buff,end,start,maxSize,(int)skip);
+			sprintf(sqlCount,"select count(*) from '%s' where time > %llu and time <%llu",buff,end,start);
 		}
 	}
 	sqlite3_get_table( db , sqlCount , &azResult , &nrow , &ncolumn , &zErrMsg );
@@ -944,32 +956,36 @@ static UA_StatusCode getHistoryData_backend_sqlite(UA_Server *server,
 }
 
 
-
-UA_HistoryDataBackend
-UA_HistoryDataBackend_Sqlite(char* path) {
-	//初始化数据库
-	sqlite3 *db;
-	if (sqlite3_open(path, &db))
-    {
-		printf("Can't open database: %s\n", sqlite3_errmsg(db));
-		exit(0);
-    }
+void creat_table(sqlite3 *db,char *node_name) //这个在外部实现的有可以不要
+{
 	/* 创建表 */
 	char *zErrMsg =NULL;	
-	char *sql = " CREATE TABLE variable (\
+	char sql[256] = {0};
+	str_replace_chr(node_name);
+	sprintf(sql," CREATE TABLE %s (\
 		ID INTEDER PRIMARY KEY,\
-		name VARCHAR(64),\
 		datatype INTEGER,\
 		result VARCHAR(64),\
 		time INTEGER\
-	);" ; 
+	);",node_name);
     sqlite3_exec(db,sql,NULL,NULL,&zErrMsg);
 	sqlite3_free(zErrMsg);
 	/* 创建索引 */
-	char *index =  " CREATE INDEX variable_index ON variable (name,time);";
+	sprintf(sql,"CREATE INDEX variable_index ON %s (time);",node_name);
     sqlite3_exec(db,index,NULL,NULL,&zErrMsg);
 	sqlite3_free(zErrMsg);
+}
 
+UA_HistoryDataBackend
+UA_HistoryDataBackend_Sqlite(char* path) {
+	//初始化数据库
+	sqlite3 *db;
+	if (sqlite3_open(path, &db))
+    {
+		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);
 	sqlite3_free(zErrMsg);
-- 
2.17.1

