-
Notifications
You must be signed in to change notification settings - Fork 182
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
Added generator for Table class for StorIOSQLiteProcessor. (Issue #694) #734
Conversation
Since all data for creating such class is already in annotation processors META classes it might save some boilerplate to create such class automatically. It generates SQL code for creating table, for additive update (only adds columns) and stores table name and column names in easy to use way.
Codecov Report@@ Coverage Diff @@
## master #734 +/- ##
=======================================
Coverage 96.05% 96.05%
=======================================
Files 87 87
Lines 2459 2459
Branches 329 329
=======================================
Hits 2362 2362
Misses 68 68
Partials 29 29 Continue to review full report at Codecov.
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great work!
In few cases (updating, deleting fields, adding constrains) we have to (and we can) handle it manually and in all other it will be really helpful!
* | ||
* @return version of database on which column was added. | ||
*/ | ||
int version() default 0; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we start from 1
? Because it is the base value by docs
String content; | ||
|
||
// This column is not used in app, but shows how update code is generated | ||
@StorIOSQLiteColumn(name = "NewColumn", ignoreNull = true, version = 1) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe make name snake case like another fields?
String content; | ||
|
||
// This column is not used in app, but shows how update code is generated | ||
@StorIOSQLiteColumn(name = "NewColumn", ignoreNull = true, version = 1) | ||
String newColumn; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And add nullability please ;)
|
||
import com.pushtorefresh.storio.sqlite.queries.Query; | ||
|
||
// We suggest to store table meta such as table name, columns names, queries, etc in separate class |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
to store table meta such as table name,
DOUBLE_OBJECT, | ||
STRING, | ||
BYTE_ARRAY; | ||
JavaType(String sqlType) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
NotNull
import static com.pushtorefresh.storio.common.annotations.processor.generate.Common.INDENT; | ||
|
||
/** | ||
* Created by Pawel Bochenski on 11.12.2016. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We try to remove file headers
.build(); | ||
} | ||
|
||
private Iterable<FieldSpec> generateFields(String table, Collection<StorIOSQLiteColumnMeta> columns) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add please nullability for methods in this file)
.initializer("$S", table) | ||
.addModifiers(Modifier.PUBLIC, Modifier.STATIC, Modifier.FINAL) | ||
.build()); | ||
for (StorIOSQLiteColumnMeta m : columns) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
m -> meta or column?!
int i = 0; | ||
for (StorIOSQLiteColumnMeta entry : columns) { | ||
code += entry.storIOColumn.name() + " " + entry.javaType.getSqlType(); | ||
if (!entry.storIOColumn.ignoreNull()) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
=( ignoreNull
absence doesn't guarantee database value existence.
It's just prevent overriding db field of there is no value in java entity.
Can we check NotNull
, NonNull
annotations?
if (!entry.storIOColumn.ignoreNull()) { | ||
code += " NOT NULL"; | ||
} | ||
if (entry.storIOColumn.key()) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
Closes #694 |
Creating NOT NULL column now depends on field annotation (NonNull or NotNull)
Thanks, @pbochenski ! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Tests!!
|
||
import static com.pushtorefresh.storio.common.annotations.processor.generate.Common.INDENT; | ||
|
||
public class TableGenerator implements Generator<StorIOSQLiteTypeMeta> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This needs lots of tests :)
// 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") |
There was a problem hiding this comment.
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?
@geralt-encore can you please rebase this and merge? You're expert in StorIO annotation processing :) |
That will be tough since annotation processor was rewritten quite a lot since this PR... |
I'll try to convert it in the near future |
Closed via #840 |
Since all data for creating such class is already in annotation processors META classes it might save some boilerplate to create such class automatically.
It generates SQL code for creating table, for additive update (only adds columns) and stores table name and column names in easy to use way.