Skip to content

Commit

Permalink
now handles multiple generated keys on insert
Browse files Browse the repository at this point in the history
  • Loading branch information
ccleve committed Nov 30, 2020
1 parent 7dfe902 commit 99be2fa
Show file tree
Hide file tree
Showing 10 changed files with 219 additions and 180 deletions.
21 changes: 14 additions & 7 deletions 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.10</version>
<version>1.0</version>
<packaging>jar</packaging>

<description>An extremely lightweight data access layer over JDBC</description>
Expand Down Expand Up @@ -138,34 +138,41 @@
<dependency>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
<version>3.4.1</version>
<version>3.4.5</version>
</dependency>

<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.17</version>
<version>8.0.22</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.2.8</version>
<version>42.2.18</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.4.199</version>
<version>1.4.200</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.xerial</groupId>
<artifactId>sqlite-jdbc</artifactId>
<version>3.28.0</version>
<version>3.32.3.2</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.apache.derby</groupId>
<artifactId>derby</artifactId>
<version>10.15.2.0</version>
<scope>test</scope>
</dependency>

Expand All @@ -179,7 +186,7 @@
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<version>4.13.1</version>
<scope>test</scope>
</dependency>

Expand Down
32 changes: 23 additions & 9 deletions src/main/java/com/dieselpoint/norm/Query.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import java.util.Map;

import com.dieselpoint.norm.sqlmakers.PojoInfo;
import com.dieselpoint.norm.sqlmakers.Property;
import com.dieselpoint.norm.sqlmakers.SqlMaker;

/**
Expand Down Expand Up @@ -257,10 +256,10 @@ public Query insert(Object row) {

if (this.generatedKeyReceiver == null) {
PojoInfo pojoInfo = sqlMaker.getPojoInfo(row.getClass());
Property prop = pojoInfo.getGeneratedColumnProperty();
if (prop != null) {
String[] names = pojoInfo.getGeneratedColumnNames();
if (names.length != 0) {
this.generatedKeyReceiver = row;
this.generatedKeyNames = new String[] { prop.name };
this.generatedKeyNames = names;
}
}

Expand Down Expand Up @@ -402,26 +401,41 @@ private void populateGeneratedKeys(PreparedStatement state, Object generatedKeyR
} else {

for (String generatedKeyName : generatedKeyNames) {

Object value;
if (colCount == 1) {
value = rs.getObject(1);
} else {
value = rs.getObject(generatedKeyName);
}
pojoInfo.putValue(generatedKeyReceiver, generatedKeyName, value);

/*-
Property prop = pojoInfo.getProperty(generatedKeyName);
if (prop == null) {
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 colValue = sqlMaker.convertValue(rs.getObject(i), meta.getColumnTypeName(i));
Object newKey;
if (colCount == 1) {
newKey = rs.getObject(1, type);
} else {
newKey = rs.getObject(prop.name, type);
}

pojoInfo.putValue(generatedKeyReceiver, prop.name, newKey);
*/
}
}
}
Expand Down
9 changes: 6 additions & 3 deletions src/main/java/com/dieselpoint/norm/sqlmakers/PojoInfo.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package com.dieselpoint.norm.sqlmakers;



public interface PojoInfo {

public Object getValue(Object pojo, String name);

public void putValue(Object pojo, String name, Object value);

public void putValue(Object pojo, String name, Object value, boolean ignoreIfMissing);
public Property getGeneratedColumnProperty();

public String[] getGeneratedColumnNames();

public Property getProperty(String name);

}
15 changes: 9 additions & 6 deletions src/main/java/com/dieselpoint/norm/sqlmakers/SqlMaker.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,28 @@

import com.dieselpoint.norm.Query;



public interface SqlMaker {

public String getInsertSql(Query query, Object row);

public Object[] getInsertArgs(Query query, Object row);

public String getUpdateSql(Query query, Object row);

public Object[] getUpdateArgs(Query query, Object row);

public String getDeleteSql(Query query, Object row);

public Object[] getDeleteArgs(Query query, Object row);

public String getUpsertSql(Query query, Object row);

public Object[] getUpsertArgs(Query query, Object row);

public String getSelectSql(Query query, Class<?> rowClass);

public String getCreateTableSql(Class<?> clazz);

public PojoInfo getPojoInfo(Class<?> rowClass);

public Object convertValue(Object value, String columnTypeName);
Expand Down
27 changes: 18 additions & 9 deletions src/main/java/com/dieselpoint/norm/sqlmakers/StandardPojoInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public class StandardPojoInfo implements PojoInfo {
public LinkedHashMap<String, Property> propertyMap = new LinkedHashMap<String, Property>();
public String table;
public String primaryKeyName;
public String generatedColumnName;
public String[] generatedColumnNames;

public String insertSql;
public int insertSqlArgCount;
Expand Down Expand Up @@ -158,6 +158,16 @@ private List<Property> populateProperties(Class<?> clazz)
props.add(prop);
}

List<String> genCols = new ArrayList<>();
for (Property prop : props) {
if (prop.isGenerated) {
genCols.add(prop.name);
}
}

this.generatedColumnNames = new String[genCols.size()];
genCols.toArray(this.generatedColumnNames);

return props;
}

Expand Down Expand Up @@ -185,7 +195,6 @@ private void applyAnnotations(Property prop, AnnotatedElement ae)
}

if (ae.getAnnotation(GeneratedValue.class) != null) {
generatedColumnName = prop.name;
prop.isGenerated = true;
}

Expand Down Expand Up @@ -234,10 +243,10 @@ public Object getValue(Object pojo, String name) {
if (value != null) {
if (prop.serializer != null) {
value = prop.serializer.serialize(value);

} else if (prop.converter != null) {
value = prop.converter.convertToDatabaseColumn(value);

} else if (prop.isEnumField) {
// handle enums according to selected enum type
if (prop.enumType == EnumType.ORDINAL) {
Expand Down Expand Up @@ -274,7 +283,7 @@ public void putValue(Object pojo, String name, Object value, boolean ignoreIfMis
if (value != null) {
if (prop.serializer != null) {
value = prop.serializer.deserialize((String) value, prop.dataType);

} else if (prop.converter != null) {
value = prop.converter.convertToEntityAttribute(value);

Expand Down Expand Up @@ -329,13 +338,13 @@ private <T extends Enum<T>> Object getEnumConst(Class<T> enumType, EnumType type
}

@Override
public Property getGeneratedColumnProperty() {
return propertyMap.get(generatedColumnName);
public Property getProperty(String name) {
return propertyMap.get(name);
}

@Override
public Property getProperty(String name) {
return propertyMap.get(name);
public String[] getGeneratedColumnNames() {
return this.generatedColumnNames;
}

}
Loading

0 comments on commit 99be2fa

Please sign in to comment.