You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

110 lines
3.1 KiB
C#

using iBoxDB.LocalServer;
using System;
using System.IO;
namespace Highway.Assemble.common
{
public class BoxDB : IBoxDB
{
public BoxDB()
{
CreatDB();
}
public AutoBox auto = null;
private void CreatDB()
{
if (auto == null)
{
string dbpath = System.Environment.CurrentDirectory + "\\DB";
if (!Directory.Exists(dbpath))
{
Directory.CreateDirectory(dbpath);
}
DB.Root(dbpath);
DB db = new DB(1);
//load from Resources
//db = new DB(((TextAsset)(UnityEngine.Resources.Load("db2"))).bytes);
// two tables(Players,Items) and their keys(ID,Name)
db.GetConfig().EnsureTable<mesSoft>("MesSoft", "index");
db.GetConfig().EnsureTable<Equip>("Equip", "index");
db.GetConfig().EnsureTable<Sensor>("Sensor", "index");
db.GetConfig().EnsureTable<Collect>("Collect", "index");
{
// [Optional]
// if device has small memory & disk
db.MinConfig();
// smaller DB file size
db.GetConfig().DBConfig.FileIncSize = 1;
}
auto = db.Open();
}
}
public AutoBox GetAutoBox()
{
if (auto == null)
{
// Debug.LogWarning("请先创建数据库");
return null;
}
return auto;
}
/// <summary>
/// 添加数据 索引自增
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="dataName">表名</param>
/// <param name="data">数据</param>
public void Insert<T>(string table, T data) where T : BaseObject
{
data.index = auto.Id(1);//索引自增
data.time = System.DateTime.Now;// CatLib.SystemTime.Timestamp(System.DateTime.Now);
auto.Insert(table, data);
}
//foreach (PlayerInfo item in auto.Select<PlayerInfo>("from PlayerInfo Age ==?",15))
/// <summary>
/// 删除数据库IBoxDB中没有直接删除一个表的接口所以这个方法显得格外亲切~
/// 注意:删除数据库之前要关闭该数据库
/// </summary>
/// <param name="address">数据库地址</param>
public void DeleteDataBase(int address)
{
iBoxDB.DBDebug.DDebug.DeleteDBFiles(address);
}
}
public interface IBoxDB
{
void Insert<T>(string table, T data) where T : BaseObject;
AutoBox GetAutoBox();
}
public class BaseObject
{
public int index;
public DateTime time;//=DateTime.Now;//long
public BaseObject() { }
}
public static class IDHelper
{
// helper long -> int if using int ID
public static int Id(this AutoBox auto, byte pos, int step = 1)
{
return (int)auto.NewId(pos, step);
}
}
}