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

add default value and pk autoincrement #349

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
9 changes: 9 additions & 0 deletions DaoGenerator/src-template/dao.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -135,10 +135,19 @@ as property>\"${property.columnName}\"<#if property_has_next>,</#if></#list>);")
}
</#if>
<#else> <#-- nullable, non-protobuff -->
<#if property.pkAutoincrement>
//PrimaryKey Autoincrement,so we do not care this property.
/** ${property.javaTypeInEntity} ${property.propertyName} = entity.get${property.propertyName?cap_first}();
if (${property.propertyName} != null) {
stmt.bind${toBindType[property.propertyType]}(${property_index + 1}, ${property.databaseValueExpression});
}
*/
<#else>
${property.javaTypeInEntity} ${property.propertyName} = entity.get${property.propertyName?cap_first}();
if (${property.propertyName} != null) {
stmt.bind${toBindType[property.propertyType]}(${property_index + 1}, ${property.databaseValueExpression});
}
</#if>
</#if>
</#list>
<#list entity.toOneRelations as toOne>
Expand Down
38 changes: 34 additions & 4 deletions DaoGenerator/src-template/entity.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -101,21 +101,48 @@ ${keepFields!} // KEEP FIELDS END
</#if>
<#if entity.constructors>
public ${entity.className}() {
<#list entity.properties as property>
<#if property.defValType>
this.${property.propertyName} = ${property.defValue};
</#if>
</#list>
}
<#if entity.propertiesPk?has_content && entity.propertiesPk?size != entity.properties?size>

<#if entity.propertiesPk?has_content && entity.propertiesPk?size == 1 && entity.propertiesPk[0].pkAutoincrement>
//pass one aleady constructor.
<#else>
public ${entity.className}(<#list entity.propertiesPk as
property>${property.javaType} ${property.propertyName}<#if property_has_next>, </#if></#list>) {
<#list entity.propertiesPk as property>
this.${property.propertyName} = ${property.propertyName};
</#list>
property><#if !property.pkAutoincrement>${property.javaType} ${property.propertyName}<#if property_has_next>, </#if></#if></#list>) {
this();
<#list entity.propertiesPk as property>
<#if !property.pkAutoincrement>this.${property.propertyName} = ${property.propertyName};</#if>
<#if property.defValType>
if(this.${property.propertyName}==null)this.${property.propertyName} = ${property.defValue};
</#if>
</#list>
}
</#if>
</#if>

<#if entity.propertiesPk?has_content && (entity.propertiesPk?size > 0) && entity.propertiesPk[0].pkAutoincrement>
public ${entity.className}(<#list entity.properties as
property><#if !property.pkAutoincrement>${property.javaTypeInEntity} ${property.propertyName}<#if property_has_next>, </#if></#if></#list>) {
<#list entity.properties as property>
<#if !property.pkAutoincrement>this.${property.propertyName} = ${property.propertyName};</#if>
<#if property.defValType>
if(this.${property.propertyName}==null)this.${property.propertyName} = ${property.defValue};
</#if>
</#list>
}
</#if>
public ${entity.className}(<#list entity.properties as
property>${property.javaTypeInEntity} ${property.propertyName}<#if property_has_next>, </#if></#list>) {
<#list entity.properties as property>
this.${property.propertyName} = ${property.propertyName};
<#if property.defValType>
if(this.${property.propertyName}==null)this.${property.propertyName} = ${property.defValue};
</#if>
</#list>
}
</#if>
Expand Down Expand Up @@ -153,6 +180,9 @@ ${property.javaDocSetter}
</#if>
public void set${property.propertyName?cap_first}(${property.javaTypeInEntity} ${property.propertyName}) {
this.${property.propertyName} = ${property.propertyName};
<#if property.defValType>
if(this.${property.propertyName}==null) this.${property.propertyName} = ${property.defValue};
</#if>
}

</#list>
Expand Down
20 changes: 18 additions & 2 deletions DaoGenerator/src/de/greenrobot/daogenerator/Property.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,12 @@ public static class PropertyBuilder {
public PropertyBuilder(Schema schema, Entity entity, PropertyType propertyType, String propertyName) {
property = new Property(schema, entity, propertyType, propertyName);
}

public PropertyBuilder defValue(String pVal)
{
property.defValue = pVal;
property.defValType = true;
return this;
}
public PropertyBuilder columnName(String columnName) {
property.columnName = columnName;
return this;
Expand Down Expand Up @@ -166,6 +171,8 @@ public Property getProperty() {
private final Entity entity;
private PropertyType propertyType;
private final String propertyName;
private boolean defValType;
private Object defValue;

private String columnName;
private String columnType;
Expand Down Expand Up @@ -199,6 +206,7 @@ public Property getProperty() {
private String javaType;

public Property(Schema schema, Entity entity, PropertyType propertyType, String propertyName) {
defValType = false;
this.schema = schema;
this.entity = entity;
this.propertyName = propertyName;
Expand Down Expand Up @@ -229,7 +237,7 @@ public boolean isPrimaryKey() {
return primaryKey;
}

public boolean isAutoincrement() {
public boolean isPkAutoincrement() {
return pkAutoincrement;
}

Expand All @@ -248,6 +256,14 @@ public boolean isNotNull() {
public String getJavaType() {
return javaType;
}
public String getDefValue()
{
return (String)defValue;
}
public boolean isDefValType()
{
return defValType;
}

public String getJavaTypeInEntity() {
if (customTypeClassName != null) {
Expand Down