greenrobot/greenDAO — pull request #100 by mdrabic (2013-10-11 01:30:14+00:00)
Title: Add support for the sqlite DEFAULT column constraint

Changes affecting the generated entityDao class

Methods were added to daogenerator.Property.PropertyBuilder in the form of
addDefaultValue( defaultValue) to enable support for adding a default
value to the property. daogenerator.Property was modified by adding a member
(sqlDefaultValue) that contains the default value in the format expected by Sqlite.
If this value is not null during initConstraint(), the value
"DEFAULT "+sqlDefaultValue is appened to daogenerator.Property#constraints.

Changes to the generated entity class

Support is enabled in .java for default values in the following ways:

1. When the Entity's class members are created in the Freemarker entity.ftl, for
each Property that has a default value, a corresponding 'default' member is generated:
[Format] private static default = Property#javaDefaultValue;
[Example] private static Short defaultMyShortValue = new Short("32767");

2. In the Entity's constructor, a null check is performed. If the parameter is
null, the property is assigned the default value.

Currently when the .java class is generated, the construct's parameters
contain either the primitive type (eg short), if notNull() is applied, or the
primitive wrapper (eg Short),if notNull() is *not* applied. Therefore, the
Property#addDefaultValue( defaultValue) methods support setting a default value
for all primitive wrapper classes, ie Boolean, Byte, Short, Integer, Long, Float,
and Double. String and Date are also support since they do not have a primitive
type, ie it can be null.

A byte[] maps to BLOB in sqlite and sqlite does support default values for the
BLOB affinity. However, this commit does not support default values for byte[].

3. Getters are generated for each default mainly as a convience.
Setters are not generated because if the default value for a column changes,
that is a schema change. This should result in the Developer updating their
Generation code and incrememting their Schema version. This would cause new
Entities to be generated with the new, correct default value.

Unit Test Updates
SimpleEntityDefaultValuesTest class was added to the DaoTest project.