Skip to content

Commit

Permalink
Fixed generated keys bug with primitives
Browse files Browse the repository at this point in the history
  • Loading branch information
ccleve committed Jun 14, 2020
1 parent 5166b04 commit 7dfe902
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 23 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<groupId>com.dieselpoint</groupId>
<artifactId>norm</artifactId>
<name>Norm</name>
<version>0.8.9</version>
<version>0.8.10</version>
<packaging>jar</packaging>

<description>An extremely lightweight data access layer over JDBC</description>
Expand Down
24 changes: 18 additions & 6 deletions src/main/java/com/dieselpoint/norm/Query.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.dieselpoint.norm;

import java.lang.reflect.InvocationTargetException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
Expand Down Expand Up @@ -137,7 +138,7 @@ private List<Map<String, Object>> resultsMap(Class<Map<String, Object>> clazz) {
int colCount = meta.getColumnCount();

while (rs.next()) {
Map<String, Object> map = clazz.newInstance();
Map<String, Object> map = clazz.getDeclaredConstructor().newInstance();

for (int i = 1; i <= colCount; i++) {
String colName = meta.getColumnLabel(i);
Expand All @@ -146,7 +147,8 @@ private List<Map<String, Object>> resultsMap(Class<Map<String, Object>> clazz) {
out.add(map);
}

} catch (InstantiationException | IllegalAccessException | SQLException | IllegalArgumentException e) {
} catch (InstantiationException | IllegalAccessException | SQLException | IllegalArgumentException
| InvocationTargetException | NoSuchMethodException | SecurityException e) {
throw new DbException(e);
} finally {
close(state);
Expand Down Expand Up @@ -203,7 +205,7 @@ public <T> List<T> results(Class<T> clazz) {
} else {
PojoInfo pojoInfo = sqlMaker.getPojoInfo(clazz);
while (rs.next()) {
T row = clazz.newInstance();
T row = clazz.getDeclaredConstructor().newInstance();

for (int i = 1; i <= colCount; i++) {
String colName = meta.getColumnLabel(i);
Expand All @@ -215,7 +217,8 @@ public <T> List<T> results(Class<T> clazz) {
}
}

} catch (InstantiationException | IllegalAccessException | SQLException e) {
} catch (InstantiationException | IllegalAccessException | SQLException | IllegalArgumentException
| InvocationTargetException | NoSuchMethodException | SecurityException e) {
DbException dbe = new DbException(e);
dbe.setSql(sql);
throw dbe;
Expand Down Expand Up @@ -404,11 +407,18 @@ private void populateGeneratedKeys(PreparedStatement state, Object generatedKeyR
throw new DbException("Generated key name not found: " + generatedKeyName);
}

/*
* getObject() below doesn't handle primitives correctly. Must convert to object
* equivalent.
*/

Class<?> type = Util.wrap(prop.dataType);

Object newKey;
if (colCount == 1) {
newKey = rs.getObject(1, prop.dataType);
newKey = rs.getObject(1, type);
} else {
newKey = rs.getObject(prop.name, prop.dataType);
newKey = rs.getObject(prop.name, type);
}

pojoInfo.putValue(generatedKeyReceiver, prop.name, newKey);
Expand All @@ -430,6 +440,8 @@ private void populateGeneratedKeys(PreparedStatement state, Object generatedKeyR

}

// similar to Guava's Primitives.wrap

/**
* Specify the object and its fields that should receive any column values that
* the database server generates during an insert or update. If a column is
Expand Down
57 changes: 41 additions & 16 deletions src/main/java/com/dieselpoint/norm/Util.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package com.dieselpoint.norm;

import java.util.Collection;
import java.util.List;

public class Util {

public static String join(String [] strs) {
public static String join(String[] strs) {
StringBuilder buf = new StringBuilder();
for (int i = 0; i < strs.length; i++) {
if (i > 0) {
Expand All @@ -15,12 +14,11 @@ public static String join(String [] strs) {
}
return buf.toString();
}



public static String join(Collection<String> strs) {
StringBuilder sb = new StringBuilder();
boolean first = true;
for (String col: strs) {
for (String col : strs) {
if (first) {
first = false;
} else {
Expand All @@ -30,7 +28,7 @@ public static String join(Collection<String> strs) {
}
return sb.toString();
}

public static String getQuestionMarks(int count) {
StringBuilder sb = new StringBuilder(count * 2);
for (int i = 0; i < count; i++) {
Expand All @@ -41,22 +39,49 @@ public static String getQuestionMarks(int count) {
}
return sb.toString();
}

public static boolean isPrimitiveOrString(Class<?> c) {
if (c.isPrimitive()) {
return true;
} else if (c == Byte.class
|| c == Short.class
|| c == Integer.class
|| c == Long.class
|| c == Float.class
|| c == Double.class
|| c == Boolean.class
|| c == Character.class
|| c == String.class) {
} else if (c == Byte.class || c == Short.class || c == Integer.class || c == Long.class || c == Float.class
|| c == Double.class || c == Boolean.class || c == Character.class || c == String.class) {
return true;
} else {
return false;
}
}

public static Class<?> wrap(Class<?> type) {
if (!type.isPrimitive()) {
return type;
}
if (type == int.class) {
return Integer.class;
}
if (type == long.class) {
return Long.class;
}
if (type == boolean.class) {
return Boolean.class;
}
if (type == byte.class) {
return Byte.class;
}
if (type == char.class) {
return Character.class;
}
if (type == double.class) {
return Double.class;
}
if (type == float.class) {
return Float.class;
}
if (type == short.class) {
return Short.class;
}
if (type == void.class) {
return Void.class;
}
throw new RuntimeException("Will never get here");
}
}

0 comments on commit 7dfe902

Please sign in to comment.