Browse Source

old egsqlite and webdav move to other tools,absolute the egdapper for sqlite and mysql

master
jkpete 4 months ago
parent
commit
5d19433e15
  1. 1
      EGFramework.csproj
  2. 100
      addons/EGFramework/Module/EGSave.cs
  3. 0
      addons/EGFramework/Module/OtherTools/EGSqlite.cs
  4. 0
      addons/EGFramework/Module/OtherTools/EGWebDav.cs
  5. 153
      addons/EGFramework/Module/SaveTools/EGDapper.cs
  6. 134
      addons/EGFramework/Module/SaveTools/EGMysqlSave.cs
  7. 135
      addons/EGFramework/Module/SaveTools/EGSqliteSave.cs
  8. 5
      addons/EGFramework/Module/SaveTools/SaveToolsInterface.cs

1
EGFramework.csproj

@ -19,5 +19,6 @@ @@ -19,5 +19,6 @@
<PackageReference Include="MySql.Data" Version="9.1.0" />
<PackageReference Include="Dapper" Version="2.1.35" />
<PackageReference Include="SSH.NET" Version="2024.2.0" />
<PackageReference Include="StackExchange.Redis" Version="2.8.31" />
</ItemGroup>
</Project>

100
addons/EGFramework/Module/EGSave.cs

