-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix the generation issues when primary key is a custom type
When primary key is a custom type, the generated code for updateKeyAfterInsert and getKey will return String but the actual primary key is not. see #1051 for details
- Loading branch information
Showing
10 changed files
with
292 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
...t/src/androidTest/java/org/greenrobot/greendao/daotest/entity/CustomPkTypeEntityTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
tests/DaoTestBase/src/main/java/org/greenrobot/greendao/daotest/CustomPkTypeEntity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
130 changes: 130 additions & 0 deletions
130
tests/DaoTestBase/src/main/java/org/greenrobot/greendao/daotest/CustomPkTypeEntityDao.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
...s/DaoTestBase/src/main/java/org/greenrobot/greendao/daotest/customtype/UuidConverter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
Oops, something went wrong.