提交 0a5fbc18 编写于 作者: haililiu's avatar haililiu 🎨

修改

上级 c42916f3
......@@ -9,7 +9,7 @@ namespace Lemon.CacheDB
/// <summary>
/// 一个数据库
/// </summary>
internal class DB
public class DB
{
/// <summary>
/// 数据库名称
......@@ -23,38 +23,57 @@ namespace Lemon.CacheDB
this.Name = name;
Tables = new Dictionary<Type, DBTable>();
}
public void ClearAll()
{
Tables.Clear();
}
public void Clear(Type type)
public DBTable GetTable(Type type)
{
if (!Tables.ContainsKey(type))
return null;
return Tables[type];
}
public IEnumerable<Type> GetAllType()
{
return Tables.Keys;
}
public bool HasTable(Type type)
{
return Tables.ContainsKey(type);
}
public void RemoveTable(Type type)
{
if (!Tables.ContainsKey(type))
return;
Tables[type].Clear();
Tables.Remove(type);
}
/// <summary>
/// 获取区域下某个类型的列表
/// </summary>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
public DBTable GetTable(Type type)
public void AddTable<T>(List<T> datas) where T : IData
{
lock (tablesLock)
Type type = typeof(T);
if (Tables.ContainsKey(type))
{
return Tables[type] as DBTable;
Tables.Remove(type);
}
var dbtable = new DBTable();
dbtable.Load<T>(datas);
Tables.Add(type, dbtable);
}
public List<Type> GetAllType()
public void AddTable(Type type,List<object> datas)
{
lock (tablesLock)
if (Tables.ContainsKey(type))
{
return new List<Type>(Tables.Keys);
Tables.Remove(type);
}
var dbtable = new DBTable();
dbtable.Load(datas);
Tables.Add(type, dbtable);
}
}
}
using Lemon;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
namespace Lemon.CacheDB
{
/// <summary>
/// 内存数据库管理者
/// 3.调用SetSerialization设置序列化工具,可以不设,默认json序列化
/// </summary>
public class DBManager : SingletonBase<DBManager>
{
private const string defaultDBName = "defaultdb";
private readonly object dblistLock = new object();
private Dictionary<string, DB> dbs = new Dictionary<string, DB>();
private readonly object ObjectIndexLock = new object();
private Dictionary<string, List<object>> ObjectIndexList = new Dictionary<string, List<object>>();
public void LoadData(Type type, List<object> data)
{
DBTable table = GetTable(type);
if (table == null)
return;
table.Load(data);
}
public DBTable GetTable(Type type, string dbname = defaultDBName)
{
lock (dblistLock)
{
if (!dbs.ContainsKey(dbname))
return null;
DBTable table = dbs[dbname].GetTable(type);
return table;
}
}
private List<T> SelectByIndex<T>(string indexName, string dbname = defaultDBName, params Func<T, object>[] attrFunc) where T : class
{
lock (ObjectIndexLock)
{
Type type = typeof(T);
DBTable table = GetTable(type, dbname);
if (table == null)
return new List<T>();
if (ObjectIndexList.Count == 0)
{
List<T> allobj = table.Select<T>();
List<KeyValuePair<string, T>> newList = new List<KeyValuePair<string, T>>();
for (int i = 0; i < allobj.Count; i++)
{
T obj = allobj[i];
string oldkey = "";
for (int f = 0; f < attrFunc.Length; f++)
{
oldkey += attrFunc[f](obj).ToString() + "_";
}
newList.Add(new KeyValuePair<string, T>(oldkey, obj));
}
foreach (var g in newList.GroupBy(c => c.Key))
{
ObjectIndexList.Add(g.Key, g.Select(c => c.Value as object).ToList());
}
}
if (!ObjectIndexList.ContainsKey(indexName))
return new List<T>();
return ObjectIndexList[indexName].Select(c => c as T).ToList();
}
}
}
}
......@@ -8,16 +8,37 @@ namespace Lemon.CacheDB
public class DBTable
{
private readonly object TableLock = new object();
private const string idname = "ID";
private Dictionary<int, object> ObjectList;
private Dictionary<int, IData> ObjectList;
public Type ObjectType { get; set; }
public DBTable()
{
ObjectList = new Dictionary<int, IData>();
}
public DBTable(Type objtype)
public void Load<T>(List<T> data) where T : IData
{
ObjectType = objtype;
ObjectList = new Dictionary<int, object>();
lock (TableLock)
{
ObjectList.Clear();
int simulateId = 0;
foreach (T d in data)
{
try
{
if (d.ID == 0)
{
simulateId++;
ObjectList.Add(simulateId, d);
}
else
ObjectList.Add(d.ID, d);
}
catch
{
throw new Exception("请检查" + typeof(T) + " ID" + d.ID + "是否重复!");
}
}
}
}
public void Load(List<object> data)
......@@ -26,85 +47,74 @@ namespace Lemon.CacheDB
{
ObjectList.Clear();
int simulateId = 0;
foreach (object d in data)
foreach (var da in data)
{
IData d = da as IData;
try
{
if (d.GetProperty(idname) == null)
if (d.ID == 0)
{
simulateId++;
ObjectList.Add(simulateId, d);
}
else
ObjectList.Add(int.Parse(d.GetProperty(idname).ToString()), d);
ObjectList.Add(d.ID, d);
}
catch
{
int idvalue = (int)d.GetProperty(idname);
throw new Exception("请检查id" + idvalue + "是否重复!");
throw new Exception("请检查" + d.GetType() + " ID" + d.ID + "是否重复!");
}
}
}
}
public List<object> GetAll()
public IEnumerable<T> GetAll<T>() where T : IData
{
lock (TableLock)
{
List<object> result = ObjectList.Values.ToList();
List<object> newt = CopyObj(result) as List<object>;
return newt;
return ObjectList.Values.Select(c => c as T).AsEnumerable();
}
}
public T Add<T>(T t) where T : class
public IEnumerable<object> GetAll()
{
lock (TableLock)
{
T newt = CopyObj(t) as T;
int idvalue = (int)newt.GetProperty(idname);
ObjectList.Add(idvalue, newt);
return newt;
return ObjectList.Values;
}
}
public object Add(object obj)
public void Add<T>(T obj) where T : IData
{
lock (TableLock)
{
object newt = CopyObj(obj);
int idvalue = (int)newt.GetProperty(idname);
if (ObjectList.ContainsKey(idvalue))
return ObjectList[idvalue];
ObjectList.Add(idvalue, newt);
return newt;
if (ObjectList.ContainsKey(obj.ID))
return;
ObjectList.Add(obj.ID, obj);
}
}
public int Update<T>(T t) where T : class
public void Save(object obj)
{
IData data = obj as IData;
lock (TableLock)
{
int idvalue = (int)t.GetProperty(idname);
if (!ObjectList.ContainsKey(idvalue))
return -1;
var oldobj = ObjectList[idvalue];
T newt = CopyObj(t) as T;
ObjectList[idvalue] = newt;
return 0;
if (ObjectList.ContainsKey(data.ID))
{
ObjectList[data.ID] = data;
}
else
ObjectList.Add(data.ID, data);
}
}
public int Update(object obj)
public int Update<T>(T t) where T : IData
{
lock (TableLock)
{
int idvalue = (int)obj.GetProperty(idname);
if (!ObjectList.ContainsKey(idvalue))
if (!ObjectList.ContainsKey(t.ID))
return -1;
var oldobj = ObjectList[idvalue];
object newt = CopyObj(obj);
ObjectList[idvalue] = newt;
ObjectList[t.ID] = t;
return 0;
}
}
......@@ -115,25 +125,23 @@ namespace Lemon.CacheDB
{
if (!ObjectList.ContainsKey(id))
return -1;
var obj = ObjectList[id];
ObjectList.Remove(id);
return 0;
}
}
public int Delete<T>(T t) where T : class
public int Delete<T>(T t) where T : IData
{
int idvalue = (int)t.GetProperty(idname);
return Delete(idvalue);
return Delete(t.ID);
}
public T Get<T>(int id) where T : class
public T Get<T>(int id) where T : IData
{
lock (TableLock)
{
if (!ObjectList.ContainsKey(id))
return null;
T newt = CopyObj(ObjectList[id]) as T;
T newt = ObjectList[id] as T;
return newt;
}
}
......@@ -144,25 +152,24 @@ namespace Lemon.CacheDB
{
if (!ObjectList.ContainsKey(id))
return null;
return CopyObj(ObjectList[id]);
return ObjectList[id];
}
}
public List<T> Select<T>(Func<T, bool> predicate = null) where T : class
public IEnumerable<T> Select<T>(Func<T, bool> predicate = null) where T : IData
{
lock (TableLock)
{
List<T> result = null;
IEnumerable<T> result = null;
if (predicate != null)
result = ObjectList.Values.Select(c => c as T).Where(predicate).ToList<T>();
result = ObjectList.Values.Select(c => c as T).Where(predicate);
else
result = ObjectList.Values.Select(c => c as T).ToList<T>();
List<T> newt = CopyObj(result) as List<T>;
return newt;
result = ObjectList.Values.Select(c => c as T);
return result;
}
}
public int Count<T>(Func<T, bool> predicate = null) where T : class
public int Count<T>(Func<T, bool> predicate = null) where T : IData
{
lock (TableLock)
{
......@@ -172,15 +179,6 @@ namespace Lemon.CacheDB
}
}
private object CopyObj(object obj)
{
//if (DBManager.Instance.SerializeTool == null)
// throw new Exception("SerializeTool is NULL,please Set for DBManager");
//byte[] bs = DBManager.Instance.SerializeTool.Serialize(obj);
//return DBManager.Instance.SerializeTool.Deserialize(bs, obj.GetType());
return obj;
}
public void Clear()
{
lock (TableLock)
......
using System;
using System.Collections.Generic;
using System.Text;
namespace Lemon.CacheDB
{
public class IData
{
public int ID { get; set; }
}
}
using System;
using System.Text;
using System.Collections;
using System.IO;
using System.Data;
using System.Text.RegularExpressions;
using System.Diagnostics;
using System.Collections.Generic;
namespace Lemon.Csv
{
......@@ -24,15 +26,15 @@ namespace Lemon.Csv
#endregion
public class CsvStreamReader
{
private List<object> rowAL; //行链表,CSV文件的每一行就是一个链
private ArrayList rowAL; //行链表,CSV文件的每一行就是一个链
private string fileName; //文件名
private Encoding encoding; //编码
public CsvStreamReader()
{
this.rowAL = new List<object>();
this.rowAL = new ArrayList();
this.fileName = "";
this.encoding = Encoding.UTF8;
this.encoding = Encoding.Default;
}
/// <summary>
......@@ -41,15 +43,15 @@ namespace Lemon.Csv
/// <param name="fileName">文件名,包括文件路径</param>
public CsvStreamReader(string fileName)
{
this.rowAL = new List<object>();
this.rowAL = new ArrayList();
this.fileName = fileName;
this.encoding = Encoding.UTF8;
this.encoding = Encoding.Default;
LoadCsvFile();
}
public CsvStreamReader(StreamReader sr)
{
this.rowAL = new List<object>();
this.rowAL = new ArrayList();
LoadCsvFile(sr);
}
......@@ -60,7 +62,7 @@ namespace Lemon.Csv
/// <param name="encoding">文件编码</param>
public CsvStreamReader(string fileName, Encoding encoding)
{
this.rowAL = new List<object>();
this.rowAL = new ArrayList();
this.fileName = fileName;
this.encoding = encoding;
LoadCsvFile();
......@@ -113,7 +115,7 @@ namespace Lemon.Csv
maxCol = 0;
for (int i = 0; i < this.rowAL.Count; i++)
{
List<object> colAL = (List<object>)this.rowAL[i];
ArrayList colAL = (ArrayList)this.rowAL[i];
maxCol = (maxCol > colAL.Count) ? maxCol : colAL.Count;
}
......@@ -137,7 +139,7 @@ namespace Lemon.Csv
//数据有效性验证
CheckRowValid(row);
CheckColValid(col);
List<object> colAL = (List<object>)this.rowAL[row - 1];
ArrayList colAL = (ArrayList)this.rowAL[row - 1];
//如果请求列数据大于当前行的列时,返回空值
......@@ -150,7 +152,72 @@ namespace Lemon.Csv
}
}
public List<object> GetRawData()
/// <summary>
/// 根据最小行,最大行,最小列,最大列,来生成一个DataTable类型的数据
/// 行等于1代表第一行
/// 列等于1代表第一列
/// maxrow: -1代表最大行
/// maxcol: -1代表最大列
/// </summary>
public DataTable this[int minRow, int maxRow, int minCol, int maxCol]
{
get
{
//数据有效性验证
CheckRowValid(minRow);
CheckMaxRowValid(maxRow);
CheckColValid(minCol);
CheckMaxColValid(maxCol);
if (maxRow == -1)
{
maxRow = RowCount;
}
if (maxCol == -1)
{
maxCol = ColCount;
}
if (maxRow < minRow)
{
throw new Exception("最大行数不能小于最小行数");
}
if (maxCol < minCol)
{
throw new Exception("最大列数不能小于最小列数");
}
DataTable csvDT = new DataTable();
int i;
int col;
int row;
//增加列
for (i = minCol; i <= maxCol; i++)
{
csvDT.Columns.Add(i.ToString());
}
for (row = minRow; row <= maxRow; row++)
{
DataRow csvDR = csvDT.NewRow();
i = 0;
for (col = minCol; col <= maxCol; col++)
{
csvDR[i] = this[row, col];
i++;
}
csvDT.Rows.Add(csvDR);
}
return csvDT;
}
}
public ArrayList GetRawData()
{
return this.rowAL;
}
......@@ -243,19 +310,16 @@ namespace Lemon.Csv
}
if (this.encoding == null)
{
this.encoding = Encoding.UTF8;
this.encoding = Encoding.Default;
}
FileStream fsr = new FileStream(this.fileName,FileMode.Open);
StreamReader sr = new StreamReader(fsr);
StreamReader sr = new StreamReader(this.fileName, this.encoding);
LoadCsvFile(sr);
fsr.Dispose();
}
private void LoadCsvFile(StreamReader sr)
{
string csvDataLine;
csvDataLine = "";
string csvDataLine = "";
while (true)
{
string fileDataLine;
......@@ -280,11 +344,11 @@ namespace Lemon.Csv
csvDataLine = "";
}
}
sr.Dispose();
sr.Close();
//数据行出现奇数个引号
if (csvDataLine.Length > 0)
{
throw new Exception("CSV文件的格式有错误");
throw new Exception("CSV文件的格式有错误" + csvDataLine);
}
}
......@@ -402,7 +466,7 @@ namespace Lemon.Csv
//return;
List<object> colAL = new List<object>();
ArrayList colAL = new ArrayList();
string[] dataArray = newDataLine.Split(',');
bool oddStartQuota; //是否以奇数个引号开始
......@@ -452,7 +516,7 @@ namespace Lemon.Csv
}
if (oddStartQuota)
{
throw new Exception("数据格式有问题");
throw new Exception("数据格式有问题" + newDataLine);
}
this.rowAL.Add(colAL);
}
......
......@@ -141,7 +141,7 @@ namespace Lemon
else if (propertyType == typeof(System.Boolean))
propertyValue = val.ToString();
else
propertyValue = "'" + val.ToString() + "'";
propertyValue = "'" + val.ToString().Replace("'", "\\'") + "'";
}
else
{
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册