@ -1,6 +1,7 @@ @@ -1,6 +1,7 @@
using System;
using System.IO;
using System.Collections.Generic;
using Dapper;
namespace EGFramework
{
@ -113,6 +114,11 @@ namespace EGFramework @@ -113,6 +114,11 @@ namespace EGFramework
#endregion
#region Keys Operation
/// <summary>
/// Get all file keys which has been loaded.
/// </summary>
/// <returns></returns>
public List<string> GetKeys(){
List<string> keys = new List<string>();
foreach(string key in DataBaseReadOnly.Keys){
@ -130,6 +136,36 @@ namespace EGFramework @@ -130,6 +136,36 @@ namespace EGFramework
return keys;
}
/// <summary>
/// Get all data file keys (or relational database in remote such as mysql) which has been loaded.
/// </summary>
/// <returns></returns>
public List<string> GetDataKeys(){
List<string> keys = new List<string>();
foreach(string key in DataBaseReadOnly.Keys){
keys.Add(key);
}
foreach(string key in DataBaseFiles.Keys){
keys.Add(key);
}
return keys;
}
/// <summary>
/// Get all object file keys (or key-value database in remote such as redis) which has been loaded.
/// </summary>
/// <returns></returns>
public List<string> GetObjectKeys(){
List<string> keys = new List<string>();
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 @@ -161,6 +197,16 @@ namespace EGFramework
throw new Exception("File not loaded, you should use LoadDataFile(key) or ReadData(key,data) first.");
}
}
public IEnumerable<TData> FindData<TData>(string keyOrPath,string key,System.Linq.Expressions.Expression<Func<TData, bool>> expression) where TData : new(){
if(DataBaseFiles.ContainsKey(keyOrPath)){
return DataBaseFiles[keyOrPath].FindData<TData>(key,expression);
}else if(DataBaseReadOnly.ContainsKey(keyOrPath)){
return DataBaseReadOnly[keyOrPath].FindData<TData>(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 @@ -180,16 +226,56 @@ namespace EGFramework
}
}
public IEnumerable<TData> FindData<TData>(string keyOrPath,string key,System.Linq.Expressions.Expression<Func<TData, bool>> expression) where TData : new(){
if(DataBaseFiles.ContainsKey(keyOrPath)){
return DataBaseFiles[keyOrPath].FindData<TData>(key,expression);
}else if(DataBaseReadOnly.ContainsKey(keyOrPath)){
return DataBaseReadOnly[keyOrPath].FindData<TData>(key,expression);
public void AddObject<TObject>(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<TData>(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<TData>(string path,string dataKey,IEnumerable<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 UpdateObject<TObject>(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<TData>(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<TObject>(string path,string objectKey){
if(ObjectFiles.ContainsKey(path)){
ObjectFiles[path].RemoveObject<TObject>(objectKey);
}else{
throw new Exception("File not loaded, you should use LoadObjectFile(key) first.");
}
}
public void RemoveData<TData>(string path,string dataKey,int id){
if(DataBaseFiles.ContainsKey(path)){
DataBaseFiles[path].RemoveData<TData>(dataKey,id);
}else{
throw new Exception("File not loaded, you should use LoadDataFile(path) first.");
}
}
#endregion
#region Default Json Operation

0
addons/EGFramework/Module/SaveTools/EGSqlite.cs → addons/EGFramework/Module/OtherTools/EGSqlite.cs

0
addons/EGFramework/Module/WebDav/EGWebDav.cs → addons/EGFramework/Module/OtherTools/EGWebDav.cs

153
addons/EGFramework/Module/SaveTools/EGDapper.cs

@ -0,0 +1,153 @@ @@ -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<TData> GetAll<TData>(string dataKey) where TData : new()
{
IEnumerable<TData> result = Connection.Query<TData>("select * from "+dataKey);
return result;
}
public TData GetData<TData>(string dataKey, object id) where TData : new()
{
TData result = Connection.QuerySingle<TData>("select * from "+dataKey+" where ID = @ID",new {ID = id});
return result;
}
public IEnumerable<TData> FindData<TData>(string dataKey, Expression<Func<TData, bool>> expression) where TData : new()
{
IEnumerable<TData> sourceList = Connection.Query<TData>("select * from "+dataKey);
return sourceList.Where(expression.Compile());
}
public void SetData<TData>(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<TData>(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<TData>(string dataKey, IEnumerable<TData> 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<TData>(string dataKey, object id)
{
int count = Connection.Execute(@"delete from "+dataKey+" where ID = @ID",new {ID = id});
//EG.Print("count:" + count);
}
public void UpdateData<TData>(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<string> GetKeys()
{
IEnumerable<string> result = Connection.Query<string>("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;
}
}
}

134
addons/EGFramework/Module/SaveTools/EGMysqlSave.cs

@ -5,22 +5,22 @@ using MySql.Data.MySqlClient; @@ -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{
/// <summary>
/// This Class used Dapper for operate MySQL database.
/// </summary>
public class EGMysqlSave : IEGSave, IEGSaveData
public class EGMysqlSave : EGDapper
{
private string Conn { set; get; }
public MySqlConnection Connection { set; get; }
public bool IsInit { set; get; }
/// <summary>
/// "server="+Address+";port="+Port+";uid="+UserName+";pwd="+Password+";database="+DataBase+";"
/// </summary>
/// <param name="conn">files conn Str or address ip port,username and passwd</param>
public void InitSave(string conn)
public override void InitSave(string conn)
{
try
{
@ -32,133 +32,5 @@ namespace EGFramework{ @@ -32,133 +32,5 @@ namespace EGFramework{
EG.Print("e:" + e);
}
}
public IEnumerable<TData> GetAll<TData>(string dataKey) where TData : new()
{
IEnumerable<TData> result = Connection.Query<TData>("select * from "+dataKey);
return result;
}
public TData GetData<TData>(string dataKey, object id) where TData : new()
{
TData result = Connection.QuerySingle<TData>("select * from "+dataKey+" where ID = @ID",new {ID = id});
return result;
}
public IEnumerable<TData> FindData<TData>(string dataKey, Expression<Func<TData, bool>> expression) where TData : new()
{
IEnumerable<TData> sourceList = Connection.Query<TData>("select * from "+dataKey);
return sourceList.Where(expression.Compile());
}
public void SetData<TData>(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<TData>(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<TData>(string dataKey, IEnumerable<TData> 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<TData>(string dataKey, object id)
{
int count = Connection.Execute(@"delete from "+dataKey+" where ID = @ID",new {ID = id});
//EG.Print("count:" + count);
}
public void UpdateData<TData>(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<string> GetKeys()
{
IEnumerable<string> result = Connection.Query<string>("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;
}
}
}
}

135
addons/EGFramework/Module/SaveTools/EGSqliteSave.cs

@ -1,5 +1,6 @@ @@ -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; @@ -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;
/// <summary>
/// If path not exist, create it.
/// </summary>
/// <param name="path"></param>
public void InitSave(string path)
public override void InitSave(string path)
{
if (!Directory.Exists(path))
{
@ -44,133 +43,5 @@ namespace EGFramework{ @@ -44,133 +43,5 @@ namespace EGFramework{
ExceptionMsg = ex.ToString();
}
}
public IEnumerable<TData> GetAll<TData>(string dataKey) where TData : new()
{
IEnumerable<TData> result = Connection.Query<TData>("select * from "+dataKey);
return result;
}
public TData GetData<TData>(string dataKey, object id) where TData : new()
{
TData result = Connection.QuerySingle<TData>("select * from "+dataKey+" where ID = @ID",new {ID = id});
return result;
}
public IEnumerable<TData> FindData<TData>(string dataKey, Expression<Func<TData, bool>> expression) where TData : new()
{
IEnumerable<TData> sourceList = Connection.Query<TData>("select * from "+dataKey);
return sourceList.Where(expression.Compile());
}
public void SetData<TData>(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<TData>(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<TData>(string dataKey, IEnumerable<TData> 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<TData>(string dataKey, object id)
{
int count = Connection.Execute(@"delete from "+dataKey+" where ID = @ID",new {ID = id});
//EG.Print("count:" + count);
}
public void UpdateData<TData>(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<string> GetKeys()
{
IEnumerable<string> result = Connection.Query<string>("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;
}
}
}
}

5
addons/EGFramework/Module/SaveTools/SaveToolsInterface.cs

@ -1,5 +1,6 @@ @@ -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 @@ -16,6 +17,10 @@ namespace EGFramework
void InitReadOnly(byte[] data);
}
public interface IEGCanGetDBConnection{
DbConnection GetConnection();
}
public interface IEGSaveObjectReadOnly{
TObject GetObject<TObject>(string objectKey) where TObject : new();
IEnumerable<string> GetKeys();

Loading…
Cancel
Save