Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Table generator #840

Merged
merged 2 commits into from
Oct 28, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import org.gradle.internal.jvm.Jvm
// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
ext.kotlin_version = '1.1.3-2'
ext.kotlin_version = '1.1.51'
repositories {
jcenter()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ public DbOpenHelper(@NonNull Context context) {

@Override
public void onCreate(@NonNull SQLiteDatabase db) {
db.execSQL(TweetsTable.getCreateTableQuery());
TweetTable.createTable(db);
}

@Override
public void onUpgrade(@NonNull SQLiteDatabase db, int oldVersion, int newVersion) {
// no impl
TweetTable.updateTable(db, oldVersion);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ void addTweets() {
List<Tweet> receivedTweets = storIOSQLite
.get()
.listOfObjects(Tweet.class)
.withQuery(TweetsTable.QUERY_ALL)
.withQuery(TweetQueries.QUERY_ALL)
.prepare()
.executeAsBlocking();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,22 @@
// This annotation will trigger annotation processor
// Which will generate type mapping code in compile time,
// You just need to link it in your code.
@StorIOSQLiteType(table = TweetsTable.TABLE)
@StorIOSQLiteType(table = "tweets")
public class Tweet {

/**
* If object was not inserted into db, id will be null
*/
@Nullable
@StorIOSQLiteColumn(name = TweetsTable.COLUMN_ID, key = true)
@StorIOSQLiteColumn(name = "_id", key = true)
Long id;

@NonNull
@StorIOSQLiteColumn(name = TweetsTable.COLUMN_AUTHOR)
@StorIOSQLiteColumn(name = "author")
String author;

@NonNull
@StorIOSQLiteColumn(name = TweetsTable.COLUMN_CONTENT)
@StorIOSQLiteColumn(name = "content")
String content;

// leave default constructor for AutoGenerated code!
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.pushtorefresh.storio2.basic_sample;

import android.support.annotation.NonNull;

import com.pushtorefresh.storio2.sqlite.queries.Query;

// We suggest to store table meta such as table name, columns names, queries, etc in separate class
// Because it makes code of the Entity itself cleaner and easier to read/understand/support
public class TweetQueries {

// This is just class with Meta Data, we don't need instances
private TweetQueries() {
throw new IllegalStateException("No instances please");
}

// Yep, with StorIO you can safely store queries as objects and reuse them, they are immutable
@NonNull
public static final Query QUERY_ALL = Query.builder()
.table(TweetTable.NAME)
.build();
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,17 @@ package com.pushtorefresh.storio2.common.annotations.processor

fun String.startsWithIs(): Boolean = this.startsWith("is") && this.length > 2
&& Character.isUpperCase(this[2])

fun String.toUpperSnakeCase(): String {
val builder = StringBuilder()

this.forEachIndexed { index, char ->
when {
char.isDigit() -> builder.append("_$char")
char.isUpperCase() -> if (index == 0) builder.append(char) else builder.append("_$char")
char.isLowerCase() -> builder.append(char.toUpperCase())
}
}

return builder.toString()
}
Original file line number Diff line number Diff line change
Expand Up @@ -221,12 +221,14 @@ abstract class StorIOAnnotationsProcessor<TypeMeta : StorIOTypeMeta<*, *>, out C
val getResolverGenerator = createGetResolver()
val deleteResolverGenerator = createDeleteResolver()
val mappingGenerator = createMapping()
val tableGenerator = createTableGenerator()

annotatedClasses.values.forEach {
putResolverGenerator.generateJavaFile(it).writeTo(filer)
getResolverGenerator.generateJavaFile(it).writeTo(filer)
deleteResolverGenerator.generateJavaFile(it).writeTo(filer)
mappingGenerator.generateJavaFile(it).writeTo(filer)
if (it.generateTableClass) tableGenerator?.generateJavaFile(it)?.writeTo(filer)
}
} catch (e: ProcessingException) {
messager.printMessage(ERROR, e.message, e.element)
Expand Down Expand Up @@ -290,4 +292,6 @@ abstract class StorIOAnnotationsProcessor<TypeMeta : StorIOTypeMeta<*, *>, out C
protected abstract fun createDeleteResolver(): Generator<TypeMeta>

protected abstract fun createMapping(): Generator<TypeMeta>

protected abstract fun createTableGenerator(): Generator<TypeMeta>?
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,22 @@ enum class JavaType {
}
}
}

val sqliteType: String
get() = when (this) {
BOOLEAN,
BOOLEAN_OBJECT,
SHORT,
SHORT_OBJECT,
INTEGER,
INTEGER_OBJECT,
LONG,
LONG_OBJECT -> "INTEGER"
FLOAT,
FLOAT_OBJECT,
DOUBLE,
DOUBLE_OBJECT -> "REAL"
STRING -> "TEXT"
BYTE_ARRAY -> "BLOB"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package com.pushtorefresh.storio2.common.annotations.processor.introspection

import javax.lang.model.element.ExecutableElement

open class StorIOTypeMeta<out TypeAnnotation : Annotation, ColumnMeta : StorIOColumnMeta<*>>
abstract class StorIOTypeMeta<out TypeAnnotation : Annotation, ColumnMeta : StorIOColumnMeta<*>>
@JvmOverloads constructor(
val simpleName: String,
val packageName: String,
Expand All @@ -22,7 +22,7 @@ open class StorIOTypeMeta<out TypeAnnotation : Annotation, ColumnMeta : StorIOCo
it.parameters.mapTo(params) { it.simpleName.toString() }
}
val orderedColumns = mutableListOf<ColumnMeta?>().apply {
(0..columns.size - 1).forEach { add(null) }
(0 until columns.size).forEach { add(null) }
}
columns.values.forEach { orderedColumns[params.indexOf(it.realElementName)] = it }
orderedColumns.map { it as ColumnMeta }
Expand All @@ -44,6 +44,8 @@ open class StorIOTypeMeta<out TypeAnnotation : Annotation, ColumnMeta : StorIOCo
return true
}

abstract val generateTableClass: Boolean

override fun hashCode(): Int {
var result = simpleName.hashCode()
result = 31 * result + packageName.hashCode()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,8 @@ class StorIOTestTypeMeta(simpleName: String,
simpleName,
packageName,
storIOType,
needCreator)
needCreator) {

override val generateTableClass: Boolean
get() = false
}
Original file line number Diff line number Diff line change
Expand Up @@ -259,4 +259,6 @@ open class StorIOContentResolverProcessor : StorIOAnnotationsProcessor<StorIOCon
override fun createDeleteResolver(): Generator<StorIOContentResolverTypeMeta> = DeleteResolverGenerator

override fun createMapping(): Generator<StorIOContentResolverTypeMeta> = MappingGenerator

override fun createTableGenerator(): Generator<StorIOContentResolverTypeMeta>? = null
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,8 @@ class StorIOContentResolverTypeMeta(
simpleName,
packageName,
storIOType,
needsCreator)
needsCreator) {

override val generateTableClass: Boolean
get() = false
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ public void onCreate(@NonNull SQLiteDatabase db) {
db.execSQL(TweetsTable.getCreateTableQuery());
db.execSQL(UsersTable.getCreateTableQuery());

db.execSQL(CarTable.getCreateTableQuery());
db.execSQL(PersonTable.getCreateTableQuery());
CarTable.createTable(db);
PersonTable.createTable(db);
db.execSQL(PersonCarRelationTable.getCreateTableQuery());

db.execSQL(Customer.CREATE_TABLE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
// This annotation will trigger annotation processor
// Which will generate type mapping code in compile time,
// You just need to link it in your code.
@StorIOSQLiteType(table = TweetsTable.TABLE)
@StorIOSQLiteType(table = TweetsTable.TABLE, generateTableClass = false)
@StorIOContentResolverType(uri = TweetMeta.URI_STRING)
public class Tweet {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
// This annotation will trigger annotation processor
// Which will generate type mapping code in compile time,
// You just need to link it in your code.
@StorIOSQLiteType(table = UsersTable.TABLE)
@StorIOSQLiteType(table = UsersTable.TABLE, generateTableClass = false)
public class User {

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
@NonNull
Subscription subscribeToPersonsAndCars() {
Set<String> tables = new HashSet<String>(3);
tables.add(PersonTable.TABLE);
tables.add(CarTable.TABLE);
tables.add(PersonTable.NAME);
tables.add(CarTable.NAME);
tables.add(PersonCarRelationTable.TABLE);
return Observable.merge(
storIOSQLite.observeChangesInTables(tables),
Expand All @@ -95,7 +95,7 @@ Subscription subscribeToPersonsAndCars() {
public List<Person> call(Changes changes) {
return storIOSQLite.get()
.listOfObjects(Person.class)
.withQuery(Query.builder().table(PersonTable.TABLE).build())
.withQuery(Query.builder().table(PersonTable.NAME).build())
.prepare()
.executeAsBlocking();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@

import java.util.List;

@StorIOSQLiteType(table = CarTable.TABLE)
@StorIOSQLiteType(table = "cars")
public class Car {

@StorIOSQLiteColumn(key = true, name = CarTable.COLUMN_ID)
@StorIOSQLiteColumn(key = true, name = "_car_id")
@Nullable
Long id;

@StorIOSQLiteColumn(name = CarTable.COLUMN_MODEL)
@StorIOSQLiteColumn(name = "model")
@NonNull
String model;

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@

import java.util.List;

@StorIOSQLiteType(table = PersonTable.TABLE)
@StorIOSQLiteType(table = "persons")
public class Person {

@StorIOSQLiteColumn(key = true, name = PersonTable.COLUMN_ID)
@StorIOSQLiteColumn(key = true, name = "_person_id")
@Nullable
Long id;

@StorIOSQLiteColumn(name = PersonTable.COLUMN_NAME)
@StorIOSQLiteColumn(name = "name")
@NonNull
String name;

Expand Down

This file was deleted.

Loading