jdbc优化
This commit is contained in:
@@ -2150,15 +2150,17 @@ public class DataJdbcSource extends AbstractDataSqlSource {
|
|||||||
try {
|
try {
|
||||||
String prepareSQL = info.getFindQuestionPrepareSQL(ids[0]);
|
String prepareSQL = info.getFindQuestionPrepareSQL(ids[0]);
|
||||||
PreparedStatement ps = conn.prepareStatement(prepareSQL);
|
PreparedStatement ps = conn.prepareStatement(prepareSQL);
|
||||||
|
DataJdbcResultSet rr = new DataJdbcResultSet(info);
|
||||||
for (Serializable pk : ids) {
|
for (Serializable pk : ids) {
|
||||||
ps.setObject(1, pk);
|
ps.setObject(1, pk);
|
||||||
|
ResultSet set = ps.executeQuery();
|
||||||
|
rr.resultSet(set);
|
||||||
|
if (set.next()) {
|
||||||
|
list.add(getEntityValue(info, null, rr));
|
||||||
|
} else {
|
||||||
|
list.add(null);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
ResultSet set = ps.executeQuery();
|
|
||||||
final DataResultSet rr = createDataResultSet(info, set);
|
|
||||||
while (set.next()) {
|
|
||||||
list.add(getEntityValue(info, null, rr));
|
|
||||||
}
|
|
||||||
set.close();
|
|
||||||
ps.close();
|
ps.close();
|
||||||
slowLog(s, prepareSQL);
|
slowLog(s, prepareSQL);
|
||||||
return list;
|
return list;
|
||||||
@@ -2553,120 +2555,132 @@ public class DataJdbcSource extends AbstractDataSqlSource {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static DataResultSet createDataResultSet(EntityInfo info, ResultSet set) {
|
public static DataResultSet createDataResultSet(EntityInfo info, ResultSet set) {
|
||||||
|
return new DataJdbcResultSet(info).resultSet(set);
|
||||||
|
}
|
||||||
|
|
||||||
final ResultSet rr = set;
|
protected static class DataJdbcResultSet implements DataResultSet {
|
||||||
|
|
||||||
return new DataResultSet() {
|
EntityInfo info;
|
||||||
|
|
||||||
@Override
|
ResultSet rr;
|
||||||
public <T> Serializable getObject(Attribute<T, Serializable> attr, int index, String column) {
|
|
||||||
Class t = attr.type();
|
|
||||||
|
|
||||||
if (t == java.util.Date.class) {
|
public DataJdbcResultSet(EntityInfo info) {
|
||||||
Object val = index > 0 ? getObject(index) : getObject(column);
|
this.info = info;
|
||||||
|
|
||||||
if (val
|
}
|
||||||
== null) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
return new java.util.Date(
|
|
||||||
((java.sql.Date) val).getTime());
|
|
||||||
} else if (t == java.time.LocalDate.class) {
|
|
||||||
Object val = index > 0 ? getObject(index) : getObject(column);
|
|
||||||
|
|
||||||
if (val
|
public DataJdbcResultSet resultSet(ResultSet set) {
|
||||||
== null) {
|
this.rr = set;
|
||||||
return null;
|
return this;
|
||||||
}
|
}
|
||||||
return ((java.sql.Date) val).toLocalDate();
|
|
||||||
} else if (t == java.time.LocalTime.class) {
|
|
||||||
Object val = index > 0 ? getObject(index) : getObject(column);
|
|
||||||
|
|
||||||
if (val
|
@Override
|
||||||
== null) {
|
public <T> Serializable getObject(Attribute<T, Serializable> attr, int index, String column) {
|
||||||
return null;
|
Class t = attr.type();
|
||||||
}
|
|
||||||
return ((java.sql.Time) val).toLocalTime();
|
|
||||||
} else if (t == java.time.LocalDateTime.class) {
|
|
||||||
Object val = index > 0 ? getObject(index) : getObject(column);
|
|
||||||
|
|
||||||
if (val
|
if (t == java.util.Date.class) {
|
||||||
== null) {
|
Object val = index > 0 ? getObject(index) : getObject(column);
|
||||||
return null;
|
|
||||||
}
|
if (val
|
||||||
return ((java.sql.Timestamp) val).toLocalDateTime();
|
== null) {
|
||||||
} else if (t.getName().startsWith("java.sql.")) {
|
return null;
|
||||||
return index > 0 ? (Serializable) getObject(index) : (Serializable) getObject(column);
|
|
||||||
}
|
}
|
||||||
return DataResultSet.getRowColumnValue(this, attr, index, column);
|
return new java.util.Date(
|
||||||
}
|
((java.sql.Date) val).getTime());
|
||||||
|
} else if (t == java.time.LocalDate.class) {
|
||||||
|
Object val = index > 0 ? getObject(index) : getObject(column);
|
||||||
|
|
||||||
@Override
|
if (val
|
||||||
public boolean next() {
|
== null) {
|
||||||
try {
|
return null;
|
||||||
return rr.next();
|
|
||||||
} catch (SQLException e) {
|
|
||||||
throw new SourceException(e);
|
|
||||||
}
|
}
|
||||||
}
|
return ((java.sql.Date) val).toLocalDate();
|
||||||
|
} else if (t == java.time.LocalTime.class) {
|
||||||
|
Object val = index > 0 ? getObject(index) : getObject(column);
|
||||||
|
|
||||||
@Override
|
if (val
|
||||||
public List<String> getColumnLabels() {
|
== null) {
|
||||||
try {
|
return null;
|
||||||
ResultSetMetaData meta = rr.getMetaData();
|
|
||||||
int count = meta.getColumnCount();
|
|
||||||
List<String> labels = new ArrayList<>(count);
|
|
||||||
for (int i = 1; i <= count; i++) {
|
|
||||||
labels.add(meta.getColumnLabel(i));
|
|
||||||
}
|
|
||||||
return labels;
|
|
||||||
} catch (SQLException e) {
|
|
||||||
throw new SourceException(e);
|
|
||||||
}
|
}
|
||||||
}
|
return ((java.sql.Time) val).toLocalTime();
|
||||||
|
} else if (t == java.time.LocalDateTime.class) {
|
||||||
|
Object val = index > 0 ? getObject(index) : getObject(column);
|
||||||
|
|
||||||
@Override
|
if (val
|
||||||
public boolean wasNull() {
|
== null) {
|
||||||
try {
|
return null;
|
||||||
return rr.wasNull();
|
|
||||||
} catch (SQLException e) {
|
|
||||||
throw new SourceException(e);
|
|
||||||
}
|
}
|
||||||
|
return ((java.sql.Timestamp) val).toLocalDateTime();
|
||||||
|
} else if (t.getName().startsWith("java.sql.")) {
|
||||||
|
return index > 0 ? (Serializable) getObject(index) : (Serializable) getObject(column);
|
||||||
}
|
}
|
||||||
|
return DataResultSet.getRowColumnValue(this, attr, index, column);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void close() {
|
public boolean next() {
|
||||||
try {
|
try {
|
||||||
rr.close();
|
return rr.next();
|
||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
throw new SourceException(e);
|
throw new SourceException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<String> getColumnLabels() {
|
||||||
|
try {
|
||||||
|
ResultSetMetaData meta = rr.getMetaData();
|
||||||
|
int count = meta.getColumnCount();
|
||||||
|
List<String> labels = new ArrayList<>(count);
|
||||||
|
for (int i = 1; i <= count; i++) {
|
||||||
|
labels.add(meta.getColumnLabel(i));
|
||||||
}
|
}
|
||||||
|
return labels;
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new SourceException(e);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Object getObject(int index) {
|
public boolean wasNull() {
|
||||||
try {
|
try {
|
||||||
return rr.getObject(index);
|
return rr.wasNull();
|
||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
throw new SourceException(e);
|
throw new SourceException(e);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Object getObject(String column) {
|
public void close() {
|
||||||
try {
|
try {
|
||||||
return rr.getObject(column);
|
rr.close();
|
||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
throw new SourceException(e);
|
throw new SourceException(e);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public EntityInfo getEntityInfo() {
|
public Object getObject(int index) {
|
||||||
return info;
|
try {
|
||||||
|
return rr.getObject(index);
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new SourceException(e);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
};
|
@Override
|
||||||
|
public Object getObject(String column) {
|
||||||
|
try {
|
||||||
|
return rr.getObject(column);
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new SourceException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public EntityInfo getEntityInfo() {
|
||||||
|
return info;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user