-
Notifications
You must be signed in to change notification settings - Fork 581
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
introduced Table annotation to handle custom table naming and removin…
…g the SugarRecord dependency
- Loading branch information
Satya Narayan
committed
Jul 16, 2014
1 parent
25588f3
commit 36649d5
Showing
7 changed files
with
215 additions
and
234 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,50 +1,59 @@ | ||
package com.orm; | ||
|
||
import com.orm.dsl.Column; | ||
import com.orm.dsl.Table; | ||
|
||
import java.lang.reflect.Field; | ||
|
||
public class StringUtil { | ||
public static String toSQLNameDefault(String javaNotation) { | ||
if(javaNotation.equalsIgnoreCase("_id")) | ||
return "_id"; | ||
|
||
StringBuilder sb = new StringBuilder(); | ||
char[] buf = javaNotation.toCharArray(); | ||
|
||
for (int i = 0; i < buf.length; i++) { | ||
char prevChar = (i > 0) ? buf[i - 1] : 0; | ||
char c = buf[i]; | ||
char nextChar = (i < buf.length - 1) ? buf[i + 1] : 0; | ||
boolean isFirstChar = (i == 0); | ||
|
||
if (isFirstChar || Character.isLowerCase(c) || Character.isDigit(c)) { | ||
sb.append(Character.toUpperCase(c)); | ||
} else if (Character.isUpperCase(c)) { | ||
if (Character.isLetterOrDigit(prevChar)) { | ||
if (Character.isLowerCase(prevChar)) { | ||
sb.append('_').append(Character.toUpperCase(c)); | ||
} else if (nextChar > 0 && Character.isLowerCase(nextChar)) { | ||
sb.append('_').append(Character.toUpperCase(c)); | ||
} else { | ||
sb.append(c); | ||
} | ||
} | ||
else { | ||
sb.append(c); | ||
} | ||
if (javaNotation.equalsIgnoreCase("_id")) | ||
return "_id"; | ||
|
||
StringBuilder sb = new StringBuilder(); | ||
char[] buf = javaNotation.toCharArray(); | ||
|
||
for (int i = 0; i < buf.length; i++) { | ||
char prevChar = (i > 0) ? buf[i - 1] : 0; | ||
char c = buf[i]; | ||
char nextChar = (i < buf.length - 1) ? buf[i + 1] : 0; | ||
boolean isFirstChar = (i == 0); | ||
|
||
if (isFirstChar || Character.isLowerCase(c) || Character.isDigit(c)) { | ||
sb.append(Character.toUpperCase(c)); | ||
} else if (Character.isUpperCase(c)) { | ||
if (Character.isLetterOrDigit(prevChar)) { | ||
if (Character.isLowerCase(prevChar)) { | ||
sb.append('_').append(Character.toUpperCase(c)); | ||
} else if (nextChar > 0 && Character.isLowerCase(nextChar)) { | ||
sb.append('_').append(Character.toUpperCase(c)); | ||
} else { | ||
sb.append(c); | ||
} | ||
} else { | ||
sb.append(c); | ||
} | ||
} | ||
} | ||
|
||
return sb.toString(); | ||
return sb.toString(); | ||
} | ||
|
||
public static String toSQLName(Field field){ | ||
if(field.isAnnotationPresent(Column.class)){ | ||
public static String toSQLName(Field field) { | ||
if (field.isAnnotationPresent(Column.class)) { | ||
Column annotation = field.getAnnotation(Column.class); | ||
return annotation.name(); | ||
} | ||
|
||
return toSQLNameDefault(field.getName()); | ||
} | ||
|
||
public static String toSQLName(Class<?> table) { | ||
|
||
if (table.isAnnotationPresent(Table.class)) { | ||
Table annotation = table.getAnnotation(Table.class); | ||
return annotation.name(); | ||
} | ||
return StringUtil.toSQLNameDefault(table.getSimpleName()); | ||
} | ||
} |
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
Oops, something went wrong.
is not wrapped around the sugar-1.3.jar library, I want to use it please help