smeacham

ItemDatabaseBasic.cs

Jun 7th, 2016
439
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.87 KB | None | 0 0
  1. using SQLite;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7.  
  8. using Xamarin.Forms;
  9.  
  10. namespace SQLiteNetPCLVS
  11. {
  12.     public class ItemDatabaseBasic
  13.     {
  14.         protected static object locker = new object();
  15.         protected static SQLiteConnection database;
  16.  
  17.         public ItemDatabaseBasic()
  18.         {
  19.             database = DependencyService.Get<IDatabase>().DBConnect();
  20.             database.CreateTable<Item>();
  21.         }
  22.  
  23.         public IEnumerable<Item> GetItems ()
  24.         {
  25.             lock (locker)
  26.             {
  27.                 return (from i in database.Table<Item>() select i).ToList();
  28.             }
  29.         }
  30.  
  31.         public IEnumerable<Item> GetFirstItems()
  32.         {
  33.             lock (locker)
  34.             {
  35.                 return (from i in database.Query<Item>("SELECT * FROM Item WHERE Name = 'First'"));
  36.             }
  37.         }
  38.  
  39.         public Item GetItem(int id)
  40.         {
  41.             lock (locker)
  42.             {
  43.                 return database.Table<Item>().FirstOrDefault(x => x.ID == id);
  44.             }
  45.         }
  46.  
  47.         public int SaveItem(Item item)
  48.         {
  49.             lock (locker)
  50.             {
  51.                 if (item.ID != 0)
  52.                 {
  53.                     database.Update(item);
  54.                     return item.ID;
  55.                 } else
  56.                 {
  57.                     return database.Insert(item);
  58.                 }
  59.             }
  60.         }
  61.  
  62.         public int DeleteItem(int id)
  63.         {
  64.             lock (locker)
  65.             {
  66.                 return database.Delete<Item>(id);
  67.             }
  68.         }
  69.  
  70.         public void DeleteAllItems()
  71.         {
  72.             lock (locker)
  73.             {
  74.                 database.DropTable<Item>();
  75.                 database.CreateTable<Item>();
  76.             }
  77.         }
  78.     }
  79. }
Advertisement
Add Comment
Please, Sign In to add comment