Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding fluent capabilities #430

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions src/com/activeandroid/util/Database.java
Original file line number Diff line number Diff line change
@@ -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 <T extends Model> Repository<T> Repository(Class<T> type) {
return new Repository<T>(type);
}
}
75 changes: 75 additions & 0 deletions src/com/activeandroid/util/Repository.java
Original file line number Diff line number Diff line change
@@ -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<T extends Model> {

private Class<T> clazz;

public Repository(Class<T> clazz)
{
this.clazz = clazz;
}

public void save(T m) {
m.save();
}

public void save(List<T> m) {
ActiveAndroid.beginTransaction();
try {
for(T item : m) {
item.save();
}
ActiveAndroid.setTransactionSuccessful();
}
finally {
ActiveAndroid.endTransaction();
}
}

public List<T> 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();
}
}