Browse Source

changed remove method to int,add a modify for egsave interface,new SaveTable for godot UI

master
jkpete 2 months ago
parent
commit
e6af6d264b
  1. 9
      addons/EGFramework/Module/GenerateTools/Templete/Godot/UI/EGodotEditParam.cs
  2. 72
      addons/EGFramework/Module/GenerateTools/Templete/Godot/UI/EGodotSaveTable.cs
  3. 2
      addons/EGFramework/Module/GenerateTools/Templete/Godot/UI/EGodotTable.cs
  4. 57
      addons/EGFramework/Module/SaveTools/Data/EGCsvSave.cs
  5. 43
      addons/EGFramework/Module/SaveTools/Data/EGDapper.cs
  6. 25
      addons/EGFramework/Module/SaveTools/Data/EGLiteDBSave.cs
  7. 4
      addons/EGFramework/Module/SaveTools/EGSave.cs
  8. 3
      addons/EGFramework/Module/SaveTools/SaveToolsInterface.cs

9
addons/EGFramework/Module/GenerateTools/Templete/Godot/UI/EGodotEditParam.cs

@ -18,6 +18,15 @@ namespace EGFramework.UI
public override void Init(KeyValuePair<string, object> editValue) public override void Init(KeyValuePair<string, object> editValue)
{ {
base.Init(editValue); base.Init(editValue);
if (editValue.Key == "id" || editValue.Key == "ID" || editValue.Key == "Id")
{
this.ParamReadOnly = new Label();
ParamReadOnly.Name = "ParamReadOnly";
ParamReadOnly.SizeFlagsHorizontal = SizeFlags.ExpandFill;
ParamReadOnly.Text = editValue.Value.ToString();
this.AddChild(ParamReadOnly);
return;
}
if (editValue.Value is string || editValue.Value is null) if (editValue.Value is string || editValue.Value is null)
{ {
this.ParamEdit = new LineEdit(); this.ParamEdit = new LineEdit();

72
addons/EGFramework/Module/GenerateTools/Templete/Godot/UI/EGodotSaveTable.cs

@ -1,6 +1,7 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using Godot; using Godot;
using MySqlX.XDevAPI.Relational;
namespace EGFramework.UI namespace EGFramework.UI
{ {
@ -8,6 +9,9 @@ namespace EGFramework.UI
{ {
public IEGSaveData SaveData { set; get; } public IEGSaveData SaveData { set; get; }
public Dictionary<string, string> TitleList { set; get; } = new Dictionary<string, string>(); public Dictionary<string, string> TitleList { set; get; } = new Dictionary<string, string>();
public string CurrentDataKey { set; get; }
public EasyEvent QueryPage { set; get; } = new EasyEvent();
public void InitSaveData<TSaveData>(IEGSaveData eGSaveData) where TSaveData : IEGSaveData public void InitSaveData<TSaveData>(IEGSaveData eGSaveData) where TSaveData : IEGSaveData
{ {
@ -16,7 +20,7 @@ namespace EGFramework.UI
} }
public void InitData<T>(string key) where T : new() public void InitData<T>(string key) where T : new()
{ {
int count = SaveData.GetAll<T>(key).Count(); int count = SaveData.GetDataCount(key);
if (PageAdapter == null) if (PageAdapter == null)
{ {
PageAdapter = new EGodotTablePageAdapter(count, PageLimit); PageAdapter = new EGodotTablePageAdapter(count, PageLimit);
@ -26,10 +30,74 @@ namespace EGFramework.UI
PageAdapter.Reload(count, PageLimit); PageAdapter.Reload(count, PageLimit);
} }
this.Vertical = true; this.Vertical = true;
CurrentDataKey = key;
QueryPage.Register(() => QueryPageData<T>());
InitFunctionMenu(); InitFunctionMenu();
InitTitle(typeof(T).EGenerateDictiontaryByType()); InitTitle(typeof(T).EGenerateDictiontaryByType());
InitRowData(SaveData.GetAll<T>(key).EGenerateDictionaryByGroup()); InitRowData(null);
InitPageMenu(); InitPageMenu();
}
public void QueryPageData<T>() where T : new()
{
if (PageAdapter.CurrentPage <= 1)
{
PageAdapter.CurrentPage = 1;
}
TableData = SaveData.GetPage<T>(CurrentDataKey, PageAdapter.CurrentPage, PageAdapter.PageLimit).EGenerateDictionaryByGroup();
GD.Print(TableData.Count());
}
public void ModifyData(Dictionary<string, object> eData)
{
string primaryKey = "";
if (eData.ContainsKey("ID")) primaryKey = "ID";
if (eData.ContainsKey("id")) primaryKey = "id";
if (eData.ContainsKey("Id")) primaryKey = "Id";
if (primaryKey == "")
{
this.EGAlert("Parmary key 'id' not defined!", "Error");
return;
}
SaveData.UpdateData(CurrentDataKey, eData, eData[primaryKey]);
InitPageData();
}
public override void InitPageData()
{
RowDataContainer.ClearChildren();
QueryPage.Invoke();
int pointer = 0;
foreach (Dictionary<string, object> data in TableData)
{
EGodotTableRowData rowData = RowDataContainer.CreateNode<EGodotTableRowData>("row" + pointer);
rowData.Init(data);
rowData.OnModify.Register(eData =>
{
this.EGEditDialog(eData, ModifyData, "Modify");
});
rowData.OnDelete.Register(() =>
{
string primaryKey = "";
if (rowData.GetData().ContainsKey("ID")) primaryKey = "ID";
if (rowData.GetData().ContainsKey("id")) primaryKey = "id";
if (rowData.GetData().ContainsKey("Id")) primaryKey = "Id";
if (primaryKey == "")
{
this.EGAlert("Parmary key 'id' not defined!","Error");
return;
}
int remove_count = SaveData.RemoveData(CurrentDataKey, rowData.GetData()[primaryKey]);
PageAdapter.DataLength -= remove_count;
PageAdapter.Reload(PageAdapter.DataLength, PageLimit);
InitPageData();
OnPageChanged.Invoke();
});
}
//base.InitPageData();
} }
} }
} }

2
addons/EGFramework/Module/GenerateTools/Templete/Godot/UI/EGodotTable.cs

@ -176,6 +176,8 @@ namespace EGFramework.UI
Label labelPage = PageContainer.CreateNode<Label>("page"); Label labelPage = PageContainer.CreateNode<Label>("page");
labelPage.Text = "page"; labelPage.Text = "page";
firstPage.Disabled = true;
lastPage.Disabled = true;
PageChangedRealease = this.OnPageChanged.Register(() => PageChangedRealease = this.OnPageChanged.Register(() =>
{ {

57
addons/EGFramework/Module/SaveTools/Data/EGCsvSave.cs

@ -279,27 +279,36 @@ namespace EGFramework
this.WriteDataBlock(DefaultPath); this.WriteDataBlock(DefaultPath);
} }
public void RemoveData<TData>(string dataKey, object id) public int RemoveData(string dataKey, object id)
{
if (IsReadOnly)
{ {
if(IsReadOnly){
throw new Exception("This file is readonly! can't set any data to file."); throw new Exception("This file is readonly! can't set any data to file.");
} }
bool IsAdd = false; bool IsAdd = false;
int dataID = 0; int dataID = 0;
if(id.GetType()==typeof(int)){ if (id.GetType() == typeof(int))
{
dataID = (int)id; dataID = (int)id;
}else if(int.TryParse(id.ToString() ,out dataID)){ }
else if (int.TryParse(id.ToString(), out dataID))
{
throw new Exception("Id cannot be convert to int!"); throw new Exception("Id cannot be convert to int!");
} }
if(dataID>=CsvDataBlock.Count() || dataID < 0){ if (dataID >= CsvDataBlock.Count() || dataID < 0)
{
IsAdd = true; IsAdd = true;
} }
if(IsAdd){ if (IsAdd)
return; {
}else{ return 0;
}
else
{
CsvDataBlock.RemoveAt(dataID); CsvDataBlock.RemoveAt(dataID);
} }
this.WriteDataBlock(DefaultPath); this.WriteDataBlock(DefaultPath);
return 1;
} }
public void UpdateData<TData>(string dataKey, TData data, object id) public void UpdateData<TData>(string dataKey, TData data, object id)
@ -332,6 +341,36 @@ namespace EGFramework
this.WriteDataBlock(DefaultPath); this.WriteDataBlock(DefaultPath);
} }
public void UpdateData(string dataKey, Dictionary<string, object> data, object id)
{
if(IsReadOnly){
throw new Exception("This file is readonly! can't set any data to file.");
}
bool IsAdd = false;
int dataID = 0;
if(id.GetType()==typeof(int)){
dataID = (int)id;
}else if(int.TryParse(id.ToString() ,out dataID)){
throw new Exception("Id cannot be convert to int!");
}
if(dataID>=CsvDataBlock.Count() || dataID < 0){
IsAdd = true;
}
string[] csvSet = new string[CsvDataHeader.Keys.Count()];
foreach(KeyValuePair<string,object> param in data){
if(CsvDataHeader.ContainsKey(param.Key)){
csvSet[CsvDataHeader[param.Key]] = param.Value.ToString();
}
}
if(!IsAdd){
CsvDataBlock[dataID] = csvSet;
}else{
throw new Exception("Data not found!");
}
this.WriteDataBlock(DefaultPath);
}
public IEnumerable<string> GetKeys() public IEnumerable<string> GetKeys()
{ {
return CsvDataHeader.Keys; return CsvDataHeader.Keys;
@ -351,6 +390,8 @@ namespace EGFramework
{ {
return CsvDataBlock.Count(); return CsvDataBlock.Count();
} }
} }
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field)] [AttributeUsage(AttributeTargets.Property | AttributeTargets.Field)]

43
addons/EGFramework/Module/SaveTools/Data/EGDapper.cs

@ -26,7 +26,7 @@ namespace EGFramework
pageIndex = 1; pageIndex = 1;
} }
int startPointer = (pageIndex - 1) * pageSize; int startPointer = (pageIndex - 1) * pageSize;
IEnumerable<TData> result = Connection.Query<TData>("select * from "+dataKey+" limit "+startPointer+","+pageIndex); IEnumerable<TData> result = Connection.Query<TData>("select * from "+dataKey+" limit "+startPointer+","+pageSize);
return result; return result;
} }
@ -95,9 +95,10 @@ namespace EGFramework
//EG.Print("count:" + count); //EG.Print("count:" + count);
} }
public void RemoveData<TData>(string dataKey, object id) public int RemoveData(string dataKey, object id)
{ {
int count = Connection.Execute(@"delete from "+dataKey+" where ID = @ID",new {ID = id}); int count = Connection.Execute(@"delete from " + dataKey + " where id = @ID", new { ID = id });
return count;
//EG.Print("count:" + count); //EG.Print("count:" + count);
} }
@ -108,22 +109,51 @@ namespace EGFramework
throw new ArgumentNullException(nameof(data)); throw new ArgumentNullException(nameof(data));
} }
Type DataType = typeof(TData); Type DataType = typeof(TData);
EG.Print("----"+DataType.Name);
var properties = DataType.GetProperties(); var properties = DataType.GetProperties();
string keyMap = ""; string keyMap = "";
foreach(PropertyInfo key in properties){ foreach(PropertyInfo key in properties){
if(key.Name=="ID"){ if(key.Name=="ID" || key.Name == "id" || key.Name == "Id"){
continue; continue;
} }
keyMap += key.Name + " = @"+key.Name +","; keyMap += key.Name + " = @"+key.Name +",";
} }
keyMap = keyMap.TrimEnd(','); keyMap = keyMap.TrimEnd(',');
string sql = @"update "+DataType.Name+" set "+ keyMap +" where ID = " + id; string sql = @"update "+dataKey+" set "+ keyMap +" where ID = " + id;
EG.Print(sql); EG.Print(sql);
int count = Connection.Execute(sql,data); int count = Connection.Execute(sql,data);
//EG.Print("count:" + count); //EG.Print("count:" + count);
} }
public void UpdateData(string dataKey, Dictionary<string, object> data, object id)
{
if (data == null)
{
throw new ArgumentNullException(nameof(data));
}
string keyMap = "";
foreach (KeyValuePair<string, object> param in data) {
if (param.Key == "ID" || param.Key == "Id" || param.Key == "id"){
continue;
}
if (param.Value is string)
{
keyMap += param.Key + " = '"+param.Value +"',";
}
else
{
keyMap += param.Key + " = "+param.Value +",";
}
}
keyMap = keyMap.TrimEnd(',');
if (id is string)
{
id = "'" + id + "'";
}
string sql = @"update "+dataKey+" set "+ keyMap +" where ID = " + id;
EG.Print(sql);
int count = Connection.Execute(sql,data);
}
public IEnumerable<string> GetKeys() public IEnumerable<string> GetKeys()
{ {
IEnumerable<string> result = Connection.Query<string>("show tables"); IEnumerable<string> result = Connection.Query<string>("show tables");
@ -162,5 +192,6 @@ namespace EGFramework
{ {
return Connection; return Connection;
} }
} }
} }

