From e26e3886ee2d79151c7ec35a1211be175f32917c Mon Sep 17 00:00:00 2001 From: AlexZam Date: Tue, 24 Mar 2015 23:53:38 +0300 Subject: [PATCH] Views support --- src/com/activeandroid/DatabaseHelper.java | 1 + src/com/activeandroid/Model.java | 6 ++--- src/com/activeandroid/TableInfo.java | 9 ++++++- src/com/activeandroid/View.java | 30 +++++++++++++++++++++ src/com/activeandroid/annotation/Table.java | 1 + 5 files changed, 43 insertions(+), 4 deletions(-) create mode 100644 src/com/activeandroid/View.java diff --git a/src/com/activeandroid/DatabaseHelper.java b/src/com/activeandroid/DatabaseHelper.java index cc3bebec7..c4b14d2b8 100644 --- a/src/com/activeandroid/DatabaseHelper.java +++ b/src/com/activeandroid/DatabaseHelper.java @@ -157,6 +157,7 @@ private void executeCreate(SQLiteDatabase db) { db.beginTransaction(); try { for (TableInfo tableInfo : Cache.getTableInfos()) { + if(tableInfo.isView()) continue; db.execSQL(SQLiteUtils.createTableDefinition(tableInfo)); } db.setTransactionSuccessful(); diff --git a/src/com/activeandroid/Model.java b/src/com/activeandroid/Model.java index 276b10c9c..7b9fb087d 100644 --- a/src/com/activeandroid/Model.java +++ b/src/com/activeandroid/Model.java @@ -63,11 +63,11 @@ public Model() { // PUBLIC METHODS ////////////////////////////////////////////////////////////////////////////////////// - public final Long getId() { + public Long getId() { return mId; } - public final void delete() { + public void delete() { Cache.openDatabase().delete(mTableInfo.getTableName(), idName+"=?", new String[] { getId().toString() }); Cache.removeEntity(this); @@ -75,7 +75,7 @@ public final void delete() { .notifyChange(ContentProvider.createUri(mTableInfo.getType(), mId), null); } - public final Long save() { + public Long save() { final SQLiteDatabase db = Cache.openDatabase(); ContentValues values = new ContentValues(); diff --git a/src/com/activeandroid/TableInfo.java b/src/com/activeandroid/TableInfo.java index e55b63ea0..846d498ad 100644 --- a/src/com/activeandroid/TableInfo.java +++ b/src/com/activeandroid/TableInfo.java @@ -39,6 +39,7 @@ public final class TableInfo { private Class mType; private String mTableName; private String mIdName = Table.DEFAULT_ID_NAME; + private boolean mView; private Map mColumnNames = new LinkedHashMap(); @@ -54,9 +55,11 @@ public TableInfo(Class type) { if (tableAnnotation != null) { mTableName = tableAnnotation.name(); mIdName = tableAnnotation.id(); + mView = tableAnnotation.view(); } else { mTableName = type.getSimpleName(); + mView = View.class.isAssignableFrom(type); } // Manually add the id column since it is not declared like the other columns. @@ -95,7 +98,11 @@ public String getIdName() { return mIdName; } - public Collection getFields() { + public boolean isView() { + return mView; + } + + public Collection getFields() { return mColumnNames.keySet(); } diff --git a/src/com/activeandroid/View.java b/src/com/activeandroid/View.java new file mode 100644 index 000000000..b91267cb0 --- /dev/null +++ b/src/com/activeandroid/View.java @@ -0,0 +1,30 @@ +package com.activeandroid; + +import com.activeandroid.util.SQLiteUtils; + +import java.util.List; + +public abstract class View extends Model { + public abstract String getQuery(); + + @Override + public void delete() { + throw new RuntimeException("Cannot delete view row"); + } + + @Override + public Long save() { + throw new RuntimeException("Cannot save view row"); + } + + @SuppressWarnings("UnusedDeclaration") + public static List getRows(Class viewClass, String... params) { + String query; + try { + query = viewClass.newInstance().getQuery(); + return SQLiteUtils.rawQuery(viewClass, query, params); + } catch (Exception e) { + throw new RuntimeException("Cannot create view row.", e); + } + } +} diff --git a/src/com/activeandroid/annotation/Table.java b/src/com/activeandroid/annotation/Table.java index 541dfbe92..efefdc07c 100644 --- a/src/com/activeandroid/annotation/Table.java +++ b/src/com/activeandroid/annotation/Table.java @@ -28,4 +28,5 @@ public static final String DEFAULT_ID_NAME = "Id"; public String name(); public String id() default DEFAULT_ID_NAME; + boolean view() default false; }