Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using SQLite;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Xml.Linq;
- using Xamarin.Forms;
- namespace SQLiteNetPCLVS
- {
- public class ItemDatabaseGeneric
- {
- protected static object locker = new object();
- protected SQLiteConnection database;
- public ItemDatabaseGeneric()
- {
- database = DependencyService.Get<IDatabase>().DBConnect();
- database.CreateTable<Item>();
- database.CreateTable<Person>();
- }
- public IEnumerable<T> GetObjects<T> () where T: IObject, new ()
- {
- lock (locker)
- {
- return (from i in database.Table<T>() select i).ToList();
- }
- }
- public IEnumerable<T> GetFirstObjects<T> () where T: IObject, new ()
- {
- lock (locker)
- {
- // TODO - fix this so the table in the SELECT is also dynamic
- return database.Query<T>("SELECT * FROM Item WHERE Name = 'First'");
- }
- }
- public T GetObject<T>(int id) where T: IObject, new ()
- {
- lock (locker)
- {
- return database.Table<T>().FirstOrDefault(x => x.ID == id);
- }
- }
- public int SaveObject<T>(T obj) where T : IObject
- {
- lock (locker)
- {
- if (obj.ID != 0)
- {
- database.Update(obj);
- return obj.ID;
- } else
- {
- return database.Insert(obj);
- }
- }
- }
- public int DeleteObject<T>(int id) where T: IObject, new ()
- {
- lock (locker)
- {
- return database.Delete<T>(id);
- }
- }
- public void DeleteAllObjects<T>() where T: IObject, new()
- {
- lock (locker)
- {
- database.DropTable<T>();
- database.CreateTable<T>();
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment