Skip to content

Commit

Permalink
Added the ability to specify the column name converter to use for SQL…
Browse files Browse the repository at this point in the history
… column names
  • Loading branch information
Wackymax committed May 4, 2017
1 parent 687a41d commit 491b155
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 34 deletions.
2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions CPOrm/CPOrm.iml
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/manifests" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/res" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/shaders" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/symbols" />
<excludeFolder url="file://$MODULE_DIR$/build/libs" />
<excludeFolder url="file://$MODULE_DIR$/build/outputs" />
<excludeFolder url="file://$MODULE_DIR$/build/reports" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import za.co.cporm.model.map.SqlColumnMapping;
import za.co.cporm.model.map.SqlColumnMappingFactory;
import za.co.cporm.model.util.ManifestHelper;
import za.co.cporm.model.util.NamingUtils;
import za.co.cporm.model.util.DefaultColumnNameConverter;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
Expand All @@ -36,7 +36,7 @@ public static TableDetails getTableDetails(Context context, Class<?> dataModelOb
Authority authority = dataModelObject.getAnnotation(Authority.class);
String authorityName = authority == null ? ManifestHelper.getAuthority(context) : authority.value();

String tableName = TextUtils.isEmpty(table.tableName()) ? NamingUtils.getSQLName(dataModelObject.getSimpleName()) : table.tableName();
String tableName = TextUtils.isEmpty(table.tableName()) ? TableDetails.COLUMN_NAME_CONVERTER.convertToSql(dataModelObject.getSimpleName()) : table.tableName();
TableDetails tableDetails = new TableDetails(tableName, authorityName, dataModelObject);
SqlColumnMappingFactory columnMappingFactory = ManifestHelper.getMappingFactory(context);

Expand All @@ -51,7 +51,7 @@ public static TableDetails getTableDetails(Context context, Class<?> dataModelOb
}

String columnName = column.columnName();
if(TextUtils.isEmpty(columnName)) columnName = NamingUtils.getSQLName(field.getName());
if(TextUtils.isEmpty(columnName)) columnName = TableDetails.COLUMN_NAME_CONVERTER.convertToSql(field.getName());
SqlColumnMapping columnMapping = columnMappingFactory.findColumnMapping(field);

tableDetails.addColumn(new TableDetails.ColumnDetails(columnName, field, columnMapping, field.isAnnotationPresent(PrimaryKey.class), field.isAnnotationPresent(Unique.class), column.required(), autoIncrement, column.notifyChanges()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import za.co.cporm.model.annotation.Index;
import za.co.cporm.model.annotation.TableConstraint;
import za.co.cporm.model.map.SqlColumnMapping;
import za.co.cporm.model.naming.ColumnNameConverter;
import za.co.cporm.model.util.DefaultColumnNameConverter;

import java.io.Serializable;
import java.lang.reflect.Constructor;
Expand All @@ -21,6 +23,8 @@
*/
public class TableDetails {

public static ColumnNameConverter COLUMN_NAME_CONVERTER = new DefaultColumnNameConverter();

private final String tableName;
private final String authority;
private final Class tableClass;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package za.co.cporm.model.naming;

/**
* Created by hennie.brink on 2017/05/04.
*/

public interface ColumnNameConverter {

String convertToSql(String fieldName);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package za.co.cporm.model.util;

import za.co.cporm.model.naming.ColumnNameConverter;

/**
* Contains common naming utilities to convert strings to SQL formats
*/
public class DefaultColumnNameConverter implements ColumnNameConverter {

/**
* Converts the source string to a SQL compatible string. It does this
* by checking for camelcase values, and applying a underscore '_' to whenever one is found. All
* characters are also converted to lower case.
*
* Example: If the provided string is "helloWorld" the resulting string will be "hello_world"
* @param original String to converted
* @return the converted string
*/
public static String getSQLName(String original){

StringBuilder sqlName = new StringBuilder();

for (char character : original.toCharArray()) {

if(Character.isUpperCase(character) && sqlName.length() > 0) sqlName.append("_");

sqlName.append(Character.toLowerCase(character));
}

return sqlName.toString();
}

/**
* Converts the source string to a SQL compatible string. It does this
* by checking for camelcase values, and applying a underscore '_' to whenever one is found. All
* characters are also converted to lower case.
*
* Example: If the provided string is "helloWorld" the resulting string will be "hello_world"
* @param fieldName String to convert
* @return the converted string
*/
@Override
public String convertToSql(String fieldName) {
StringBuilder sqlName = new StringBuilder();

for (char character : fieldName.toCharArray()) {

if(Character.isUpperCase(character) && sqlName.length() > 0) sqlName.append("_");

sqlName.append(Character.toLowerCase(character));
}

return sqlName.toString();
}
}
30 changes: 0 additions & 30 deletions CPOrm/src/main/java/za/co/cporm/model/util/NamingUtils.java

This file was deleted.

0 comments on commit 491b155

Please sign in to comment.