diff --git a/README.md b/README.md index eca9856..cc23589 100644 --- a/README.md +++ b/README.md @@ -17,9 +17,24 @@ buildscript { apply plugin: 'com.neenbedankt.android-apt' dependencies { - compile 'fr.xebia.android.freezer:freezer:1.0.6' - provided 'fr.xebia.android.freezer:freezer-annotations:1.0.6' - apt 'fr.xebia.android.freezer:freezer-compiler:1.0.6' + compile 'fr.xebia.android.freezer:freezer:2.0.1' + provided 'fr.xebia.android.freezer:freezer-annotations:2.0.1' + apt 'fr.xebia.android.freezer:freezer-compiler:2.0.1' +} +``` + +#It's always better with a context + +Don't forget to initialise Freezer in your application: + +```java +public class MyApplication extends Application { + + @Override public void onCreate() { + super.onCreate(); + Freezer.onCreate(this); + } + } ``` @@ -127,6 +142,16 @@ float ageMax = userEntityManager.select().max(UserColumns.age); int count = userEntityManager.select().count(); ``` +## Limit + +The `QueryBuilder` offers a limitation method, for example, getting 10 users, starting from the 5th: + +```java +ist someUsers = userEntityManager.select() + .limit(5, 10) //start, count + .asList(); +``` + #Asynchronous Freezer offers various asynchronous methods: @@ -246,21 +271,6 @@ You can log all SQL queries from entities managers: userEntityManager.logQueries((query, datas) -> Log.d(TAG, query) } ``` -#It's always better with a context - -Don't forget to initialise Freezer in your application: - -```java -public class MyApplication extends Application { - - @Override public void onCreate() { - super.onCreate(); - Freezer.onCreate(this); - } - -} -``` - #Migration To handle schema migration, just add `@Migration(newVersion)` in a static method, @@ -296,11 +306,6 @@ Migration isn't yet capable of: - adding/modifying One To One - adding/modifying One To Many - handling collections/arrays - -#TODO - -- Improve migration -- Add Observable support #Changelog @@ -329,12 +334,16 @@ Introduced Migration Engine. - Model update -##1.0.6 +##2.0.0 - Async API - Support Observables - Added @DatabaseName +##2.0.1 + +- Limit + #A project initiated by Xebia This project was first developed by Xebia and has been open-sourced since. We will continue working on it. diff --git a/app/src/test/java/com/github/florent37/dao/UserQueryBuilderTest.java b/app/src/test/java/com/github/florent37/dao/UserQueryBuilderTest.java index eee09b4..d32a0d4 100644 --- a/app/src/test/java/com/github/florent37/dao/UserQueryBuilderTest.java +++ b/app/src/test/java/com/github/florent37/dao/UserQueryBuilderTest.java @@ -509,4 +509,35 @@ public void onError(List data) { assertThat(numberOfUsers.get()).isEqualTo(3); } + @Test + public void testSelectUsers_limit() { + //given + List users = Arrays.asList( + new User(21, "a", null, null, true), + new User(21, "b", null, null, true), + new User(21, "c", null, null, true), + new User(21, "d", null, null, true), + new User(21, "e", null, null, true),//4 + new User(21, "f", null, null, true), + new User(21, "g", null, null, true), + new User(21, "h", null, null, true), + new User(21, "i", null, null, true), + new User(21, "j", null, null, true), + new User(21, "k", null, null, true), + new User(21, "l", null, null, true), + new User(21, "m", null, null, true) + ); + userEntityManager.add(users); + + //when + List usersFromBase = userEntityManager + .select() + .limit(4,5) + .asList(); + + //then + assertThat(usersFromBase.size()).isEqualTo(5); + assertThat(usersFromBase.get(0).getName()).isEqualTo("e"); + } + } diff --git a/build.gradle b/build.gradle index bb2cfb4..74818f4 100644 --- a/build.gradle +++ b/build.gradle @@ -6,7 +6,7 @@ buildscript { mavenCentral() } dependencies { - classpath 'com.android.tools.build:gradle:1.5.0' + classpath 'com.android.tools.build:gradle:2.0.0' classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8' classpath 'com.github.dcendents:android-maven-gradle-plugin:1.3' classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.0' @@ -23,7 +23,7 @@ ext{ sourceCompatibilityVersion = JavaVersion.VERSION_1_7 targetCompatibilityVersion = JavaVersion.VERSION_1_7 - libraryVersion="1.0.6" + libraryVersion="2.0.1" } allprojects { @@ -34,8 +34,4 @@ allprojects { url "http://dl.bintray.com/florent37/maven" } } -} - -task clean(type: Delete) { - delete rootProject.buildDir -} +} \ No newline at end of file diff --git a/freezer-compiler/src/main/java/fr/xebia/android/freezer/generator/ModelORMGenerator.java b/freezer-compiler/src/main/java/fr/xebia/android/freezer/generator/ModelORMGenerator.java index d7f9c46..76bbe4a 100644 --- a/freezer-compiler/src/main/java/fr/xebia/android/freezer/generator/ModelORMGenerator.java +++ b/freezer-compiler/src/main/java/fr/xebia/android/freezer/generator/ModelORMGenerator.java @@ -83,553 +83,562 @@ public ModelORMGenerator generate() { TypeName listObjectsClassName = ProcessUtils.listOf(modelClassName); this.queryBuilder = TypeSpec.classBuilder(ProcessUtils.getQueryBuilderName(modelName)) //UserDAOQueryBuilder + .addModifiers(Modifier.PUBLIC) + + .superclass(Constants.queryBuilderClassName) + + .addField(ProcessUtils.listOf(enumColums), "fields") + + .addMethod(MethodSpec.constructorBuilder() + .addModifiers(Modifier.PUBLIC) + .addStatement("super()") + .build()) + + .addMethod(MethodSpec.constructorBuilder() + .addModifiers(Modifier.PUBLIC) + .addParameter(TypeName.BOOLEAN, "named") + .addParameter(ClassName.get(Constants.DAO_PACKAGE, Constants.QUERY_LOGGER), "logger") + .addStatement("this()") + .addStatement("this.named = named") + .addStatement("this.logger = logger") + .build()) + + .addMethods(generateQueryMethods()) + + .addMethod(MethodSpec.methodBuilder("or") + .returns(queryBuilderClassName) + .addModifiers(Modifier.PUBLIC) + .addStatement("super.appendOr()") + .addStatement("return this") + .build()) + + .addMethod(MethodSpec.methodBuilder("and") + .returns(queryBuilderClassName) + .addModifiers(Modifier.PUBLIC) + .addStatement("super.appendAnd()") + .addStatement("return this") + .build()) + + .addMethod(MethodSpec.methodBuilder("beginGroup") + .returns(queryBuilderClassName) + .addModifiers(Modifier.PUBLIC) + .addStatement("super.appendBeginGroup()") + .addStatement("return this") + .build()) + + .addMethod(MethodSpec.methodBuilder("endGroup") + .returns(queryBuilderClassName) + .addModifiers(Modifier.PUBLIC) + .addStatement("super.appendEndGroup()") + .addStatement("return this") + .build()) + + .addMethod(MethodSpec.methodBuilder("limit") + .returns(queryBuilderClassName) + .addModifiers(Modifier.PUBLIC) + .addParameter(TypeName.INT, "start") + .addParameter(TypeName.INT, "count") + .addStatement("super.limitStartNumber(start, count)") + .addStatement("return this") + .build()) + + .addMethod(MethodSpec.methodBuilder("asList") + .returns(listObjectsClassName) + .addModifiers(Modifier.PUBLIC) + .addStatement("return execute()") + .build()) + + .addMethod(MethodSpec.methodBuilder("asObservable") + .returns(ParameterizedTypeName.get(ClassName.get(Observable.class), listObjectsClassName)) + .addModifiers(Modifier.PUBLIC) + .addCode("return $T.create(new Observable.OnSubscribe<$T>(){\n", ClassName.get(Observable.class), listObjectsClassName) + .addCode("@$T\n", ClassName.get(Override.class)) + .addCode("public void call($T subscriber) {\n", ClassName.get(Subscriber.class), listObjectsClassName) + .addStatement("subscriber.onNext(asList())") + .addStatement("subscriber.onCompleted()") + .addCode("}\n") + .addCode("});\n") + .build()) + + .addMethod(MethodSpec.methodBuilder("async") + .addParameter(ParameterizedTypeName.get(Constants.callback, listObjectsClassName), "callback") + .addModifiers(Modifier.PUBLIC) + .addStatement("final $T<$T<$T>> weakReference = new $T<>(callback)", ClassName.get(WeakReference.class), Constants.callback, listObjectsClassName, ClassName.get(WeakReference.class)) + .addStatement("final $T current = new Handler($T.myLooper())", ClassName.get("android.os", "Handler"), ClassName.get("android.os", "Looper")) + .addCode("new Handler().post(new $T() {\n", ClassName.get(Runnable.class)) + .addCode("@$T public void run() {\n", ClassName.get(Override.class)) + .addStatement("final $T objects = asList()", listObjectsClassName) + .addCode("current.post(new Runnable() {\n") + .addCode("@Override public void run() {\n") + .addStatement("$T callback1 = weakReference.get()", ParameterizedTypeName.get(Constants.callback, listObjectsClassName)) + .addStatement("if(callback1 != null) callback1.onSuccess(objects)") + .addCode("}\n") + .addCode("});\n") + .addCode("}\n") + .addCode("});\n") + .build()) + + .addMethod(MethodSpec.methodBuilder("first") + .returns(modelClassName) + .addModifiers(Modifier.PUBLIC) + .addStatement("$T objects = asList()", listObjectsClassName) + .addStatement("if(objects.isEmpty()) return null") + .addStatement("else return objects.get(0)") + .build()) + + .addMethod(MethodSpec.methodBuilder("sortAsc") + .returns(queryBuilderClassName) + .addModifiers(Modifier.PUBLIC) + .addParameter(enumColums, "column") + .addStatement("super.appendSortAsc($S,column.getName())", " " + TABLE_NAME + " .") + .addStatement("return this") + .build()) + + .addMethod(MethodSpec.methodBuilder("sortDesc") + .returns(queryBuilderClassName) .addModifiers(Modifier.PUBLIC) + .addParameter(enumColums, "column") + .addStatement("super.appendSortDesc($S,column.getName())", " " + TABLE_NAME + " .") + .addStatement("return this") + .build()) + + .addMethod(MethodSpec.methodBuilder("sum") + .returns(TypeName.FLOAT) + .addParameter(enumColums, "column") + .addModifiers(Modifier.PUBLIC) + .addStatement("$T db = $T.getInstance().open().getDatabase()", Constants.databaseClassName, Constants.daoClassName) + .addStatement("$T cursor = db.rawQuery($S + column.getName() + $S + constructQuery(), constructArgs())", Constants.cursorClassName, String.format("select sum(%s.", TABLE_NAME), String.format(") from %s ", TABLE_NAME)) + .addStatement("cursor.moveToNext()") + .addStatement("float value = cursor.getFloat(0)") + + .addStatement("cursor.close()") + .addStatement("$T.getInstance().close()", Constants.daoClassName) + + .addStatement("return value") + .build()) + + .addMethod(MethodSpec.methodBuilder("min") + .returns(TypeName.FLOAT) + .addParameter(enumColums, "column") + .addModifiers(Modifier.PUBLIC) + .addStatement("$T db = $T.getInstance().open().getDatabase()", Constants.databaseClassName, Constants.daoClassName) + .addStatement("$T cursor = db.rawQuery($S + column.getName() + $S + constructQuery(), constructArgs())", Constants.cursorClassName, String.format("select min(%s.", TABLE_NAME), String.format(") from %s ", TABLE_NAME)) + .addStatement("cursor.moveToNext()") + .addStatement("float value = cursor.getFloat(0)") + + .addStatement("cursor.close()") + .addStatement("$T.getInstance().close()", Constants.daoClassName) + + .addStatement("return value") + .build()) + + .addMethod(MethodSpec.methodBuilder("max") + .returns(TypeName.FLOAT) + .addParameter(enumColums, "column") + .addModifiers(Modifier.PUBLIC) + .addStatement("$T db = $T.getInstance().open().getDatabase()", Constants.databaseClassName, Constants.daoClassName) + .addStatement("$T cursor = db.rawQuery($S + column.getName() + $S + constructQuery(), constructArgs())", Constants.cursorClassName, String.format("select max(%s.", TABLE_NAME), String.format(") from %s ", TABLE_NAME)) + .addStatement("cursor.moveToNext()") + .addStatement("float value = cursor.getFloat(0)") + + .addStatement("cursor.close()") + .addStatement("$T.getInstance().close()", Constants.daoClassName) + + .addStatement("return value") + .build()) + + .addMethod(MethodSpec.methodBuilder("average") + .returns(TypeName.FLOAT) + .addParameter(enumColums, "column") + .addModifiers(Modifier.PUBLIC) + .addStatement("$T db = $T.getInstance().open().getDatabase()", Constants.databaseClassName, Constants.daoClassName) + .addStatement("$T cursor = db.rawQuery($S + column.getName() + $S + constructQuery(), constructArgs())", Constants.cursorClassName, String.format("select avg(%s.", TABLE_NAME), String.format(") from %s ", TABLE_NAME)) + .addStatement("cursor.moveToNext()") + .addStatement("float value = cursor.getFloat(0)") + + .addStatement("cursor.close()") + .addStatement("$T.getInstance().close()", Constants.daoClassName) + .addStatement("return value") + + .build()) + + .addMethod(MethodSpec.methodBuilder("count") + .returns(TypeName.INT) + .addModifiers(Modifier.PUBLIC) + .addStatement("$T db = $T.getInstance().open().getDatabase()", Constants.databaseClassName, Constants.daoClassName) + .addStatement("$T cursor = db.rawQuery($S + constructQuery(), constructArgs())", Constants.cursorClassName, String.format("select count(distinct(%s.%s)) from %s ", TABLE_NAME, Constants.FIELD_ID, TABLE_NAME)) + .addStatement("cursor.moveToNext()") + .addStatement("int value = cursor.getInt(0)") + + .addStatement("cursor.close()") + .addStatement("$T.getInstance().close()", Constants.daoClassName) + .addStatement("return value") + + .build()) + + .addMethod(MethodSpec.methodBuilder("fields") + .returns(queryBuilderClassName) + .addModifiers(Modifier.PUBLIC) + .addParameter(ArrayTypeName.of(enumColums), "fields") + .varargs() + .addStatement("this.fields = new $T<>()", TypeName.get(ArrayList.class)) + .addStatement("this.fields.addAll($T.asList(fields))", TypeName.get(Arrays.class)) + .addStatement("return this") + + .build()) + + .addMethod(MethodSpec.methodBuilder("fieldsWithout") + .returns(queryBuilderClassName) + .addModifiers(Modifier.PUBLIC) + .addParameter(ArrayTypeName.of(enumColums), "fields") + .varargs() + .addStatement("this.fields = new $T<>($T.asList($L.values()))", TypeName.get(ArrayList.class), TypeName.get(Arrays.class), enumColums) + .addStatement("this.fields.removeAll($T.asList(fields))", TypeName.get(Arrays.class)) + .addStatement("return this") + + .build()) - .superclass(Constants.queryBuilderClassName) - - .addField(ProcessUtils.listOf(enumColums), "fields") - - .addMethod(MethodSpec.constructorBuilder() - .addModifiers(Modifier.PUBLIC) - .addStatement("super()") - .build()) - - .addMethod(MethodSpec.constructorBuilder() - .addModifiers(Modifier.PUBLIC) - .addParameter(TypeName.BOOLEAN, "named") - .addParameter(ClassName.get(Constants.DAO_PACKAGE, Constants.QUERY_LOGGER), "logger") - .addStatement("this()") - .addStatement("this.named = named") - .addStatement("this.logger = logger") - .build()) - - .addMethods(generateQueryMethods()) - - .addMethod(MethodSpec.methodBuilder("or") - .returns(queryBuilderClassName) - .addModifiers(Modifier.PUBLIC) - .addStatement("super.appendOr()") - .addStatement("return this") - .build()) - - .addMethod(MethodSpec.methodBuilder("and") - .returns(queryBuilderClassName) - .addModifiers(Modifier.PUBLIC) - .addStatement("super.appendAnd()") - .addStatement("return this") - .build()) - - .addMethod(MethodSpec.methodBuilder("beginGroup") - .returns(queryBuilderClassName) - .addModifiers(Modifier.PUBLIC) - .addStatement("super.appendBeginGroup()") - .addStatement("return this") - .build()) - - .addMethod(MethodSpec.methodBuilder("endGroup") - .returns(queryBuilderClassName) - .addModifiers(Modifier.PUBLIC) - .addStatement("super.appendEndGroup()") - .addStatement("return this") - .build()) - - .addMethod(MethodSpec.methodBuilder("asList") - .returns(listObjectsClassName) - .addModifiers(Modifier.PUBLIC) - .addStatement("return execute()") - .build()) - - .addMethod(MethodSpec.methodBuilder("asObservable") - .returns(ParameterizedTypeName.get(ClassName.get(Observable.class), listObjectsClassName)) - .addModifiers(Modifier.PUBLIC) - .addCode("return $T.create(new Observable.OnSubscribe<$T>(){\n", ClassName.get(Observable.class), listObjectsClassName) - .addCode("@$T\n", ClassName.get(Override.class)) - .addCode("public void call($T subscriber) {\n", ClassName.get(Subscriber.class), listObjectsClassName) - .addStatement("subscriber.onNext(asList())") - .addStatement("subscriber.onCompleted()") - .addCode("}\n") - .addCode("});\n") - .build()) - - .addMethod(MethodSpec.methodBuilder("async") - .addParameter(ParameterizedTypeName.get(Constants.callback, listObjectsClassName), "callback") - .addModifiers(Modifier.PUBLIC) - .addStatement("final $T<$T<$T>> weakReference = new $T<>(callback)", ClassName.get(WeakReference.class), Constants.callback, listObjectsClassName, ClassName.get(WeakReference.class)) - .addStatement("final $T current = new Handler($T.myLooper())", ClassName.get("android.os", "Handler"), ClassName.get("android.os", "Looper")) - .addCode("new Handler().post(new $T() {\n", ClassName.get(Runnable.class)) - .addCode("@$T public void run() {\n", ClassName.get(Override.class)) - .addStatement("final $T objects = asList()", listObjectsClassName) - .addCode("current.post(new Runnable() {\n") - .addCode("@Override public void run() {\n") - .addStatement("$T callback1 = weakReference.get()", ParameterizedTypeName.get(Constants.callback, listObjectsClassName)) - .addStatement("if(callback1 != null) callback1.onSuccess(objects)") - .addCode("}\n") - .addCode("});\n") - .addCode("}\n") - .addCode("});\n") - .build()) - - .addMethod(MethodSpec.methodBuilder("first") - .returns(modelClassName) - .addModifiers(Modifier.PUBLIC) - .addStatement("$T objects = asList()", listObjectsClassName) - .addStatement("if(objects.isEmpty()) return null") - .addStatement("else return objects.get(0)") - .build()) - - .addMethod(MethodSpec.methodBuilder("sortAsc") - .returns(queryBuilderClassName) - .addModifiers(Modifier.PUBLIC) - .addParameter(enumColums, "column") - .addStatement("super.appendSortAsc($S,column.getName())", " " + TABLE_NAME + " .") - .addStatement("return this") - .build()) - - .addMethod(MethodSpec.methodBuilder("sortDesc") - .returns(queryBuilderClassName) - .addModifiers(Modifier.PUBLIC) - .addParameter(enumColums, "column") - .addStatement("super.appendSortDesc($S,column.getName())", " " + TABLE_NAME + " .") - .addStatement("return this") - .build()) - - .addMethod(MethodSpec.methodBuilder("sum") - .returns(TypeName.FLOAT) - .addParameter(enumColums, "column") - .addModifiers(Modifier.PUBLIC) - .addStatement("$T db = $T.getInstance().open().getDatabase()", Constants.databaseClassName, Constants.daoClassName) - .addStatement("$T cursor = db.rawQuery($S + column.getName() + $S + constructQuery(), constructArgs())", Constants.cursorClassName, String.format("select sum(%s.", TABLE_NAME), String.format(") from %s ", TABLE_NAME)) - .addStatement("cursor.moveToNext()") - .addStatement("float value = cursor.getFloat(0)") - - .addStatement("cursor.close()") - .addStatement("$T.getInstance().close()", Constants.daoClassName) - - .addStatement("return value") - .build()) - - .addMethod(MethodSpec.methodBuilder("min") - .returns(TypeName.FLOAT) - .addParameter(enumColums, "column") - .addModifiers(Modifier.PUBLIC) - .addStatement("$T db = $T.getInstance().open().getDatabase()", Constants.databaseClassName, Constants.daoClassName) - .addStatement("$T cursor = db.rawQuery($S + column.getName() + $S + constructQuery(), constructArgs())", Constants.cursorClassName, String.format("select min(%s.", TABLE_NAME), String.format(") from %s ", TABLE_NAME)) - .addStatement("cursor.moveToNext()") - .addStatement("float value = cursor.getFloat(0)") - - .addStatement("cursor.close()") - .addStatement("$T.getInstance().close()", Constants.daoClassName) - - .addStatement("return value") - .build()) - - .addMethod(MethodSpec.methodBuilder("max") - .returns(TypeName.FLOAT) - .addParameter(enumColums, "column") - .addModifiers(Modifier.PUBLIC) - .addStatement("$T db = $T.getInstance().open().getDatabase()", Constants.databaseClassName, Constants.daoClassName) - .addStatement("$T cursor = db.rawQuery($S + column.getName() + $S + constructQuery(), constructArgs())", Constants.cursorClassName, String.format("select max(%s.", TABLE_NAME), String.format(") from %s ", TABLE_NAME)) - .addStatement("cursor.moveToNext()") - .addStatement("float value = cursor.getFloat(0)") - - .addStatement("cursor.close()") - .addStatement("$T.getInstance().close()", Constants.daoClassName) - - .addStatement("return value") - .build()) - - .addMethod(MethodSpec.methodBuilder("average") - .returns(TypeName.FLOAT) - .addParameter(enumColums, "column") - .addModifiers(Modifier.PUBLIC) - .addStatement("$T db = $T.getInstance().open().getDatabase()", Constants.databaseClassName, Constants.daoClassName) - .addStatement("$T cursor = db.rawQuery($S + column.getName() + $S + constructQuery(), constructArgs())", Constants.cursorClassName, String.format("select avg(%s.", TABLE_NAME), String.format(") from %s ", TABLE_NAME)) - .addStatement("cursor.moveToNext()") - .addStatement("float value = cursor.getFloat(0)") - - .addStatement("cursor.close()") - .addStatement("$T.getInstance().close()", Constants.daoClassName) - .addStatement("return value") - - .build()) - - .addMethod(MethodSpec.methodBuilder("count") - .returns(TypeName.INT) - .addModifiers(Modifier.PUBLIC) - .addStatement("$T db = $T.getInstance().open().getDatabase()", Constants.databaseClassName, Constants.daoClassName) - .addStatement("$T cursor = db.rawQuery($S + constructQuery(), constructArgs())", Constants.cursorClassName, String.format("select count(distinct(%s.%s)) from %s ", TABLE_NAME, Constants.FIELD_ID, TABLE_NAME)) - .addStatement("cursor.moveToNext()") - .addStatement("int value = cursor.getInt(0)") - - .addStatement("cursor.close()") - .addStatement("$T.getInstance().close()", Constants.daoClassName) - .addStatement("return value") - - .build()) - - .addMethod(MethodSpec.methodBuilder("fields") - .returns(queryBuilderClassName) - .addModifiers(Modifier.PUBLIC) - .addParameter(ArrayTypeName.of(enumColums), "fields") - .varargs() - .addStatement("this.fields = new $T<>()", TypeName.get(ArrayList.class)) - .addStatement("this.fields.addAll($T.asList(fields))", TypeName.get(Arrays.class)) - .addStatement("return this") - - .build()) - - .addMethod(MethodSpec.methodBuilder("fieldsWithout") - .returns(queryBuilderClassName) - .addModifiers(Modifier.PUBLIC) - .addParameter(ArrayTypeName.of(enumColums), "fields") - .varargs() - .addStatement("this.fields = new $T<>($T.asList($L.values()))", TypeName.get(ArrayList.class), TypeName.get(Arrays.class), enumColums) - .addStatement("this.fields.removeAll($T.asList(fields))", TypeName.get(Arrays.class)) - .addStatement("return this") - - .build()) - - .addMethod(MethodSpec.methodBuilder("field") - .returns(queryBuilderClassName) - .addModifiers(Modifier.PUBLIC) - .addParameter(enumColums, "field") - .addStatement("return this") - - .build()) - - .addMethod(MethodSpec.methodBuilder("execute") - .returns(listObjectsClassName) - .addModifiers(Modifier.PRIVATE) - .addStatement("$T db = $T.getInstance().open().getDatabase()", Constants.databaseClassName, Constants.daoClassName) - .addStatement("$T stringBuilder = new $T()", Constants.stringBuilderClassName, Constants.stringBuilderClassName) - .addStatement("stringBuilder.append($S)", "select distinct ") - .addStatement("if(fields == null) stringBuilder.append(\"$L.* \")", TABLE_NAME) - .beginControlFlow("else") - .addStatement("stringBuilder.append($S)", TABLE_NAME + "." + Constants.FIELD_ID) - .addStatement("final int fieldsSize = fields.size()") - .beginControlFlow("for(int i=0;i> weakReference = new WeakReference<>(callback)", ClassName.get(WeakReference.class), Constants.callback, modelClassName) + .addStatement("final $T current = new Handler($T.myLooper())", ClassName.get("android.os", "Handler"), ClassName.get("android.os", "Looper")) + .addCode("new Handler().post(new $T() {\n", ClassName.get(Runnable.class)) + .addCode("@$T public void run() {\n", ClassName.get(Override.class)) + .addStatement("add(object)") + .addCode("current.post(new Runnable() {\n") + .addCode("@Override public void run() {\n") + .addStatement("$T callback1 = weakReference.get()", ParameterizedTypeName.get(Constants.callback, modelClassName)) + .addStatement("if(callback1 != null) callback1.onSuccess(object)") + .addCode("}\n") + .addCode("});\n") + .addCode("}\n") + .addCode("});\n") + .build()) + + .addMethod(MethodSpec.methodBuilder("addAsync") + .addParameter(modelClassName, "object", Modifier.FINAL) + .addModifiers(Modifier.PUBLIC) + .addStatement("addAsync(object, null)") + .build()) + + .addMethod(MethodSpec.methodBuilder("addAsync") + .addParameter(ProcessUtils.listOf(modelClassName), "objects", Modifier.FINAL) + .addParameter(ParameterizedTypeName.get(Constants.callback, ProcessUtils.listOf(modelClassName)), "callback") + .addModifiers(Modifier.PUBLIC) + .addStatement("final $T weakReference = new WeakReference<>(callback)", + ParameterizedTypeName.get(ClassName.get(WeakReference.class), ParameterizedTypeName.get(Constants.callback, ProcessUtils.listOf(modelClassName)))) + .addStatement("final $T current = new Handler($T.myLooper())", ClassName.get("android.os", "Handler"), ClassName.get("android.os", "Looper")) + .addCode("new Handler().post(new $T() {\n", ClassName.get(Runnable.class)) + .addCode("@$T public void run() {\n", ClassName.get(Override.class)) + .addStatement("add(objects)") + .addCode("current.post(new Runnable() {\n") + .addCode("@Override public void run() {\n") + .addStatement("$T callback1 = weakReference.get()", ParameterizedTypeName.get(Constants.callback, ProcessUtils.listOf(modelClassName))) + .addStatement("if(callback1 != null) callback1.onSuccess(objects)") + .addCode("}\n") + .addCode("});\n") + .addCode("}\n") + .addCode("});\n") + .build()) + + .addMethod(MethodSpec.methodBuilder("addAsync") + .addParameter(ProcessUtils.listOf(modelClassName), "objects", Modifier.FINAL) + .addModifiers(Modifier.PUBLIC) + .addStatement("addAsync(objects, null)") + .build()) + + .addMethod(MethodSpec.methodBuilder("update") + .addParameter(modelClassName, "object") + .returns(TypeName.LONG) + .addModifiers(Modifier.PUBLIC) + .addStatement("$T database = $T.getInstance().open().getDatabase()", Constants.databaseClassName, Constants.daoClassName) + .addStatement("long objectId = $T.update(database,object)", modelCursorHelperClassName) + .addStatement("$T.getInstance().close()", Constants.daoClassName) + .addStatement("return objectId") + .build()) + + .addMethod(MethodSpec.methodBuilder("update") + .addParameter(ProcessUtils.listOf(modelClassName), "objects") + .addModifiers(Modifier.PUBLIC) + .addStatement("for($T object : objects) update(object)", modelClassName) + .build()) + + .addMethod(MethodSpec.methodBuilder("updateAsync") + .addParameter(modelClassName, "object", Modifier.FINAL) + .addParameter(ParameterizedTypeName.get(Constants.callback, modelClassName), "callback") + .addModifiers(Modifier.PUBLIC) + .addStatement("final $T<$T<$T>> weakReference = new WeakReference<>(callback)", ClassName.get(WeakReference.class), Constants.callback, modelClassName) + .addStatement("final $T current = new Handler($T.myLooper())", ClassName.get("android.os", "Handler"), ClassName.get("android.os", "Looper")) + .addCode("new Handler().post(new $T() {\n", ClassName.get(Runnable.class)) + .addCode("@$T public void run() {\n", ClassName.get(Override.class)) + .addStatement("update(object)") + .addCode("current.post(new Runnable() {\n") + .addCode("@Override public void run() {\n") + .addStatement("$T callback1 = weakReference.get()", ParameterizedTypeName.get(Constants.callback, modelClassName)) + .addStatement("if(callback1 != null) callback1.onSuccess(object)") + .addCode("}\n") + .addCode("});\n") + .addCode("}\n") + .addCode("});\n") + .build()) + + .addMethod(MethodSpec.methodBuilder("updateAsync") + .addParameter(modelClassName, "object", Modifier.FINAL) .addModifiers(Modifier.PUBLIC) + .addStatement("updateAsync(object, null)") + .build()) - .addField(ClassName.get(Constants.DAO_PACKAGE, Constants.QUERY_LOGGER), "logger") - - .addMethod(MethodSpec.constructorBuilder().addModifiers(Modifier.PUBLIC).build()) - - .addMethod(MethodSpec.methodBuilder("create") - .addModifiers(Modifier.PUBLIC, Modifier.STATIC) - .returns(ArrayTypeName.get(String[].class)) - - //for - .addStatement("return new $T[]{$L}", ClassName.get(String.class), generateCreationString()) - .build()) - - .addMethod(MethodSpec.methodBuilder("update") - .returns(ClassName.get(String.class)) - .addModifiers(Modifier.PUBLIC, Modifier.STATIC) - .addStatement("return $S", "") - .build()) - - //.addMethod(MethodSpec.methodBuilder("drop") - // .addModifiers(Modifier.PUBLIC, Modifier.STATIC) - // .returns(ArrayTypeName.get(String[].class)) - - // //for - // .addStatement("return new $T[]{$L}", ClassName.get(String.class), generateDropString()) - // .build()) - - .addMethod(MethodSpec.methodBuilder("select") - .addModifiers(Modifier.PUBLIC) - .returns(queryBuilderClassName) - .addStatement("return new $T(false,logger)", queryBuilderClassName) - .build()) - - .addMethod(MethodSpec.methodBuilder("where") - .addModifiers(Modifier.PUBLIC) - .addModifiers(Modifier.STATIC) - .returns(queryBuilderClassName) - .addStatement("return new $T(true,null)", queryBuilderClassName) - .build()) - - .addMethod(MethodSpec.methodBuilder("add") - .addParameter(modelClassName, "object") - .returns(TypeName.LONG) - .addModifiers(Modifier.PUBLIC) - .addStatement("$T database = $T.getInstance().open().getDatabase()", Constants.databaseClassName, Constants.daoClassName) - .addStatement("long objectId = $T.insert(database,object)", modelCursorHelperClassName) - .addStatement("$T.getInstance().close()", Constants.daoClassName) - .addStatement("return objectId") - .build()) - - .addMethod(MethodSpec.methodBuilder("add") - .addParameter(ProcessUtils.listOf(modelClassName), "objects") - .addModifiers(Modifier.PUBLIC) - .addStatement("for($T object : objects) add(object)", modelClassName) - .build()) - - .addMethod(MethodSpec.methodBuilder("addAsync") - .addParameter(modelClassName, "object", Modifier.FINAL) - .addParameter(ParameterizedTypeName.get(Constants.callback, modelClassName), "callback") - .addModifiers(Modifier.PUBLIC) - .addStatement("final $T<$T<$T>> weakReference = new WeakReference<>(callback)", ClassName.get(WeakReference.class), Constants.callback, modelClassName) - .addStatement("final $T current = new Handler($T.myLooper())", ClassName.get("android.os", "Handler"), ClassName.get("android.os", "Looper")) - .addCode("new Handler().post(new $T() {\n", ClassName.get(Runnable.class)) - .addCode("@$T public void run() {\n", ClassName.get(Override.class)) - .addStatement("add(object)") - .addCode("current.post(new Runnable() {\n") - .addCode("@Override public void run() {\n") - .addStatement("$T callback1 = weakReference.get()", ParameterizedTypeName.get(Constants.callback, modelClassName)) - .addStatement("if(callback1 != null) callback1.onSuccess(object)") - .addCode("}\n") - .addCode("});\n") - .addCode("}\n") - .addCode("});\n") - .build()) - - .addMethod(MethodSpec.methodBuilder("addAsync") - .addParameter(modelClassName, "object", Modifier.FINAL) - .addModifiers(Modifier.PUBLIC) - .addStatement("addAsync(object, null)") - .build()) - - .addMethod(MethodSpec.methodBuilder("addAsync") - .addParameter(ProcessUtils.listOf(modelClassName), "objects", Modifier.FINAL) - .addParameter(ParameterizedTypeName.get(Constants.callback, ProcessUtils.listOf(modelClassName)), "callback") - .addModifiers(Modifier.PUBLIC) - .addStatement("final $T weakReference = new WeakReference<>(callback)", - ParameterizedTypeName.get(ClassName.get(WeakReference.class), ParameterizedTypeName.get(Constants.callback, ProcessUtils.listOf(modelClassName)))) - .addStatement("final $T current = new Handler($T.myLooper())", ClassName.get("android.os", "Handler"), ClassName.get("android.os", "Looper")) - .addCode("new Handler().post(new $T() {\n", ClassName.get(Runnable.class)) - .addCode("@$T public void run() {\n", ClassName.get(Override.class)) - .addStatement("add(objects)") - .addCode("current.post(new Runnable() {\n") - .addCode("@Override public void run() {\n") - .addStatement("$T callback1 = weakReference.get()", ParameterizedTypeName.get(Constants.callback, ProcessUtils.listOf(modelClassName))) - .addStatement("if(callback1 != null) callback1.onSuccess(objects)") - .addCode("}\n") - .addCode("});\n") - .addCode("}\n") - .addCode("});\n") - .build()) - - .addMethod(MethodSpec.methodBuilder("addAsync") - .addParameter(ProcessUtils.listOf(modelClassName), "objects", Modifier.FINAL) - .addModifiers(Modifier.PUBLIC) - .addStatement("addAsync(objects, null)") - .build()) - - .addMethod(MethodSpec.methodBuilder("update") - .addParameter(modelClassName, "object") - .returns(TypeName.LONG) - .addModifiers(Modifier.PUBLIC) - .addStatement("$T database = $T.getInstance().open().getDatabase()", Constants.databaseClassName, Constants.daoClassName) - .addStatement("long objectId = $T.update(database,object)", modelCursorHelperClassName) - .addStatement("$T.getInstance().close()", Constants.daoClassName) - .addStatement("return objectId") - .build()) - - .addMethod(MethodSpec.methodBuilder("update") - .addParameter(ProcessUtils.listOf(modelClassName), "objects") - .addModifiers(Modifier.PUBLIC) - .addStatement("for($T object : objects) update(object)", modelClassName) - .build()) - - .addMethod(MethodSpec.methodBuilder("updateAsync") - .addParameter(modelClassName, "object", Modifier.FINAL) - .addParameter(ParameterizedTypeName.get(Constants.callback, modelClassName), "callback") - .addModifiers(Modifier.PUBLIC) - .addStatement("final $T<$T<$T>> weakReference = new WeakReference<>(callback)", ClassName.get(WeakReference.class), Constants.callback, modelClassName) - .addStatement("final $T current = new Handler($T.myLooper())", ClassName.get("android.os", "Handler"), ClassName.get("android.os", "Looper")) - .addCode("new Handler().post(new $T() {\n", ClassName.get(Runnable.class)) - .addCode("@$T public void run() {\n", ClassName.get(Override.class)) - .addStatement("update(object)") - .addCode("current.post(new Runnable() {\n") - .addCode("@Override public void run() {\n") - .addStatement("$T callback1 = weakReference.get()", ParameterizedTypeName.get(Constants.callback, modelClassName)) - .addStatement("if(callback1 != null) callback1.onSuccess(object)") - .addCode("}\n") - .addCode("});\n") - .addCode("}\n") - .addCode("});\n") - .build()) - - .addMethod(MethodSpec.methodBuilder("updateAsync") - .addParameter(modelClassName, "object", Modifier.FINAL) - .addModifiers(Modifier.PUBLIC) - .addStatement("updateAsync(object, null)") - .build()) - - .addMethod(MethodSpec.methodBuilder("updateAsync") - .addParameter(ProcessUtils.listOf(modelClassName), "objects", Modifier.FINAL) - .addParameter(ParameterizedTypeName.get(Constants.callback, ProcessUtils.listOf(modelClassName)), "callback") - .addModifiers(Modifier.PUBLIC) - .addStatement("final $T weakReference = new WeakReference<>(callback)", - ParameterizedTypeName.get(ClassName.get(WeakReference.class), ParameterizedTypeName.get(Constants.callback, ProcessUtils.listOf(modelClassName)))) - .addStatement("final $T current = new Handler($T.myLooper())", ClassName.get("android.os", "Handler"), ClassName.get("android.os", "Looper")) - .addCode("new Handler().post(new $T() {\n", ClassName.get(Runnable.class)) - .addCode("@$T public void run() {\n", ClassName.get(Override.class)) - .addStatement("update(objects)") - .addCode("current.post(new Runnable() {\n") - .addCode("@Override public void run() {\n") - .addStatement("$T callback1 = weakReference.get()", ParameterizedTypeName.get(Constants.callback, ProcessUtils.listOf(modelClassName))) - .addStatement("if(callback1 != null) callback1.onSuccess(objects)") - .addCode("}\n") - .addCode("});\n") - .addCode("}\n") - .addCode("});\n") - .build()) - - .addMethod(MethodSpec.methodBuilder("updateAsync") - .addParameter(ProcessUtils.listOf(modelClassName), "objects", Modifier.FINAL) - .addModifiers(Modifier.PUBLIC) - .addStatement("updateAsync(objects, null)") - .build()) - - .addMethod(MethodSpec.methodBuilder("delete") - .addParameter(Constants.databaseClassName, "db") - .addParameter(modelClassName, "object") - .addModifiers(Modifier.PRIVATE) - .returns(TypeName.VOID) - .addStatement(ProcessUtils.getModelId(element, "object", "id")) - .addStatement("db.delete($S, $S, new String[]{String.valueOf(id)})", TABLE_NAME, "_id = ?") - .build()) - - .addMethod(MethodSpec.methodBuilder("delete") - .addParameter(modelClassName, "object") - .addModifiers(Modifier.PUBLIC) - .returns(TypeName.VOID) - .addStatement("$T db = $T.getInstance().open().getDatabase()", Constants.databaseClassName, Constants.daoClassName) - .addStatement("delete(db,object)") - .addStatement("$T.getInstance().close()", Constants.daoClassName) - .build()) - - .addMethod(MethodSpec.methodBuilder("delete") - .addParameter(listObjectsClassName, "objects") - .addModifiers(Modifier.PUBLIC) - .returns(TypeName.VOID) - .addStatement("$T db = $T.getInstance().open().getDatabase()", Constants.databaseClassName, Constants.daoClassName) - .beginControlFlow("for($T object : objects)", modelClassName) - .addStatement("delete(db,object)") - .endControlFlow() - .addStatement("$T.getInstance().close()", Constants.daoClassName) - .build()) - - .addMethod(MethodSpec.methodBuilder("deleteAll") - .addModifiers(Modifier.PUBLIC) - .returns(TypeName.VOID) - .addStatement("$T.getInstance().open().getDatabase().execSQL($S)", Constants.daoClassName, "delete from " + TABLE_NAME) - .addStatement("$T.getInstance().close()", Constants.daoClassName) - .build()) - - .addMethod(MethodSpec.methodBuilder("deleteAllAsync") - .addParameter(modelClassName, "object", Modifier.FINAL) - .addParameter(ParameterizedTypeName.get(Constants.callback, modelClassName), "callback") - .addModifiers(Modifier.PUBLIC) - .addStatement("final $T<$T<$T>> weakReference = new WeakReference<>(callback)", ClassName.get(WeakReference.class), Constants.callback, modelClassName) - .addStatement("final $T current = new Handler($T.myLooper())", ClassName.get("android.os", "Handler"), ClassName.get("android.os", "Looper")) - .addCode("new Handler().post(new $T() {\n", ClassName.get(Runnable.class)) - .addCode("@$T public void run() {\n", ClassName.get(Override.class)) - .addStatement("deleteAll()") - .addCode("current.post(new Runnable() {\n") - .addCode("@Override public void run() {\n") - .addStatement("$T callback1 = weakReference.get()", ParameterizedTypeName.get(Constants.callback, modelClassName)) - .addStatement("if(callback1 != null) callback1.onSuccess(null)") - .addCode("}\n") - .addCode("});\n") - .addCode("}\n") - .addCode("});\n") - .build()) - - .addMethod(MethodSpec.methodBuilder("deleteAsync") - .addParameter(modelClassName, "object", Modifier.FINAL) - .addParameter(ParameterizedTypeName.get(Constants.callback, modelClassName), "callback") - .addModifiers(Modifier.PUBLIC) - .addStatement("final $T<$T<$T>> weakReference = new WeakReference<>(callback)", ClassName.get(WeakReference.class), Constants.callback, modelClassName) - .addStatement("final $T current = new Handler($T.myLooper())", ClassName.get("android.os", "Handler"), ClassName.get("android.os", "Looper")) - .addCode("new Handler().post(new $T() {\n", ClassName.get(Runnable.class)) - .addCode("@$T public void run() {\n", ClassName.get(Override.class)) - .addStatement("delete(object)") - .addCode("current.post(new Runnable() {\n") - .addCode("@Override public void run() {\n") - .addStatement("$T callback1 = weakReference.get()", ParameterizedTypeName.get(Constants.callback, modelClassName)) - .addStatement("if(callback1 != null) callback1.onSuccess(object)") - .addCode("}\n") - .addCode("});\n") - .addCode("}\n") - .addCode("});\n") - .build()) - - .addMethod(MethodSpec.methodBuilder("deleteAsync") - .addParameter(modelClassName, "object", Modifier.FINAL) - .addModifiers(Modifier.PUBLIC) - .addStatement("deleteAsync(object, null)") - .build()) - - .addMethod(MethodSpec.methodBuilder("deleteAsync") - .addParameter(ProcessUtils.listOf(modelClassName), "objects", Modifier.FINAL) - .addParameter(ParameterizedTypeName.get(Constants.callback, ProcessUtils.listOf(modelClassName)), "callback") - .addModifiers(Modifier.PUBLIC) - .addStatement("final $T weakReference = new WeakReference<>(callback)", - ParameterizedTypeName.get(ClassName.get(WeakReference.class), ParameterizedTypeName.get(Constants.callback, ProcessUtils.listOf(modelClassName)))) - .addStatement("final $T current = new Handler($T.myLooper())", ClassName.get("android.os", "Handler"), ClassName.get("android.os", "Looper")) - .addCode("new Handler().post(new $T() {\n", ClassName.get(Runnable.class)) - .addCode("@$T public void run() {\n", ClassName.get(Override.class)) - .addStatement("delete(objects)") - .addCode("current.post(new Runnable() {\n") - .addCode("@Override public void run() {\n") - .addStatement("$T callback1 = weakReference.get()", ParameterizedTypeName.get(Constants.callback, ProcessUtils.listOf(modelClassName))) - .addStatement("if(callback1 != null) callback1.onSuccess(objects)") - .addCode("}\n") - .addCode("});\n") - .addCode("}\n") - .addCode("});\n") - .build()) - - .addMethod(MethodSpec.methodBuilder("deleteAsync") - .addParameter(ProcessUtils.listOf(modelClassName), "objects", Modifier.FINAL) - .addModifiers(Modifier.PUBLIC) - .addStatement("deleteAsync(objects, null)") - .build()) - - .addMethod(MethodSpec.methodBuilder("count") - .addModifiers(Modifier.PUBLIC) - .returns(TypeName.INT) - .addStatement("$T db = $T.getInstance().open().getDatabase()", Constants.databaseClassName, Constants.daoClassName) - .addStatement("$T cursor = db.rawQuery($S,null)", Constants.cursorClassName, "select count(*) from " + TABLE_NAME) - .addStatement("cursor.moveToFirst()") - .addStatement("int recCount = cursor.getInt(0)") - .addStatement("cursor.close()") - .addStatement("$T.getInstance().close()", Constants.daoClassName) - .addStatement("return recCount") - .build()) - - .addMethod(MethodSpec.methodBuilder("logQueries") - .addModifiers(Modifier.PUBLIC) - .addParameter(ClassName.get(Constants.DAO_PACKAGE, Constants.QUERY_LOGGER), "logger") - .addStatement("this.logger = logger") - .build()) - - .build(); + .addMethod(MethodSpec.methodBuilder("updateAsync") + .addParameter(ProcessUtils.listOf(modelClassName), "objects", Modifier.FINAL) + .addParameter(ParameterizedTypeName.get(Constants.callback, ProcessUtils.listOf(modelClassName)), "callback") + .addModifiers(Modifier.PUBLIC) + .addStatement("final $T weakReference = new WeakReference<>(callback)", + ParameterizedTypeName.get(ClassName.get(WeakReference.class), ParameterizedTypeName.get(Constants.callback, ProcessUtils.listOf(modelClassName)))) + .addStatement("final $T current = new Handler($T.myLooper())", ClassName.get("android.os", "Handler"), ClassName.get("android.os", "Looper")) + .addCode("new Handler().post(new $T() {\n", ClassName.get(Runnable.class)) + .addCode("@$T public void run() {\n", ClassName.get(Override.class)) + .addStatement("update(objects)") + .addCode("current.post(new Runnable() {\n") + .addCode("@Override public void run() {\n") + .addStatement("$T callback1 = weakReference.get()", ParameterizedTypeName.get(Constants.callback, ProcessUtils.listOf(modelClassName))) + .addStatement("if(callback1 != null) callback1.onSuccess(objects)") + .addCode("}\n") + .addCode("});\n") + .addCode("}\n") + .addCode("});\n") + .build()) + + .addMethod(MethodSpec.methodBuilder("updateAsync") + .addParameter(ProcessUtils.listOf(modelClassName), "objects", Modifier.FINAL) + .addModifiers(Modifier.PUBLIC) + .addStatement("updateAsync(objects, null)") + .build()) + + .addMethod(MethodSpec.methodBuilder("delete") + .addParameter(Constants.databaseClassName, "db") + .addParameter(modelClassName, "object") + .addModifiers(Modifier.PRIVATE) + .returns(TypeName.VOID) + .addStatement(ProcessUtils.getModelId(element, "object", "id")) + .addStatement("db.delete($S, $S, new String[]{String.valueOf(id)})", TABLE_NAME, "_id = ?") + .build()) + + .addMethod(MethodSpec.methodBuilder("delete") + .addParameter(modelClassName, "object") + .addModifiers(Modifier.PUBLIC) + .returns(TypeName.VOID) + .addStatement("$T db = $T.getInstance().open().getDatabase()", Constants.databaseClassName, Constants.daoClassName) + .addStatement("delete(db,object)") + .addStatement("$T.getInstance().close()", Constants.daoClassName) + .build()) + + .addMethod(MethodSpec.methodBuilder("delete") + .addParameter(listObjectsClassName, "objects") + .addModifiers(Modifier.PUBLIC) + .returns(TypeName.VOID) + .addStatement("$T db = $T.getInstance().open().getDatabase()", Constants.databaseClassName, Constants.daoClassName) + .beginControlFlow("for($T object : objects)", modelClassName) + .addStatement("delete(db,object)") + .endControlFlow() + .addStatement("$T.getInstance().close()", Constants.daoClassName) + .build()) + + .addMethod(MethodSpec.methodBuilder("deleteAll") + .addModifiers(Modifier.PUBLIC) + .returns(TypeName.VOID) + .addStatement("$T.getInstance().open().getDatabase().execSQL($S)", Constants.daoClassName, "delete from " + TABLE_NAME) + .addStatement("$T.getInstance().close()", Constants.daoClassName) + .build()) + + .addMethod(MethodSpec.methodBuilder("deleteAllAsync") + .addParameter(modelClassName, "object", Modifier.FINAL) + .addParameter(ParameterizedTypeName.get(Constants.callback, modelClassName), "callback") + .addModifiers(Modifier.PUBLIC) + .addStatement("final $T<$T<$T>> weakReference = new WeakReference<>(callback)", ClassName.get(WeakReference.class), Constants.callback, modelClassName) + .addStatement("final $T current = new Handler($T.myLooper())", ClassName.get("android.os", "Handler"), ClassName.get("android.os", "Looper")) + .addCode("new Handler().post(new $T() {\n", ClassName.get(Runnable.class)) + .addCode("@$T public void run() {\n", ClassName.get(Override.class)) + .addStatement("deleteAll()") + .addCode("current.post(new Runnable() {\n") + .addCode("@Override public void run() {\n") + .addStatement("$T callback1 = weakReference.get()", ParameterizedTypeName.get(Constants.callback, modelClassName)) + .addStatement("if(callback1 != null) callback1.onSuccess(null)") + .addCode("}\n") + .addCode("});\n") + .addCode("}\n") + .addCode("});\n") + .build()) + + .addMethod(MethodSpec.methodBuilder("deleteAsync") + .addParameter(modelClassName, "object", Modifier.FINAL) + .addParameter(ParameterizedTypeName.get(Constants.callback, modelClassName), "callback") + .addModifiers(Modifier.PUBLIC) + .addStatement("final $T<$T<$T>> weakReference = new WeakReference<>(callback)", ClassName.get(WeakReference.class), Constants.callback, modelClassName) + .addStatement("final $T current = new Handler($T.myLooper())", ClassName.get("android.os", "Handler"), ClassName.get("android.os", "Looper")) + .addCode("new Handler().post(new $T() {\n", ClassName.get(Runnable.class)) + .addCode("@$T public void run() {\n", ClassName.get(Override.class)) + .addStatement("delete(object)") + .addCode("current.post(new Runnable() {\n") + .addCode("@Override public void run() {\n") + .addStatement("$T callback1 = weakReference.get()", ParameterizedTypeName.get(Constants.callback, modelClassName)) + .addStatement("if(callback1 != null) callback1.onSuccess(object)") + .addCode("}\n") + .addCode("});\n") + .addCode("}\n") + .addCode("});\n") + .build()) + + .addMethod(MethodSpec.methodBuilder("deleteAsync") + .addParameter(modelClassName, "object", Modifier.FINAL) + .addModifiers(Modifier.PUBLIC) + .addStatement("deleteAsync(object, null)") + .build()) + + .addMethod(MethodSpec.methodBuilder("deleteAsync") + .addParameter(ProcessUtils.listOf(modelClassName), "objects", Modifier.FINAL) + .addParameter(ParameterizedTypeName.get(Constants.callback, ProcessUtils.listOf(modelClassName)), "callback") + .addModifiers(Modifier.PUBLIC) + .addStatement("final $T weakReference = new WeakReference<>(callback)", + ParameterizedTypeName.get(ClassName.get(WeakReference.class), ParameterizedTypeName.get(Constants.callback, ProcessUtils.listOf(modelClassName)))) + .addStatement("final $T current = new Handler($T.myLooper())", ClassName.get("android.os", "Handler"), ClassName.get("android.os", "Looper")) + .addCode("new Handler().post(new $T() {\n", ClassName.get(Runnable.class)) + .addCode("@$T public void run() {\n", ClassName.get(Override.class)) + .addStatement("delete(objects)") + .addCode("current.post(new Runnable() {\n") + .addCode("@Override public void run() {\n") + .addStatement("$T callback1 = weakReference.get()", ParameterizedTypeName.get(Constants.callback, ProcessUtils.listOf(modelClassName))) + .addStatement("if(callback1 != null) callback1.onSuccess(objects)") + .addCode("}\n") + .addCode("});\n") + .addCode("}\n") + .addCode("});\n") + .build()) + + .addMethod(MethodSpec.methodBuilder("deleteAsync") + .addParameter(ProcessUtils.listOf(modelClassName), "objects", Modifier.FINAL) + .addModifiers(Modifier.PUBLIC) + .addStatement("deleteAsync(objects, null)") + .build()) + + .addMethod(MethodSpec.methodBuilder("count") + .addModifiers(Modifier.PUBLIC) + .returns(TypeName.INT) + .addStatement("$T db = $T.getInstance().open().getDatabase()", Constants.databaseClassName, Constants.daoClassName) + .addStatement("$T cursor = db.rawQuery($S,null)", Constants.cursorClassName, "select count(*) from " + TABLE_NAME) + .addStatement("cursor.moveToFirst()") + .addStatement("int recCount = cursor.getInt(0)") + .addStatement("cursor.close()") + .addStatement("$T.getInstance().close()", Constants.daoClassName) + .addStatement("return recCount") + .build()) + + .addMethod(MethodSpec.methodBuilder("logQueries") + .addModifiers(Modifier.PUBLIC) + .addParameter(ClassName.get(Constants.DAO_PACKAGE, Constants.QUERY_LOGGER), "logger") + .addStatement("this.logger = logger") + .build()) + + .build(); return this; } @@ -641,18 +650,19 @@ protected List generateQueryMethods() { ClassName className = ProcessUtils.getSelectorName(variableElement); if (className != null) { TypeName selector = null; - if (className == Constants.queryBuilder_NumberSelectorClassName) + if (className == Constants.queryBuilder_NumberSelectorClassName) { selector = ParameterizedTypeName.get(className, queryBuilderClassName, ProcessUtils.getUnboxedClass(variableElement)); - else + } else { selector = ParameterizedTypeName.get(className, queryBuilderClassName); + } methodSpecs.add(MethodSpec.methodBuilder(variableElement.getSimpleName().toString()) - .returns(selector) - .addModifiers(Modifier.PUBLIC) + .returns(selector) + .addModifiers(Modifier.PUBLIC) - .addStatement("return new $T(this, $T.$L.getName())", selector, enumColums, variableElement.getSimpleName()) + .addStatement("return new $T(this, $T.$L.getName())", selector, enumColums, variableElement.getSimpleName()) - .build()); + .build()); } } @@ -661,18 +671,19 @@ protected List generateQueryMethods() { if (className != null) { TypeName selector = null; - if (className == Constants.queryBuilder_ListNumberSelectorClassName) + if (className == Constants.queryBuilder_ListNumberSelectorClassName) { selector = ParameterizedTypeName.get(className, queryBuilderClassName, ProcessUtils.getUnboxedClass(variableElement)); - else + } else { selector = ParameterizedTypeName.get(className, queryBuilderClassName); + } methodSpecs.add(MethodSpec.methodBuilder(variableElement.getSimpleName().toString()) - .returns(selector) - .addModifiers(Modifier.PUBLIC) + .returns(selector) + .addModifiers(Modifier.PUBLIC) - .addStatement("return new $T(this, $T.$L.getName())", selector, enumColums, variableElement.getSimpleName()) + .addStatement("return new $T(this, $T.$L.getName())", selector, enumColums, variableElement.getSimpleName()) - .build()); + .build()); } } @@ -681,12 +692,12 @@ protected List generateQueryMethods() { String JOINTABLE = ProcessUtils.getTableName(modelName) + "_" + ProcessUtils.getTableName(variableElement); methodSpecs.add(MethodSpec.methodBuilder(variableElement.getSimpleName().toString()) - .returns(queryBuilderClassName) - .addModifiers(Modifier.PUBLIC) - .addParameter(ProcessUtils.getFieldQueryBuilderClass(variableElement), "query") - .addStatement("queryBuilder.append('(').append(query.query($S,getTableId($S),$S,$S,getTableId($S),$S,args)).append(')')", TABLE_NAME, JOINTABLE, ProcessUtils.getKeyName(modelName), ProcessUtils.getKeyName(variableElement), ProcessUtils.getTableName(variableElement), ProcessUtils.getObjectName(variableElement)) - .addStatement("return this") - .build()); + .returns(queryBuilderClassName) + .addModifiers(Modifier.PUBLIC) + .addParameter(ProcessUtils.getFieldQueryBuilderClass(variableElement), "query") + .addStatement("queryBuilder.append('(').append(query.query($S,getTableId($S),$S,$S,getTableId($S),$S,args)).append(')')", TABLE_NAME, JOINTABLE, ProcessUtils.getKeyName(modelName), ProcessUtils.getKeyName(variableElement), ProcessUtils.getTableName(variableElement), ProcessUtils.getObjectName(variableElement)) + .addStatement("return this") + .build()); } return methodSpecs; @@ -702,14 +713,14 @@ protected String generateCreationString() { String table = TABLE_NAME + "_" + ProcessUtils.getTableName(variableElement); if (!addedTables.contains(table)) { stringBuilder - .append(",\n") - .append('"') - .append("create table ").append(table) - .append(" ( _id integer primary key autoincrement, ") - .append(ProcessUtils.getKeyName(modelName)).append(" integer, ") - .append(ProcessUtils.getKeyName(variableElement)).append(" integer, ") - .append(Constants.FIELD_NAME).append(" text )") - .append('"'); + .append(",\n") + .append('"') + .append("create table ").append(table) + .append(" ( _id integer primary key autoincrement, ") + .append(ProcessUtils.getKeyName(modelName)).append(" integer, ") + .append(ProcessUtils.getKeyName(variableElement)).append(" integer, ") + .append(Constants.FIELD_NAME).append(" text )") + .append('"'); addedTables.add(table); } } @@ -743,19 +754,22 @@ protected String generateTableCreate() { //filter / remove fieldId List elements = new ArrayList<>(); - for (VariableElement variableElement : fields) - if (variableElement != fieldId) + 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 < elements.size() - 1) + .append(variableElement.getSimpleName()) + .append(" ") + .append(ProcessUtils.getFieldTableType(variableElement)); + if (i < elements.size() - 1) { stringBuilder.append(", "); + } } } return stringBuilder.toString(); diff --git a/freezer-compiler/src/main/java/fr/xebia/android/freezer/generator/QueryBuilderGenerator.java b/freezer-compiler/src/main/java/fr/xebia/android/freezer/generator/QueryBuilderGenerator.java index a9072d1..1b25517 100644 --- a/freezer-compiler/src/main/java/fr/xebia/android/freezer/generator/QueryBuilderGenerator.java +++ b/freezer-compiler/src/main/java/fr/xebia/android/freezer/generator/QueryBuilderGenerator.java @@ -6,14 +6,15 @@ import com.squareup.javapoet.TypeName; import com.squareup.javapoet.TypeSpec; import com.squareup.javapoet.TypeVariableName; -import fr.xebia.android.freezer.Constants; -import fr.xebia.android.freezer.ProcessUtils; import java.util.ArrayList; import java.util.List; 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. */ @@ -21,133 +22,143 @@ public class QueryBuilderGenerator { public TypeSpec generate() { return TypeSpec.classBuilder(Constants.QUERY_BUILDER_SUFFIX) - .addModifiers(Modifier.PUBLIC, Modifier.ABSTRACT) - - .addField(ClassName.get(StringBuilder.class), "queryBuilder", Modifier.PROTECTED) - .addField(ClassName.get(StringBuilder.class), "orderBuilder", Modifier.PROTECTED) - .addField(ProcessUtils.listOf(String.class), "args", Modifier.PROTECTED) - .addField(ProcessUtils.listOf(String.class), "fromTables", Modifier.PROTECTED) - .addField(ProcessUtils.listOf(String.class), "fromTablesNames", Modifier.PROTECTED) - .addField(ProcessUtils.listOf(String.class), "fromTablesId", Modifier.PROTECTED) - .addField(TypeName.BOOLEAN, "named", Modifier.PROTECTED) - .addField(ClassName.get(Constants.DAO_PACKAGE, Constants.QUERY_LOGGER), "logger", Modifier.PROTECTED) - - .addMethod(MethodSpec.constructorBuilder() - .addModifiers(Modifier.PUBLIC) - .addStatement("this.queryBuilder = new $T()", ClassName.get(StringBuilder.class)) - .addStatement("this.orderBuilder = new $T()", ClassName.get(StringBuilder.class)) - .addStatement("this.args = new $T()", ProcessUtils.arraylistOf(String.class)) - .addStatement("this.fromTables = new $T()", ProcessUtils.arraylistOf(String.class)) - .addStatement("this.fromTablesNames = new $T()", ProcessUtils.arraylistOf(String.class)) - .addStatement("this.fromTablesId = new $T()", ProcessUtils.arraylistOf(String.class)) - .build()) - - .addMethod(MethodSpec.constructorBuilder() - .addModifiers(Modifier.PUBLIC) - .addParameter(TypeName.BOOLEAN, "named") - .addParameter(ClassName.get(Constants.DAO_PACKAGE, Constants.QUERY_LOGGER), "logger") - .addStatement("this()") - .addStatement("this.named = named") - .addStatement("this.logger = logger") - .build()) - - .addMethod(MethodSpec.methodBuilder("appendOr") - .addModifiers(Modifier.PROTECTED) - .addStatement("queryBuilder.append($S)", " or ") - .build()) - - .addMethod(MethodSpec.methodBuilder("appendAnd") - .addModifiers(Modifier.PROTECTED) - .addStatement("queryBuilder.append($S)", " and ") - .build()) - - .addMethod(MethodSpec.methodBuilder("appendBeginGroup") - .addModifiers(Modifier.PROTECTED) - .addStatement("queryBuilder.append($S)", " ( ") - .build()) - - .addMethod(MethodSpec.methodBuilder("appendEndGroup") - .addModifiers(Modifier.PROTECTED) - .addStatement("queryBuilder.append($S)", " ) ") - .build()) - - .addMethod(MethodSpec.methodBuilder("appendSortAsc") - .addModifiers(Modifier.PROTECTED) - .addParameter(ClassName.get(String.class), "tableName") - .addParameter(ClassName.get(String.class), "column") - .addStatement("if(orderBuilder.length() != 0) queryBuilder.append(',')") - .addStatement("orderBuilder.append(tableName).append(column)") - .addStatement("orderBuilder.append($S)", " ASC ") - .build()) - - .addMethod(MethodSpec.methodBuilder("appendSortDesc") - .addModifiers(Modifier.PROTECTED) - .addParameter(ClassName.get(String.class), "tableName") - .addParameter(ClassName.get(String.class), "column") - .addStatement("if(orderBuilder.length() != 0) queryBuilder.append(',')") - .addStatement("orderBuilder.append(tableName).append(column)") - .addStatement("orderBuilder.append($S)", " DESC ") - .build()) - - .addMethod(MethodSpec.methodBuilder("query") - .returns(TypeName.get(String.class)) - .addModifiers(Modifier.PUBLIC) - .addParameter(TypeName.get(String.class), "fromTable") - .addParameter(TypeName.get(String.class), "joinTable") - .addParameter(TypeName.get(String.class), "joinIdFrom") - .addParameter(TypeName.get(String.class), "joinIdTo") - .addParameter(TypeName.get(String.class), "table") - .addParameter(TypeName.get(String.class), "variable") - .addParameter(ProcessUtils.listOf(String.class), "args") - .addStatement("args.addAll(this.args)") - .addStatement("queryBuilder.append(\" AND \").append(joinTable).append(\".\").append(joinIdFrom).append(\" = \").append(fromTable).append(\".$L\")", Constants.FIELD_ID) - .addStatement("queryBuilder.append(\" AND \").append(joinTable).append(\".\").append(joinIdTo).append(\" = \").append(table).append(\".$L\")", Constants.FIELD_ID) - .addStatement("queryBuilder.append(\" AND \").append(joinTable).append(\".$L = '\").append(variable).append(\"'\")", Constants.FIELD_NAME) - .addStatement("return queryBuilder.toString().replace($S,table)", Constants.QUERY_NAMED) - .build()) - - .addMethod(MethodSpec.methodBuilder("constructArgs") - .returns(TypeName.get(String[].class)) - .addModifiers(Modifier.PROTECTED) - .addStatement("return args.toArray(new String[args.size()])") - .build()) - - .addMethod(MethodSpec.methodBuilder("constructQuery") - .returns(TypeName.get(String.class)) - .addModifiers(Modifier.PUBLIC) - .addStatement("$T query = new $T()", ClassName.get(StringBuilder.class), ClassName.get(StringBuilder.class)) - .addStatement("for($T s : fromTables) query.append($S).append(s)", ClassName.get(String.class), ", ") - .addStatement("if (queryBuilder.length() != 0) query.append($S)", " where ") - .addStatement("query.append(queryBuilder.toString())") - .addStatement("if(orderBuilder.length() != 0) query.append($S)", " ORDER BY ") - .addStatement("query.append(orderBuilder.toString())") - .addStatement("return query.toString()") - .build()) - - .addMethod(MethodSpec.methodBuilder("appendQuery") - .addModifiers(Modifier.PROTECTED) - .addParameter(TypeName.get(String.class), "conditional") - .addParameter(TypeName.get(String.class), "arg") - .addStatement("if (named) queryBuilder.append($S)", "NAMED.") - .addStatement("queryBuilder.append(conditional)") - .addStatement("if(arg != null) args.add(arg)") - .build()) - - .addMethod(MethodSpec.methodBuilder("getTableId") - .addModifiers(Modifier.PROTECTED) - .returns(ClassName.get(String.class)) - .addParameter(ClassName.get(String.class), "tableName") - .addStatement("$T tableId", ClassName.get(String.class)) - .addStatement("int tablePos = fromTablesNames.indexOf(tableName)") - .addStatement("if(tablePos != -1) tableId = fromTablesId.get(tablePos)") - .addStatement("else{ tableId = $S + fromTables.size(); fromTablesId.add(tableId); fromTables.add(tableName + \" \" + tableId); fromTablesNames.add(tableName); }", Constants.QUERY_TABLE_VARIABLE) - .addStatement("return tableId") - - .build()) - - .addTypes(generateSelectors()) - - .build(); + .addModifiers(Modifier.PUBLIC, Modifier.ABSTRACT) + + .addField(ClassName.get(StringBuilder.class), "queryBuilder", Modifier.PROTECTED) + .addField(ClassName.get(StringBuilder.class), "orderBuilder", Modifier.PROTECTED) + .addField(ClassName.get(StringBuilder.class), "limitBuilder", Modifier.PROTECTED) + .addField(ProcessUtils.listOf(String.class), "args", Modifier.PROTECTED) + .addField(ProcessUtils.listOf(String.class), "fromTables", Modifier.PROTECTED) + .addField(ProcessUtils.listOf(String.class), "fromTablesNames", Modifier.PROTECTED) + .addField(ProcessUtils.listOf(String.class), "fromTablesId", Modifier.PROTECTED) + .addField(TypeName.BOOLEAN, "named", Modifier.PROTECTED) + .addField(ClassName.get(Constants.DAO_PACKAGE, Constants.QUERY_LOGGER), "logger", Modifier.PROTECTED) + + .addMethod(MethodSpec.constructorBuilder() + .addModifiers(Modifier.PUBLIC) + .addStatement("this.queryBuilder = new $T()", ClassName.get(StringBuilder.class)) + .addStatement("this.orderBuilder = new $T()", ClassName.get(StringBuilder.class)) + .addStatement("this.args = new $T()", ProcessUtils.arraylistOf(String.class)) + .addStatement("this.fromTables = new $T()", ProcessUtils.arraylistOf(String.class)) + .addStatement("this.fromTablesNames = new $T()", ProcessUtils.arraylistOf(String.class)) + .addStatement("this.fromTablesId = new $T()", ProcessUtils.arraylistOf(String.class)) + .build()) + + .addMethod(MethodSpec.constructorBuilder() + .addModifiers(Modifier.PUBLIC) + .addParameter(TypeName.BOOLEAN, "named") + .addParameter(ClassName.get(Constants.DAO_PACKAGE, Constants.QUERY_LOGGER), "logger") + .addStatement("this()") + .addStatement("this.named = named") + .addStatement("this.logger = logger") + .build()) + + .addMethod(MethodSpec.methodBuilder("appendOr") + .addModifiers(Modifier.PROTECTED) + .addStatement("queryBuilder.append($S)", " or ") + .build()) + + .addMethod(MethodSpec.methodBuilder("appendAnd") + .addModifiers(Modifier.PROTECTED) + .addStatement("queryBuilder.append($S)", " and ") + .build()) + + .addMethod(MethodSpec.methodBuilder("appendBeginGroup") + .addModifiers(Modifier.PROTECTED) + .addStatement("queryBuilder.append($S)", " ( ") + .build()) + + .addMethod(MethodSpec.methodBuilder("appendEndGroup") + .addModifiers(Modifier.PROTECTED) + .addStatement("queryBuilder.append($S)", " ) ") + .build()) + + .addMethod(MethodSpec.methodBuilder("limitStartNumber") + .addModifiers(Modifier.PROTECTED) + .addParameter(TypeName.INT, "start") + .addParameter(TypeName.INT, "count") + .addStatement("this.limitBuilder = new $T()", ClassName.get(StringBuilder.class)) + .addStatement("this.limitBuilder.append(start).append($S).append(count)", ", ") + .build()) + + .addMethod(MethodSpec.methodBuilder("appendSortAsc") + .addModifiers(Modifier.PROTECTED) + .addParameter(ClassName.get(String.class), "tableName") + .addParameter(ClassName.get(String.class), "column") + .addStatement("if(orderBuilder.length() != 0) queryBuilder.append(',')") + .addStatement("orderBuilder.append(tableName).append(column)") + .addStatement("orderBuilder.append($S)", " ASC ") + .build()) + + .addMethod(MethodSpec.methodBuilder("appendSortDesc") + .addModifiers(Modifier.PROTECTED) + .addParameter(ClassName.get(String.class), "tableName") + .addParameter(ClassName.get(String.class), "column") + .addStatement("if(orderBuilder.length() != 0) queryBuilder.append(',')") + .addStatement("orderBuilder.append(tableName).append(column)") + .addStatement("orderBuilder.append($S)", " DESC ") + .build()) + + .addMethod(MethodSpec.methodBuilder("query") + .returns(TypeName.get(String.class)) + .addModifiers(Modifier.PUBLIC) + .addParameter(TypeName.get(String.class), "fromTable") + .addParameter(TypeName.get(String.class), "joinTable") + .addParameter(TypeName.get(String.class), "joinIdFrom") + .addParameter(TypeName.get(String.class), "joinIdTo") + .addParameter(TypeName.get(String.class), "table") + .addParameter(TypeName.get(String.class), "variable") + .addParameter(ProcessUtils.listOf(String.class), "args") + .addStatement("args.addAll(this.args)") + .addStatement("queryBuilder.append(\" AND \").append(joinTable).append(\".\").append(joinIdFrom).append(\" = \").append(fromTable).append(\".$L\")", Constants.FIELD_ID) + .addStatement("queryBuilder.append(\" AND \").append(joinTable).append(\".\").append(joinIdTo).append(\" = \").append(table).append(\".$L\")", Constants.FIELD_ID) + .addStatement("queryBuilder.append(\" AND \").append(joinTable).append(\".$L = '\").append(variable).append(\"'\")", Constants.FIELD_NAME) + .addStatement("return queryBuilder.toString().replace($S,table)", Constants.QUERY_NAMED) + .build()) + + .addMethod(MethodSpec.methodBuilder("constructArgs") + .returns(TypeName.get(String[].class)) + .addModifiers(Modifier.PROTECTED) + .addStatement("return args.toArray(new String[args.size()])") + .build()) + + .addMethod(MethodSpec.methodBuilder("constructQuery") + .returns(TypeName.get(String.class)) + .addModifiers(Modifier.PUBLIC) + .addStatement("$T query = new $T()", ClassName.get(StringBuilder.class), ClassName.get(StringBuilder.class)) + .addStatement("for($T s : fromTables) query.append($S).append(s)", ClassName.get(String.class), ", ") + .addStatement("if (queryBuilder.length() != 0) query.append($S)", " where ") + .addStatement("query.append(queryBuilder.toString())") + .addStatement("if(orderBuilder.length() != 0) query.append($S)", " ORDER BY ") + .addStatement("query.append(orderBuilder.toString())") + .addStatement("if(limitBuilder != null && limitBuilder.length() != 0){ query.append($S); query.append(limitBuilder.toString()); }", " LIMIT ") + .addStatement("return query.toString()") + .build()) + + .addMethod(MethodSpec.methodBuilder("appendQuery") + .addModifiers(Modifier.PROTECTED) + .addParameter(TypeName.get(String.class), "conditional") + .addParameter(TypeName.get(String.class), "arg") + .addStatement("if (named) queryBuilder.append($S)", "NAMED.") + .addStatement("queryBuilder.append(conditional)") + .addStatement("if(arg != null) args.add(arg)") + .build()) + + .addMethod(MethodSpec.methodBuilder("getTableId") + .addModifiers(Modifier.PROTECTED) + .returns(ClassName.get(String.class)) + .addParameter(ClassName.get(String.class), "tableName") + .addStatement("$T tableId", ClassName.get(String.class)) + .addStatement("int tablePos = fromTablesNames.indexOf(tableName)") + .addStatement("if(tablePos != -1) tableId = fromTablesId.get(tablePos)") + .addStatement("else{ tableId = $S + fromTables.size(); fromTablesId.add(tableId); fromTables.add(tableName + \" \" + tableId); fromTablesNames.add(tableName); }", Constants.QUERY_TABLE_VARIABLE) + .addStatement("return tableId") + + .build()) + + .addTypes(generateSelectors()) + + .build(); } @@ -155,276 +166,276 @@ protected List generateSelectors() { List typeSpecs = new ArrayList<>(); typeSpecs.add(TypeSpec.classBuilder(Constants.SELECTOR_NUMBER) - .addModifiers(Modifier.PUBLIC, Modifier.STATIC) - .addTypeVariable(TypeVariableName.get("Q1", Constants.queryBuilderClassName)) - .addTypeVariable(TypeVariableName.get("M1", TypeName.get(Number.class))) - .addField(TypeVariableName.get("Q1"), "queryBuilder", Modifier.PROTECTED) - .addField(TypeName.get(String.class), "column", Modifier.PROTECTED) - - .addMethod(MethodSpec.constructorBuilder() - .addModifiers(Modifier.PUBLIC) - .addParameter(TypeVariableName.get("Q1"), "queryBuilder") - .addParameter(TypeName.get(String.class), "column") - .addStatement("this.queryBuilder = queryBuilder") - .addStatement("this.column = column") - .build()) - - .addMethod(MethodSpec.methodBuilder("equalsTo") - .addModifiers(Modifier.PUBLIC) - .returns(TypeVariableName.get("Q1")) - .addParameter(TypeVariableName.get("M1"), "value") - .addStatement("queryBuilder.appendQuery(column+\" = ?\",String.valueOf(value))") - .addStatement("return queryBuilder") - .build()) - - .addMethod(MethodSpec.methodBuilder("notEqualsTo") - .addModifiers(Modifier.PUBLIC) - .returns(TypeVariableName.get("Q1")) - .addParameter(TypeVariableName.get("M1"), "value") - .addStatement("queryBuilder.appendQuery(column+\" != ?\",String.valueOf(value))") - .addStatement("return queryBuilder") - .build()) - - .addMethod(MethodSpec.methodBuilder("between") - .addModifiers(Modifier.PUBLIC) - .returns(TypeVariableName.get("Q1")) - .addParameter(TypeVariableName.get("M1"), "min") - .addParameter(TypeVariableName.get("M1"), "max") - .addStatement("queryBuilder.appendQuery(column+\" > ?\",String.valueOf(min))") - .addStatement("queryBuilder.appendAnd()") - .addStatement("queryBuilder.appendQuery(column+\" < ?\",String.valueOf(max))") - .addStatement("return queryBuilder") - .build()) - - .addMethod(MethodSpec.methodBuilder("greatherThan") - .addModifiers(Modifier.PUBLIC) - .returns(TypeVariableName.get("Q1")) - .addParameter(TypeVariableName.get("M1"), "value") - .addStatement("queryBuilder.appendQuery(column+\" > ?\",String.valueOf(value))") - .addStatement("return queryBuilder") - .build()) - - .addMethod(MethodSpec.methodBuilder("lessThan") - .addModifiers(Modifier.PUBLIC) - .returns(TypeVariableName.get("Q1")) - .addParameter(TypeVariableName.get("M1"), "value") - .addStatement("queryBuilder.appendQuery(column+\" < ?\",String.valueOf(value))") - .addStatement("return queryBuilder") - .build()) - - .build()); + .addModifiers(Modifier.PUBLIC, Modifier.STATIC) + .addTypeVariable(TypeVariableName.get("Q1", Constants.queryBuilderClassName)) + .addTypeVariable(TypeVariableName.get("M1", TypeName.get(Number.class))) + .addField(TypeVariableName.get("Q1"), "queryBuilder", Modifier.PROTECTED) + .addField(TypeName.get(String.class), "column", Modifier.PROTECTED) + + .addMethod(MethodSpec.constructorBuilder() + .addModifiers(Modifier.PUBLIC) + .addParameter(TypeVariableName.get("Q1"), "queryBuilder") + .addParameter(TypeName.get(String.class), "column") + .addStatement("this.queryBuilder = queryBuilder") + .addStatement("this.column = column") + .build()) + + .addMethod(MethodSpec.methodBuilder("equalsTo") + .addModifiers(Modifier.PUBLIC) + .returns(TypeVariableName.get("Q1")) + .addParameter(TypeVariableName.get("M1"), "value") + .addStatement("queryBuilder.appendQuery(column+\" = ?\",String.valueOf(value))") + .addStatement("return queryBuilder") + .build()) + + .addMethod(MethodSpec.methodBuilder("notEqualsTo") + .addModifiers(Modifier.PUBLIC) + .returns(TypeVariableName.get("Q1")) + .addParameter(TypeVariableName.get("M1"), "value") + .addStatement("queryBuilder.appendQuery(column+\" != ?\",String.valueOf(value))") + .addStatement("return queryBuilder") + .build()) + + .addMethod(MethodSpec.methodBuilder("between") + .addModifiers(Modifier.PUBLIC) + .returns(TypeVariableName.get("Q1")) + .addParameter(TypeVariableName.get("M1"), "min") + .addParameter(TypeVariableName.get("M1"), "max") + .addStatement("queryBuilder.appendQuery(column+\" > ?\",String.valueOf(min))") + .addStatement("queryBuilder.appendAnd()") + .addStatement("queryBuilder.appendQuery(column+\" < ?\",String.valueOf(max))") + .addStatement("return queryBuilder") + .build()) + + .addMethod(MethodSpec.methodBuilder("greatherThan") + .addModifiers(Modifier.PUBLIC) + .returns(TypeVariableName.get("Q1")) + .addParameter(TypeVariableName.get("M1"), "value") + .addStatement("queryBuilder.appendQuery(column+\" > ?\",String.valueOf(value))") + .addStatement("return queryBuilder") + .build()) + + .addMethod(MethodSpec.methodBuilder("lessThan") + .addModifiers(Modifier.PUBLIC) + .returns(TypeVariableName.get("Q1")) + .addParameter(TypeVariableName.get("M1"), "value") + .addStatement("queryBuilder.appendQuery(column+\" < ?\",String.valueOf(value))") + .addStatement("return queryBuilder") + .build()) + + .build()); typeSpecs.add(TypeSpec.classBuilder(Constants.SELECTOR_NUMBER_LIST) - .addModifiers(Modifier.PUBLIC, Modifier.STATIC) - .addTypeVariable(TypeVariableName.get("P1", Constants.queryBuilderClassName)) - .addTypeVariable(TypeVariableName.get("N1", TypeName.get(Number.class))) - .addField(TypeVariableName.get("P1"), "queryBuilder", Modifier.PROTECTED) - .addField(TypeName.get(String.class), "column", Modifier.PROTECTED) - - .addMethod(MethodSpec.constructorBuilder() - .addModifiers(Modifier.PUBLIC) - .addParameter(TypeVariableName.get("P1"), "queryBuilder") - .addParameter(TypeName.get(String.class), "column") - .addStatement("this.queryBuilder = queryBuilder") - .addStatement("this.column = column") - .build()) - - .addMethod(MethodSpec.methodBuilder("contains") - .addModifiers(Modifier.PUBLIC) - .returns(ParameterizedTypeName.get(Constants.queryBuilder_NumberSelectorClassName, TypeVariableName.get("P1"), TypeVariableName.get("N1"))) - .addStatement("return new $L<$L,$L>(queryBuilder,column)", Constants.SELECTOR_NUMBER, "P1", "N1") - .build()) - - .build()); + .addModifiers(Modifier.PUBLIC, Modifier.STATIC) + .addTypeVariable(TypeVariableName.get("P1", Constants.queryBuilderClassName)) + .addTypeVariable(TypeVariableName.get("N1", TypeName.get(Number.class))) + .addField(TypeVariableName.get("P1"), "queryBuilder", Modifier.PROTECTED) + .addField(TypeName.get(String.class), "column", Modifier.PROTECTED) + + .addMethod(MethodSpec.constructorBuilder() + .addModifiers(Modifier.PUBLIC) + .addParameter(TypeVariableName.get("P1"), "queryBuilder") + .addParameter(TypeName.get(String.class), "column") + .addStatement("this.queryBuilder = queryBuilder") + .addStatement("this.column = column") + .build()) + + .addMethod(MethodSpec.methodBuilder("contains") + .addModifiers(Modifier.PUBLIC) + .returns(ParameterizedTypeName.get(Constants.queryBuilder_NumberSelectorClassName, TypeVariableName.get("P1"), TypeVariableName.get("N1"))) + .addStatement("return new $L<$L,$L>(queryBuilder,column)", Constants.SELECTOR_NUMBER, "P1", "N1") + .build()) + + .build()); typeSpecs.add(TypeSpec.classBuilder(Constants.SELECTOR_STRING) - .addModifiers(Modifier.PUBLIC, Modifier.STATIC) - .addTypeVariable(TypeVariableName.get("Q2", Constants.queryBuilderClassName)) - .addField(TypeVariableName.get("Q2"), "queryBuilder", Modifier.PROTECTED) - .addField(TypeName.get(String.class), "column", Modifier.PROTECTED) - - .addMethod(MethodSpec.constructorBuilder() - .addModifiers(Modifier.PUBLIC) - .addParameter(TypeVariableName.get("Q2"), "queryBuilder") - .addParameter(TypeName.get(String.class), "column") - .addStatement("this.queryBuilder = queryBuilder") - .addStatement("this.column = column") - .build()) - - .addMethod(MethodSpec.methodBuilder("equalsTo") - .addModifiers(Modifier.PUBLIC) - .returns(TypeVariableName.get("Q2")) - .addParameter(TypeName.get(String.class), "value") - .addStatement("queryBuilder.appendQuery(column+\" = ?\",value)") - .addStatement("return queryBuilder") - .build()) - - .addMethod(MethodSpec.methodBuilder("notEqualsTo") - .addModifiers(Modifier.PUBLIC) - .returns(TypeVariableName.get("Q2")) - .addParameter(TypeName.get(String.class), "value") - .addStatement("queryBuilder.appendQuery(column+\" != ?\",value)") - .addStatement("return queryBuilder") - .build()) - - .addMethod(MethodSpec.methodBuilder("contains") - .addModifiers(Modifier.PUBLIC) - .returns(TypeVariableName.get("Q2")) - .addParameter(TypeName.get(String.class), "value") - .addStatement("queryBuilder.appendQuery(column+\" LIKE '%\"+value+\"%'\",null)") - .addStatement("return queryBuilder") - .build()) - - .addMethod(MethodSpec.methodBuilder("like") - .addModifiers(Modifier.PUBLIC) - .returns(TypeVariableName.get("Q2")) - .addParameter(TypeName.get(String.class), "value") - .addStatement("queryBuilder.appendQuery(column+\" LIKE '\"+value+\"'\",null)") - .addStatement("return queryBuilder") - .build()) - - .build()); + .addModifiers(Modifier.PUBLIC, Modifier.STATIC) + .addTypeVariable(TypeVariableName.get("Q2", Constants.queryBuilderClassName)) + .addField(TypeVariableName.get("Q2"), "queryBuilder", Modifier.PROTECTED) + .addField(TypeName.get(String.class), "column", Modifier.PROTECTED) + + .addMethod(MethodSpec.constructorBuilder() + .addModifiers(Modifier.PUBLIC) + .addParameter(TypeVariableName.get("Q2"), "queryBuilder") + .addParameter(TypeName.get(String.class), "column") + .addStatement("this.queryBuilder = queryBuilder") + .addStatement("this.column = column") + .build()) + + .addMethod(MethodSpec.methodBuilder("equalsTo") + .addModifiers(Modifier.PUBLIC) + .returns(TypeVariableName.get("Q2")) + .addParameter(TypeName.get(String.class), "value") + .addStatement("queryBuilder.appendQuery(column+\" = ?\",value)") + .addStatement("return queryBuilder") + .build()) + + .addMethod(MethodSpec.methodBuilder("notEqualsTo") + .addModifiers(Modifier.PUBLIC) + .returns(TypeVariableName.get("Q2")) + .addParameter(TypeName.get(String.class), "value") + .addStatement("queryBuilder.appendQuery(column+\" != ?\",value)") + .addStatement("return queryBuilder") + .build()) + + .addMethod(MethodSpec.methodBuilder("contains") + .addModifiers(Modifier.PUBLIC) + .returns(TypeVariableName.get("Q2")) + .addParameter(TypeName.get(String.class), "value") + .addStatement("queryBuilder.appendQuery(column+\" LIKE '%\"+value+\"%'\",null)") + .addStatement("return queryBuilder") + .build()) + + .addMethod(MethodSpec.methodBuilder("like") + .addModifiers(Modifier.PUBLIC) + .returns(TypeVariableName.get("Q2")) + .addParameter(TypeName.get(String.class), "value") + .addStatement("queryBuilder.appendQuery(column+\" LIKE '\"+value+\"'\",null)") + .addStatement("return queryBuilder") + .build()) + + .build()); typeSpecs.add(TypeSpec.classBuilder(Constants.SELECTOR_STRING_LIST) - .addModifiers(Modifier.PUBLIC, Modifier.STATIC) - .addTypeVariable(TypeVariableName.get("P2", Constants.queryBuilderClassName)) - .addField(TypeVariableName.get("P2"), "queryBuilder", Modifier.PROTECTED) - .addField(TypeName.get(String.class), "column", Modifier.PROTECTED) - - .addMethod(MethodSpec.constructorBuilder() - .addModifiers(Modifier.PUBLIC) - .addParameter(TypeVariableName.get("P2"), "queryBuilder") - .addParameter(TypeName.get(String.class), "column") - .addStatement("this.queryBuilder = queryBuilder") - .addStatement("this.column = column") - .build()) - - .addMethod(MethodSpec.methodBuilder("contains") - .addModifiers(Modifier.PUBLIC) - .returns(ParameterizedTypeName.get(Constants.queryBuilder_StringSelectorClassName, TypeVariableName.get("P2"))) - .addStatement("return new $L<$L>(queryBuilder,column)", Constants.SELECTOR_STRING, "P2") - .build()) - - .build()); + .addModifiers(Modifier.PUBLIC, Modifier.STATIC) + .addTypeVariable(TypeVariableName.get("P2", Constants.queryBuilderClassName)) + .addField(TypeVariableName.get("P2"), "queryBuilder", Modifier.PROTECTED) + .addField(TypeName.get(String.class), "column", Modifier.PROTECTED) + + .addMethod(MethodSpec.constructorBuilder() + .addModifiers(Modifier.PUBLIC) + .addParameter(TypeVariableName.get("P2"), "queryBuilder") + .addParameter(TypeName.get(String.class), "column") + .addStatement("this.queryBuilder = queryBuilder") + .addStatement("this.column = column") + .build()) + + .addMethod(MethodSpec.methodBuilder("contains") + .addModifiers(Modifier.PUBLIC) + .returns(ParameterizedTypeName.get(Constants.queryBuilder_StringSelectorClassName, TypeVariableName.get("P2"))) + .addStatement("return new $L<$L>(queryBuilder,column)", Constants.SELECTOR_STRING, "P2") + .build()) + + .build()); typeSpecs.add(TypeSpec.classBuilder(Constants.SELECTOR_BOOLEAN) - .addModifiers(Modifier.PUBLIC, Modifier.STATIC) - .addTypeVariable(TypeVariableName.get("Q3", Constants.queryBuilderClassName)) - .addField(TypeVariableName.get("Q3"), "queryBuilder", Modifier.PROTECTED) - .addField(TypeName.get(String.class), "column", Modifier.PROTECTED) - - .addMethod(MethodSpec.constructorBuilder() - .addModifiers(Modifier.PUBLIC) - .addParameter(TypeVariableName.get("Q3"), "queryBuilder") - .addParameter(TypeName.get(String.class), "column") - .addStatement("this.queryBuilder = queryBuilder") - .addStatement("this.column = column") - .build()) - - .addMethod(MethodSpec.methodBuilder("equalsTo") - .addModifiers(Modifier.PUBLIC) - .returns(TypeVariableName.get("Q3")) - .addParameter(TypeName.BOOLEAN, "value") - .addStatement("queryBuilder.appendQuery(column+\" = ?\", String.valueOf(value ? 1 : 0))") - .addStatement("return queryBuilder") - .build()) - - .addMethod(MethodSpec.methodBuilder("isTrue") - .addModifiers(Modifier.PUBLIC) - .returns(TypeVariableName.get("Q3")) - .addStatement("queryBuilder.appendQuery(column+\" = ?\", String.valueOf(1))") - .addStatement("return queryBuilder") - .build()) - - .addMethod(MethodSpec.methodBuilder("isFalse") - .addModifiers(Modifier.PUBLIC) - .returns(TypeVariableName.get("Q3")) - .addStatement("queryBuilder.appendQuery(column+\" = ?\", String.valueOf(0))") - .addStatement("return queryBuilder") - .build()) - - .build()); + .addModifiers(Modifier.PUBLIC, Modifier.STATIC) + .addTypeVariable(TypeVariableName.get("Q3", Constants.queryBuilderClassName)) + .addField(TypeVariableName.get("Q3"), "queryBuilder", Modifier.PROTECTED) + .addField(TypeName.get(String.class), "column", Modifier.PROTECTED) + + .addMethod(MethodSpec.constructorBuilder() + .addModifiers(Modifier.PUBLIC) + .addParameter(TypeVariableName.get("Q3"), "queryBuilder") + .addParameter(TypeName.get(String.class), "column") + .addStatement("this.queryBuilder = queryBuilder") + .addStatement("this.column = column") + .build()) + + .addMethod(MethodSpec.methodBuilder("equalsTo") + .addModifiers(Modifier.PUBLIC) + .returns(TypeVariableName.get("Q3")) + .addParameter(TypeName.BOOLEAN, "value") + .addStatement("queryBuilder.appendQuery(column+\" = ?\", String.valueOf(value ? 1 : 0))") + .addStatement("return queryBuilder") + .build()) + + .addMethod(MethodSpec.methodBuilder("isTrue") + .addModifiers(Modifier.PUBLIC) + .returns(TypeVariableName.get("Q3")) + .addStatement("queryBuilder.appendQuery(column+\" = ?\", String.valueOf(1))") + .addStatement("return queryBuilder") + .build()) + + .addMethod(MethodSpec.methodBuilder("isFalse") + .addModifiers(Modifier.PUBLIC) + .returns(TypeVariableName.get("Q3")) + .addStatement("queryBuilder.appendQuery(column+\" = ?\", String.valueOf(0))") + .addStatement("return queryBuilder") + .build()) + + .build()); typeSpecs.add(TypeSpec.classBuilder(Constants.SELECTOR_BOOLEAN_LIST) - .addModifiers(Modifier.PUBLIC, Modifier.STATIC) - .addTypeVariable(TypeVariableName.get("P3", Constants.queryBuilderClassName)) - .addField(TypeVariableName.get("P3"), "queryBuilder", Modifier.PROTECTED) - .addField(TypeName.get(String.class), "column", Modifier.PROTECTED) - - .addMethod(MethodSpec.constructorBuilder() - .addModifiers(Modifier.PUBLIC) - .addParameter(TypeVariableName.get("P3"), "queryBuilder") - .addParameter(TypeName.get(String.class), "column") - .addStatement("this.queryBuilder = queryBuilder") - .addStatement("this.column = column") - .build()) - - .addMethod(MethodSpec.methodBuilder("contains") - .addModifiers(Modifier.PUBLIC) - .returns(ParameterizedTypeName.get(Constants.queryBuilder_BooleanSelectorClassName, TypeVariableName.get("P3"))) - .addStatement("return new $L<$L>(queryBuilder,column)", Constants.SELECTOR_BOOLEAN, "P3") - .build()) - - .build()); - - typeSpecs.add(TypeSpec.classBuilder(Constants.SELECTOR_DATE) - .addModifiers(Modifier.PUBLIC, Modifier.STATIC) - .addTypeVariable(TypeVariableName.get("Q3", Constants.queryBuilderClassName)) - .addField(TypeVariableName.get("Q3"), "queryBuilder", Modifier.PROTECTED) - .addField(TypeName.get(String.class), "column", Modifier.PROTECTED) - - .addMethod(MethodSpec.constructorBuilder() - .addModifiers(Modifier.PUBLIC) - .addParameter(TypeVariableName.get("Q3"), "queryBuilder") - .addParameter(TypeName.get(String.class), "column") - .addStatement("this.queryBuilder = queryBuilder") - .addStatement("this.column = column") - .build()) - - .addMethod(MethodSpec.methodBuilder("equalsTo") - .addModifiers(Modifier.PUBLIC) - .returns(TypeVariableName.get("Q3")) - .addParameter(Constants.dateClassName, "date") - .addStatement("queryBuilder.appendQuery(column+\" = Datetime(?)\", new $T($S).format(date))",Constants.simpleDateFormatClassName, Constants.DATE_FORMAT) - .addStatement("return queryBuilder") - .build()) - - .addMethod(MethodSpec.methodBuilder("notEqualsTo") - .addModifiers(Modifier.PUBLIC) - .returns(TypeVariableName.get("Q3")) - .addParameter(Constants.dateClassName, "date") - .addStatement("queryBuilder.appendQuery(column+\" != Datetime(?)\", new $T($S).format(date))", Constants.simpleDateFormatClassName, Constants.DATE_FORMAT) - .addStatement("return queryBuilder") - .build()) - - .addMethod(MethodSpec.methodBuilder("before") - .addModifiers(Modifier.PUBLIC) - .returns(TypeVariableName.get("Q3")) - .addParameter(Constants.dateClassName, "date") - .addStatement("queryBuilder.appendQuery(column+\" < Datetime(?)\", new $T($S).format(date))", Constants.simpleDateFormatClassName, Constants.DATE_FORMAT) - .addStatement("return queryBuilder") - .build()) - - .addMethod(MethodSpec.methodBuilder("after") - .addModifiers(Modifier.PUBLIC) - .returns(TypeVariableName.get("Q3")) - .addParameter(Constants.dateClassName, "date") - .addStatement("queryBuilder.appendQuery(column+\" > Datetime(?)\", new $T($S).format(date))", Constants.simpleDateFormatClassName, Constants.DATE_FORMAT) - .addStatement("return queryBuilder") - .build()) - - .addMethod(MethodSpec.methodBuilder("between") - .addModifiers(Modifier.PUBLIC) - .returns(TypeVariableName.get("Q3")) - .addParameter(Constants.dateClassName, "min") - .addParameter(Constants.dateClassName, "max") - .addStatement("queryBuilder.appendQuery(column+\" > Datetime(?)\", new $T($S).format(min))", Constants.simpleDateFormatClassName, Constants.DATE_FORMAT) - .addStatement("queryBuilder.appendAnd()") - .addStatement("queryBuilder.appendQuery(column+\" < Datetime(?)\", new $T($S).format(max))", Constants.simpleDateFormatClassName, Constants.DATE_FORMAT) - .addStatement("return queryBuilder") - .build()) - - .build()); + .addModifiers(Modifier.PUBLIC, Modifier.STATIC) + .addTypeVariable(TypeVariableName.get("P3", Constants.queryBuilderClassName)) + .addField(TypeVariableName.get("P3"), "queryBuilder", Modifier.PROTECTED) + .addField(TypeName.get(String.class), "column", Modifier.PROTECTED) + + .addMethod(MethodSpec.constructorBuilder() + .addModifiers(Modifier.PUBLIC) + .addParameter(TypeVariableName.get("P3"), "queryBuilder") + .addParameter(TypeName.get(String.class), "column") + .addStatement("this.queryBuilder = queryBuilder") + .addStatement("this.column = column") + .build()) + + .addMethod(MethodSpec.methodBuilder("contains") + .addModifiers(Modifier.PUBLIC) + .returns(ParameterizedTypeName.get(Constants.queryBuilder_BooleanSelectorClassName, TypeVariableName.get("P3"))) + .addStatement("return new $L<$L>(queryBuilder,column)", Constants.SELECTOR_BOOLEAN, "P3") + .build()) + + .build()); + + typeSpecs.add(TypeSpec.classBuilder(Constants.SELECTOR_DATE) + .addModifiers(Modifier.PUBLIC, Modifier.STATIC) + .addTypeVariable(TypeVariableName.get("Q3", Constants.queryBuilderClassName)) + .addField(TypeVariableName.get("Q3"), "queryBuilder", Modifier.PROTECTED) + .addField(TypeName.get(String.class), "column", Modifier.PROTECTED) + + .addMethod(MethodSpec.constructorBuilder() + .addModifiers(Modifier.PUBLIC) + .addParameter(TypeVariableName.get("Q3"), "queryBuilder") + .addParameter(TypeName.get(String.class), "column") + .addStatement("this.queryBuilder = queryBuilder") + .addStatement("this.column = column") + .build()) + + .addMethod(MethodSpec.methodBuilder("equalsTo") + .addModifiers(Modifier.PUBLIC) + .returns(TypeVariableName.get("Q3")) + .addParameter(Constants.dateClassName, "date") + .addStatement("queryBuilder.appendQuery(column+\" = Datetime(?)\", new $T($S).format(date))", Constants.simpleDateFormatClassName, Constants.DATE_FORMAT) + .addStatement("return queryBuilder") + .build()) + + .addMethod(MethodSpec.methodBuilder("notEqualsTo") + .addModifiers(Modifier.PUBLIC) + .returns(TypeVariableName.get("Q3")) + .addParameter(Constants.dateClassName, "date") + .addStatement("queryBuilder.appendQuery(column+\" != Datetime(?)\", new $T($S).format(date))", Constants.simpleDateFormatClassName, Constants.DATE_FORMAT) + .addStatement("return queryBuilder") + .build()) + + .addMethod(MethodSpec.methodBuilder("before") + .addModifiers(Modifier.PUBLIC) + .returns(TypeVariableName.get("Q3")) + .addParameter(Constants.dateClassName, "date") + .addStatement("queryBuilder.appendQuery(column+\" < Datetime(?)\", new $T($S).format(date))", Constants.simpleDateFormatClassName, Constants.DATE_FORMAT) + .addStatement("return queryBuilder") + .build()) + + .addMethod(MethodSpec.methodBuilder("after") + .addModifiers(Modifier.PUBLIC) + .returns(TypeVariableName.get("Q3")) + .addParameter(Constants.dateClassName, "date") + .addStatement("queryBuilder.appendQuery(column+\" > Datetime(?)\", new $T($S).format(date))", Constants.simpleDateFormatClassName, Constants.DATE_FORMAT) + .addStatement("return queryBuilder") + .build()) + + .addMethod(MethodSpec.methodBuilder("between") + .addModifiers(Modifier.PUBLIC) + .returns(TypeVariableName.get("Q3")) + .addParameter(Constants.dateClassName, "min") + .addParameter(Constants.dateClassName, "max") + .addStatement("queryBuilder.appendQuery(column+\" > Datetime(?)\", new $T($S).format(min))", Constants.simpleDateFormatClassName, Constants.DATE_FORMAT) + .addStatement("queryBuilder.appendAnd()") + .addStatement("queryBuilder.appendQuery(column+\" < Datetime(?)\", new $T($S).format(max))", Constants.simpleDateFormatClassName, Constants.DATE_FORMAT) + .addStatement("return queryBuilder") + .build()) + + .build()); return typeSpecs; } diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index f23df6e..8d34f99 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,6 +1,6 @@ -#Wed Oct 21 11:34:03 PDT 2015 +#Wed Apr 13 10:35:52 CEST 2016 distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-2.8-all.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-2.10-all.zip