1010import java .lang .reflect .Method ;
1111import java .lang .reflect .Modifier ;
1212import java .sql .*;
13+ import java .text .MessageFormat ;
1314import java .util .*;
1415import java .util .concurrent .ConcurrentHashMap ;
1516
@@ -107,7 +108,7 @@ private synchronized <T> Map<String, PropertyInfo> determinePropertyInfo(Class<T
107108 try {
108109 st = connection .createStatement ();
109110 // gives us real column names with case.
110- String sql = new StringBuilder (). append ("SELECT * FROM " ). append ( sd ). append ( tableName ). append ( ed ). append ( " WHERE 1=0"). toString ();
111+ String sql = MessageFormat . format ("SELECT * FROM {0}{1}{2} WHERE 1=0" , sd , tableName , ed ); // todo this is repeated - put the string in a static final
111112 if (log .isDebugEnabled ()) {
112113 log .debug ("determineColumns: %s" , sql );
113114 }
@@ -132,7 +133,8 @@ private synchronized <T> Map<String, PropertyInfo> determinePropertyInfo(Class<T
132133 Collection <PropertyInfo > properties = getPropertyInfo (objectClass );
133134
134135 int columnCount = rsmd .getColumnCount ();
135- Map <String , PropertyInfo > columns = new TreeMap <>(String .CASE_INSENSITIVE_ORDER );
136+ //Map<String, PropertyInfo> columns = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
137+ Map <String , PropertyInfo > columns = new LinkedHashMap <>(columnCount );
136138 for (int j = 1 ; j <= columnCount ; j ++) {
137139 String realColumnName = rsmd .getColumnLabel (j );
138140 String columnName = realColumnName .toLowerCase ().replace ("_" , "" ).replace (" " , "" );
@@ -382,9 +384,9 @@ private static synchronized <T> Collection<PropertyInfo> determinePropertyInfo(C
382384 if (Modifier .isStatic (field .getModifiers ())) {
383385 continue ;
384386 }
385- log .debug ("Field Name: %s" , field .getName ());
387+ // log.debug("Field Name: %s", field.getName());
386388 String propertyName = field .getName ();
387- log .debug ("Property Name: *%s* " , propertyName );
389+ // log.debug("Property Name: *%s* ", propertyName);
388390
389391 PropertyInfo propertyInfo = new PropertyInfo ();
390392 propertyInfo .propertyName = propertyName ;
@@ -396,15 +398,15 @@ private static synchronized <T> Collection<PropertyInfo> determinePropertyInfo(C
396398
397399 for (Method method : methods ) {
398400 String propertyNameToTest = field .getName ().substring (0 , 1 ).toUpperCase () + field .getName ().substring (1 );
399- log .debug ("property name for testing %s" , propertyNameToTest );
401+ // log.debug("property name for testing %s", propertyNameToTest);
400402 if (propertyNameToTest .startsWith ("Is" ) && propertyNameToTest .length () > 2 && Character .isUpperCase (propertyNameToTest .charAt (2 ))) {
401403 propertyNameToTest = propertyName .substring (2 );
402404 }
403405
404406 String [] candidates = {"set" + propertyNameToTest , "get" + propertyNameToTest , "is" + propertyNameToTest , field .getName ()};
405407
406408 if (Arrays .asList (candidates ).contains (method .getName ())) {
407- log .debug (" METHOD: %s" , method .getName ());
409+ // log.debug(" METHOD: %s", method.getName());
408410
409411 annotations = method .getAnnotations ();
410412 for (Annotation annotation : annotations ) {
@@ -476,7 +478,7 @@ private void populateTableList(Connection con) throws PersismException {
476478 String getUpdateStatement (Object object , Connection connection ) throws PersismException , NoChangesDetectedForUpdateException {
477479
478480 if (object instanceof Persistable ) {
479- Map <String , PropertyInfo > changes = getChangedProperties ((Persistable ) object , connection );
481+ Map <String , PropertyInfo > changes = getChangedProperties ((Persistable <?> ) object , connection );
480482 if (changes .size () == 0 ) {
481483 throw new NoChangesDetectedForUpdateException ();
482484 }
@@ -549,8 +551,13 @@ private synchronized String determineInsertStatement(Object object, Connection c
549551
550552 Map <String , ColumnInfo > columns = getColumns (object .getClass (), connection );
551553 Map <String , PropertyInfo > properties = getTableColumnsPropertyInfo (object .getClass (), connection );
552- StringBuilder sb = new StringBuilder ();
553- sb .append ("INSERT INTO " ).append (sd ).append (tableName ).append (ed ).append (" (" );
554+
555+ StringBuilder sbi = new StringBuilder ();
556+ sbi .append ("INSERT INTO " ).append (sd ).append (tableName ).append (ed ).append (" (" );
557+
558+ StringBuilder sbp = new StringBuilder ();
559+ sbp .append (") VALUES (" );
560+
554561 String sep = "" ;
555562 boolean saveInMap = true ;
556563
@@ -568,31 +575,17 @@ private synchronized String determineInsertStatement(Object object, Connection c
568575 }
569576
570577 }
571- sb .append (sep ).append (sd ).append (column .columnName ).append (ed );
572- sep = ", " ;
573- }
574- }
575- sb .append (") VALUES (" );
576- sep = "" ;
577- for (ColumnInfo column : columns .values ()) {
578- if (!column .autoIncrement ) {
579578
580- if (column .hasDefault ) {
581- // Do not include if this column has a default and no value has been
582- // set on it's associated property.
583- if (properties .get (column .columnName ).getter .invoke (object ) == null ) {
584- continue ;
585- }
586- }
587-
588- sb .append (sep ).append (" ? " );
579+ sbi .append (sep ).append (sd ).append (column .columnName ).append (ed );
580+ sbp .append (sep ).append ("?" );
589581 sep = ", " ;
590582 }
591583 }
592- sb .append (") " );
584+
585+ sbi .append (sbp ).append (") " );
593586
594587 String insertStatement ;
595- insertStatement = sb .toString ();
588+ insertStatement = sbi .toString ();
596589
597590 if (log .isDebugEnabled ()) {
598591 log .debug ("determineInsertStatement for %s is %s" , object .getClass (), insertStatement );
@@ -733,10 +726,10 @@ private String buildUpdateString(Object object, Iterator<String> it, Connection
733726 return sb .toString ();
734727 }
735728
736- Map <String , PropertyInfo > getChangedProperties (Persistable persistable , Connection connection ) throws PersismException {
729+ Map <String , PropertyInfo > getChangedProperties (Persistable <?> persistable , Connection connection ) throws PersismException {
737730
738731 try {
739- Persistable original = (Persistable ) persistable .readOriginalValue ();
732+ Persistable <?> original = (Persistable <?> ) persistable .readOriginalValue ();
740733
741734 Map <String , PropertyInfo > columns = getTableColumnsPropertyInfo (persistable .getClass (), connection );
742735
0 commit comments