Permalink
Browse files

Merge branch 'develop'

  • Loading branch information...
casidiablo committed May 22, 2012
2 parents dc0968c + 385138a commit 2c808042b6768690086b60b10116de2aafee9759
View
14 pom.xml
@@ -25,7 +25,7 @@
<groupId>com.codeslap</groupId>
<artifactId>persistence</artifactId>
- <version>0.9.14-SNAPSHOT</version>
+ <version>0.9.16-SNAPSHOT</version>
<packaging>jar</packaging>
<name>Codeslap Persistence</name>
<description>Core functionality provided by MarkdownJ.</description>
@@ -77,9 +77,9 @@
<scope>test</scope>
</dependency>
<dependency>
- <groupId>com.seventheye.android</groupId>
+ <groupId>com.codeslap</groupId>
<artifactId>robolectric-sqlite</artifactId>
- <version>1.0</version>
+ <version>1.0.1-SNAPSHOT</version>
<scope>test</scope>
</dependency>
<dependency>
@@ -108,14 +108,6 @@
<artifactId>maven-source-plugin</artifactId>
<version>2.1.2</version>
</plugin>
- <plugin>
- <groupId>com.atlassian.maven.plugins</groupId>
- <artifactId>maven-clover2-plugin</artifactId>
- <version>3.1.3</version>
- <configuration>
- <licenseLocation>/Users/cristian/Dropbox/clover.license</licenseLocation>
- </configuration>
- </plugin>
</plugins>
</build>
@@ -44,4 +44,9 @@
* @return the value to set to this element when no argument is assigned. This works for TEXT columns only.
*/
String defaultValue() default NULL;
+
+ /**
+ * @return true if the name of this column must be forced. Useful when handling primary keys whose name is not _id
+ */
+ boolean forceName() default false;
}
@@ -21,7 +21,7 @@
*/
public class Constraint {
private String mOrderBy;
- private int mLimit;
+ private Integer mLimit;
private String mGroupBy;
public Constraint() {
@@ -32,7 +32,7 @@ public Constraint orderBy(String column) {
return this;
}
- public Constraint limit(int limit) {
+ public Constraint limit(Integer limit) {
mLimit = limit;
return this;
}
@@ -46,7 +46,7 @@ String getOrderBy() {
return mOrderBy;
}
- int getLimit() {
+ Integer getLimit() {
return mLimit;
}
@@ -53,7 +53,7 @@ public ManyToMany(Class<?> classA, Class<?> classB) {
String getCreateTableStatement() {
StringBuilder builder = new StringBuilder();
builder.append("CREATE TABLE IF NOT EXISTS ").append(buildTableName(mClassA, mClassB));
- builder.append(" (").append(SQLHelper.PRIMARY_KEY).append(" AUTOINCREMENT, ");
+ builder.append(" (_id INTEGER PRIMARY KEY AUTOINCREMENT, ");
builder.append(getMainKey()).append(" TEXT NOT NULL, ");
builder.append(getSecondaryKey()).append(" TEXT NOT NULL");
builder.append(");");
@@ -48,7 +48,7 @@
@Override
public <T> void store(T bean) {
- Class<? extends Object> theClass = bean.getClass();
+ Class<?> theClass = bean.getClass();
if (!PersistenceConfig.getPreference(mName).belongsToPreferences(theClass)) {
throw new IllegalStateException("This object is not associated with a preference persister");
}
@@ -104,7 +104,7 @@
}
field.set(bean, value);
}
- } catch (Exception e) {
+ } catch (Exception ignored) {
}
return bean;
}
@@ -150,12 +150,12 @@
} else if (field.getType() == String.class) {
editor.putString(keyName, String.valueOf(value));
}
- } catch (IllegalAccessException e) {
+ } catch (IllegalAccessException ignored) {
}
}
}
- private SharedPreferences getSharedPreferences(Class<? extends Object> theClass) {
+ private SharedPreferences getSharedPreferences(Class<?> theClass) {
String key = theClass.getSimpleName();
if (!mPreferences.containsKey(key)) {
SharedPreferences prefs;
@@ -28,8 +28,7 @@
static final String ID = "id";
static final String _ID = "_id";
- static final String PRIMARY_KEY = _ID + " INTEGER PRIMARY KEY";
- private static final String PRIMARY_KEY_TEXT = "_id TEXT PRIMARY KEY";
+ static final String PRIMARY_KEY = "%s INTEGER PRIMARY KEY";
private static final String HEXES = "0123456789ABCDEF";
private static final Map<Class<?>, String> INSERT_COLUMNS_CACHE = new HashMap<Class<?>, String>();
@@ -48,9 +47,9 @@ static String getCreateTableSentence(String dbName, Class clazz) {
for (Field field : declaredFields) {
String columnName = getColumnName(field);
if (isPrimaryKey(field)) {
- String primaryKeySentence = PRIMARY_KEY;
+ String primaryKeySentence = getCreatePrimaryKey(field);
if (field.getType() == String.class) {// what types are supported
- primaryKeySentence = PRIMARY_KEY_TEXT;
+ primaryKeySentence = primaryKeySentence.replace("INTEGER PRIMARY KEY", "TEXT PRIMARY KEY");
} else if (PersistenceConfig.getDatabase(dbName).isAutoincrement(clazz)) {
primaryKeySentence += " AUTOINCREMENT";
}
@@ -88,10 +87,10 @@ static String getCreateTableSentence(String dbName, Class clazz) {
Collections.sort(fieldSentences, new Comparator<String>() {
@Override
public int compare(String s1, String s2) {
- if (s1.contains(PRIMARY_KEY)) {
+ if (s1.contains(String.format(PRIMARY_KEY, ""))) {
return -1;
}
- if (s2.contains(PRIMARY_KEY)) {
+ if (s2.contains(String.format(PRIMARY_KEY, ""))) {
return 1;
}
return 0;
@@ -182,7 +181,7 @@ private static String getFieldSentence(String name, Class<?> type, boolean notNu
conditions.add(String.format("%s LIKE '%s'", columnName,
String.valueOf(value).replace("'", "''")));
} else if (field.getType() == Boolean.class || field.getType() == boolean.class) {
- int intValue = ((Boolean) value).booleanValue() ? 1 : 0;
+ int intValue = (Boolean) value ? 1 : 0;
conditions.add(String.format("%s = '%d'", columnName, intValue));
} else {
conditions.add(String.format("%s = '%s'", columnName, value));
@@ -194,7 +193,7 @@ private static String getFieldSentence(String name, Class<?> type, boolean notNu
conditions.add(String.format("%s = ?", columnName));
}
if (field.getType() == Boolean.class || field.getType() == boolean.class) {
- value = ((Boolean) value).booleanValue() ? 1 : 0;
+ value = (Boolean) value ? 1 : 0;
}
args.add(String.valueOf(value));
}
@@ -243,7 +242,7 @@ private static String getFieldSentence(String name, Class<?> type, boolean notNu
boolean isBoolean = field.getType() == Boolean.class || field.getType() == boolean.class;
if (isBoolean || hasData(type, value)) {
if (isBoolean) {
- int intValue = ((Boolean) value).booleanValue() ? 1 : 0;
+ int intValue = (Boolean) value ? 1 : 0;
sets.add(String.format("%s = '%d'", getColumnName(field), intValue));
} else if (field.getType() == byte[].class || field.getType() == Byte[].class) {
String hex = getHex((byte[]) value);
@@ -325,8 +324,8 @@ static String getColumnName(Field field) {
if (COLUMN_NAMES_CACHE.containsKey(field)) {
return COLUMN_NAMES_CACHE.get(field);
}
- if (isPrimaryKey(field)) {
- return _ID;
+ if (isPrimaryKey(field) && !forcedName(field)) {
+ return getIdColumn(field);
}
Column column = field.getAnnotation(Column.class);
if (column != null) {
@@ -376,7 +375,7 @@ static String getColumnName(Field field) {
// build insert statement for the main object
if (values.size() == 0 && persistence.isAutoincrement(bean.getClass())) {
String hack = String.format("(SELECT seq FROM sqlite_sequence WHERE name = '%s')+1", getTableName(bean));
- return String.format("INSERT OR IGNORE INTO %s (%s) VALUES (%s);%s", getTableName(bean), _ID, hack, STATEMENT_SEPARATOR);
+ return String.format("INSERT OR IGNORE INTO %s (%s) VALUES (%s);%s", getTableName(bean), getIdColumn(getPrimaryKeyField(bean.getClass())), hack, STATEMENT_SEPARATOR);
}
return String.format("INSERT OR IGNORE INTO %s (%s) VALUES (%s);%s", getTableName(bean), columnsSet, join(values, ", "), STATEMENT_SEPARATOR);
}
@@ -389,7 +388,7 @@ static String getColumnName(Field field) {
Field[] fields = getDeclaredFields(theClass);
for (Field field : fields) {
// if the class has an autoincrement, ignore the ID
- if (persistence.isAutoincrement(theClass) && isPrimaryKey(field)) {
+ if (isPrimaryKey(field) && persistence.isAutoincrement(theClass)) {
continue;
}
try {
@@ -406,7 +405,7 @@ static String getColumnName(Field field) {
continue;
}
if (field.getType() == Boolean.class || field.getType() == boolean.class) {
- int intValue = ((Boolean) value).booleanValue() ? 1 : 0;
+ int intValue = (Boolean) value ? 1 : 0;
values.add(String.valueOf(intValue));
} else if (field.getType() == Byte[].class || field.getType() == byte[].class) {
if (value == null) {
@@ -417,7 +416,10 @@ static String getColumnName(Field field) {
}
} else if (value == null) {
Column columnAnnotation = field.getAnnotation(Column.class);
- boolean hasDefault = !columnAnnotation.defaultValue().equals(Column.NULL);
+ boolean hasDefault = false;
+ if (columnAnnotation != null) {
+ hasDefault = !columnAnnotation.defaultValue().equals(Column.NULL);
+ }
if (columnAnnotation != null && columnAnnotation.notNull() && !hasDefault) {
String msg = String.format("Field %s from class %s cannot be null. It was marked with the @Column not null annotation and it has not a default value", field.getName(), theClass.getSimpleName());
throw new IllegalStateException(msg);
@@ -462,7 +464,11 @@ private static boolean isPrimaryKey(Field field) {
if (field.isAnnotationPresent(PrimaryKey.class)) {
return true;
}
- return field.getName().equals(ID) || field.getName().equals(_ID);
+ return field.getName().equals(ID) || field.getName().equals(getIdColumn(field));
+ }
+
+ private static boolean forcedName(Field field) {
+ return field.isAnnotationPresent(Column.class) && field.getAnnotation(Column.class).forceName();
}
static String getTableName(Class<?> theClass) {
@@ -537,6 +543,17 @@ public static Field getPrimaryKeyField(Class<?> theClass) {
throw new IllegalStateException("Class " + theClass + " does not have a primary key");
}
+ static String getIdColumn(Field field) {
+ if (forcedName(field)) {
+ return getColumnName(field);
+ }
+ return _ID;
+ }
+
+ static String getCreatePrimaryKey(Field field) {
+ return String.format(PRIMARY_KEY, getIdColumn(field));
+ }
+
static <T, G> Cursor getCursorFindAllWhere(SQLiteDatabase db, String dbName, Class<? extends T> clazz, T sample, G attachedTo, Constraint constraint) {
String[] selectionArgs = null;
String where = null;
@@ -554,9 +571,47 @@ public static Field getPrimaryKeyField(Class<?> theClass) {
String groupBy = null;
if (constraint != null) {
orderBy = constraint.getOrderBy();
- limit = String.valueOf(constraint.getLimit());
+ if (constraint.getLimit() != null) {
+ limit = constraint.getLimit().toString();
+ }
groupBy = constraint.getGroupBy();
}
return db.query(getTableName(clazz), null, where, selectionArgs, groupBy, null, orderBy, limit);
}
+
+ static <T> String getFastInsertSqlHeader(T bean, SqlPersistence persistence) {
+ ArrayList<String> values = new ArrayList<String>();
+ ArrayList<String> columns = new ArrayList<String>();
+ populateColumnsAndValues(bean, null, values, columns, persistence);
+
+ StringBuilder result = new StringBuilder();
+
+ result.append("INSERT OR IGNORE INTO ").append(getTableName(bean.getClass())).append(" ");
+ // set insert columns
+ result.append("(");
+ result.append(join(columns, ", "));
+ result.append(")");
+ // add first insertion body
+ result.append(" SELECT ");
+
+ ArrayList<String> columnsAndValues = new ArrayList<String>();
+ for (int i = 0, valuesSize = values.size(); i < valuesSize; i++) {
+ String column = columns.get(i);
+ String value = values.get(i);
+ StringBuilder columnAndValue = new StringBuilder();
+ columnAndValue.append(value).append(" AS ").append(column);
+ columnsAndValues.add(columnAndValue.toString());
+ }
+ result.append(join(columnsAndValues, ", "));
+ return result.toString();
+ }
+
+ static <T> String getUnionInsertSql(T bean, SqlPersistence persistence) {
+ ArrayList<String> values = new ArrayList<String>();
+ populateColumnsAndValues(bean, null, values, null, persistence);
+ StringBuilder builder = new StringBuilder();
+ builder.append(" UNION SELECT ");
+ builder.append(join(values, ", "));
+ return builder.toString();
+ }
}
Oops, something went wrong.

0 comments on commit 2c80804

Please sign in to comment.