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

Added generator for Table class for StorIOSQLiteProcessor. (Issue #694) #734

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 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
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,17 @@

public class DbOpenHelper extends SQLiteOpenHelper {

public DbOpenHelper(@NonNull Context context) {
super(context, "sample_db", null, 1);
public DbOpenHelper(@NonNull Context context, int version) {
super(context, "sample_db", null, version);
}

@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 @@ -3,7 +3,6 @@
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.Toast;
Expand All @@ -16,8 +15,6 @@
import java.util.ArrayList;
import java.util.List;

import static java.util.concurrent.TimeUnit.SECONDS;

public class MainActivity extends AppCompatActivity {

private StorIOSQLite storIOSQLite;
Expand All @@ -30,21 +27,18 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
storIOSQLite = DefaultStorIOSQLite.builder()
.sqliteOpenHelper(new DbOpenHelper(this))
.addTypeMapping(Tweet.class, new TweetSQLiteTypeMapping())
.build();

.sqliteOpenHelper(new DbOpenHelper(this, 1))
.addTypeMapping(Tweet.class, new TweetSQLiteTypeMapping())
.build();
tweetsAdapter = new TweetsAdapter();
recyclerView = (RecyclerView) findViewById(R.id.tweets_recycler_view);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setAdapter(tweetsAdapter);

addTweets();
}

void addTweets() {
final List<Tweet> tweets = new ArrayList<Tweet>();

tweets.add(Tweet.newTweet("artem_zin", "Checkout StorIO — modern API for SQLiteDatabase & ContentResolver"));
tweets.add(Tweet.newTweet("HackerNews", "It's revolution! Dolphins can write news on HackerNews with our new app!"));
tweets.add(Tweet.newTweet("AndroidDevReddit", "Awesome library — StorIO"));
Expand All @@ -54,26 +48,21 @@ void addTweets() {
tweets.add(Tweet.newTweet("ElonMusk", "Tesla Model S OTA update with Android Auto 7.2, fixes for memory leaks"));
tweets.add(Tweet.newTweet("AndroidWeekly", "Special issue #1: StorIO — forget about SQLiteDatabase, ContentResolver APIs, ORMs suck!"));
tweets.add(Tweet.newTweet("Apple", "Yosemite update: fixes for Wifi issues, yosemite-wifi-patch#142"));

// Looks/reads nice, isn't it?
try {
PutResults<Tweet> results = storIOSQLite
.put()
.objects(tweets)
.prepare()
.executeAsBlocking();

.put()
.objects(tweets)
.prepare()
.executeAsBlocking();
Toast.makeText(this, getResources().getQuantityString(R.plurals.tweets_inserted, results.results().size()), Toast.LENGTH_LONG).show();

List<Tweet> receivedTweets = storIOSQLite
.get()
.listOfObjects(Tweet.class)
.withQuery(TweetsTable.QUERY_ALL)
.prepare()
.executeAsBlocking();

.get()
.listOfObjects(Tweet.class)
.withQuery(TweetsQueries.QUERY_ALL)
.prepare()
.executeAsBlocking();
Toast.makeText(this, getResources().getQuantityString(R.plurals.tweets_loaded, receivedTweets.size()), Toast.LENGTH_LONG).show();

tweetsAdapter.setTweets(receivedTweets);
} catch (StorIOException e) {
Toast.makeText(this, R.string.tweets_add_error_toast, Toast.LENGTH_LONG).show();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,34 @@

import com.pushtorefresh.storio.sqlite.annotations.StorIOSQLiteColumn;
import com.pushtorefresh.storio.sqlite.annotations.StorIOSQLiteType;


// Just a class for demonstration, real Tweet structure is more complex.

// 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")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! But I not sure that everyone will need generated class, maybe add flag to this annotation? Something like @StorIOSQLiteType(table = "tweets", generateTableClass = false) which will be true by default?

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;

// This column is not used in app, but shows how update code is generated
@Nullable
@StorIOSQLiteColumn(name = "new_column", ignoreNull = true, version = 2)
String newColumn;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And add nullability please ;)


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

import android.support.annotation.NonNull;

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

// We suggest to store queries in separate class
// Because it makes code of the Entity itself cleaner and easier to read/understand/support
public class TweetsQueries {

// 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.tableName)
.build();

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

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import java.lang.annotation.Annotation;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

Expand Down Expand Up @@ -220,17 +221,12 @@ public boolean process(@Nullable final Set<? extends TypeElement> annotations, @
processAnnotatedExecutables(roundEnv, annotatedClasses);

validateAnnotatedClassesAndColumns(annotatedClasses);

final Generator<TypeMeta> putResolverGenerator = createPutResolver();
final Generator<TypeMeta> getResolverGenerator = createGetResolver();
final Generator<TypeMeta> deleteResolverGenerator = createDeleteResolver();
final Generator<TypeMeta> mappingGenerator = createMapping();
final List<Generator<TypeMeta>> generators = createGenerators();

for (TypeMeta typeMeta : annotatedClasses.values()) {
putResolverGenerator.generateJavaFile(typeMeta).writeTo(filer);
getResolverGenerator.generateJavaFile(typeMeta).writeTo(filer);
deleteResolverGenerator.generateJavaFile(typeMeta).writeTo(filer);
mappingGenerator.generateJavaFile(typeMeta).writeTo(filer);
for (Generator<TypeMeta> generator : generators) {
generator.generateJavaFile(typeMeta).writeTo(filer);
}
}
} catch (ProcessingException e) {
messager.printMessage(ERROR, e.getMessage(), e.element());
Expand Down Expand Up @@ -288,14 +284,5 @@ public boolean process(@Nullable final Set<? extends TypeElement> annotations, @
protected abstract Class<? extends Annotation> getCreatorAnnotationClass();

@NotNull
protected abstract Generator<TypeMeta> createPutResolver();

@NotNull
protected abstract Generator<TypeMeta> createGetResolver();

@NotNull
protected abstract Generator<TypeMeta> createDeleteResolver();

@NotNull
protected abstract Generator<TypeMeta> createMapping();
protected abstract List<Generator<TypeMeta>> createGenerators();
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,26 @@
import javax.lang.model.type.TypeMirror;

public enum JavaType {
BOOLEAN("INTEGER"),
BOOLEAN_OBJECT("INTEGER"),
SHORT("INTEGER"),
SHORT_OBJECT("INTEGER"),
INTEGER("INTEGER"),
INTEGER_OBJECT("INTEGER"),
LONG("INTEGER"),
LONG_OBJECT("INTEGER"),
FLOAT("REAL"),
FLOAT_OBJECT("REAL"),
DOUBLE("REAL"),
DOUBLE_OBJECT("REAL"),
STRING("TEXT"),
BYTE_ARRAY("BLOB");

BOOLEAN,
BOOLEAN_OBJECT,
SHORT,
SHORT_OBJECT,
INTEGER,
INTEGER_OBJECT,
LONG,
LONG_OBJECT,
FLOAT,
FLOAT_OBJECT,
DOUBLE,
DOUBLE_OBJECT,
STRING,
BYTE_ARRAY;
JavaType(@NotNull String sqlType) {
this.sqlType = sqlType;
}

private String sqlType;

@NotNull
public static JavaType from(@NotNull TypeMirror typeMirror) {
Expand Down Expand Up @@ -73,4 +78,9 @@ public boolean isBoxedType() {
return false;
}
}

@NotNull
public String getSqlType() {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and here please too =)

return sqlType;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import org.jetbrains.annotations.NotNull;

import java.lang.annotation.Annotation;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import javax.annotation.processing.RoundEnvironment;
Expand Down Expand Up @@ -57,9 +59,14 @@ protected Class<? extends Annotation> getCreatorAnnotationClass() {
return null;
}

@SuppressWarnings("unchecked")
@NotNull
@Override
protected Generator<StorIOTypeMeta> createPutResolver() {
protected List<Generator<StorIOTypeMeta>> createGenerators() {
return Arrays.asList(createPutResolver(), createGetResolver(), createDeleteResolver(), createMapping());
}

@SuppressWarnings("unchecked")
private Generator<StorIOTypeMeta> createPutResolver() {
Generator resolver = new Generator<StorIOTypeMeta>() {

@Override
Expand All @@ -73,8 +80,7 @@ public JavaFile generateJavaFile(StorIOTypeMeta storIOContentResolverTypeMeta) {
}

@SuppressWarnings("unchecked")
@Override
protected Generator<StorIOTypeMeta> createGetResolver() {
private Generator<StorIOTypeMeta> createGetResolver() {
Generator resolver = new Generator<StorIOTypeMeta>() {

@Override
Expand All @@ -88,8 +94,7 @@ public JavaFile generateJavaFile(StorIOTypeMeta storIOContentResolverTypeMeta) {
}

@SuppressWarnings("unchecked")
@Override
protected Generator<StorIOTypeMeta> createDeleteResolver() {
private Generator<StorIOTypeMeta> createDeleteResolver() {
Generator resolver = new Generator<StorIOTypeMeta>() {

@Override
Expand All @@ -103,8 +108,7 @@ public JavaFile generateJavaFile(StorIOTypeMeta storIOContentResolverTypeMeta) {
}

@SuppressWarnings("unchecked")
@Override
protected Generator<StorIOTypeMeta> createMapping() {
private Generator<StorIOTypeMeta> createMapping() {
Generator mapping = new Generator<StorIOTypeMeta>() {

@Override
Expand Down
Loading