Skip to content

Commit

Permalink
fix the generation issue when primary key is a custom type
Browse files Browse the repository at this point in the history
fix for issue #1051
When primary key is a custom type, the generated code for updateKeyAfterInsert and getKey will return String but the actual primary key is not.
  • Loading branch information
passos authored and jinyuliu committed Aug 11, 2020
1 parent 0cce6c0 commit 87ebebd
Show file tree
Hide file tree
Showing 10 changed files with 292 additions and 3 deletions.
3 changes: 2 additions & 1 deletion DaoGenerator/src-template/dao.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,8 @@ as property>\"${property.dbName}\"<#if (index.propertiesOrder[property_index])??
<#if entity.pkProperty??>
return <#if !entity.pkProperty.notNull>cursor.isNull(offset + ${entity.pkProperty.ordinal}) ? null : </#if><#if
entity.pkProperty.propertyType == "Byte">(byte) </#if><#if
entity.pkProperty.propertyType == "Date">new java.util.Date(</#if>cursor.get${toCursorType[entity.pkProperty.propertyType]}(offset + ${entity.pkProperty.ordinal})<#if
entity.pkProperty.propertyType == "Date">new java.util.Date(</#if><#--
-->${entity.pkProperty.getEntityValueExpression("cursor.get${toCursorType[entity.pkProperty.propertyType]}(offset + ${entity.pkProperty.ordinal})")}<#if
entity.pkProperty.propertyType == "Boolean"> != 0</#if><#if
entity.pkProperty.propertyType == "Date">)</#if>;
<#else>
Expand Down
2 changes: 1 addition & 1 deletion DaoGenerator/src-template/entity.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ ${keepFields!} // KEEP FIELDS END
<#if entity.propertiesPk?has_content && entity.propertiesPk?size != entity.properties?size>

public ${entity.className}(<#list entity.propertiesPk as
property>${property.javaType} ${property.propertyName}<#if property_has_next>, </#if></#list>) {
property>${property.javaTypeInEntity} ${property.propertyName}<#if property_has_next>, </#if></#list>) {
<#list entity.propertiesPk as property>
this.${property.propertyName} = ${property.propertyName};
</#list>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -530,7 +530,9 @@ void init2ndPass() {

if (propertiesPk.size() == 1) {
pkProperty = propertiesPk.get(0);
pkType = schema.mapToJavaTypeNullable(pkProperty.getPropertyType());
pkType = pkProperty.getCustomTypeClassName() != null ?
pkProperty.getCustomTypeClassName() :
schema.mapToJavaTypeNullable(pkProperty.getPropertyType());
} else {
pkType = "Void";
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* Copyright (C) 2011-2016 Markus Junginger, greenrobot (http://greenrobot.org)
*
* This file is part of greenDAO Generator.
*
* greenDAO Generator is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* greenDAO Generator is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with greenDAO Generator. If not, see <http://www.gnu.org/licenses/>.
*/

package org.greenrobot.greendao.daotest.entity;

import org.greenrobot.greendao.daotest.CustomPkTypeEntity;
import org.greenrobot.greendao.daotest.CustomPkTypeEntityDao;
import org.greenrobot.greendao.test.AbstractDaoTestSinglePk;

import java.util.List;
import java.util.UUID;

public class CustomPkTypeEntityTest extends AbstractDaoTestSinglePk<CustomPkTypeEntityDao, CustomPkTypeEntity, UUID> {

public CustomPkTypeEntityTest() {
super(CustomPkTypeEntityDao.class);
}

@Override
protected CustomPkTypeEntity createEntity(UUID key) {
CustomPkTypeEntity entity = new CustomPkTypeEntity();
entity.setId(key);
return entity;
}

@Override
protected UUID createRandomPk() {
return UUID.randomUUID();
}

public void testCustomPkTypeValue() {
CustomPkTypeEntity entity = createEntityWithRandomPk();
UUID id = entity.getId();
dao.insert(entity);

List<CustomPkTypeEntity> all = dao.loadAll();
assertEquals(1, all.size());
assertEquals(id, all.get(0).getId());
}

public void testCustomPkTypeValueNull() {
CustomPkTypeEntity entity = createEntityWithRandomPk();
entity.setId(null);
dao.insert(entity);

List<CustomPkTypeEntity> all = dao.loadAll();
assertEquals(1, all.size());
assertNull(all.get(0).getId());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package org.greenrobot.greendao.daotest;

import org.greenrobot.greendao.annotation.*;

import java.util.UUID;

// THIS CODE IS GENERATED BY greenDAO, DO NOT EDIT. Enable "keep" sections if you want to edit.

/**
* Entity mapped to table "CUSTOM_PK_TYPE_ENTITY".
*/
@Entity
public class CustomPkTypeEntity {

@Id
@Convert(converter = org.greenrobot.greendao.daotest.customtype.UuidConverter.class, columnType = String.class)
private UUID id;
private Integer value;

@Generated
public CustomPkTypeEntity() {
}

public CustomPkTypeEntity(UUID id) {
this.id = id;
}

@Generated
public CustomPkTypeEntity(UUID id, Integer value) {
this.id = id;
this.value = value;
}

public UUID getId() {
return id;
}

public void setId(UUID id) {
this.id = id;
}

public Integer getValue() {
return value;
}

public void setValue(Integer value) {
this.value = value;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
package org.greenrobot.greendao.daotest;

import android.database.Cursor;
import android.database.sqlite.SQLiteStatement;

import org.greenrobot.greendao.AbstractDao;
import org.greenrobot.greendao.Property;
import org.greenrobot.greendao.internal.DaoConfig;
import org.greenrobot.greendao.database.Database;
import org.greenrobot.greendao.database.DatabaseStatement;

import java.util.UUID;
import org.greenrobot.greendao.daotest.customtype.UuidConverter;

// THIS CODE IS GENERATED BY greenDAO, DO NOT EDIT.
/**
* DAO for table "CUSTOM_PK_TYPE_ENTITY".
*/
public class CustomPkTypeEntityDao extends AbstractDao<CustomPkTypeEntity, UUID> {

public static final String TABLENAME = "CUSTOM_PK_TYPE_ENTITY";

/**
* Properties of entity CustomPkTypeEntity.<br/>
* Can be used for QueryBuilder and for referencing column names.
*/
public static class Properties {
public final static Property Id = new Property(0, String.class, "id", true, "ID");
public final static Property Value = new Property(1, Integer.class, "value", false, "VALUE");
}

private final UuidConverter idConverter = new UuidConverter();

public CustomPkTypeEntityDao(DaoConfig config) {
super(config);
}

public CustomPkTypeEntityDao(DaoConfig config, DaoSession daoSession) {
super(config, daoSession);
}

/** Creates the underlying database table. */
public static void createTable(Database db, boolean ifNotExists) {
String constraint = ifNotExists? "IF NOT EXISTS ": "";
db.execSQL("CREATE TABLE " + constraint + "\"CUSTOM_PK_TYPE_ENTITY\" (" + //
"\"ID\" TEXT PRIMARY KEY NOT NULL ," + // 0: id
"\"VALUE\" INTEGER);"); // 1: value
}

/** Drops the underlying database table. */
public static void dropTable(Database db, boolean ifExists) {
String sql = "DROP TABLE " + (ifExists ? "IF EXISTS " : "") + "\"CUSTOM_PK_TYPE_ENTITY\"";
db.execSQL(sql);
}

@Override
protected final void bindValues(DatabaseStatement stmt, CustomPkTypeEntity entity) {
stmt.clearBindings();

UUID id = entity.getId();
if (id != null) {
stmt.bindString(1, idConverter.convertToDatabaseValue(id));
}

Integer value = entity.getValue();
if (value != null) {
stmt.bindLong(2, value);
}
}

@Override
protected final void bindValues(SQLiteStatement stmt, CustomPkTypeEntity entity) {
stmt.clearBindings();

UUID id = entity.getId();
if (id != null) {
stmt.bindString(1, idConverter.convertToDatabaseValue(id));
}

Integer value = entity.getValue();
if (value != null) {
stmt.bindLong(2, value);
}
}

@Override
public UUID readKey(Cursor cursor, int offset) {
return cursor.isNull(offset + 0) ? null : idConverter.convertToEntityProperty(cursor.getString(offset + 0));
}

@Override
public CustomPkTypeEntity readEntity(Cursor cursor, int offset) {
CustomPkTypeEntity entity = new CustomPkTypeEntity( //
cursor.isNull(offset + 0) ? null : idConverter.convertToEntityProperty(cursor.getString(offset + 0)), // id
cursor.isNull(offset + 1) ? null : cursor.getInt(offset + 1) // value
);
return entity;
}

@Override
public void readEntity(Cursor cursor, CustomPkTypeEntity entity, int offset) {
entity.setId(cursor.isNull(offset + 0) ? null : idConverter.convertToEntityProperty(cursor.getString(offset + 0)));
entity.setValue(cursor.isNull(offset + 1) ? null : cursor.getInt(offset + 1));
}

@Override
protected final UUID updateKeyAfterInsert(CustomPkTypeEntity entity, long rowId) {
return entity.getId();
}

@Override
public UUID getKey(CustomPkTypeEntity entity) {
if(entity != null) {
return entity.getId();
} else {
return null;
}
}

@Override
public boolean hasKey(CustomPkTypeEntity entity) {
return entity.getId() != null;
}

@Override
protected final boolean isEntityUpdateable() {
return true;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public static void createAllTables(Database db, boolean ifNotExists) {
StringKeyValueEntityDao.createTable(db, ifNotExists);
AutoincrementEntityDao.createTable(db, ifNotExists);
CustomTypeEntityDao.createTable(db, ifNotExists);
CustomPkTypeEntityDao.createTable(db, ifNotExists);
IndexedStringEntityDao.createTable(db, ifNotExists);
}

Expand All @@ -58,6 +59,7 @@ public static void dropAllTables(Database db, boolean ifExists) {
StringKeyValueEntityDao.dropTable(db, ifExists);
AutoincrementEntityDao.dropTable(db, ifExists);
CustomTypeEntityDao.dropTable(db, ifExists);
CustomPkTypeEntityDao.dropTable(db, ifExists);
IndexedStringEntityDao.dropTable(db, ifExists);
}

Expand Down Expand Up @@ -94,6 +96,7 @@ public DaoMaster(Database db) {
registerDaoClass(AutoincrementEntityDao.class);
registerDaoClass(SqliteMasterDao.class);
registerDaoClass(CustomTypeEntityDao.class);
registerDaoClass(CustomPkTypeEntityDao.class);
registerDaoClass(IndexedStringEntityDao.class);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.greenrobot.greendao.daotest.AutoincrementEntity;
import org.greenrobot.greendao.daotest.SqliteMaster;
import org.greenrobot.greendao.daotest.CustomTypeEntity;
import org.greenrobot.greendao.daotest.CustomPkTypeEntity;
import org.greenrobot.greendao.daotest.IndexedStringEntity;

import org.greenrobot.greendao.daotest.SimpleEntityDao;
Expand All @@ -44,6 +45,7 @@
import org.greenrobot.greendao.daotest.AutoincrementEntityDao;
import org.greenrobot.greendao.daotest.SqliteMasterDao;
import org.greenrobot.greendao.daotest.CustomTypeEntityDao;
import org.greenrobot.greendao.daotest.CustomPkTypeEntityDao;
import org.greenrobot.greendao.daotest.IndexedStringEntityDao;

// THIS CODE IS GENERATED BY greenDAO, DO NOT EDIT.
Expand Down Expand Up @@ -72,6 +74,7 @@ public class DaoSession extends AbstractDaoSession {
private final DaoConfig autoincrementEntityDaoConfig;
private final DaoConfig sqliteMasterDaoConfig;
private final DaoConfig customTypeEntityDaoConfig;
private final DaoConfig customPkTypeEntityDaoConfig;
private final DaoConfig indexedStringEntityDaoConfig;

private final SimpleEntityDao simpleEntityDao;
Expand All @@ -91,6 +94,7 @@ public class DaoSession extends AbstractDaoSession {
private final AutoincrementEntityDao autoincrementEntityDao;
private final SqliteMasterDao sqliteMasterDao;
private final CustomTypeEntityDao customTypeEntityDao;
private final CustomPkTypeEntityDao customPkTypeEntityDao;
private final IndexedStringEntityDao indexedStringEntityDao;

public DaoSession(Database db, IdentityScopeType type, Map<Class<? extends AbstractDao<?, ?>>, DaoConfig>
Expand Down Expand Up @@ -148,6 +152,9 @@ public DaoSession(Database db, IdentityScopeType type, Map<Class<? extends Abstr
customTypeEntityDaoConfig = daoConfigMap.get(CustomTypeEntityDao.class).clone();
customTypeEntityDaoConfig.initIdentityScope(type);

customPkTypeEntityDaoConfig = daoConfigMap.get(CustomPkTypeEntityDao.class).clone();
customPkTypeEntityDaoConfig.initIdentityScope(type);

indexedStringEntityDaoConfig = daoConfigMap.get(IndexedStringEntityDao.class).clone();
indexedStringEntityDaoConfig.initIdentityScope(type);

Expand All @@ -168,6 +175,7 @@ public DaoSession(Database db, IdentityScopeType type, Map<Class<? extends Abstr
autoincrementEntityDao = new AutoincrementEntityDao(autoincrementEntityDaoConfig, this);
sqliteMasterDao = new SqliteMasterDao(sqliteMasterDaoConfig, this);
customTypeEntityDao = new CustomTypeEntityDao(customTypeEntityDaoConfig, this);
customPkTypeEntityDao = new CustomPkTypeEntityDao(customPkTypeEntityDaoConfig, this);
indexedStringEntityDao = new IndexedStringEntityDao(indexedStringEntityDaoConfig, this);

registerDao(SimpleEntity.class, simpleEntityDao);
Expand All @@ -187,6 +195,7 @@ public DaoSession(Database db, IdentityScopeType type, Map<Class<? extends Abstr
registerDao(AutoincrementEntity.class, autoincrementEntityDao);
registerDao(SqliteMaster.class, sqliteMasterDao);
registerDao(CustomTypeEntity.class, customTypeEntityDao);
registerDao(CustomPkTypeEntity.class, customPkTypeEntityDao);
registerDao(IndexedStringEntity.class, indexedStringEntityDao);
}

Expand All @@ -208,6 +217,7 @@ public void clear() {
autoincrementEntityDaoConfig.clearIdentityScope();
sqliteMasterDaoConfig.clearIdentityScope();
customTypeEntityDaoConfig.clearIdentityScope();
customPkTypeEntityDaoConfig.clearIdentityScope();
indexedStringEntityDaoConfig.clearIdentityScope();
}

Expand Down Expand Up @@ -279,6 +289,10 @@ public CustomTypeEntityDao getCustomTypeEntityDao() {
return customTypeEntityDao;
}

public CustomPkTypeEntityDao getCustomPkTypeEntityDao() {
return customPkTypeEntityDao;
}

public IndexedStringEntityDao getIndexedStringEntityDao() {
return indexedStringEntityDao;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package org.greenrobot.greendao.daotest.customtype;

import org.greenrobot.greendao.converter.PropertyConverter;

import java.util.UUID;

public class UuidConverter implements PropertyConverter<UUID, String> {
@Override
public UUID convertToEntityProperty(String databaseValue) {
return UUID.fromString(databaseValue);
}

@Override
public String convertToDatabaseValue(UUID entityProperty) {
return entityProperty.toString();
}
}
Loading

0 comments on commit 87ebebd

Please sign in to comment.