Пример #1
0
 public void Insert(VideoItem item)
 {
     using (connection.Lock())
     {
         connection.Insert(item);
     }
 }
Пример #2
0
 public void Add(AlbumItem album)
 {
     using (connection.Lock())
     {
         connection.Insert(album);
     }
 }
Пример #3
0
 public void Add(BackgroundTrackItem track)
 {
     using (connection.Lock())
     {
         connection.Insert(track);
     }
 }
Пример #4
0
        private Recipe CreateRecipe(Recipe recipe)
        {
            var row = recipe.ToRecipeRow();

            using (_connection.Lock())
            {
                _connection.RunInTransaction(() =>
                {
                    _connection.Insert(row);
                    recipe.Id  = row.Id;
                    var fields = FieldExtractor.Extract(recipe);
                    _connection.InsertAll(fields);
                    var searchFields = fields
                                       .Where(x => x.IsSearchable)
                                       .Select(x => new RecipeTextSearchRow
                    {
                        Id    = x.Id,
                        Value = _queryParser.Parse(x.Value)
                    })
                                       .ToList();
                    _connection.InsertAll(searchFields);
                });
            }
            return(recipe);
        }
Пример #5
0
        public void UpdateToLatestVersion()
        {
            var context = Mvx.Resolve <UpdateContext>();

            context.Connection = _connection;
            var version = GetCurrentVersion();
            var updates = GetPendingUpdates(version);

            foreach (var update in updates)
            {
                var meta = update.Item2;
                var code = update.Item1;
                Trace($"Applying updated #{meta.Version} [{meta.Tag}]...");
                using (_connection.Lock())
                {
                    _connection.RunInTransaction(() =>
                    {
                        code.Apply(context);
                        var row = new VersionRow {
                            Tag = meta.Tag, Timestamp = new DateTime(), Version = meta.Version
                        };
                        _connection.Insert(row);
                    });
                }
                Trace($"Update #{meta.Version} [{meta.Tag}] was successfully applied.");
            }
        }
Пример #6
0
        public void Insert(string result)
        {
            var example = new QrCodeResult {
                Result = result, DateTime = GetTimeDate()
            };

            _connection.Insert(example);
        }
        public void Insert(string result)
        {
            var example = new Example()
            {
                Result = result
            };

            _connection.Insert(example);
        }
Пример #8
0
        public void Insert <T>(T entity)
            where T : class
        {
            if (entity is EntityBase)
            {
                PrepareForInsert(entity as EntityBase);
            }

            using (Connection.Lock())
            {
                Connection.Insert(entity);
            }
        }
Пример #9
0
 public Task <int> InsertAsync(object item)
 {
     if (item == null)
     {
         throw new ArgumentNullException("item");
     }
     return(Task.Factory.StartNew(() =>
     {
         SQLiteConnectionWithLock conn = GetConnection();
         using (conn.Lock())
         {
             return conn.Insert(item);
         }
     }, CancellationToken.None, _taskCreationOptions, _taskScheduler ?? TaskScheduler.Default));
 }
Пример #10
0
 public Task <int> InsertAsync(object item)
 {
     if (item == null)
     {
         throw new ArgumentNullException("item");
     }
     return(_taskFactory.StartNew(() =>
     {
         SQLiteConnectionWithLock conn = GetConnection();
         using (conn.Lock())
         {
             return conn.Insert(item);
         }
     }));
 }
Пример #11
0
 public int Upsert(T entity)
 {
     //using (SQLiteConnection _dbManager = new SQLiteConnection(_baseUrl.GetDatabasePath(), WriteOnlyFlags))
     using (SQLiteConnectionWithLock _dbManager = new SQLiteConnectionWithLock(new SQLiteConnectionString(_baseUrl.GetDatabasePath(), false, null), SQLiteOpenFlags.ReadWrite | SQLiteOpenFlags.Create | SQLiteOpenFlags.FullMutex | SQLiteOpenFlags.SharedCache))
     {
         using (_dbManager.Lock())
         {
             try
             {
                 return(entity.Id == 0 ? _dbManager.Insert(entity) : _dbManager.Update(entity));
             }
             catch (Exception ex)
             {
                 Debug.WriteLine($"SQLiteError: {ex.Message}");
                 return(-1);
             }
         }
     }
 }
