-
Notifications
You must be signed in to change notification settings - Fork 113
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add caches for mapped interface details and for current result set me…
…tadata
- Loading branch information
1 parent
620b829
commit ab60350
Showing
4 changed files
with
97 additions
and
58 deletions.
There are no files selected for viewing
42 changes: 42 additions & 0 deletions
42
src/main/java/com/github/davidmoten/rx/jdbc/AutoMapCache.java
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 |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package com.github.davidmoten.rx.jdbc; | ||
|
||
import java.lang.reflect.Method; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import com.github.davidmoten.rx.jdbc.Util.Col; | ||
import com.github.davidmoten.rx.jdbc.Util.IndexedCol; | ||
import com.github.davidmoten.rx.jdbc.Util.NamedCol; | ||
import com.github.davidmoten.rx.jdbc.annotations.Column; | ||
import com.github.davidmoten.rx.jdbc.annotations.Index; | ||
|
||
class AutoMapCache { | ||
final Map<String, Col> methodCols; | ||
public Class<?> cls; | ||
|
||
AutoMapCache(Class<?> cls) { | ||
this.cls = cls; | ||
this.methodCols = getMethodCols(cls); | ||
} | ||
|
||
private static Map<String, Col> getMethodCols(Class<?> cls) { | ||
Map<String, Col> methodCols = new HashMap<String, Col>(); | ||
for (Method method : cls.getMethods()) { | ||
String name = method.getName(); | ||
Column column = method.getAnnotation(Column.class); | ||
if (column != null) { | ||
//TODO check method has no params and has a mappable return type | ||
methodCols.put(name, new NamedCol(column.value(), method.getReturnType())); | ||
} else { | ||
Index index = method.getAnnotation(Index.class); | ||
if (index != null) { | ||
//TODO check method has no params and has a mappable return type | ||
methodCols.put(name, new IndexedCol(index.value(), method.getReturnType())); | ||
} | ||
} | ||
} | ||
return methodCols; | ||
} | ||
|
||
|
||
} |
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
27 changes: 20 additions & 7 deletions
27
src/main/java/com/github/davidmoten/rx/jdbc/ResultSetCache.java
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,20 +1,33 @@ | ||
package com.github.davidmoten.rx.jdbc; | ||
|
||
import java.sql.ResultSet; | ||
import java.sql.ResultSetMetaData; | ||
import java.sql.SQLException; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import com.github.davidmoten.rx.jdbc.Util.Col; | ||
|
||
class ResultSetCache { | ||
|
||
final ResultSet rs; | ||
final Map<String, Integer> colIndexes; | ||
final Map<String, Col> methodCols; | ||
|
||
ResultSetCache(ResultSet rs, Map<String, Integer> colIndexes, Map<String, Col> methodCols) { | ||
|
||
ResultSetCache(ResultSet rs) { | ||
this.rs = rs; | ||
this.colIndexes = colIndexes; | ||
this.methodCols = methodCols; | ||
|
||
this.colIndexes = collectColIndexes(rs); | ||
} | ||
|
||
private static Map<String, Integer> collectColIndexes(ResultSet rs) { | ||
HashMap<String, Integer> map = new HashMap<String, Integer>(); | ||
try { | ||
ResultSetMetaData metadata = rs.getMetaData(); | ||
for (int i=1;i<=metadata.getColumnCount();i++) { | ||
map.put(metadata.getColumnName(i),i); | ||
} | ||
return map; | ||
} catch (SQLException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
} |
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