示例#1
0
        private int InternalSaveAll <TStorageModel, TModel>(
            SQLiteParallelConnection db,
            ICollection <TModel> models,
            bool overwriteExisting = true) where TModel : BaseModel, new()
            where TStorageModel : BaseStorageModel <TModel>, new()
        {
            using (new PerformanceTimer(typeof(TStorageModel).Name + ".batch.insert", true))
            {
                int rowCount = 0;
                try
                {
                    db.BeginTransaction();
                    foreach (var model in models)
                    {
                        TStorageModel storageModel = StorageModelInitializers.FromModel <TStorageModel, TModel>(model);
                        if (overwriteExisting)
                        {
                            rowCount += db.InsertOrReplace(storageModel);
                        }
                        else
                        {
                            rowCount += db.Insert(storageModel);
                        }
                    }
                    db.Commit();
                }
                catch (Exception)
                {
                    db.Rollback();
                    rowCount = -1;
                }

                return(rowCount);
            }
        }
示例#2
0
 public Task Delete <TStorageModel, TModel>(TModel model) where TModel : BaseModel
     where TStorageModel : BaseStorageModel <TModel>
 {
     return(this.WithDBAsync(db => {
         using (new PerformanceTimer(typeof(TStorageModel).Name + ".delete", false))
         {
             var storageModel = StorageModelInitializers.FromModel <TStorageModel, TModel>(model);
             db.Delete(storageModel);
         }
     },
                             true));
 }
示例#3
0
 public Task <int> SaveAsync <TStorageModel, TModel>(TModel model) where TModel : BaseModel, new()
     where TStorageModel : BaseStorageModel <TModel>, new()
 {
     return(Task.Run(async() => {
         using (var db = await this.connectionPool.GetConnectionAsync(true))
         {
             using (new PerformanceTimer(typeof(TStorageModel).Name + ".insert", false))
             {
                 var storageModel = StorageModelInitializers.FromModel <TStorageModel, TModel>(model);
                 return db.InsertOrReplace(storageModel);
             }
         }
     }));
 }