From 0f0ff5c9fff2015b70b06f781beee03a6abd724f Mon Sep 17 00:00:00 2001 From: Antonio Date: Tue, 24 Nov 2015 17:09:21 +0100 Subject: [PATCH 1/2] Create Database.java Base for fluent database access --- src/com/activeandroid/util/Database.java | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 src/com/activeandroid/util/Database.java 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); + } +} From 012b16ae10cd76bf1914923cc2ddb17088f85abf Mon Sep 17 00:00:00 2001 From: Antonio Date: Tue, 24 Nov 2015 17:10:43 +0100 Subject: [PATCH 2/2] Create Repository.java Repository for loading and editing data --- src/com/activeandroid/util/Repository.java | 75 ++++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 src/com/activeandroid/util/Repository.java 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(); + } +}