From 5d19433e1583d2378ec4b465ef29e5d85fd17edb Mon Sep 17 00:00:00 2001
From: jkpete <1031139173@qq.com>
Date: Tue, 15 Apr 2025 16:30:51 +0800
Subject: [PATCH] old egsqlite and webdav move to other tools,absolute the
egdapper for sqlite and mysql
---
EGFramework.csproj | 1 +
addons/EGFramework/Module/EGSave.cs | 100 +++++++++++-
.../{SaveTools => OtherTools}/EGSqlite.cs | 0
.../Module/{WebDav => OtherTools}/EGWebDav.cs | 0
.../EGFramework/Module/SaveTools/EGDapper.cs | 153 ++++++++++++++++++
.../Module/SaveTools/EGMysqlSave.cs | 134 +--------------
.../Module/SaveTools/EGSqliteSave.cs | 135 +---------------
.../Module/SaveTools/SaveToolsInterface.cs | 5 +
8 files changed, 258 insertions(+), 270 deletions(-)
rename addons/EGFramework/Module/{SaveTools => OtherTools}/EGSqlite.cs (100%)
rename addons/EGFramework/Module/{WebDav => OtherTools}/EGWebDav.cs (100%)
create mode 100644 addons/EGFramework/Module/SaveTools/EGDapper.cs
diff --git a/EGFramework.csproj b/EGFramework.csproj
index b16baa5..6f1ba14 100644
--- a/EGFramework.csproj
+++ b/EGFramework.csproj
@@ -19,5 +19,6 @@
+
\ No newline at end of file
diff --git a/addons/EGFramework/Module/EGSave.cs b/addons/EGFramework/Module/EGSave.cs
index 3517891..2e53dc8 100644
--- a/addons/EGFramework/Module/EGSave.cs
+++ b/addons/EGFramework/Module/EGSave.cs
@@ -1,6 +1,7 @@
using System;
using System.IO;
using System.Collections.Generic;
+using Dapper;
namespace EGFramework
{
@@ -113,6 +114,11 @@ namespace EGFramework
#endregion
#region Keys Operation
+
+ ///
+ /// Get all file keys which has been loaded.
+ ///
+ ///
public List GetKeys(){
List keys = new List();
foreach(string key in DataBaseReadOnly.Keys){
@@ -130,6 +136,36 @@ namespace EGFramework
return keys;
}
+ ///
+ /// Get all data file keys (or relational database in remote such as mysql) which has been loaded.
+ ///
+ ///
+ public List GetDataKeys(){
+ List keys = new List();
+ foreach(string key in DataBaseReadOnly.Keys){
+ keys.Add(key);
+ }
+ foreach(string key in DataBaseFiles.Keys){
+ keys.Add(key);
+ }
+ return keys;
+ }
+
+ ///
+ /// Get all object file keys (or key-value database in remote such as redis) which has been loaded.
+ ///
+ ///
+ public List GetObjectKeys(){
+ List keys = new List();
+ foreach(string key in ObjectReadOnly.Keys){
+ keys.Add(key);
+ }
+ foreach(string key in ObjectFiles.Keys){
+ keys.Add(key);
+ }
+ return keys;
+ }
+
#endregion
#region Get or Search data and object
@@ -161,6 +197,16 @@ namespace EGFramework
throw new Exception("File not loaded, you should use LoadDataFile(key) or ReadData(key,data) first.");
}
}
+
+ public IEnumerable FindData(string keyOrPath,string key,System.Linq.Expressions.Expression> expression) where TData : new(){
+ if(DataBaseFiles.ContainsKey(keyOrPath)){
+ return DataBaseFiles[keyOrPath].FindData(key,expression);
+ }else if(DataBaseReadOnly.ContainsKey(keyOrPath)){
+ return DataBaseReadOnly[keyOrPath].FindData(key,expression);
+ }else{
+ throw new Exception("File not loaded, you should use LoadDataFile(key) or ReadData(key,data) first.");
+ }
+ }
#endregion
#region Set or Add or Update data and object
@@ -180,16 +226,56 @@ namespace EGFramework
}
}
-
- public IEnumerable FindData(string keyOrPath,string key,System.Linq.Expressions.Expression> expression) where TData : new(){
- if(DataBaseFiles.ContainsKey(keyOrPath)){
- return DataBaseFiles[keyOrPath].FindData(key,expression);
- }else if(DataBaseReadOnly.ContainsKey(keyOrPath)){
- return DataBaseReadOnly[keyOrPath].FindData(key,expression);
+ public void AddObject(string path,string objectKey,TObject obj){
+ if(ObjectFiles.ContainsKey(path)){
+ ObjectFiles[path].AddObject(objectKey,obj);
}else{
- throw new Exception("File not loaded, you should use LoadDataFile(key) or ReadData(key,data) first.");
+ throw new Exception("File not loaded, you should use LoadObjectFile(key) first.");
+ }
+ }
+ public void AddData(string path,string dataKey,TData data){
+ if(DataBaseFiles.ContainsKey(path)){
+ DataBaseFiles[path].AddData(dataKey,data);
+ }else{
+ throw new Exception("File not loaded, you should use LoadDataFile(path) first.");
}
}
+ public void AddData(string path,string dataKey,IEnumerable data){
+ if(DataBaseFiles.ContainsKey(path)){
+ DataBaseFiles[path].AddData(dataKey,data);
+ }else{
+ throw new Exception("File not loaded, you should use LoadDataFile(path) first.");
+ }
+ }
+ public void UpdateObject(string path,string objectKey,TObject obj){
+ if(ObjectFiles.ContainsKey(path)){
+ ObjectFiles[path].UpdateObject(objectKey,obj);
+ }else{
+ throw new Exception("File not loaded, you should use LoadObjectFile(key) first.");
+ }
+ }
+ public void UpdateData(string path,string dataKey,TData data,int id){
+ if(DataBaseFiles.ContainsKey(path)){
+ DataBaseFiles[path].UpdateData(dataKey,data,id);
+ }else{
+ throw new Exception("File not loaded, you should use LoadDataFile(path) first.");
+ }
+ }
+ public void RemoveObject(string path,string objectKey){
+ if(ObjectFiles.ContainsKey(path)){
+ ObjectFiles[path].RemoveObject(objectKey);
+ }else{
+ throw new Exception("File not loaded, you should use LoadObjectFile(key) first.");
+ }
+ }
+ public void RemoveData(string path,string dataKey,int id){
+ if(DataBaseFiles.ContainsKey(path)){
+ DataBaseFiles[path].RemoveData(dataKey,id);
+ }else{
+ throw new Exception("File not loaded, you should use LoadDataFile(path) first.");
+ }
+ }
+
#endregion
#region Default Json Operation
diff --git a/addons/EGFramework/Module/SaveTools/EGSqlite.cs b/addons/EGFramework/Module/OtherTools/EGSqlite.cs
similarity index 100%
rename from addons/EGFramework/Module/SaveTools/EGSqlite.cs
rename to addons/EGFramework/Module/OtherTools/EGSqlite.cs
diff --git a/addons/EGFramework/Module/WebDav/EGWebDav.cs b/addons/EGFramework/Module/OtherTools/EGWebDav.cs
similarity index 100%
rename from addons/EGFramework/Module/WebDav/EGWebDav.cs
rename to addons/EGFramework/Module/OtherTools/EGWebDav.cs
diff --git a/addons/EGFramework/Module/SaveTools/EGDapper.cs b/addons/EGFramework/Module/SaveTools/EGDapper.cs
new file mode 100644
index 0000000..745ed95
--- /dev/null
+++ b/addons/EGFramework/Module/SaveTools/EGDapper.cs
@@ -0,0 +1,153 @@
+using System;
+using System.Collections.Generic;
+using System.Data.Common;
+using System.Linq;
+using System.Linq.Expressions;
+using System.Reflection;
+using Dapper;
+
+namespace EGFramework
+{
+
+ public abstract class EGDapper : IEGSave, IEGSaveData, IEGCanGetDBConnection
+ {
+ public string DBName = "Default";
+ private string DefaultDBFolder = "SaveData";
+ public DbConnection Connection { get; set; }
+ public string ExceptionMsg;
+
+ public abstract void InitSave(string conn);
+ public IEnumerable GetAll(string dataKey) where TData : new()
+ {
+ IEnumerable result = Connection.Query("select * from "+dataKey);
+ return result;
+ }
+
+ public TData GetData(string dataKey, object id) where TData : new()
+ {
+ TData result = Connection.QuerySingle("select * from "+dataKey+" where ID = @ID",new {ID = id});
+ return result;
+ }
+
+ public IEnumerable FindData(string dataKey, Expression> expression) where TData : new()
+ {
+ IEnumerable sourceList = Connection.Query("select * from "+dataKey);
+ return sourceList.Where(expression.Compile());
+ }
+
+ public void SetData(string dataKey, TData data, object id)
+ {
+ if(data == null)
+ {
+ throw new ArgumentNullException(nameof(data));
+ }else{
+ if(this.ContainsData(dataKey,id)){
+ UpdateData(dataKey,data,id);
+ }else{
+ AddData(dataKey,data);
+ }
+ //EG.Print("data:" + data);
+ }
+ }
+
+ public void AddData(string dataKey, TData data)
+ {
+ if(data == null)
+ {
+ throw new ArgumentNullException(nameof(data));
+ }
+ // throw new System.NotImplementedException();
+ Type DataType = typeof(TData);
+ var properties = DataType.GetProperties();
+ string keySet = "";
+ string keySetParam = "";
+ foreach(PropertyInfo key in properties){
+ keySet += key.Name + ",";
+ keySetParam += "@" + key.Name + ",";
+ }
+ keySet = keySet.TrimEnd(',');
+ keySetParam = keySetParam.TrimEnd(',');
+ int count = Connection.Execute(@"insert "+dataKey+"("+keySet+") values("+keySetParam+")",data);
+ //EG.Print("count:" + count);
+ }
+
+ public void AddData(string dataKey, IEnumerable data)
+ {
+ Type DataType = typeof(TData);
+ var properties = DataType.GetProperties();
+ string keySet = "";
+ string keySetParam = "";
+ foreach(PropertyInfo key in properties){
+ keySet += key.Name + ",";
+ keySetParam += "@" + key.Name + ",";
+ }
+ keySet = keySet.TrimEnd(',');
+ keySetParam = keySetParam.TrimEnd(',');
+ string sql = @"insert "+dataKey+"("+keySet+") values("+keySetParam+")";
+ int count = Connection.Execute(sql,data);
+ //EG.Print("count:" + count);
+ }
+
+ public void RemoveData(string dataKey, object id)
+ {
+ int count = Connection.Execute(@"delete from "+dataKey+" where ID = @ID",new {ID = id});
+ //EG.Print("count:" + count);
+ }
+
+ public void UpdateData(string dataKey, TData data, object id)
+ {
+ if(data == null)
+ {
+ throw new ArgumentNullException(nameof(data));
+ }
+ Type DataType = typeof(TData);
+ EG.Print("----"+DataType.Name);
+ var properties = DataType.GetProperties();
+ string keyMap = "";
+ foreach(PropertyInfo key in properties){
+ if(key.Name=="ID"){
+ continue;
+ }
+ keyMap += key.Name + " = @"+key.Name +",";
+ }
+ keyMap = keyMap.TrimEnd(',');
+ string sql = @"update "+DataType.Name+" set "+ keyMap +" where ID = " + id;
+ EG.Print(sql);
+ int count = Connection.Execute(sql,data);
+ //EG.Print("count:" + count);
+ }
+
+ public IEnumerable GetKeys()
+ {
+ IEnumerable result = Connection.Query("show tables");
+ return result;
+ }
+
+ public bool ContainsKey(string dataKey)
+ {
+ return GetKeys().Contains(dataKey);
+ }
+
+ public bool ContainsData(string dataKey, object id)
+ {
+ try
+ {
+ var result = Connection.QuerySingle("select * from "+dataKey+" where ID = @ID",new {ID = id});
+ if(result == null){
+ return false;
+ }else{
+ return true;
+ }
+ }
+ catch (System.Exception)
+ {
+ return false;
+ }
+ }
+
+ public DbConnection GetConnection()
+ {
+ return Connection;
+ }
+ }
+}
\ No newline at end of file
diff --git a/addons/EGFramework/Module/SaveTools/EGMysqlSave.cs b/addons/EGFramework/Module/SaveTools/EGMysqlSave.cs
index 2834805..a716178 100644
--- a/addons/EGFramework/Module/SaveTools/EGMysqlSave.cs
+++ b/addons/EGFramework/Module/SaveTools/EGMysqlSave.cs
@@ -5,22 +5,22 @@ using MySql.Data.MySqlClient;
using Dapper;
using System.Reflection;
using System.Linq;
+using System.Data.Common;
//ORM Save tools. First support SQLite and MySQL,In future we will support other Database who implement DBConnection.
namespace EGFramework{
///
/// This Class used Dapper for operate MySQL database.
///
- public class EGMysqlSave : IEGSave, IEGSaveData
+ public class EGMysqlSave : EGDapper
{
private string Conn { set; get; }
- public MySqlConnection Connection { set; get; }
public bool IsInit { set; get; }
///
/// "server="+Address+";port="+Port+";uid="+UserName+";pwd="+Password+";database="+DataBase+";"
///
/// files conn Str or address ip port,username and passwd
- public void InitSave(string conn)
+ public override void InitSave(string conn)
{
try
{
@@ -32,133 +32,5 @@ namespace EGFramework{
EG.Print("e:" + e);
}
}
-
- public IEnumerable GetAll(string dataKey) where TData : new()
- {
- IEnumerable result = Connection.Query("select * from "+dataKey);
- return result;
- }
-
- public TData GetData(string dataKey, object id) where TData : new()
- {
- TData result = Connection.QuerySingle("select * from "+dataKey+" where ID = @ID",new {ID = id});
- return result;
- }
-
- public IEnumerable FindData(string dataKey, Expression> expression) where TData : new()
- {
- IEnumerable sourceList = Connection.Query("select * from "+dataKey);
- return sourceList.Where(expression.Compile());
- }
-
- public void SetData(string dataKey, TData data, object id)
- {
- if(data == null)
- {
- throw new ArgumentNullException(nameof(data));
- }else{
- if(this.ContainsData(dataKey,id)){
- UpdateData(dataKey,data,id);
- }else{
- AddData(dataKey,data);
- }
- //EG.Print("data:" + data);
- }
- }
-
- public void AddData(string dataKey, TData data)
- {
- if(data == null)
- {
- throw new ArgumentNullException(nameof(data));
- }
- // throw new System.NotImplementedException();
- Type DataType = typeof(TData);
- var properties = DataType.GetProperties();
- string keySet = "";
- string keySetParam = "";
- foreach(PropertyInfo key in properties){
- keySet += key.Name + ",";
- keySetParam += "@" + key.Name + ",";
- }
- keySet = keySet.TrimEnd(',');
- keySetParam = keySetParam.TrimEnd(',');
- int count = Connection.Execute(@"insert "+dataKey+"("+keySet+") values("+keySetParam+")",data);
- //EG.Print("count:" + count);
- }
-
- public void AddData(string dataKey, IEnumerable data)
- {
- Type DataType = typeof(TData);
- var properties = DataType.GetProperties();
- string keySet = "";
- string keySetParam = "";
- foreach(PropertyInfo key in properties){
- keySet += key.Name + ",";
- keySetParam += "@" + key.Name + ",";
- }
- keySet = keySet.TrimEnd(',');
- keySetParam = keySetParam.TrimEnd(',');
- string sql = @"insert "+dataKey+"("+keySet+") values("+keySetParam+")";
- int count = Connection.Execute(sql,data);
- //EG.Print("count:" + count);
- }
-
- public void RemoveData(string dataKey, object id)
- {
- int count = Connection.Execute(@"delete from "+dataKey+" where ID = @ID",new {ID = id});
- //EG.Print("count:" + count);
- }
-
- public void UpdateData(string dataKey, TData data, object id)
- {
- if(data == null)
- {
- throw new ArgumentNullException(nameof(data));
- }
- Type DataType = typeof(TData);
- EG.Print("----"+DataType.Name);
- var properties = DataType.GetProperties();
- string keyMap = "";
- foreach(PropertyInfo key in properties){
- if(key.Name=="ID"){
- continue;
- }
- keyMap += key.Name + " = @"+key.Name +",";
- }
- keyMap = keyMap.TrimEnd(',');
- string sql = @"update "+DataType.Name+" set "+ keyMap +" where ID = " + id;
- EG.Print(sql);
- int count = Connection.Execute(sql,data);
- //EG.Print("count:" + count);
- }
-
- public IEnumerable GetKeys()
- {
- IEnumerable result = Connection.Query("show tables");
- return result;
- }
-
- public bool ContainsKey(string dataKey)
- {
- return GetKeys().Contains(dataKey);
- }
-
- public bool ContainsData(string dataKey, object id)
- {
- try
- {
- var result = Connection.QuerySingle("select * from "+dataKey+" where ID = @ID",new {ID = id});
- if(result == null){
- return false;
- }else{
- return true;
- }
- }
- catch (System.Exception)
- {
- return false;
- }
- }
}
}
\ No newline at end of file
diff --git a/addons/EGFramework/Module/SaveTools/EGSqliteSave.cs b/addons/EGFramework/Module/SaveTools/EGSqliteSave.cs
index 5f565a1..7e2636a 100644
--- a/addons/EGFramework/Module/SaveTools/EGSqliteSave.cs
+++ b/addons/EGFramework/Module/SaveTools/EGSqliteSave.cs
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
+using System.Data.Common;
using System.IO;
using System.Linq;
using System.Linq.Expressions;
@@ -8,18 +9,16 @@ using Dapper;
using Microsoft.Data.Sqlite;
namespace EGFramework{
- public class EGSqliteSave : IEGSave, IEGSaveData
+ public class EGSqliteSave : EGDapper
{
private string Conn { set; get; }
- public SqliteConnection Connection { set; get; }
public bool IsInit { set; get; }
- public string ExceptionMsg;
///
/// If path not exist, create it.
///
///
- public void InitSave(string path)
+ public override void InitSave(string path)
{
if (!Directory.Exists(path))
{
@@ -44,133 +43,5 @@ namespace EGFramework{
ExceptionMsg = ex.ToString();
}
}
-
- public IEnumerable GetAll(string dataKey) where TData : new()
- {
- IEnumerable result = Connection.Query("select * from "+dataKey);
- return result;
- }
-
- public TData GetData(string dataKey, object id) where TData : new()
- {
- TData result = Connection.QuerySingle("select * from "+dataKey+" where ID = @ID",new {ID = id});
- return result;
- }
-
- public IEnumerable FindData(string dataKey, Expression> expression) where TData : new()
- {
- IEnumerable sourceList = Connection.Query("select * from "+dataKey);
- return sourceList.Where(expression.Compile());
- }
-
- public void SetData(string dataKey, TData data, object id)
- {
- if(data == null)
- {
- throw new ArgumentNullException(nameof(data));
- }else{
- if(this.ContainsData(dataKey,id)){
- UpdateData(dataKey,data,id);
- }else{
- AddData(dataKey,data);
- }
- //EG.Print("data:" + data);
- }
- }
-
- public void AddData(string dataKey, TData data)
- {
- if(data == null)
- {
- throw new ArgumentNullException(nameof(data));
- }
- // throw new System.NotImplementedException();
- Type DataType = typeof(TData);
- var properties = DataType.GetProperties();
- string keySet = "";
- string keySetParam = "";
- foreach(PropertyInfo key in properties){
- keySet += key.Name + ",";
- keySetParam += "@" + key.Name + ",";
- }
- keySet = keySet.TrimEnd(',');
- keySetParam = keySetParam.TrimEnd(',');
- int count = Connection.Execute(@"insert "+dataKey+"("+keySet+") values("+keySetParam+")",data);
- //EG.Print("count:" + count);
- }
-
- public void AddData(string dataKey, IEnumerable data)
- {
- Type DataType = typeof(TData);
- var properties = DataType.GetProperties();
- string keySet = "";
- string keySetParam = "";
- foreach(PropertyInfo key in properties){
- keySet += key.Name + ",";
- keySetParam += "@" + key.Name + ",";
- }
- keySet = keySet.TrimEnd(',');
- keySetParam = keySetParam.TrimEnd(',');
- string sql = @"insert "+dataKey+"("+keySet+") values("+keySetParam+")";
- int count = Connection.Execute(sql,data);
- //EG.Print("count:" + count);
- }
-
- public void RemoveData(string dataKey, object id)
- {
- int count = Connection.Execute(@"delete from "+dataKey+" where ID = @ID",new {ID = id});
- //EG.Print("count:" + count);
- }
-
- public void UpdateData(string dataKey, TData data, object id)
- {
- if(data == null)
- {
- throw new ArgumentNullException(nameof(data));
- }
- Type DataType = typeof(TData);
- EG.Print("----"+DataType.Name);
- var properties = DataType.GetProperties();
- string keyMap = "";
- foreach(PropertyInfo key in properties){
- if(key.Name=="ID"){
- continue;
- }
- keyMap += key.Name + " = @"+key.Name +",";
- }
- keyMap = keyMap.TrimEnd(',');
- string sql = @"update "+DataType.Name+" set "+ keyMap +" where ID = " + id;
- EG.Print(sql);
- int count = Connection.Execute(sql,data);
- //EG.Print("count:" + count);
- }
-
- public IEnumerable GetKeys()
- {
- IEnumerable result = Connection.Query("show tables");
- return result;
- }
-
- public bool ContainsKey(string dataKey)
- {
- return GetKeys().Contains(dataKey);
- }
-
- public bool ContainsData(string dataKey, object id)
- {
- try
- {
- var result = Connection.QuerySingle("select * from "+dataKey+" where ID = @ID",new {ID = id});
- if(result == null){
- return false;
- }else{
- return true;
- }
- }
- catch (System.Exception)
- {
- return false;
- }
- }
}
}
\ No newline at end of file
diff --git a/addons/EGFramework/Module/SaveTools/SaveToolsInterface.cs b/addons/EGFramework/Module/SaveTools/SaveToolsInterface.cs
index 19c6086..01e64a0 100644
--- a/addons/EGFramework/Module/SaveTools/SaveToolsInterface.cs
+++ b/addons/EGFramework/Module/SaveTools/SaveToolsInterface.cs
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
+using System.Data.Common;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;
@@ -16,6 +17,10 @@ namespace EGFramework
void InitReadOnly(byte[] data);
}
+ public interface IEGCanGetDBConnection{
+ DbConnection GetConnection();
+ }
+
public interface IEGSaveObjectReadOnly{
TObject GetObject(string objectKey) where TObject : new();
IEnumerable GetKeys();