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

Added useCamelCaseNameWithoutInsertions #394

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
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,16 @@ public void testMinimalSchema() throws Exception {

@Test
public void testDbName() {
assertEquals("NORMAL", DaoUtil.dbName("normal"));
assertEquals("NORMAL", DaoUtil.dbName("Normal"));
assertEquals("CAMEL_CASE", DaoUtil.dbName("CamelCase"));
assertEquals("CAMEL_CASE_THREE", DaoUtil.dbName("CamelCaseThree"));
assertEquals("CAMEL_CASE_XXXX", DaoUtil.dbName("CamelCaseXXXX"));
assertEquals("NORMAL", DaoUtil.dbName("normal", false));
assertEquals("NORMAL", DaoUtil.dbName("Normal", false));
assertEquals("CAMEL_CASE", DaoUtil.dbName("CamelCase", false));
assertEquals("CAMEL_CASE_THREE", DaoUtil.dbName("CamelCaseThree", false));
assertEquals("CAMEL_CASE_XXXX", DaoUtil.dbName("CamelCaseXXXX", false));
assertEquals("NORMAL", DaoUtil.dbName("normal", true));
assertEquals("NORMAL", DaoUtil.dbName("Normal", true));
assertEquals("CAMELCASE", DaoUtil.dbName("CamelCase", true));
assertEquals("CAMELCASETHREE", DaoUtil.dbName("CamelCaseThree", true));
assertEquals("CAMELCASEXXXX", DaoUtil.dbName("CamelCaseXXXX", true));
}

@Test(expected = RuntimeException.class)
Expand Down
16 changes: 9 additions & 7 deletions DaoGenerator/src/org/greenrobot/greendao/generator/DaoUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,16 @@

/** Internal API */
public class DaoUtil {
public static String dbName(String javaName) {
public static String dbName(String javaName, boolean useCamelCaseNameWithoutInsertions) {
StringBuilder builder = new StringBuilder(javaName);
for (int i = 1; i < builder.length(); i++) {
boolean lastWasUpper = Character.isUpperCase(builder.charAt(i - 1));
boolean isUpper = Character.isUpperCase(builder.charAt(i));
if (isUpper && !lastWasUpper) {
builder.insert(i, '_');
i++;
if(!useCamelCaseNameWithoutInsertions) {
for (int i = 1; i < builder.length(); i++) {
boolean lastWasUpper = Character.isUpperCase(builder.charAt(i - 1));
boolean isUpper = Character.isUpperCase(builder.charAt(i));
if (isUpper && !lastWasUpper) {
builder.insert(i, '_');
i++;
}
}
}
return builder.toString().toUpperCase();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -558,7 +558,7 @@ void init2ndPass() {

protected void init2ndPassNamesWithDefaults() {
if (tableName == null) {
tableName = DaoUtil.dbName(className);
tableName = DaoUtil.dbName(className, schema.isUseCamelCaseNameWithoutInsertions());
nonDefaultTableName = false;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ void init2ndPass() {
columnType = schema.mapToDbType(propertyType);
}
if (columnName == null) {
columnName = DaoUtil.dbName(propertyName);
columnName = DaoUtil.dbName(propertyName, schema.isUseCamelCaseNameWithoutInsertions());
nonDefaultColumnName = false;
} else if (primaryKey && propertyType == PropertyType.Long && columnName.equals("_id")) {
nonDefaultColumnName = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public class Schema {
private Map<PropertyType, String> propertyToJavaTypeNullable;
private boolean hasKeepSectionsByDefault;
private boolean useActiveEntitiesByDefault;
private boolean useCamelCaseNameWithoutInsertions;
private final String name;
private final String prefix;

Expand All @@ -66,6 +67,8 @@ public void enableActiveEntitiesByDefault() {
useActiveEntitiesByDefault = true;
}

public void enableCamelCaseNameWithoutInsertions() { useCamelCaseNameWithoutInsertions = true; }

private void initTypeMappings() {
propertyToDbType = new HashMap<>();
propertyToDbType.put(PropertyType.Boolean, "INTEGER");
Expand Down Expand Up @@ -180,6 +183,8 @@ public boolean isUseActiveEntitiesByDefault() {
return useActiveEntitiesByDefault;
}

public boolean isUseCamelCaseNameWithoutInsertions() { return useCamelCaseNameWithoutInsertions; }

public String getName() {
return name;
}
Expand Down