/// <summary> /// RefreshTable /// </summary> void RefreshTable() { Stopwatch watch = new Stopwatch(); watch.Start(); SQLiteTableMapAttribute tableAttribute = StrayFogConfigHelper.GetTableAttribute <XLS_Report_Table_Report>(); UnityEngine.Debug.LogFormat("Refresh Table【{0}->{1}】", GetSrcData(tableAttribute), tableAttribute.sqliteTableName); List <XLS_Report_Table_Report> reports = StrayFogConfigHelper.Select <XLS_Report_Table_Report>(); watch.Stop(); UnityEngine.Debug.LogFormat("Select Table SQLite Data =>{0} , Time=>{1} , Data=>【{2}】", reports.Count, watch.Elapsed, reports.JsonSerialize()); if (reports.Count > 0) { watch.Reset(); watch.Start(); reports[0].Set_stringCol("Refresh Table"); watch.Stop(); UnityEngine.Debug.LogFormat("Modify Table SQLite Data =>{0} , Time=>{1}, Data=>【{2}】", reports.Count, watch.Elapsed, reports.JsonSerialize()); } watch.Reset(); watch.Start(); StrayFogConfigHelper.Reload <XLS_Report_Table_Report>(); reports = StrayFogConfigHelper.Select <XLS_Report_Table_Report>(); watch.Stop(); UnityEngine.Debug.LogFormat("Select Table SQLite Data =>{0} , Time=>{1}", reports.JsonSerialize(), watch.Elapsed, reports.JsonSerialize()); }
/// <summary> /// 插入NoPK表数据 /// </summary> void InsertNoPKTable() { Stopwatch watch = new Stopwatch(); watch.Start(); SQLiteTableMapAttribute tableAttribute = StrayFogConfigHelper.GetTableAttribute <XLS_Report_Table_ReportColumnMaping>(); UnityEngine.Debug.LogFormat("Insert 【NoPk Table】 【{0}->{1}】", GetSrcData(tableAttribute), tableAttribute.sqliteTableName); watch.Reset(); watch.Start(); List <XLS_Report_Table_ReportColumnMaping> reportColumnMapings = StrayFogConfigHelper.Select <XLS_Report_Table_ReportColumnMaping>(); watch.Stop(); UnityEngine.Debug.LogFormat("Select【NoPk Table】SQLite Data =>{0} , Time=>{1}", reportColumnMapings.Count, watch.Elapsed); if (reportColumnMapings.Count > 0) { watch.Reset(); watch.Start(); reportColumnMapings[0].Set_stringCol("Insert NoPk Table"); StrayFogConfigHelper.Insert(reportColumnMapings[0]); watch.Stop(); UnityEngine.Debug.LogFormat("Insert【NoPk Table】SQLite Data =>{0} , Time=>{1}", reportColumnMapings.Count, watch.Elapsed); } watch.Reset(); watch.Start(); reportColumnMapings = StrayFogConfigHelper.Select <XLS_Report_Table_ReportColumnMaping>(); watch.Stop(); UnityEngine.Debug.LogFormat("Select【NoPk Table】SQLite Data =>{0} , Time=>{1}", reportColumnMapings.Count, watch.Elapsed); }
/// <summary> /// 更新数据到XLS表 /// </summary> /// <typeparam name="T">类型</typeparam> /// <param name="_entity">实体</param> /// <param name="_tableAttribute">表属性</param> /// <param name="_xlsRowIndex">行索引</param> static void OnUpdateToXLS <T>(T _entity, SQLiteTableMapAttribute _tableAttribute, int _xlsRowIndex) where T : AbsStrayFogSQLiteEntity { if (File.Exists(_tableAttribute.xlsFilePath)) { ExcelPackage pck = OnGetExcelPackage(_tableAttribute); { if (pck.Workbook.Worksheets.Count > 0)//消耗2秒 { ExcelWorksheet sheet = pck.Workbook.Worksheets[1]; if (_tableAttribute.isDeterminant) { foreach (KeyValuePair <int, SQLiteFieldTypeAttribute> key in msEntitySQLitePropertySQLiteFieldTypeAttributeMaping[_tableAttribute.id]) { sheet.Cells[key.Value.xlsColumnValueIndex, _tableAttribute.xlsColumnValueIndex].Value = StrayFogSQLiteDataTypeHelper.GetValueFromEntityPropertyToXlsColumn(_entity, msEntityPropertyInfoMaping[_tableAttribute.id][key.Key], key.Value); } } else { if (sheet.Dimension.Rows >= _tableAttribute.xlsColumnValueIndex) { foreach (KeyValuePair <int, SQLiteFieldTypeAttribute> key in msEntitySQLitePropertySQLiteFieldTypeAttributeMaping[_tableAttribute.id]) { sheet.Cells[_xlsRowIndex, key.Value.xlsColumnValueIndex].Value = StrayFogSQLiteDataTypeHelper.GetValueFromEntityPropertyToXlsColumn(_entity, msEntityPropertyInfoMaping[_tableAttribute.id][key.Key], key.Value); } } } } } } }
/// <summary> /// 更新行列式表 /// </summary> void UpdateDeterminantTable() { Stopwatch watch = new Stopwatch(); watch.Start(); SQLiteTableMapAttribute tableAttribute = StrayFogConfigHelper.GetTableAttribute <XLS_Report_Determinant_Table_ReportDeterminant>(); UnityEngine.Debug.LogFormat("Update【Determinant Table 】【{0}->{1}】", GetSrcData(tableAttribute), tableAttribute.sqliteTableName); List <XLS_Report_Determinant_Table_ReportDeterminant> reports = StrayFogConfigHelper.Select <XLS_Report_Determinant_Table_ReportDeterminant>(); watch.Stop(); UnityEngine.Debug.LogFormat("Select【Determinant Table 】SQLite Data =>{0} , Time=>{1} , Data=>【{2}】", reports.Count, watch.Elapsed, reports.JsonSerialize()); if (reports.Count > 0) { watch.Reset(); watch.Start(); reports[0].Set_ReportTip("Update Determinant Table"); reports[0].Set_DeterminantTip(Guid.NewGuid().ToString()); StrayFogConfigHelper.Update(reports[0]); watch.Stop(); UnityEngine.Debug.LogFormat("Update【Determinant Table】SQLite Data 【Same PK】=>{0} , Time=>{1}, Data=>【{2}】", reports.Count, watch.Elapsed, reports.JsonSerialize()); } watch.Reset(); watch.Start(); reports = StrayFogConfigHelper.Select <XLS_Report_Determinant_Table_ReportDeterminant>(); watch.Stop(); UnityEngine.Debug.LogFormat("Select【Determinant Table】SQLite Data =>{0} , Time=>{1}", reports.Count, watch.Elapsed, reports.JsonSerialize()); }
/// <summary> /// 查询指定条件的数据集 /// </summary> /// <typeparam name="T">实体类型</typeparam> /// <param name="_condition">条件</param> /// <returns>数据集</returns> static Dictionary <int, T> OnSelect <T>(Func <T, bool> _condition) where T : AbsStrayFogSQLiteEntity { Dictionary <int, T> result = new Dictionary <int, T>(); SQLiteTableMapAttribute tableAttribute = GetTableAttribute <T>(); Dictionary <int, T> srcResult = new Dictionary <int, T>(); if (mCacheIsReadFromDisk.Contains(tableAttribute.id)) { srcResult = OnGetCacheData <T>(tableAttribute); } else { srcResult = OnReadAll <T>(tableAttribute); OnRefreshCacheData(srcResult, tableAttribute, true); mCacheIsReadFromDisk.Add(tableAttribute.id); } if (_condition != null) { foreach (T t in srcResult.Values) { if (_condition(t)) { result.Add(t.pkSequenceId, t); } } } else { result = srcResult; } return(result); }
/// <summary> /// 从缓存删除所有数据 /// </summary> /// <typeparam name="T">类型</typeparam> /// <param name="_tableAttribute">表属性</param> /// <returns>true:成功,false:失败</returns> static bool OnDeleteAllToCacheEntityData <T>(SQLiteTableMapAttribute _tableAttribute) where T : AbsStrayFogSQLiteEntity { bool result = false; if (_tableAttribute.canModifyData) { switch (_tableAttribute.sqliteTableType) { case enSQLiteEntityClassify.Table: if (_tableAttribute.isDeterminant) { throw new UnityException(string.Format("Can't be delete data from determinant table 【{0}->{1}】.", _tableAttribute.sqliteTableName, _tableAttribute.xlsFilePath)); } else { OnRefreshCacheData(new Dictionary <int, AbsStrayFogSQLiteEntity>(), _tableAttribute, false); result = true; } break; default: throw new UnityException(string.Format("Can't be delete data to 【{0}->{1}】 .", _tableAttribute.sqliteTableType, _tableAttribute.sqliteTableName)); } } else { throw new UnityException(string.Format("Can't be delete data to table 【{0}】's canModifyData【{1}】 .", _tableAttribute.sqliteTableName, _tableAttribute.canModifyData)); } return(result); }
/// <summary> /// 插入PK表有重复键数据 /// </summary> void InsertPKSameKeyTable() { Stopwatch watch = new Stopwatch(); watch.Start(); SQLiteTableMapAttribute tableAttribute = StrayFogConfigHelper.GetTableAttribute <XLS_Report_Table_Report>(); UnityEngine.Debug.LogFormat("【Insert PK Table 】【Same Key】【{0}->{1}】", GetSrcData(tableAttribute), tableAttribute.sqliteTableName); List <XLS_Report_Table_Report> reports = StrayFogConfigHelper.Select <XLS_Report_Table_Report>(); watch.Stop(); UnityEngine.Debug.LogFormat("【Select PK Table 】【Same Key】SQLite Data =>{0} , Time=>{1} , Data=>【{2}】", reports.Count, watch.Elapsed, reports.JsonSerialize()); if (reports.Count > 0) { watch.Reset(); watch.Start(); StrayFogConfigHelper.Insert(reports[0]); watch.Stop(); UnityEngine.Debug.LogFormat("【Insert PK Table】【Same Key】SQLite Data 【Same PK】=>{0} , Time=>{1}, Data=>【{2}】", reports.Count, watch.Elapsed, reports.JsonSerialize()); } watch.Reset(); watch.Start(); reports = StrayFogConfigHelper.Select <XLS_Report_Table_Report>(); watch.Stop(); UnityEngine.Debug.LogFormat("【Select PK Table】【Same Key】SQLite Data =>{0} , Time=>{1}", reports.Count, watch.Elapsed, reports.JsonSerialize()); }
/// <summary> /// DeleteAllTable /// </summary> void DeleteAllTable() { Stopwatch watch = new Stopwatch(); watch.Start(); SQLiteTableMapAttribute tableAttribute = StrayFogConfigHelper.GetTableAttribute <XLS_Report_Table_Report>(); UnityEngine.Debug.LogFormat("Delete Table【{0}->{1}】", GetSrcData(tableAttribute), tableAttribute.sqliteTableName); List <XLS_Report_Table_Report> reports = StrayFogConfigHelper.Select <XLS_Report_Table_Report>(); watch.Stop(); UnityEngine.Debug.LogFormat("Select Table SQLite Data =>{0} , Time=>{1} , Data=>【{2}】", reports.Count, watch.Elapsed, reports.JsonSerialize()); watch.Reset(); watch.Start(); StrayFogConfigHelper.DeleteAll <XLS_Report_Table_Report>(); watch.Stop(); UnityEngine.Debug.LogFormat("DeleteAll Table SQLite Data =>{0} , Time=>{1}, Data=>【{2}】", reports.Count, watch.Elapsed, reports.JsonSerialize()); watch.Reset(); watch.Start(); reports = StrayFogConfigHelper.Select <XLS_Report_Table_Report>(); watch.Stop(); UnityEngine.Debug.LogFormat("Select Table SQLite Data =>{0} , Time=>{1}", reports.Count, watch.Elapsed, reports.JsonSerialize()); }
/// <summary> /// 插入PK表无重复键数据 /// </summary> void InsertPKDifferentKeyTable() { Stopwatch watch = new Stopwatch(); watch.Start(); SQLiteTableMapAttribute tableAttribute = StrayFogConfigHelper.GetTableAttribute <XLS_Report_Table_Report>(); UnityEngine.Debug.LogFormat("【Insert PK Table 】【Different Key】 【{0}->{1}】", GetSrcData(tableAttribute), tableAttribute.sqliteTableName); List <XLS_Report_Table_Report> reports = StrayFogConfigHelper.Select <XLS_Report_Table_Report>(); watch.Stop(); UnityEngine.Debug.LogFormat("【Select PK Table 】【Different Key】SQLite Data =>{0} , Time=>{1} , Data=>【{2}】", reports.Count, watch.Elapsed, reports.JsonSerialize()); watch.Reset(); watch.Start(); XLS_Report_Table_Report insertReport = new XLS_Report_Table_Report(Guid.NewGuid().ToString().GetHashCode()); insertReport.Set_stringCol("Insert PK Table"); StrayFogConfigHelper.Insert(insertReport); watch.Stop(); UnityEngine.Debug.LogFormat("【Insert PK Table】【Different Key】SQLite Data 【Different PK】=>{0} , Time=>{1}, Data=>【{2}】", reports.Count, watch.Elapsed, reports.JsonSerialize()); watch.Reset(); watch.Start(); reports = StrayFogConfigHelper.Select <XLS_Report_Table_Report>(); watch.Stop(); UnityEngine.Debug.LogFormat("【Select PK Table】【Different Key】SQLite Data =>{0} , Time=>{1}", reports.Count, watch.Elapsed, reports.JsonSerialize()); }
/// <summary> /// 获得ExcelPackage /// </summary> /// <param name="_tableAttribute">表属性</param> /// <returns>ExcelPackage</returns> static ExcelPackage OnGetExcelPackage(SQLiteTableMapAttribute _tableAttribute) { if (!mExcelPackageMaping.ContainsKey(_tableAttribute.id)) { mExcelPackageMaping.Add(_tableAttribute.id, new ExcelPackage(new FileInfo(_tableAttribute.xlsFilePath))); } return(mExcelPackageMaping[_tableAttribute.id]); }
/// <summary> /// 从SQLite读取数据 /// </summary> /// <typeparam name="T">实体类型</typeparam> /// <param name="_tableAttribute">表属性</param> /// <returns>数据集</returns> static Dictionary <int, T> OnReadFromSQLite <T>(SQLiteTableMapAttribute _tableAttribute) where T : AbsStrayFogSQLiteEntity { Dictionary <int, T> result = new Dictionary <int, T>(); SqliteDataReader reader = msStrayFogSQLiteHelperMaping[_tableAttribute.dbSQLiteKey].ReadFullTable(_tableAttribute.sqliteTableName); T tempEntity = default(T); string tempPropertyName = string.Empty; int tempPropertyKey = 0; object tempValue = null; if (_tableAttribute.isDeterminant) { tempEntity = OnCreateInstance <T>(_tableAttribute); #region 行列式表 while (reader.Read()) { tempPropertyName = reader.GetString(_tableAttribute.xlsColumnNameIndex - 1); tempPropertyKey = tempPropertyName.UniqueHashCode(); tempValue = reader.GetString(_tableAttribute.xlsColumnValueIndex - 1); tempValue = StrayFogSQLiteDataTypeHelper.GetXlsCSTypeColumnValue(tempValue, msEntityPropertyInfoMaping[_tableAttribute.id][tempPropertyKey], msEntitySQLitePropertySQLiteFieldTypeAttributeMaping[_tableAttribute.id][tempPropertyKey].dataType, msEntitySQLitePropertySQLiteFieldTypeAttributeMaping[_tableAttribute.id][tempPropertyKey].arrayDimension); msEntityPropertyInfoMaping[_tableAttribute.id][tempPropertyKey].SetValue(tempEntity, tempValue, null); } #endregion tempEntity.Resolve(); result.Add(tempEntity.pkSequenceId, tempEntity); } else { #region 普通表 while (reader.Read()) { tempEntity = OnCreateInstance <T>(_tableAttribute); for (int i = 0; i < reader.FieldCount; i++) { tempPropertyName = reader.GetName(i); tempPropertyKey = tempPropertyName.UniqueHashCode(); if (!msEntitySQLitePropertySQLiteFieldTypeAttributeMaping[_tableAttribute.id][tempPropertyKey].isIngore) { tempValue = reader.GetValue(i); tempValue = StrayFogSQLiteDataTypeHelper.GetXlsCSTypeColumnValue(tempValue, msEntityPropertyInfoMaping[_tableAttribute.id][tempPropertyKey], msEntitySQLitePropertySQLiteFieldTypeAttributeMaping[_tableAttribute.id][tempPropertyKey].dataType, msEntitySQLitePropertySQLiteFieldTypeAttributeMaping[_tableAttribute.id][tempPropertyKey].arrayDimension); msEntityPropertyInfoMaping[_tableAttribute.id][tempPropertyKey].SetValue(tempEntity, tempValue, null); } } tempEntity.Resolve(); result.Add(tempEntity.pkSequenceId, tempEntity); } reader.Close(); reader = null; #endregion } return(result); }
/// <summary> /// 从XLS表删除数据 /// </summary> /// <param name="_xlsRowIndex">删除行索引</param> /// <param name="_tableAttribute">表属性</param> static void OnDeleteFromXLS(int _xlsRowIndex, SQLiteTableMapAttribute _tableAttribute) { if (File.Exists(_tableAttribute.xlsFilePath)) { ExcelPackage pck = OnGetExcelPackage(_tableAttribute); { if (pck.Workbook.Worksheets.Count > 0)//消耗2秒 { ExcelWorksheet sheet = pck.Workbook.Worksheets[1]; sheet.DeleteRow(_xlsRowIndex); } } } }
/// <summary> /// SelectTable /// </summary> void SelectTable() { SQLiteTableMapAttribute tableAttribute = StrayFogConfigHelper.GetTableAttribute <XLS_Report_Table_Report>(); UnityEngine.Debug.LogFormat("【Select Table 】【{0}->{1}】", GetSrcData(tableAttribute), tableAttribute.sqliteTableName); Stopwatch watch = new Stopwatch(); watch.Start(); List <XLS_Report_Table_Report> maps = StrayFogConfigHelper.Select <XLS_Report_Table_Report>(); watch.Stop(); UnityEngine.Debug.LogFormat("Select Table【{0}->{1}】Count->{2} Time->{3}", GetSrcData(tableAttribute), tableAttribute.sqliteTableName, maps.Count, watch.Elapsed); }
/// <summary> /// 从XLS表加载视图 /// </summary> /// <param name="_table">表格</param> /// <param name="_type">类型</param> /// <returns>数据集</returns> Dictionary <int, T> StrayFogSQLiteEntityHelper_OnEventHandlerLoadViewFromXLS <T> (SQLiteTableMapAttribute _tableAttribute) where T : AbsStrayFogSQLiteEntity { Dictionary <int, T> result = new Dictionary <int, T>(); Type type = typeof(T); if (_tableAttribute.tableClassType.Equals(typeof(XLS_Config_View_AssetDiskMaping))) { #region View_AssetDiskMaping 数据组装 List <XLS_Config_Table_AssetDiskMapingFile> files = StrayFogConfigHelper.Select <XLS_Config_Table_AssetDiskMapingFile>(); List <XLS_Config_Table_AssetDiskMapingFolder> folders = StrayFogConfigHelper.Select <XLS_Config_Table_AssetDiskMapingFolder>(); Dictionary <int, XLS_Config_Table_AssetDiskMapingFile> dicFile = new Dictionary <int, XLS_Config_Table_AssetDiskMapingFile>(); Dictionary <int, XLS_Config_Table_AssetDiskMapingFolder> dicFolder = new Dictionary <int, XLS_Config_Table_AssetDiskMapingFolder>(); foreach (XLS_Config_Table_AssetDiskMapingFolder t in folders) { dicFolder.Add(t.folderId, t); } int fileId = StrayFogConfigHelper.GetPropertyId("fileId"); int folderId = StrayFogConfigHelper.GetPropertyId("folderId"); int fileName = StrayFogConfigHelper.GetPropertyId("fileName"); int inAssetPath = StrayFogConfigHelper.GetPropertyId("inAssetPath"); int outAssetPath = StrayFogConfigHelper.GetPropertyId("outAssetPath"); int extEnumValue = StrayFogConfigHelper.GetPropertyId("extEnumValue"); T tempEntity = null; foreach (XLS_Config_Table_AssetDiskMapingFile t in files) { tempEntity = (T)Activator.CreateInstance(type, _tableAttribute.hasPkColumn && _tableAttribute.canModifyData); StrayFogConfigHelper.GetPropertyInfo(_tableAttribute, fileId).SetValue(tempEntity, t.fileId, null); StrayFogConfigHelper.GetPropertyInfo(_tableAttribute, folderId).SetValue(tempEntity, t.folderId, null); StrayFogConfigHelper.GetPropertyInfo(_tableAttribute, fileName).SetValue(tempEntity, t.inSide + t.ext, null); StrayFogConfigHelper.GetPropertyInfo(_tableAttribute, inAssetPath).SetValue(tempEntity, Path.Combine(dicFolder[t.folderId].inSide, t.inSide + t.ext).TransPathSeparatorCharToUnityChar(), null); StrayFogConfigHelper.GetPropertyInfo(_tableAttribute, outAssetPath).SetValue(tempEntity, t.outSide, null); StrayFogConfigHelper.GetPropertyInfo(_tableAttribute, extEnumValue).SetValue(tempEntity, t.extEnumValue, null); tempEntity.Resolve(); result.Add(tempEntity.pkSequenceId, tempEntity); } #endregion } else if (_tableAttribute.tableClassType.Equals(typeof(XLS_Config_View_DeterminantVT))) { #region View_DeterminantVT 数据组装 int vtNameKey = "vtName".UniqueHashCode(); #endregion } return(result); }
/// <summary> /// 读取所有数据 /// </summary> /// <typeparam name="T">实体类型</typeparam> /// <param name="_tableAttribute">表属性</param> /// <returns>数据集合</returns> static Dictionary <int, T> OnReadAll <T>(SQLiteTableMapAttribute _tableAttribute) where T : AbsStrayFogSQLiteEntity { Dictionary <int, T> result = new Dictionary <int, T>(); if (StrayFogRunningPool.runningSetting.isUseSQLite) { result = OnReadFromSQLite <T>(_tableAttribute); } else { result = OnReadFromXLS <T>(_tableAttribute); } return(result); }
/// <summary> /// 从XLS表删除所有数据 /// </summary> /// <param name="_tableAttribute">表属性</param> static void OnDeleteAllFromXLS(SQLiteTableMapAttribute _tableAttribute) { if (File.Exists(_tableAttribute.xlsFilePath)) { ExcelPackage pck = OnGetExcelPackage(_tableAttribute); { if (pck.Workbook.Worksheets.Count > 0)//消耗2秒 { ExcelWorksheet sheet = pck.Workbook.Worksheets[1]; sheet.DeleteRow(_tableAttribute.xlsDataStartRowIndex, sheet.Dimension.Rows); pck.Save(); } } } }
/// <summary> /// 插入数据到SQLite /// </summary> /// <typeparam name="T">类型</typeparam> /// <param name="_entity">实体</param> /// <param name="_tableAttribute">表属性</param> static void OnInsertIntoSQLite <T>(T _entity, SQLiteTableMapAttribute _tableAttribute) where T : AbsStrayFogSQLiteEntity { List <string> values = new List <string>(); List <SqliteParameter> valueSps = new List <SqliteParameter>(); foreach (KeyValuePair <int, SQLiteFieldTypeAttribute> key in msEntitySQLitePropertySQLiteFieldTypeAttributeMaping[_tableAttribute.id]) { values.Add(key.Value.sqliteParameterName); valueSps.Add(new SqliteParameter(key.Value.sqliteParameterName, StrayFogSQLiteDataTypeHelper.GetValueFromEntityPropertyToXlsColumn(_entity, msEntityPropertyInfoMaping[_tableAttribute.id][key.Key], key.Value))); } string sql = string.Format("INSERT INTO {0} VALUES({1})", _tableAttribute.sqliteTableName, string.Join(",", values.ToArray())); msStrayFogSQLiteHelperMaping[_tableAttribute.dbSQLiteKey].ExecuteNonQuery(sql, valueSps.ToArray()); }
/// <summary> /// 从SQLite删除数据 /// </summary> /// <typeparam name="T">类型</typeparam> /// <param name="_entity">实体</param> /// <param name="_tableAttribute">表属性</param> static void OnDeleteFromSQLite <T>(T _entity, SQLiteTableMapAttribute _tableAttribute) { List <string> pks = new List <string>(); List <SqliteParameter> sps = new List <SqliteParameter>(); foreach (KeyValuePair <int, SQLiteFieldTypeAttribute> key in msEntitySQLitePropertySQLiteFieldTypeAttributeMaping[_tableAttribute.id]) { if (key.Value.isPK) { pks.Add(key.Value.sqliteColumnName + "=" + key.Value.sqliteParameterName); sps.Add(new SqliteParameter(key.Value.sqliteParameterName, StrayFogSQLiteDataTypeHelper.GetValueFromEntityPropertyToXlsColumn(_entity, msEntityPropertyInfoMaping[_tableAttribute.id][key.Key], key.Value))); } } string sql = string.Format("DELETE FROM {0} WHERE {1}", _tableAttribute.sqliteTableName, string.Join(",", pks.ToArray())); msStrayFogSQLiteHelperMaping[_tableAttribute.dbSQLiteKey].ExecuteNonQuery(sql, sps.ToArray()); }
/// <summary> /// 刷新内存数据 /// </summary> /// <typeparam name="T">类型</typeparam> /// <param name="_data">数据</param> /// <param name="_tableAttribute">表属性</param> /// <param name="_isReadFromDisk">是否是从磁盘读取数据</param> static void OnRefreshCacheData <T>(Dictionary <int, T> _data, SQLiteTableMapAttribute _tableAttribute, bool _isReadFromDisk) where T : AbsStrayFogSQLiteEntity { if (!mCacheEntityData.ContainsKey(_tableAttribute.id)) { mCacheEntityData.Add(_tableAttribute.id, _data); } else { mCacheEntityData[_tableAttribute.id] = _data; } if (!_isReadFromDisk && !mRefreshCacheData.Contains(_tableAttribute.id)) { mRefreshCacheData.Add(_tableAttribute.id); } }
/// <summary> /// 更新数据到SQLite /// </summary> /// <typeparam name="T">类型</typeparam> /// <param name="_entity">实体</param> /// <param name="_tableAttribute">表属性</param> static void OnUpdateToSQLite <T>(T _entity, SQLiteTableMapAttribute _tableAttribute) where T : AbsStrayFogSQLiteEntity { Dictionary <string, List <SqliteParameter> > dicSql = new Dictionary <string, List <SqliteParameter> >(); if (_tableAttribute.isDeterminant) { string pfxSet = "Set"; foreach (KeyValuePair <int, SQLiteFieldTypeAttribute> key in msEntitySQLitePropertySQLiteFieldTypeAttributeMaping[_tableAttribute.id]) { dicSql.Add(string.Format("UPDATE {0} SET {1} WHERE {2}", _tableAttribute.sqliteTableName, key.Value.sqliteColumnValue + "=" + key.Value.sqliteParameterName + pfxSet, key.Value.sqliteColumnName + "=" + key.Value.sqliteParameterName), new List <SqliteParameter>() { new SqliteParameter(key.Value.sqliteParameterName + pfxSet, StrayFogSQLiteDataTypeHelper.GetValueFromEntityPropertyToXlsColumn(_entity, msEntityPropertyInfoMaping[_tableAttribute.id][key.Key], key.Value)), new SqliteParameter(key.Value.sqliteParameterName, msEntityPropertyInfoMaping[_tableAttribute.id][key.Key].Name) } ); } msStrayFogSQLiteHelperMaping[_tableAttribute.dbSQLiteKey].ExecuteTransaction(dicSql); } else { List <string> pks = new List <string>(); List <string> sets = new List <string>(); List <SqliteParameter> sps = new List <SqliteParameter>(); foreach (KeyValuePair <int, SQLiteFieldTypeAttribute> key in msEntitySQLitePropertySQLiteFieldTypeAttributeMaping[_tableAttribute.id]) { if (key.Value.isPK) { pks.Add(key.Value.sqliteColumnName + "=" + key.Value.sqliteParameterName); } else { sets.Add(key.Value.sqliteColumnName + "=" + key.Value.sqliteParameterName); } sps.Add(new SqliteParameter(key.Value.sqliteParameterName, StrayFogSQLiteDataTypeHelper.GetValueFromEntityPropertyToXlsColumn(_entity, msEntityPropertyInfoMaping[_tableAttribute.id][key.Key], key.Value))); } string sql = string.Format("UPDATE {0} SET {1} WHERE {2}", _tableAttribute.sqliteTableName, string.Join(",", sets.ToArray()), string.Join(",", pks.ToArray())); msStrayFogSQLiteHelperMaping[_tableAttribute.dbSQLiteKey].ExecuteNonQuery(sql, sps.ToArray()); } }
/// <summary> /// DeleteAll 删除数据集 /// </summary> /// <returns>true:成功,false:失败</returns> public static bool DeleteAll <T>() where T : AbsStrayFogSQLiteEntity { SQLiteTableMapAttribute tableAttribute = GetTableAttribute <T>(); bool result = OnDeleteAllToCacheEntityData <T>(tableAttribute); if (result) { if (StrayFogRunningPool.runningSetting.isUseSQLite) { OnDeleteAllFromSQLite(tableAttribute); } else { OnDeleteAllFromXLS(tableAttribute); } } return(result); }
/// <summary> /// 从XLS读取View数据 /// </summary> /// <typeparam name="T">实体类型</typeparam> /// <param name="_tableAttribute">表属性</param> /// <returns>数据</returns> static Dictionary <int, T> OnLoadViewFromXLS <T>(SQLiteTableMapAttribute _tableAttribute) where T : AbsStrayFogSQLiteEntity { Dictionary <int, T> result = new Dictionary <int, T>(); if (_tableAttribute.sqliteTableType == enSQLiteEntityClassify.View) { Type t = typeof(T); int key = t.GetHashCode(); if (mFunLoadViewFromXLSMaping.ContainsKey(key)) { if (mFunLoadViewFromXLSMaping[key] is LoadViewFromXLSEventHandler <T> ) { LoadViewFromXLSEventHandler <T> call = (LoadViewFromXLSEventHandler <T>)mFunLoadViewFromXLSMaping[key]; result = call?.Invoke(_tableAttribute); } } } return(result); }
/// <summary> /// 插入数据 /// </summary> /// <typeparam name="T">实体类型</typeparam> /// <param name="_entity">实体</param> /// <returns>true:成功,false:失败</returns> public static bool Insert <T>(T _entity) where T : AbsStrayFogSQLiteEntity { SQLiteTableMapAttribute tableAttribute = GetTableAttribute <T>(); int xlsRowIndex = -1; bool result = OnInsertToCacheEntityData(_entity, tableAttribute, out xlsRowIndex); if (result) { if (StrayFogRunningPool.runningSetting.isUseSQLite) { OnInsertIntoSQLite(_entity, tableAttribute); } else { OnInsertIntoXLS(_entity, tableAttribute, xlsRowIndex); } } return(result); }
/// <summary> /// 获得数据来源 /// </summary> /// <param name="_tableAttribute">表属性</param> /// <returns>来源</returns> string GetSrcData(SQLiteTableMapAttribute _tableAttribute) { string path = string.Empty; if (StrayFogGamePools.setting.isUseSQLite) { if (StrayFogGamePools.setting.isUseAssetBundle) { path = _tableAttribute.dbSQLiteAssetBundleName; } else { path = _tableAttribute.dbSQLitePath; } } else { path = _tableAttribute.xlsFilePath; } return(path); }
/// <summary> /// 插入数据 /// </summary> /// <typeparam name="T">实体类型</typeparam> /// <param name="_entity">实体</param> /// <returns>true:成功,false:失败</returns> public static bool Update <T>(T _entity) where T : AbsStrayFogSQLiteEntity { SQLiteTableMapAttribute tableAttribute = GetTableAttribute <T>(); bool result = false; if (tableAttribute.canModifyData) { switch (tableAttribute.sqliteTableType) { case enSQLiteEntityClassify.Table: #region 更新数据 int xlsRowIndex = -1; result = OnUpdateToCacheEntityData(_entity, tableAttribute, out xlsRowIndex); if (result) { if (StrayFogRunningPool.runningSetting.isUseSQLite) { OnUpdateToSQLite(_entity, tableAttribute); } else { OnUpdateToXLS(_entity, tableAttribute, xlsRowIndex); } } #endregion break; default: throw new UnityException(string.Format("Can't be update data to 【{0}->{1}】 .", tableAttribute.sqliteTableType, tableAttribute.sqliteTableName)); } } else { throw new UnityException(string.Format("Can't be update data to table 【{0}】's canModifyData【{1}】 .", tableAttribute.sqliteTableName, tableAttribute.canModifyData)); } return(result); }
/// <summary> /// 删除ExcelPackage /// </summary> /// <param name="_tableAttribute">表属性</param> static void OnRemoveExcelPackage(SQLiteTableMapAttribute _tableAttribute) { mExcelPackageMaping.Remove(_tableAttribute.id); }
/// <summary> /// 创建实体 /// </summary> /// <typeparam name="T">类型</typeparam> /// <param name="_tableAttribute">表属性</param> /// <returns>实例</returns> static T OnCreateInstance <T>(SQLiteTableMapAttribute _tableAttribute) where T : AbsStrayFogSQLiteEntity { return((T)Activator.CreateInstance(typeof(T), _tableAttribute.hasPkColumn && _tableAttribute.canModifyData)); }
/// <summary> /// 获得PropertyInfo /// </summary> /// <param name="_tableAttribute">表属性</param> /// <param name="_propertyId">属性Id</param> /// <returns>PropertyInfo</returns> public static PropertyInfo GetPropertyInfo(SQLiteTableMapAttribute _tableAttribute, int _propertyId) { return(msEntityPropertyInfoMaping[_tableAttribute.id][_propertyId]); }
/// <summary> /// 获得表属性 /// </summary> /// <typeparam name="T">类型</typeparam> /// <returns>表属性</returns> public static SQLiteTableMapAttribute GetTableAttribute <T>() where T : AbsStrayFogSQLiteEntity { int key = typeof(T).GetHashCode(); int propertyKey = 0; Type entityType = typeof(T); SQLiteTableMapAttribute tableAttribute = null; SQLiteFieldTypeAttribute fieldAttribute = null; //SQLiteTableMapAttribute if (!msSQLiteTableMapAttributeMaping.ContainsKey(key)) { tableAttribute = typeof(T).GetFirstAttribute <SQLiteTableMapAttribute>(); msSQLiteTableMapAttributeMaping.Add(key, tableAttribute); } else { tableAttribute = msSQLiteTableMapAttributeMaping[key]; } //SQLiteFieldTypeAttribute if (!msEntitySQLitePropertySQLiteFieldTypeAttributeMaping.ContainsKey(tableAttribute.id)) { msEntitySQLitePropertySQLiteFieldTypeAttributeMaping.Add(tableAttribute.id, new Dictionary <int, SQLiteFieldTypeAttribute>()); } //PropertyInfo if (!msEntityPropertyInfoMaping.ContainsKey(tableAttribute.id)) { msEntityPropertyInfoMaping.Add(tableAttribute.id, new Dictionary <int, PropertyInfo>()); PropertyInfo[] pps = entityType.GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.GetProperty | BindingFlags.DeclaredOnly); if (pps != null && pps.Length > 0) { foreach (PropertyInfo p in pps) { propertyKey = p.Name.UniqueHashCode(); fieldAttribute = p.GetFirstAttribute <SQLiteFieldTypeAttribute>(); if (fieldAttribute != null) { if (!msEntityPropertyInfoMaping[tableAttribute.id].ContainsKey(propertyKey)) { msEntityPropertyInfoMaping[tableAttribute.id].Add(propertyKey, p); } if (!msEntitySQLitePropertySQLiteFieldTypeAttributeMaping[tableAttribute.id].ContainsKey(propertyKey)) { msEntitySQLitePropertySQLiteFieldTypeAttributeMaping[tableAttribute.id].Add(propertyKey, fieldAttribute); } } } } } //SQLiteHelper if (StrayFogRunningPool.runningSetting.isUseSQLite) { if (!msStrayFogSQLiteHelperMaping.ContainsKey(tableAttribute.dbSQLiteKey)) { if (StrayFogRunningPool.runningSetting.isUseAssetBundle) { msStrayFogSQLiteHelperMaping.Add(tableAttribute.dbSQLiteKey, new StrayFogSQLiteHelper(StrayFogRunningPool.runningSetting.GetSQLiteConnectionString(tableAttribute.dbSQLiteAssetBundleName))); } else { msStrayFogSQLiteHelperMaping.Add(tableAttribute.dbSQLiteKey, new StrayFogSQLiteHelper(StrayFogRunningPool.runningSetting.GetSQLiteConnectionString(tableAttribute.dbSQLitePath))); } } } return(tableAttribute); }
/// <summary> /// 获得内存数据 /// </summary> /// <typeparam name="T">类型</typeparam> /// <param name="_tableAttribute">表属性</param> static Dictionary <int, T> OnGetCacheData <T>(SQLiteTableMapAttribute _tableAttribute) where T : AbsStrayFogSQLiteEntity { return(mCacheEntityData.ContainsKey(_tableAttribute.id) ? (Dictionary <int, T>)mCacheEntityData[_tableAttribute.id] : new Dictionary <int, T>()); }