BufferSQLite.cs 9.71 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
using FLY.OBJComponents.Common;
using FLY.OBJComponents.IService;
using FObjBase;
using SQLite;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FLY.OBJComponents.Server
{
    public class BufferSQLite<TLc, TDb> : IBufferAdd<TLc>
        where TLc : IDbBase, new()
        where TDb : IDbBase, new()

    {
        DBTable<TDb> dbTable;
        SQLiteHelper sqliteHelper;
        Func<TLc, TDb> mapLc2Db;
        Func<TDb, TLc> mapDb2Lc;

        public int NewestID => Count - 1;

        public int Count { get; private set; }

        /// <summary>
        /// 不能设置,设了也没有用
        /// </summary>
        public int Capacity { get; set; } = int.MaxValue;

        public event NotifyBufferChangedEventHandler<TLc> BufferChanged;
        public event PropertyChangedEventHandler PropertyChanged;


        public BufferSQLite()
        {

        }
        public void Init(DBTable<TDb> dbTable,
            Func<TLc, TDb> mapLc2Db, Func<TDb, TLc> mapDb2Lc)
        {
            
            this.dbTable = dbTable;
            this.sqliteHelper = dbTable.sqliteHelper;
            this.mapLc2Db = mapLc2Db;
            this.mapDb2Lc = mapDb2Lc;
            Load();
        }
        void Load()
        {
            var n = sqliteHelper.ExecuteScalar($"SELECT COUNT(*) FROM {dbTable.TableName}");
            int count = System.Convert.ToInt32(n);
            Count = count;
        }

        public void Add(TLc lc)
        {
            //添加 数据库必要的 field
            lc.ID = dbTable.FreeID;

            //SQLs
            List<string> sqls = new List<string>();
            sqls.Add(SQLiteHelper.GetInsertCommandText(mapLc2Db(lc)));
            sqliteHelper.QueryTranAsync(sqls);

            Count++;
潘栩锋's avatar
潘栩锋 committed
69 70 71 72 73 74 75 76 77
            BufferChanged?.Invoke(this, new NotifyBufferChangedEventArgs<TLc>()
            {
                Action = NotifyBufferChangedAction.Add,
                EndingID = NewestID,
                Count = 1,
                Items = new TLc[] { lc },
                BufferCount = Count,
                BufferNewestID = NewestID
            });
78 79
        }
        public void AddRange(IEnumerable<TLc> lcs)
潘栩锋's avatar
潘栩锋 committed
80 81 82
        {
            if (lcs.Count() == 0)
                return;
83 84 85 86 87 88 89 90 91 92 93
            List<string> sqls = new List<string>();
            foreach (var lc in lcs)
            {
                //添加 数据库必要的 field
                lc.ID = dbTable.FreeID;
                //SQLs
                sqls.Add(SQLiteHelper.GetInsertCommandText(mapLc2Db(lc)));
            }
            sqliteHelper.QueryTranAsync(sqls);

            Count += lcs.Count();
潘栩锋's avatar
潘栩锋 committed
94 95 96 97 98 99 100 101 102
            BufferChanged?.Invoke(this, new NotifyBufferChangedEventArgs<TLc>()
            {
                Action = NotifyBufferChangedAction.Add,
                EndingID = NewestID,
                Count = lcs.Count(),
                Items = lcs,
                BufferCount = Count,
                BufferNewestID = NewestID
            });
103 104 105 106 107 108 109 110 111 112 113
        }
        /// <summary>
        /// 清空全部数据
        /// </summary>
        public void Reset()
        {
            //SQLs
            List<string> sqls = new List<string>();
            sqls.Add($"DELETE FROM {dbTable.TableName}");
            sqliteHelper.QueryTranAsync(sqls);
            Count = 0;
潘栩锋's avatar
潘栩锋 committed
114 115 116 117 118 119 120 121
            BufferChanged?.Invoke(this, new NotifyBufferChangedEventArgs<TLc>()
            {
                Action = NotifyBufferChangedAction.Reset,
                EndingID = -1,
                Count = 0,
                BufferCount = Count,
                BufferNewestID = NewestID
            });
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
        }

        /// <summary>
        /// 获取指定位置的N个数据
        /// </summary>
        /// <param name="last_id">最后的ID</param>
        /// <param name="count">数量</param>
        /// <param name="asyncCB">回调</param>
        /// <param name="asyncContext">回调里面的上下文</param>
        public async void GetRecord(int last_id, int count, AsyncCBHandler asyncCB, object asyncContext)
        {
            if (Count == 0)
            {
                asyncCB(asyncContext, new GetRecordReponse<TLc>()
                {
                    LastID = 0,
                    Items = null
                });
                return;
            }

            await Task.Factory.StartNew(() => {
                var reponse = new GetRecordReponse<TLc>();

                var dbs = dbTable.Find($"ORDER BY ID LIMIT {last_id - count + 1},{count}");
                reponse.LastID = last_id;
                List<TLc> lcs = new List<TLc>();
                foreach (var db in dbs)
                {
                    lcs.Add(mapDb2Lc(db));
                }
                reponse.Items = lcs;

                asyncCB(asyncContext, reponse);
            });

        }

        /// <summary>
        /// 获取最新的N个数据
        /// </summary>
        /// <param name="count">数量</param>
        /// <param name="asyncCB">回调</param>
        /// <param name="asyncContext">回调里面的上下文</param>
        public void GetRecord(int count, AsyncCBHandler asyncCB, object asyncContext)
        {
            GetRecord(NewestID, count, asyncCB, asyncContext);
        }
    }

    public class BufferSQLite<TDb> : IBufferAdd<TDb>
    where TDb : IDbBase, new()

    {
        DBTable<TDb> dbTable;
潘栩锋's avatar
潘栩锋 committed
177
        SQLiteHelper sqliteHelper;
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
        public int NewestID => Count - 1;

        public int Count { get; private set; }

        /// <summary>
        /// 不能设置,设了也没有用
        /// </summary>
        public int Capacity { get; set; } = int.MaxValue;

        public event NotifyBufferChangedEventHandler<TDb> BufferChanged;
        public event PropertyChangedEventHandler PropertyChanged;


        public BufferSQLite()
        {

        }
潘栩锋's avatar
潘栩锋 committed
195
        public void Init(DBTable<TDb> dbTable)
196 197
        {
            this.dbTable = dbTable;
潘栩锋's avatar
潘栩锋 committed
198
            this.sqliteHelper = dbTable.sqliteHelper;
199 200 201 202
            Load();
        }
        void Load()
        {
潘栩锋's avatar
潘栩锋 committed
203
            var n = sqliteHelper.ExecuteScalar($"SELECT COUNT(*) FROM {dbTable.TableName}");
204 205 206 207 208 209 210 211 212 213 214 215
            int count = System.Convert.ToInt32(n);
            Count = count;
        }

        public void Add(TDb db)
        {
            //添加 数据库必要的 field
            db.ID = dbTable.FreeID;

            //SQLs
            List<string> sqls = new List<string>();
            sqls.Add(SQLiteHelper.GetInsertCommandText(db));
潘栩锋's avatar
潘栩锋 committed
216
            sqliteHelper.QueryTranAsync(sqls);
217 218

            Count++;
潘栩锋's avatar
潘栩锋 committed
219 220 221 222 223
            BufferChanged?.Invoke(this, new NotifyBufferChangedEventArgs<TDb>()
            {
                Action = NotifyBufferChangedAction.Add,
                EndingID = NewestID,
                Count = 1,
224
                Items = new TDb[] { db},
潘栩锋's avatar
潘栩锋 committed
225 226 227
                BufferCount = Count,
                BufferNewestID = NewestID
            });
228 229 230
        }
        public void AddRange(IEnumerable<TDb> dbs)
        {
潘栩锋's avatar
潘栩锋 committed
231 232
            if (dbs.Count() == 0)
                return;
233 234 235 236 237 238 239 240 241
            List<string> sqls = new List<string>();
            foreach (var db in dbs)
            {
                //添加 数据库必要的 field
                db.ID = dbTable.FreeID;

                //SQLs
                sqls.Add(SQLiteHelper.GetInsertCommandText(db));
            }
潘栩锋's avatar
潘栩锋 committed
242
            sqliteHelper.QueryTranAsync(sqls);
243 244

            Count += dbs.Count();
潘栩锋's avatar
潘栩锋 committed
245 246 247 248 249
            BufferChanged?.Invoke(this, new NotifyBufferChangedEventArgs<TDb>()
            {
                Action = NotifyBufferChangedAction.Add,
                EndingID = NewestID,
                Count = dbs.Count(),
250
                Items = dbs,
潘栩锋's avatar
潘栩锋 committed
251 252 253
                BufferCount = Count,
                BufferNewestID = NewestID
            });
254 255 256 257 258 259 260 261 262
        }
        /// <summary>
        /// 清空全部数据
        /// </summary>
        public void Reset()
        {
            //SQLs
            List<string> sqls = new List<string>();
            sqls.Add($"DELETE FROM {dbTable.TableName}");
潘栩锋's avatar
潘栩锋 committed
263
            sqliteHelper.QueryTranAsync(sqls);
264
            Count = 0;
潘栩锋's avatar
潘栩锋 committed
265 266 267 268 269 270 271 272
            BufferChanged?.Invoke(this, new NotifyBufferChangedEventArgs<TDb>()
            {
                Action = NotifyBufferChangedAction.Reset,
                EndingID = -1,
                Count = 0,
                BufferCount = Count,
                BufferNewestID = NewestID
            });
273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316
        }

        /// <summary>
        /// 获取指定位置的N个数据
        /// </summary>
        /// <param name="last_id">最后的ID</param>
        /// <param name="count">数量</param>
        /// <param name="asyncCB">回调</param>
        /// <param name="asyncContext">回调里面的上下文</param>
        public async void GetRecord(int last_id, int count, AsyncCBHandler asyncCB, object asyncContext)
        {
            if (Count == 0)
            {
                asyncCB(asyncContext, new GetRecordReponse<TDb>()
                {
                    LastID = 0,
                    Items = null
                });
                return;
            }

            await Task.Factory.StartNew(() => {
                var reponse = new GetRecordReponse<TDb>();

                var dbs = dbTable.Find($"ORDER BY ID LIMIT {last_id - count + 1},{count}");
                reponse.LastID = last_id;
                reponse.Items = dbs;
                asyncCB(asyncContext, reponse);
            });

        }

        /// <summary>
        /// 获取最新的N个数据
        /// </summary>
        /// <param name="count">数量</param>
        /// <param name="asyncCB">回调</param>
        /// <param name="asyncContext">回调里面的上下文</param>
        public void GetRecord(int count, AsyncCBHandler asyncCB, object asyncContext)
        {
            GetRecord(NewestID, count, asyncCB, asyncContext);
        }
    }
}