25
addons/EGFramework/Module/SaveTools/Data/EGLiteDBSave.cs

@ -83,12 +83,19 @@ namespace EGFramework
collection.Insert(data); collection.Insert(data);
} }
public void RemoveData<TData>(string dataKey,object id) public int RemoveData(string dataKey, object id)
{
ILiteCollection<BsonDocument> collection = Database.GetCollection(dataKey);
if (collection.FindById((BsonValue)id) != null)
{ {
LiteCollection<TData> collection = (LiteCollection<TData>)Database.GetCollection<TData>(dataKey);
if(collection.FindById((BsonValue)id)==null){
collection.Delete((BsonValue)id); collection.Delete((BsonValue)id);
return 1;
} }
else
{
return 0;
}
} }
public void UpdateData<TData>(string dataKey, TData data, object id) public void UpdateData<TData>(string dataKey, TData data, object id)
@ -96,6 +103,17 @@ namespace EGFramework
LiteCollection<TData> collection = (LiteCollection<TData>)Database.GetCollection<TData>(dataKey); LiteCollection<TData> collection = (LiteCollection<TData>)Database.GetCollection<TData>(dataKey);
collection.Update((BsonValue)id,data); collection.Update((BsonValue)id,data);
} }
public void UpdateData(string dataKey, Dictionary<string, object> data, object id)
{
ILiteCollection<BsonDocument> collection = Database.GetCollection(dataKey);
BsonDocument keyValuePairs = new BsonDocument();
foreach (var param in data) {
keyValuePairs.Add(param.Key, new BsonValue(param.Value));
}
collection.Update((BsonValue)id,keyValuePairs);
}
public IEnumerable<string> GetKeys() public IEnumerable<string> GetKeys()
{ {
return Database.GetCollectionNames(); return Database.GetCollectionNames();
@ -115,5 +133,6 @@ namespace EGFramework
{ {
return Database.GetCollection(dataKey).Count(); return Database.GetCollection(dataKey).Count();
} }
} }
} }

