diff --git a/src/com/activeandroid/util/Database.java b/src/com/activeandroid/util/Database.java new file mode 100644 index 000000000..bf8d68274 --- /dev/null +++ b/src/com/activeandroid/util/Database.java @@ -0,0 +1,24 @@ +package com.activeandroid.util; + +import com.activeandroid.Model; + +/** + * Created by adifrancesco on 24/11/2015. + */ +public class Database { + + private static Database _instance; + + private Database() {} + + public static Database Instance() { + if(_instance==null) + _instance = new Database(); + + return _instance; + } + + public Repository Repository(Class type) { + return new Repository(type); + } +} diff --git a/src/com/activeandroid/util/Repository.java b/src/com/activeandroid/util/Repository.java new file mode 100644 index 000000000..b26d3b96f --- /dev/null +++ b/src/com/activeandroid/util/Repository.java @@ -0,0 +1,75 @@ +package com.activeandroid.util; + +import com.activeandroid.ActiveAndroid; +import com.activeandroid.Model; +import com.activeandroid.query.From; +import com.activeandroid.query.Select; + +import java.util.List; + +/** + * Created by adifrancesco on 24/11/2015. + */ +public class Repository { + + private Class clazz; + + public Repository(Class clazz) + { + this.clazz = clazz; + } + + public void save(T m) { + m.save(); + } + + public void save(List m) { + ActiveAndroid.beginTransaction(); + try { + for(T item : m) { + item.save(); + } + ActiveAndroid.setTransactionSuccessful(); + } + finally { + ActiveAndroid.endTransaction(); + } + } + + public List getAll() { + return new Select() + .from(clazz) + .execute(); + } + + public T get(int id) { + return T.load(clazz, id); + /* + return new Select() + .from(clazz) + .where("Id = ?", id) + .execute(); + */ + } + + public T getById(int id) { + return new Select() + .from(clazz) + .where("Id = ?", id) + .executeSingle(); + } + + public From query() { + return new Select() + .from(clazz); + } + + public void delete(long i) { + T item = T.load(clazz, i); + item.delete(); + } + + public void delete(T item) { + item.delete(); + } +}