Skip to content
This repository has been archived by the owner on Feb 8, 2022. It is now read-only.

Commit

Permalink
Merge pull request #12 from florent37/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
florent37 committed Feb 8, 2016
2 parents 62d74db + 93bcb27 commit 386f43e
Show file tree
Hide file tree
Showing 12 changed files with 151 additions and 32 deletions.
39 changes: 33 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ buildscript {
apply plugin: 'com.neenbedankt.android-apt'

dependencies {
compile 'fr.xebia.android.freezer:freezer:1.0.3'
provided 'fr.xebia.android.freezer:freezer-annotations:1.0.3'
apt 'fr.xebia.android.freezer:freezer-compiler:1.0.3'
compile 'fr.xebia.android.freezer:freezer:1.0.4'
provided 'fr.xebia.android.freezer:freezer-annotations:1.0.4'
apt 'fr.xebia.android.freezer:freezer-compiler:1.0.4'
}
```

Expand Down Expand Up @@ -47,6 +47,7 @@ public class Dog {
```java
@Model
public class Cat {
@Id long id;
String shortName;
}
```
Expand Down Expand Up @@ -162,6 +163,31 @@ public class MyEntity {
}
```

#Id

You can optionnaly set a field as an identifier:

```java
@Model
public class MyEntity {
@Id long id;
}
```
The identifier must be a `long

#Ignore

You can ignore a field:

```java
@Model
public class MyEntity {
@Ignore
int field;
}
```


#Logging

You can log all SQL queries from entities managers:
Expand Down Expand Up @@ -225,11 +251,8 @@ Migration isn't yet capable of:
- Update an entry
- Improve migration
- Add some selectors operations (like, ...)
- Add Observable support
- Provide an Asynchronous API
- Add `@Ignore` annotation
- Add unit tests
#Changelog
Expand All @@ -250,6 +273,10 @@ Introduced Migration Engine.
- Added unit tests
- Fixed one to many
##1.0.4
- Added @Id & @Ignore
#A project initiated by Xebia
This project was first developed by Xebia and has been open-sourced since. We will continue working on it.
Expand Down
11 changes: 10 additions & 1 deletion app/src/main/java/com/github/florent37/orm/model/Cat.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.github.florent37.orm.model;

import fr.xebia.android.freezer.annotations.Id;
import fr.xebia.android.freezer.annotations.Ignore;
import fr.xebia.android.freezer.annotations.Model;

Expand All @@ -12,11 +13,15 @@
@Model
public class Cat {

@Id
long id;

String shortName;
Date date;

String notIgnored;
@Ignore String ignoreThis;
@Ignore
String ignoreThis;

public Cat() {
}
Expand All @@ -37,4 +42,8 @@ public String getShortName() {
public Date getDate() {
return date;
}

public long getId() {
return id;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public void shouldAddCatWithDate(){

//then
assertThat(catEntityManager.count()).isEqualTo(1);
assertThat(cat.getId()).isNotEqualTo(0);
}

@Test
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ ext{
sourceCompatibilityVersion = JavaVersion.VERSION_1_7
targetCompatibilityVersion = JavaVersion.VERSION_1_7

libraryVersion="1.0.3"
libraryVersion="1.0.4"
}

allprojects {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package fr.xebia.android.freezer.annotations;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
* Created by florentchampigny on 07/01/2016.
*/
@Retention(RetentionPolicy.SOURCE)
@Target(ElementType.FIELD)
public @interface Id {
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import javax.lang.model.element.VariableElement;
import javax.lang.model.util.ElementFilter;

import fr.xebia.android.freezer.annotations.Id;
import fr.xebia.android.freezer.annotations.Ignore;

/**
Expand Down Expand Up @@ -61,6 +62,17 @@ public static List<VariableElement> getNonPrimitiveClassFields(Element element)
return filterIgnore(nonPrimitive);
}

public static boolean hasIdField(Element element){
return getIdField(element) != null;
}

public static Element getIdField(Element element){
for (VariableElement e : getFields(element)) {
if (isIdField(e)) return e;
}
return null;
}

public static String getFieldType(VariableElement variableElement) {
TypeName typeName = getFieldClass(variableElement);
if (typeName == TypeName.INT || typeName == TypeName.BOOLEAN || typeName == TypeName.BYTE)
Expand All @@ -83,7 +95,7 @@ public static String getFieldCast(VariableElement variableElement) {
return "%s";
}

public static String getFieldTableType(VariableElement variableElement) {
public static String getFieldTableType(Element variableElement) {
TypeName typeName = getFieldClass(variableElement);
if (typeName == TypeName.INT || typeName == TypeName.BOOLEAN || typeName == TypeName.LONG || typeName == TypeName.BYTE)
return "integer";
Expand All @@ -98,6 +110,10 @@ public static String getObjectName(Element element) {
return element.getSimpleName().toString();
}

public static boolean isIdField(Element element) {
return element.getAnnotation(Id.class) != null && TypeName.LONG.equals(TypeName.get(element.asType()));
}

public static String getObjectPackage(Element element) {
return element.getEnclosingElement().toString();
}
Expand Down Expand Up @@ -401,4 +417,5 @@ public static boolean isDate(Element element) {
public static boolean isDate(TypeName typeName) {
return Constants.dateClassName.equals(typeName);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,11 @@
*/
@SupportedSourceVersion(SourceVersion.RELEASE_7)
@SupportedAnnotationTypes(
{"fr.xebia.android.freezer.annotations.Model", "fr.xebia.android.freezer.annotations.Migration"})
{
"fr.xebia.android.freezer.annotations.Model",
"fr.xebia.android.freezer.annotations.Migration",
"fr.xebia.android.freezer.annotations.Ignore"
})
@AutoService(javax.annotation.processing.Processor.class)
public class Processor extends AbstractProcessor {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public TypeSpec generate() {
.addStatement("if(date$L != null) object.$L = new $T($S).parse(date$L)",
i, variableElement.getSimpleName(), Constants.simpleDateFormatClassName, Constants.DATE_FORMAT, i)
.addCode("} catch ($T e) { e.printStackTrace(); }", TypeName.get(Exception.class));
} else {
} else if (!ProcessUtils.isIdField(variableElement)){
cursor = String.format(ProcessUtils.getFieldCast(variableElement), cursor);

fromCursorB.addStatement("object.$L = " + cursor, variableElement.getSimpleName(), ProcessUtils.getFieldType(variableElement), variableElement.getSimpleName());
Expand Down Expand Up @@ -110,7 +110,7 @@ public TypeSpec generate() {
if (ProcessUtils.isPrimitive(variableElement)) {
if (ProcessUtils.isDate(variableElement)) {
getValuesB.addStatement("if(object.$L != null) values.put($S, new $T($S).format(object.$L))", variableElement.getSimpleName(), variableElement.getSimpleName(), Constants.simpleDateFormatClassName, Constants.DATE_FORMAT, variableElement.getSimpleName());
} else {
} else if(!ProcessUtils.isIdField(variableElement)) {
String statement = "values.put($S,object.$L)";
if (ProcessUtils.isModelId(variableElement))
statement = "if(" + ProcessUtils.getCursorHelperName("object") + " != 0) " + statement;
Expand Down Expand Up @@ -177,6 +177,10 @@ protected List<MethodSpec> generateInsertMethods() {
.addParameter(modelType, "object")
.addStatement("long objectId = database.insert($S, null, getValues(object,null))", ProcessUtils.getTableName(objectName));

Element idField = ProcessUtils.getIdField(element);
if(idField != null)
insertB.addStatement("object.$L = objectId",ProcessUtils.getObjectName(idField));

for (VariableElement variableElement : otherClassFields) {
insertB.addStatement("$T.insertFor$L(database,object.$L, objectId , $S)", ProcessUtils.getFieldCursorHelperClass(variableElement), objectName, ProcessUtils.getObjectName(variableElement), ProcessUtils.getObjectName(variableElement));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@
import com.squareup.javapoet.MethodSpec;
import com.squareup.javapoet.TypeName;
import com.squareup.javapoet.TypeSpec;
import fr.xebia.android.freezer.Constants;
import fr.xebia.android.freezer.ProcessUtils;

import java.util.List;

import javax.lang.model.element.Element;
import javax.lang.model.element.Modifier;
import javax.lang.model.element.VariableElement;

import fr.xebia.android.freezer.Constants;
import fr.xebia.android.freezer.ProcessUtils;

/**
* Created by florentchampigny on 22/01/2016.
*/
Expand Down Expand Up @@ -40,7 +41,10 @@ public TypeSpec generate() {
.build());

for (VariableElement variableElement : fields) {
enumBuilder.addEnumConstant(ProcessUtils.getObjectName(variableElement), TypeSpec.anonymousClassBuilder("$S", ProcessUtils.getObjectName(variableElement))
String fieldSqlName = ProcessUtils.getObjectName(variableElement);
if (ProcessUtils.isIdField(variableElement))
fieldSqlName = Constants.FIELD_ID;
enumBuilder.addEnumConstant(ProcessUtils.getObjectName(variableElement), TypeSpec.anonymousClassBuilder("$S", fieldSqlName)
.build());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@
import com.squareup.javapoet.MethodSpec;
import com.squareup.javapoet.TypeName;
import com.squareup.javapoet.TypeSpec;
import fr.xebia.android.freezer.Constants;
import fr.xebia.android.freezer.ProcessUtils;

import javax.lang.model.element.Element;
import javax.lang.model.element.Modifier;

import fr.xebia.android.freezer.Constants;
import fr.xebia.android.freezer.ProcessUtils;

/**
* Created by florentchampigny on 26/01/2016.
*/
Expand Down Expand Up @@ -37,23 +38,33 @@ public static TypeSpec generateModelProxyInterface() {
}

public TypeSpec generate() {
return TypeSpec.classBuilder(ProcessUtils.getObjectName(element) + Constants.MODEL_ENTITY_PROXY)
String idFieldName = Constants.FIELD_ID;
Element idField = ProcessUtils.getIdField(element);
if (idField != null)
idFieldName = ProcessUtils.getObjectName(idField);

TypeSpec.Builder builder = TypeSpec.classBuilder(ProcessUtils.getObjectName(element) + Constants.MODEL_ENTITY_PROXY)
.addModifiers(Modifier.PUBLIC)
.superclass(TypeName.get(element.asType()))
.addSuperinterface(ClassName.get(Constants.DAO_PACKAGE, Constants.MODEL_ENTITY_PROXY_INTERFACE))
.addField(TypeName.LONG, Constants.FIELD_ID)
.addMethod(MethodSpec.methodBuilder(Constants.MODEL_ENTITY_PROXY_GET_ID_METHOD)
.addModifiers(Modifier.PUBLIC)
.returns(TypeName.LONG)
.addStatement("return $L", Constants.FIELD_ID)
.build())
.addSuperinterface(ClassName.get(Constants.DAO_PACKAGE, Constants.MODEL_ENTITY_PROXY_INTERFACE));

if (idField == null) {
builder.addField(TypeName.LONG, Constants.FIELD_ID);
}

builder.addMethod(MethodSpec.methodBuilder(Constants.MODEL_ENTITY_PROXY_GET_ID_METHOD)
.addModifiers(Modifier.PUBLIC)
.returns(TypeName.LONG)
.addStatement("return $L", idFieldName)
.build())
.addMethod(MethodSpec.methodBuilder(Constants.MODEL_ENTITY_PROXY_SET_ID_METHOD)
.addModifiers(Modifier.PUBLIC)
.returns(TypeName.VOID)
.addParameter(TypeName.LONG, "id")
.addStatement("this.$L = id", Constants.FIELD_ID)
.build())
.build();
.addStatement("this.$L = id", idFieldName)
.build());

return builder.build();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public class ModelORMGenerator {
TypeSpec queryBuilder;
TypeSpec dao;

Element fieldId;
List<VariableElement> fields;
List<VariableElement> otherClassFields;
List<VariableElement> collections;
Expand All @@ -58,6 +59,8 @@ public ModelORMGenerator(Element element) {
this.fields = ProcessUtils.getPrimitiveFields(element);
this.otherClassFields = ProcessUtils.getNonPrimitiveClassFields(element);
this.collections = ProcessUtils.getCollectionsOfPrimitiveFields(element);

this.fieldId = ProcessUtils.getIdField(element);
}

public TypeSpec getDao() {
Expand Down Expand Up @@ -369,7 +372,6 @@ protected List<MethodSpec> generateQueryMethods() {
else
selector = ParameterizedTypeName.get(className, queryBuilderClassName);


methodSpecs.add(MethodSpec.methodBuilder(variableElement.getSimpleName().toString())
.returns(selector)
.addModifiers(Modifier.PUBLIC)
Expand Down Expand Up @@ -444,14 +446,21 @@ protected String generateCreationString() {

protected String generateTableCreate() {
StringBuilder stringBuilder = new StringBuilder();
for (int i = 0; i < fields.size(); ++i) {
VariableElement variableElement = fields.get(i);

//filter / remove fieldId
List<Element> elements = new ArrayList<>();
for (VariableElement variableElement : fields)
if (variableElement != fieldId)
elements.add(variableElement);

for (int i = 0; i < elements.size(); ++i) {
Element variableElement = elements.get(i);
if (!Constants.FIELD_ID.equals(variableElement.getSimpleName().toString())) {
stringBuilder
.append(variableElement.getSimpleName())
.append(" ")
.append(ProcessUtils.getFieldTableType(variableElement));
if (i < fields.size() - 1)
if (i < elements.size() - 1)
stringBuilder.append(", ");
}
}
Expand Down
Loading

0 comments on commit 386f43e

Please sign in to comment.