Skip to content

Commit cb2cd01

Browse files
committed
generated keys now work for any data type
1 parent 9247bce commit cb2cd01

File tree

6 files changed

+139
-147
lines changed

6 files changed

+139
-147
lines changed

.classpath

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,27 @@
1-
<?xml version="1.0" encoding="UTF-8"?>
2-
<classpath>
3-
<classpathentry kind="src" output="target/classes" path="src/main/java">
4-
<attributes>
5-
<attribute name="optional" value="true"/>
6-
<attribute name="maven.pomderived" value="true"/>
7-
</attributes>
8-
</classpathentry>
9-
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
10-
<attributes>
11-
<attribute name="optional" value="true"/>
12-
<attribute name="maven.pomderived" value="true"/>
13-
<attribute name="test" value="true"/>
14-
</attributes>
15-
</classpathentry>
16-
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8">
17-
<attributes>
18-
<attribute name="maven.pomderived" value="true"/>
19-
</attributes>
20-
</classpathentry>
21-
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
22-
<attributes>
23-
<attribute name="maven.pomderived" value="true"/>
24-
</attributes>
25-
</classpathentry>
26-
<classpathentry kind="output" path="target/classes"/>
27-
</classpath>
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<classpath>
3+
<classpathentry kind="src" output="target/classes" path="src/main/java">
4+
<attributes>
5+
<attribute name="optional" value="true"/>
6+
<attribute name="maven.pomderived" value="true"/>
7+
</attributes>
8+
</classpathentry>
9+
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
10+
<attributes>
11+
<attribute name="optional" value="true"/>
12+
<attribute name="maven.pomderived" value="true"/>
13+
<attribute name="test" value="true"/>
14+
</attributes>
15+
</classpathentry>
16+
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-11">
17+
<attributes>
18+
<attribute name="maven.pomderived" value="true"/>
19+
</attributes>
20+
</classpathentry>
21+
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
22+
<attributes>
23+
<attribute name="maven.pomderived" value="true"/>
24+
</attributes>
25+
</classpathentry>
26+
<classpathentry kind="output" path="target/classes"/>
27+
</classpath>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,8 @@
11
eclipse.preferences.version=1
2+
org.eclipse.jdt.core.compiler.codegen.targetPlatform=11
3+
org.eclipse.jdt.core.compiler.compliance=11
4+
org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled
25
org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
6+
org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=ignore
7+
org.eclipse.jdt.core.compiler.release=enabled
8+
org.eclipse.jdt.core.compiler.source=11

pom.xml

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<groupId>com.dieselpoint</groupId>
88
<artifactId>norm</artifactId>
99
<name>Norm</name>
10-
<version>0.8.7</version>
10+
<version>0.8.9</version>
1111
<packaging>jar</packaging>
1212

1313
<description>An extremely lightweight data access layer over JDBC</description>
@@ -62,8 +62,7 @@
6262
<artifactId>maven-compiler-plugin</artifactId>
6363
<version>3.8.0</version>
6464
<configuration>
65-
<source>1.8</source>
66-
<target>1.8</target>
65+
<release>11</release>
6766
</configuration>
6867
</plugin>
6968
<plugin>
@@ -85,7 +84,6 @@
8584
<artifactId>maven-javadoc-plugin</artifactId>
8685
<version>3.1.0</version>
8786
<configuration>
88-
<source>1.8</source>
8987
<javadocExecutable>${java.home}/bin/javadoc</javadocExecutable>
9088
<additionalparam>-Xdoclint:none</additionalparam>
9189
<additionalOptions>-Xdoclint:none</additionalOptions>

