-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implemented AndroidOqlTemplate as OQL decorator for
AndroidSqliteTemplate.
- Loading branch information
Showing
14 changed files
with
486 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
src/com/perfectworldprogramming/mobile/orm/exception/InvalidCursorException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package com.perfectworldprogramming.mobile.orm.exception; | ||
|
||
public class InvalidCursorException extends DataAccessException { | ||
|
||
private static final long serialVersionUID = 323422738996859681L; | ||
|
||
public InvalidCursorException(Class<? extends Object> clazz) { | ||
super("Invalid Cursor: "+ clazz.getName() + ". Possible reasons are 1) query does not contain all the fields, 2) query is using the wrong tables, 3) Not navigating through the Cursor correctly, either by the cursor is empty, or has past the end of the results."); | ||
} | ||
|
||
} |
5 changes: 3 additions & 2 deletions
5
src/com/perfectworldprogramming/mobile/orm/exception/InvalidCursorExtractorException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,12 @@ | ||
package com.perfectworldprogramming.mobile.orm.exception; | ||
|
||
public class InvalidCursorExtractorException extends DataAccessException { | ||
public class InvalidCursorExtractorException extends InvalidCursorException { | ||
|
||
private static final long serialVersionUID = 323422738996859681L; | ||
|
||
public InvalidCursorExtractorException(Class<? extends Object> class1) { | ||
super("Invalid CursorExtractor: " + class1.getName() + ". Possible reasons are 1) query does not contain all the fields, 2) query is using the wrong tables, 3) Not navigating through the Cursor correctly, either by the cursor is empty, or has past the end of the results."); | ||
super(class1); | ||
//super("Invalid CursorExtractor: " + class1.getName() + ". Possible reasons are 1) query does not contain all the fields, 2) query is using the wrong tables, 3) Not navigating through the Cursor correctly, either by the cursor is empty, or has past the end of the results."); | ||
} | ||
|
||
} |
5 changes: 3 additions & 2 deletions
5
src/com/perfectworldprogramming/mobile/orm/exception/InvalidCursorRowMapperException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,11 @@ | ||
package com.perfectworldprogramming.mobile.orm.exception; | ||
|
||
public class InvalidCursorRowMapperException extends DataAccessException { | ||
public class InvalidCursorRowMapperException extends InvalidCursorException { | ||
|
||
private static final long serialVersionUID = 1L; | ||
|
||
public InvalidCursorRowMapperException(Class<? extends Object> class1) { | ||
super("Invalid CursorRowMapper: " + class1.getName() + ". Possible reasons are 1) query does not contain all the fields, 2) query is using the wrong tables, 3) Not navigating through the Cursor correctly, either by the cursor is empty, or has past the end of the results."); | ||
super(class1); | ||
//super("Invalid CursorRowMapper: " + class1.getName() + ". Possible reasons are 1) query does not contain all the fields, 2) query is using the wrong tables, 3) Not navigating through the Cursor correctly, either by the cursor is empty, or has past the end of the results."); | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
src/com/perfectworldprogramming/mobile/orm/exception/TransientFieldException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package com.perfectworldprogramming.mobile.orm.exception; | ||
|
||
import java.lang.reflect.Field; | ||
|
||
public class TransientFieldException extends DataAccessException { | ||
|
||
private static final long serialVersionUID = -442347181326281845L; | ||
|
||
public TransientFieldException(Field field) { | ||
super("Cannot persist transient field " + field.getName()); | ||
} | ||
} |
119 changes: 119 additions & 0 deletions
119
src/com/perfectworldprogramming/mobile/orm/oql/AndroidOqlTemplate.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
package com.perfectworldprogramming.mobile.orm.oql; | ||
|
||
import java.util.ArrayList; | ||
import java.util.HashSet; | ||
import java.util.List; | ||
import java.util.Set; | ||
|
||
import android.database.sqlite.SQLiteDatabase; | ||
|
||
import com.perfectworldprogramming.mobile.orm.AndroidSQLiteTemplate; | ||
import com.perfectworldprogramming.mobile.orm.interfaces.CursorExtractor; | ||
import com.perfectworldprogramming.mobile.orm.interfaces.CursorRowMapper; | ||
|
||
/** | ||
* Decorates the {@link AndroidSQLiteTemplate} with operations which refer to the domain | ||
* object rather than database, allowing data access to be persistence agnostic. | ||
* @author David O'Meara <[email protected]> | ||
* @since 14/06/2012 | ||
* | ||
*/ | ||
public class AndroidOqlTemplate extends AndroidSQLiteTemplate | ||
{ | ||
public AndroidOqlTemplate(SQLiteDatabase sqLiteDatabase) { | ||
super(sqLiteDatabase); | ||
} | ||
|
||
public long insert(String oql, OqlParameter... args) | ||
{ | ||
return super.insert(QueryParser.parse(oql, getTypes(args)), convertParams(args)); | ||
} | ||
|
||
public void update(String oql, OqlParameter... args) | ||
{ | ||
//super.update(oql, args); | ||
super.update(QueryParser.parse(oql, getTypes(args)), convertParams(args)); | ||
} | ||
|
||
public void delete(String oql, OqlParameter... args) | ||
{ | ||
//super.delete(oql, args); | ||
super.delete(QueryParser.parse(oql, getTypes(args)), convertParams(args)); | ||
} | ||
|
||
public int queryForInt(String oql, OqlParameter... args) | ||
{ | ||
//return super.queryForInt(oql, args); | ||
return super.queryForInt(QueryParser.parse(oql, getTypes(args)), convertParams(args)); | ||
} | ||
|
||
public long queryForLong(String oql, OqlParameter... args) | ||
{ | ||
//return super.queryForLong(oql, args); | ||
return super.queryForLong(QueryParser.parse(oql, getTypes(args)), convertParams(args)); | ||
} | ||
|
||
public String queryForString(String oql, OqlParameter... args) | ||
{ | ||
//return super.queryForString(oql, args); | ||
return super.queryForString(QueryParser.parse(oql, getTypes(args)), convertParams(args)); | ||
} | ||
|
||
public <T> T queryForObject(String oql, CursorRowMapper<T> cursorRowMapper, OqlParameter... args) | ||
{ | ||
//return super.queryForObject(oql, cursorRowMapper, args); | ||
return super.queryForObject(QueryParser.parse(oql, getTypes(args)), cursorRowMapper, convertParams(args)); | ||
} | ||
|
||
public <T> T queryForObject(String oql, Class<T> clazz, OqlParameter... args) | ||
{ | ||
//return super.queryForObject(oql, clazz, args); | ||
return super.queryForObject(QueryParser.parse(oql, getTypes(args)), clazz, convertParams(args)); | ||
} | ||
|
||
public <T> T queryForObject(String oql, CursorExtractor<T> cursorExtractor, OqlParameter... args) | ||
{ | ||
//return super.queryForObject(oql, cursorExtractor, args); | ||
return super.queryForObject(QueryParser.parse(oql, getTypes(args)), cursorExtractor, convertParams(args)); | ||
} | ||
|
||
public <T> List<T> query(String oql, Class<T> clazz, OqlParameter... args) | ||
{ | ||
//return super.query(oql, clazz, args); | ||
return super.query(QueryParser.parse(oql, getTypes(args)), clazz, convertParams(args)); | ||
} | ||
|
||
public <T> List<T> query(String oql, CursorRowMapper<T> cursorRowMapper, OqlParameter... args) | ||
{ | ||
//return super.query(oql, cursorRowMapper, args); | ||
return super.query(QueryParser.parse(oql, getTypes(args)), cursorRowMapper, convertParams(args)); | ||
} | ||
|
||
@Override | ||
public Object mapQueryParameter(Object value, Class<?> clazz, String fieldName) | ||
{ | ||
return this.domainClassAnalyzer.mapQueryParameterByFieldName(value, clazz, fieldName); | ||
} | ||
|
||
private List<Class<?>> getTypes(OqlParameter... args) | ||
{ | ||
Set<Class<?>> types = new HashSet<Class<?>>(); | ||
for(OqlParameter arg:args) | ||
{ | ||
types.add(arg.getDomainClass()); | ||
} | ||
List<Class<?>> result = new ArrayList<Class<?>>(types.size()); | ||
result.addAll(types); | ||
return result; | ||
} | ||
private Object[] convertParams(OqlParameter... args) | ||
{ | ||
Object[] result = new Object[args.length]; | ||
for(int i=0;i<args.length;i++) | ||
{ | ||
result[i] = this.mapQueryParameter(args[i].getDomainValue(), args[i].getDomainClass(), args[i].getFieldName()); | ||
} | ||
return result; | ||
} | ||
|
||
} |
34 changes: 34 additions & 0 deletions
34
src/com/perfectworldprogramming/mobile/orm/oql/OqlParameter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package com.perfectworldprogramming.mobile.orm.oql; | ||
|
||
/** | ||
* Immutable class containing the Domain layer representation of a value to be persisted. | ||
* @author David O'Meara <[email protected]> | ||
* @since 14/06/2012 | ||
* | ||
*/ | ||
public final class OqlParameter | ||
{ | ||
public OqlParameter(Class<?> domainClass, String fieldName, Object domainValue) | ||
{ | ||
this.domainClass = domainClass; | ||
this.fieldName = fieldName; | ||
this.domainValue = domainValue; | ||
} | ||
|
||
public Class<?> getDomainClass() | ||
{ | ||
return domainClass; | ||
} | ||
public String getFieldName() | ||
{ | ||
return fieldName; | ||
} | ||
public Object getDomainValue() | ||
{ | ||
return domainValue; | ||
} | ||
|
||
private final Class<?> domainClass; | ||
private final String fieldName; | ||
private final Object domainValue; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.