Пример #12
0
 public void UpsertAll(System.Collections.IEnumerable collection)
 {
     foreach (T item in collection)
     {
         if (item.Id == 0)
         {
             //using (SQLiteConnection _dbManager = new SQLiteConnection(_baseUrl.GetDatabasePath(), WriteOnlyFlags))
             using (SQLiteConnectionWithLock _dbManager = new SQLiteConnectionWithLock(new SQLiteConnectionString(_baseUrl.GetDatabasePath(), false, null), SQLiteOpenFlags.ReadWrite | SQLiteOpenFlags.Create | SQLiteOpenFlags.FullMutex | SQLiteOpenFlags.SharedCache))
             {
                 using (_dbManager.Lock())
                 {
                     try
                     {
                         _dbManager.Insert(item);
                     }
                     catch (Exception ex)
                     {
                         Debug.WriteLine($"SQLiteError: {ex.Message}");
                     }
                 }
             }
         }
         else
         {
             //using (SQLiteConnection _dbManager = new SQLiteConnection(_baseUrl.GetDatabasePath(), WriteOnlyFlags))
             using (SQLiteConnectionWithLock _dbManager = new SQLiteConnectionWithLock(new SQLiteConnectionString(_baseUrl.GetDatabasePath(), false, null), SQLiteOpenFlags.ReadWrite | SQLiteOpenFlags.Create | SQLiteOpenFlags.FullMutex | SQLiteOpenFlags.SharedCache))
             {
                 using (_dbManager.Lock())
                 {
                     try
                     {
                         _dbManager.Update(item);
                     }
                     catch (Exception ex)
                     {
                         Debug.WriteLine($"SQLiteError: {ex.Message}");
                     }
                 }
             }
         }
     }
 }
        private void AddRecords()
        {
            SqLiteUtilitiesAlt utilities;
            int maxValue = 999;

            utilities = new SqLiteUtilitiesAlt(this);

            TimeSpan original, alternative;

            try
            {
                utilities.OpenConnection();

                var stopWatch = Stopwatch.StartNew();
                for (int i = 0; i <= maxValue; i++)
                {
                    utilities.AddRecord("test", "person", i, "12345678901234567890123456789012345678901234567890");
                }
                stopWatch.Stop();

                original = stopWatch.Elapsed;

                utilities.CloseConnection();

                var newConnection = new SQLiteConnectionWithLock(new SQLitePlatformAndroid(),
                                                                 new SQLiteConnectionString(Path.Combine(Environment.ExternalStorageDirectory.AbsolutePath, "async.db"), true));

                newConnection.DropTable <Record>();
                newConnection.CreateTable <Record>();

                stopWatch.Restart();

                newConnection.BeginTransaction();

                for (int i = 0; i <= maxValue; i++)
                {
                    newConnection.Insert(new Record()
                    {
                        FirstName = "test",
                        LastName  = "person",
                        Index     = i,
                        Misc      = "12345678901234567890123456789012345678901234567890"
                    });
                }

                newConnection.Commit();

                stopWatch.Stop();
                alternative = stopWatch.Elapsed;
            }
            catch (Exception ex)
            {
                AlertDialog.Builder dlgException = new AlertDialog.Builder(this);
                dlgException.SetMessage("An error has occurred adding records: " + ex.Message);
                dlgException.SetTitle("Error");
                dlgException.SetPositiveButton("OK", (sender, args) => { });
                dlgException.SetCancelable(true);
                dlgException.Create().Show();
                return;
            }

            AlertDialog.Builder dlgAlert = new AlertDialog.Builder(this);
            dlgAlert.SetMessage(string.Format("All records written to database. Original time: {0}, alternative time: {1}", original, alternative));
            dlgAlert.SetTitle("Success");
            dlgAlert.SetPositiveButton("OK", (sender, args) => { });
            dlgAlert.SetCancelable(true);
            dlgAlert.Create().Show();
            return;
        }
