Browse Source

add drop table by key

master
jkpete 1 month ago
parent
commit
9181ac94c4
  1. 70
      addons/EGFramework/Module/SaveTools/Data/EGDapper.cs
  2. 12
      addons/EGFramework/Module/SaveTools/EGSave.cs
  3. 1
      addons/EGFramework/Module/SaveTools/SaveToolsInterface.cs

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

@ -22,7 +22,8 @@ namespace EGFramework @@ -22,7 +22,8 @@ namespace EGFramework
public IEnumerable<TData> GetPage<TData>(string dataKey, int pageIndex, int pageSize) where TData : new()
{
if(pageIndex <= 0){
if (pageIndex <= 0)
{
pageIndex = 1;
}
int startPointer = (pageIndex - 1) * pageSize;
@ -47,10 +48,15 @@ namespace EGFramework @@ -47,10 +48,15 @@ namespace EGFramework
if (data == null)
{
throw new ArgumentNullException(nameof(data));
}else{
if(this.ContainsData(dataKey,id)){
}
else
{
if (this.ContainsData(dataKey, id))
{
UpdateData(dataKey, data, id);
}else{
}
else
{
AddData(dataKey, data);
}
//EG.Print("data:" + data);
@ -68,7 +74,8 @@ namespace EGFramework @@ -68,7 +74,8 @@ namespace EGFramework
var properties = DataType.GetProperties();
string keySet = "";
string keySetParam = "";
foreach(PropertyInfo key in properties){
foreach (PropertyInfo key in properties)
{
keySet += key.Name + ",";
keySetParam += "@" + key.Name + ",";
}
@ -84,7 +91,8 @@ namespace EGFramework @@ -84,7 +91,8 @@ namespace EGFramework
var properties = DataType.GetProperties();
string keySet = "";
string keySetParam = "";
foreach(PropertyInfo key in properties){
foreach (PropertyInfo key in properties)
{
keySet += key.Name + ",";
keySetParam += "@" + key.Name + ",";
}
@ -102,8 +110,10 @@ namespace EGFramework @@ -102,8 +110,10 @@ namespace EGFramework
}
string keySet = "";
string keySetParam = "";
foreach(KeyValuePair<string,object> param in data){
if (param.Key == "ID" || param.Key == "Id" || param.Key == "id"){
foreach (KeyValuePair<string, object> param in data)
{
if (param.Key == "ID" || param.Key == "Id" || param.Key == "id")
{
continue;
}
keySet += param.Key + ",";
@ -138,8 +148,10 @@ namespace EGFramework @@ -138,8 +148,10 @@ namespace EGFramework
Type DataType = typeof(TData);
var properties = DataType.GetProperties();
string keyMap = "";
foreach(PropertyInfo key in properties){
if(key.Name=="ID" || key.Name == "id" || key.Name == "Id"){
foreach (PropertyInfo key in properties)
{
if (key.Name == "ID" || key.Name == "id" || key.Name == "Id")
{
continue;
}
keyMap += key.Name + " = @" + key.Name + ",";
@ -158,8 +170,10 @@ namespace EGFramework @@ -158,8 +170,10 @@ namespace EGFramework
throw new ArgumentNullException(nameof(data));
}
string keyMap = "";
foreach (KeyValuePair<string, object> param in data) {
if (param.Key == "ID" || param.Key == "Id" || param.Key == "id"){
foreach (KeyValuePair<string, object> param in data)
{
if (param.Key == "ID" || param.Key == "Id" || param.Key == "id")
{
continue;
}
if (param.Value is string)
@ -197,9 +211,12 @@ namespace EGFramework @@ -197,9 +211,12 @@ namespace EGFramework
try
{
var result = Connection.QuerySingle("select * from " + dataKey + " where ID = @ID", new { ID = id });
if(result == null){
if (result == null)
{
return false;
}else{
}
else
{
return true;
}
}
@ -225,8 +242,8 @@ namespace EGFramework @@ -225,8 +242,8 @@ namespace EGFramework
// throw new System.NotImplementedException();
if (this.ContainsKey(dataKey))
{
//Drop table if has.
Connection.Execute(@"DROP TABLE IF EXISTS "+dataKey+"");
EG.Print("Table " + dataKey + " has been created!");
return;
}
string createSql = typeof(TData).ToCreateTableSQL(dataKey);
int count = Connection.Execute(createSql);
@ -234,7 +251,26 @@ namespace EGFramework @@ -234,7 +251,26 @@ namespace EGFramework
public void CreateTable(string dataKey, Dictionary<string, object> tableParam)
{
throw new NotImplementedException();
if (this.ContainsKey(dataKey))
{
EG.Print("Table " + dataKey + " has been created!");
return;
}
Dictionary<string, Type> tableType = new Dictionary<string, Type>();
foreach (KeyValuePair<string, object> pair in tableParam)
{
tableType.Add(pair.Key, pair.Value.GetType());
}
string createSql = tableType.ToCreateTableSQL(dataKey);
int count = Connection.Execute(createSql);
}
public void DropTable(string dataKey)
{
if (this.ContainsKey(dataKey))
{
Connection.Execute(@"DROP TABLE IF EXISTS "+dataKey+"");
}
}
}
}

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

@ -36,7 +36,7 @@ namespace EGFramework @@ -36,7 +36,7 @@ namespace EGFramework
LoadObjectFile<EGJsonSave>("SaveData/DefaultJsonSave.json");
}
#region Load Data or Object and Unload
public IEGSaveData LoadDataFile<TSaveData>(string path) where TSaveData : IEGSaveData, IEGSave, new()
public TSaveData LoadDataFile<TSaveData>(string path) where TSaveData : IEGSaveData, IEGSave, new()
{
TSaveData saveData = new TSaveData();
saveData.InitSave(path);
@ -51,7 +51,7 @@ namespace EGFramework @@ -51,7 +51,7 @@ namespace EGFramework
return saveData;
}
public IEGSaveDataReadOnly ReadData<TReadOnlyData>(string key, string data) where TReadOnlyData : IEGSaveDataReadOnly, IEGSaveReadOnly, new()
public TReadOnlyData ReadData<TReadOnlyData>(string key, string data) where TReadOnlyData : IEGSaveDataReadOnly, IEGSaveReadOnly, new()
{
TReadOnlyData readOnlyData = new TReadOnlyData();
readOnlyData.InitReadOnly(data);
@ -66,7 +66,7 @@ namespace EGFramework @@ -66,7 +66,7 @@ namespace EGFramework
return readOnlyData;
}
public IEGSaveDataReadOnly ReadData<TReadOnlyData>(string key, byte[] data) where TReadOnlyData : IEGSaveDataReadOnly, IEGSaveReadOnly, new()
public TReadOnlyData ReadData<TReadOnlyData>(string key, byte[] data) where TReadOnlyData : IEGSaveDataReadOnly, IEGSaveReadOnly, new()
{
TReadOnlyData readOnlyData = new TReadOnlyData();
readOnlyData.InitReadOnly(data);
@ -81,7 +81,7 @@ namespace EGFramework @@ -81,7 +81,7 @@ namespace EGFramework
return readOnlyData;
}
public IEGSaveObject LoadObjectFile<TSaveObject>(string path) where TSaveObject:IEGSaveObject,IEGSave,new(){
public TSaveObject LoadObjectFile<TSaveObject>(string path) where TSaveObject:IEGSaveObject,IEGSave,new(){
TSaveObject saveObject = new TSaveObject();
saveObject.InitSave(path);
if(!ObjectFiles.ContainsKey(path)){
@ -92,7 +92,7 @@ namespace EGFramework @@ -92,7 +92,7 @@ namespace EGFramework
return saveObject;
}
public IEGSaveObjectReadOnly ReadObject<TReadOnlyObject>(string key, string data) where TReadOnlyObject : IEGSaveObjectReadOnly, IEGSaveReadOnly, new()
public TReadOnlyObject ReadObject<TReadOnlyObject>(string key, string data) where TReadOnlyObject : IEGSaveObjectReadOnly, IEGSaveReadOnly, new()
{
TReadOnlyObject readOnlyObject = new TReadOnlyObject();
readOnlyObject.InitReadOnly(data);
@ -107,7 +107,7 @@ namespace EGFramework @@ -107,7 +107,7 @@ namespace EGFramework
return readOnlyObject;
}
public IEGSaveObjectReadOnly ReadObject<TReadOnlyObject>(string key,byte[] data) where TReadOnlyObject:IEGSaveObjectReadOnly,IEGSaveReadOnly,new(){
public TReadOnlyObject ReadObject<TReadOnlyObject>(string key,byte[] data) where TReadOnlyObject:IEGSaveObjectReadOnly,IEGSaveReadOnly,new(){
TReadOnlyObject readOnlyObject = new TReadOnlyObject();
readOnlyObject.InitReadOnly(data);
if(!ObjectReadOnly.ContainsKey(key)){

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

@ -37,6 +37,7 @@ namespace EGFramework @@ -37,6 +37,7 @@ namespace EGFramework
{
void CreateTable<TData>(string dataKey);
void CreateTable(string dataKey, Dictionary<string, object> tableParam);
void DropTable(string dataKey);
}
#endregion

Loading…
Cancel
Save