4
addons/EGFramework/Module/SaveTools/EGSave.cs

@ -359,9 +359,9 @@ namespace EGFramework
throw new Exception("File not loaded, you should use LoadObjectFile(key) first."); throw new Exception("File not loaded, you should use LoadObjectFile(key) first.");
} }
} }
public void RemoveData<TData>(string path,string dataKey,int id){ public void RemoveData(string path,string dataKey,int id){
if(DataBaseFiles.ContainsKey(path)){ if(DataBaseFiles.ContainsKey(path)){
DataBaseFiles[path].RemoveData<TData>(dataKey,id); DataBaseFiles[path].RemoveData(dataKey,id);
}else{ }else{
throw new Exception("File not loaded, you should use LoadDataFile(path) first."); throw new Exception("File not loaded, you should use LoadDataFile(path) first.");
} }

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

@ -77,7 +77,8 @@ namespace EGFramework
void SetData<TData>(string dataKey,TData data,object id); void SetData<TData>(string dataKey,TData data,object id);
void AddData<TData>(string dataKey,TData data); void AddData<TData>(string dataKey,TData data);
void AddData<TData>(string dataKey,IEnumerable<TData> data); void AddData<TData>(string dataKey,IEnumerable<TData> data);
void RemoveData<TData>(string dataKey,object id); int RemoveData(string dataKey,object id);
void UpdateData(string dataKey, Dictionary<string, object> data, object id);
void UpdateData<TData>(string dataKey,TData data,object id); void UpdateData<TData>(string dataKey,TData data,object id);
} }

Loading…
Cancel
Save