Пример #14
0
        /// <summary>
        /// 跟proxy对接的接口,接收json object,解析数据并插入数据库
        /// </summary>
        /// <param name="data">数据</param>
        /// <param name="typename">数据类型名称,JSON里没有的话就要提供</param>
        public void ReceiveDataObject(JObject data, string typename = null)
        {
            object        obj     = null;
            List <object> objList = null;
            var           type    = data.ContainsKey("type") ? data.Value <string>("type") : typename;

            Log?.Information("Database receive {type}.", type);
            try
            {
                switch (type)
                {
                case "Gun/developGun":
                {
                    // 奇数是普通建造
                    if (data.Value <int>("build_slot") % 2 == 1)
                    {
                        obj = data.ToObject <GunDevelop>();
                    }
                    else
                    {
                        obj = data.ToObject <GunDevelopHeavy>();
                    }

                    break;
                }

                case "Gun/developMultiGun":
                {
                    var guns = data["gun_ids"];
                    objList = new List <object>();
                    foreach (var gun in guns)
                    {
                        var id   = gun.Value <int>("id");
                        var slot = gun.Value <int>("slot");
                        if (slot % 2 == 1)
                        {
                            var tmp = data.ToObject <GunDevelop>();
                            tmp.gun_id = id;
                            objList.Add(tmp);
                        }
                        else
                        {
                            var tmp = data.ToObject <GunDevelopHeavy>();
                            tmp.gun_id = id;
                            objList.Add(tmp);
                        }
                    }

                    break;
                }

                case "Mission/battleFinish":
                {
                    var tmp = data.ToObject <MissionBattle>();
                    if (data.ContainsKey("battle_get_gun"))
                    {
                        var guns = data.Value <JArray>("battle_get_gun");
                        if (guns.Count > 0)
                        {
                            tmp.gun_id = guns[0].Value <int>();
                        }

                        if (guns.Count > 1)
                        {
                            tmp.gun_id_extra = guns[1].Value <int>();
                        }
                    }

                    if (data.ContainsKey("battle_get_equip"))
                    {
                        var equips = data.Value <JArray>("battle_get_equip");
                        if (equips.Count > 0)
                        {
                            tmp.equip_id = equips[0].Value <int>();
                        }

                        if (equips.Count > 1)
                        {
                            tmp.equip_id_extra = equips[1].Value <int>();
                        }
                    }

                    _enemy2mission.TryGetValue(tmp.enemy, out var mission_id);
                    tmp.mission_id = mission_id;
                    obj            = tmp;
                    break;
                }

                case "Mission/endTurn":
                {
                    var tmp = data.ToObject <MissionFinish>();
                    if (data.ContainsKey("reward_equip"))
                    {
                        var equips = data.Value <JArray>("reward_equip");
                        if (equips.Count > 0)
                        {
                            tmp.equip_id = equips[0].Value <int>();
                        }

                        if (equips.Count > 1)
                        {
                            tmp.equip_id_extra = equips[1].Value <int>();
                        }
                    }

                    if (data.ContainsKey("reward_gun"))
                    {
                        var guns = data.Value <JArray>("reward_gun");
                        if (guns.Count > 0)
                        {
                            tmp.gun_id = guns[0].Value <int>();
                        }

                        if (guns.Count > 1)
                        {
                            tmp.gun_id_extra = guns[1].Value <int>();
                        }
                    }

                    _spot2mission.TryGetValue(tmp.spot_id, out var mission_id);
                    tmp.mission_id = mission_id;
                    obj            = tmp;
                    break;
                }

                case "Equip/produceDevelop":
                {
                    objList = new List <object>();
                    var equips = data.Value <JArray>("equips");
                    foreach (var equip in equips)
                    {
                        var tmp = data.ToObject <EquipProduce>();
                        tmp.equip_id = equip.Value <int>();
                        objList.Add(tmp);
                    }

                    break;
                }

                case "Equip/develop":
                {
                    if (data.Value <int>("build_slot") % 2 == 1)
                    {
                        obj = data.ToObject <EquipDevelop>();
                    }
                    else
                    {
                        obj = data.ToObject <EquipDevelopHeavy>();
                    }

                    break;
                }

                case "Equip/developMulti":
                {
                    objList = new List <object>();
                    var equips = data.Value <JArray>("equip_ids");
                    var basic  = data.ToObject <EquipDevelopHeavy>();   // 获取基础信息
                    foreach (var equip in equips)
                    {
                        if (equip["slot"].Value <int>() % 2 == 1)
                        {
                            var tmp = equip["info"].ToObject <EquipDevelop>();
                            tmp.timestamp = basic.timestamp;
                            tmp.mp        = basic.mp;
                            tmp.ammo      = basic.ammo;
                            tmp.mre       = basic.mre;
                            tmp.part      = basic.part;
                            objList.Add(tmp);
                        }
                        else
                        {
                            var tmp = equip["info"].ToObject <EquipDevelopHeavy>();
                            tmp.timestamp   = basic.timestamp;
                            tmp.mp          = basic.mp;
                            tmp.ammo        = basic.ammo;
                            tmp.mre         = basic.mre;
                            tmp.part        = basic.part;
                            tmp.input_level = basic.input_level;
                            objList.Add(tmp);
                        }
                    }

                    break;
                }
                }
            }
            catch (Exception e)
            {
                Log?.Warning("Error during insert data : {0}", e.ToString());
            }

            if (obj != null)
            {
                Log?.Debug("Database insert {obj}", obj.ToString());
                _db.Insert(obj);
            }

            if (objList != null)
            {
                Log?.Debug("Database insert {n} objects.", objList.Count);
                _db.InsertAll(objList);
            }
        }