src/main/java/com/dieselpoint/norm/Database.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,11 @@ protected DataSource getDataSource() throws SQLException {
6969
addDataSourceProperty("password", password);
7070

7171
for (Map.Entry<String, String> entry : dataSourceProperties.entrySet()) {
72-
config.addDataSourceProperty(entry.getKey(), entry.getValue());
72+
String key = entry.getKey();
73+
String value = entry.getValue();
74+
if (value != null) {
75+
config.addDataSourceProperty(key, value);
76+
}
7377
}
7478

7579
/*

src/main/java/com/dieselpoint/norm/Query.java

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public class Query {
3333
private Object[] args;
3434

3535
private int rowsAffected;
36-
36+
3737
private ResultSetMetaData meta;
3838

3939
private Database db;
@@ -193,7 +193,8 @@ public <T> List<T> results(Class<T> clazz) {
193193
int colCount = meta.getColumnCount();
194194

195195
if (Util.isPrimitiveOrString(clazz) || clazz.getPackage().getName().startsWith("java.sql")) {
196-
// if the receiver class is a primitive or jdbc type just grab the first column and assign it
196+
// if the receiver class is a primitive or jdbc type just grab the first column
197+
// and assign it
197198
while (rs.next()) {
198199
Object colValue = rs.getObject(1);
199200
out.add((T) colValue);
@@ -250,13 +251,13 @@ private void close(AutoCloseable ac) {
250251
* specify the table, or you can specify the table with the .table() method.
251252
*/
252253
public Query insert(Object row) {
253-
254+
254255
if (this.generatedKeyReceiver == null) {
255256
PojoInfo pojoInfo = sqlMaker.getPojoInfo(row.getClass());
256257
Property prop = pojoInfo.getGeneratedColumnProperty();
257258
if (prop != null) {
258259
this.generatedKeyReceiver = row;
259-
this.generatedKeyNames = new String [] {prop.name};
260+
this.generatedKeyNames = new String[] { prop.name };
260261
}
261262
}
262263

@@ -403,25 +404,13 @@ private void populateGeneratedKeys(PreparedStatement state, Object generatedKeyR
403404
throw new DbException("Generated key name not found: " + generatedKeyName);
404405
}
405406

406-
// is it an int or a long?
407-
boolean isInt = prop.dataType.isAssignableFrom(int.class)
408-
|| prop.dataType.isAssignableFrom(Integer.class);
409-
410407
Object newKey;
411408
if (colCount == 1) {
412-
if (isInt) {
413-
newKey = rs.getInt(1);
414-
} else {
415-
newKey = rs.getLong(1);
416-
}
409+
newKey = rs.getObject(1, prop.dataType);
417410
} else {
418-
// colcount > 1, must do by name
419-
if (isInt) {
420-
newKey = rs.getInt(prop.name);
421-
} else {
422-
newKey = rs.getLong(prop.name);
423-
}
411+
newKey = rs.getObject(prop.name, prop.dataType);
424412
}
413+
425414
pojoInfo.putValue(generatedKeyReceiver, prop.name, newKey);
426415
}
427416
}
@@ -554,7 +543,7 @@ public String getWhere() {
554543
public String getTable() {
555544
return table;
556545
}
557-
546+
558547
public ResultSetMetaData getResultSetMetaData() {
559548
return meta;
560549
}

src/test/java/com/dieselpoint/norm/TestGeneratedId.java

Lines changed: 90 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -11,102 +11,97 @@
1111
import org.junit.Before;
1212
import org.junit.Test;
1313

14-
import com.dieselpoint.norm.Database;
15-
16-
1714
public class TestGeneratedId {
1815

1916
static String DB_DRIVER_CLASS_NAME = "org.postgresql.ds.PGSimpleDataSource";
20-
static String DB_SERVER_NAME = "localhost";
21-
static String DB_DATABASE = "testdb";
22-
static String DB_USERNAME = "postgres";
23-
static String DB_PASSWORD = "rootpassword";
24-
25-
private Database db;
26-
27-
@Before
28-
public void setUp() {
29-
System.setProperty("norm.dataSourceClassName", DB_DRIVER_CLASS_NAME);
30-
System.setProperty("norm.serverName", DB_SERVER_NAME);
31-
System.setProperty("norm.databaseName", DB_DATABASE);
32-
System.setProperty("norm.user", DB_USERNAME);
33-
System.setProperty("norm.password", DB_PASSWORD);
34-
35-
db = new Database();
36-
}
37-
38-
@Test
39-
public void testCreate() {
40-
NormPojo np = new NormPojo();
41-
np.setId("MyID");
42-
np.setName("My name");
43-
db.generatedKeyReceiver(np, "id").insert(np);
44-
Assert.assertNotNull(np.getDatabaseId());
45-
}
46-
47-
@Test
48-
public void testRetrieval() {
49-
List<NormPojo> npList = null;
50-
npList = db.results(NormPojo.class);
51-
Assert.assertNotNull(npList);
52-
Assert.assertTrue(npList.size() > 0);
53-
}
54-
55-
@Table(name = "pojo")
56-
public static class NormPojo {
57-
58-
/** Database record ID. */
59-
private Integer databaseId;
60-
61-
/** Unique identifier of the object. */
62-
private String id;
63-
64-
/** Human readable name. */
65-
private String name;
66-
67-
/**
68-
* @return the id
69-
*/
70-
@Column(name = "object_id", unique = true)
71-
public String getId() {
72-
return id;
73-
}
74-
75-
/**
76-
* @param id the id to set
77-
*/
78-
public void setId(String id) {
79-
this.id = id;
80-
}
81-
82-
/**
83-
* @return the name
84-
*/
85-
@Column(name = "name")
86-
public String getName() {
87-
return name;
88-
}
89-
90-
/**
91-
* @param name the name to set
92-
*/
93-
public void setName(String name) {
94-
this.name = name;
95-
}
96-
97-
@Id
98-
@GeneratedValue
99-
@Column(name = "id")
100-
public Integer getDatabaseId() {
101-
return databaseId;
102-
}
103-
104-
public void setDatabaseId(Integer databaseId) {
105-
this.databaseId = databaseId;
106-
}
107-
108-
}
109-
110-
111-
17+
static String DB_SERVER_NAME = "localhost";
18+
static String DB_DATABASE = "testdb";
19+
static String DB_USERNAME = "postgres";
20+
static String DB_PASSWORD = "rootpassword";
21+
22+
private Database db;
23+
24+
@Before
25+
public void setUp() {
26+
System.setProperty("norm.dataSourceClassName", DB_DRIVER_CLASS_NAME);
27+
System.setProperty("norm.serverName", DB_SERVER_NAME);
28+
System.setProperty("norm.databaseName", DB_DATABASE);
29+
System.setProperty("norm.user", DB_USERNAME);
30+
System.setProperty("norm.password", DB_PASSWORD);
31+
32+
db = new Database();
33+
}
34+
35+
@Test
36+
public void testCreate() {
37+
NormPojo np = new NormPojo();
38+
np.setId("MyID");
39+
np.setName("My name");
40+
db.generatedKeyReceiver(np, "id").insert(np);
41+
Assert.assertNotNull(np.getDatabaseId());
42+
}
43+
44+
@Test
45+
public void testRetrieval() {
46+
List<NormPojo> npList = null;
47+
npList = db.results(NormPojo.class);
48+
Assert.assertNotNull(npList);
49+
Assert.assertTrue(npList.size() > 0);
50+
}
51+
52+
@Table(name = "pojo")
53+
public static class NormPojo {
54+
55+
/** Database record ID. */
56+
private Integer databaseId;
57+
58+
/** Unique identifier of the object. */
59+
private String id;
60+
61+
/** Human readable name. */
62+
private String name;
63+
64+
/**
65+
* @return the id
66+
*/
67+
@Column(name = "object_id", unique = true)
68+
public String getId() {
69+
return id;
70+
}
71+
72+
/**
73+
* @param id the id to set
74+
*/
75+
public void setId(String id) {
76+
this.id = id;
77+
}
78+
79+
/**
80+
* @return the name
81+
*/
82+
@Column(name = "name")
83+
public String getName() {
84+
return name;
85+
}
86+
87+
/**
88+
* @param name the name to set
89+
*/
90+
public void setName(String name) {
91+
this.name = name;
92+
}
93+
94+
@Id
95+
@GeneratedValue
96+
@Column(name = "id")
97+
public Integer getDatabaseId() {
98+
return databaseId;
99+
}
100+
101+
public void setDatabaseId(Integer databaseId) {
102+
this.databaseId = databaseId;
103+
}
104+
105+
}
106+
112107
}

0 commit comments

Comments
 (0)