提交 96601cb8 authored 作者: noelgrandin's avatar noelgrandin

Optimise the case where we are accessing the ResultSet column values by name.

Patch by Carl Hasselskog (carl@degoo.com)
上级 7498557c
...@@ -22,6 +22,8 @@ import java.sql.SQLException; ...@@ -22,6 +22,8 @@ import java.sql.SQLException;
import java.sql.Statement; import java.sql.Statement;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Calendar; import java.util.Calendar;
import java.util.HashMap;
import org.h2.command.CommandInterface; import org.h2.command.CommandInterface;
import org.h2.constant.ErrorCode; import org.h2.constant.ErrorCode;
import org.h2.expression.ParameterInterface; import org.h2.expression.ParameterInterface;
...@@ -62,6 +64,7 @@ public class JdbcPreparedStatement extends JdbcStatement implements PreparedStat ...@@ -62,6 +64,7 @@ public class JdbcPreparedStatement extends JdbcStatement implements PreparedStat
protected CommandInterface command; protected CommandInterface command;
private final String sqlStatement; private final String sqlStatement;
private ArrayList<Value[]> batchParameters; private ArrayList<Value[]> batchParameters;
private HashMap<String, Integer> cachedColumnLabelMap = null;
JdbcPreparedStatement(JdbcConnection conn, String sql, int id, int resultSetType, JdbcPreparedStatement(JdbcConnection conn, String sql, int id, int resultSetType,
int resultSetConcurrency, boolean closeWithResultSet) { int resultSetConcurrency, boolean closeWithResultSet) {
...@@ -71,6 +74,14 @@ public class JdbcPreparedStatement extends JdbcStatement implements PreparedStat ...@@ -71,6 +74,14 @@ public class JdbcPreparedStatement extends JdbcStatement implements PreparedStat
command = conn.prepareCommand(sql, fetchSize); command = conn.prepareCommand(sql, fetchSize);
} }
/**
* Looking up the column index can sometimes show up on the performance profile,
* so cache the map.
*/
void setCachedColumnLabelMap( HashMap<String, Integer> cachedColumnLabelMap) {
this.cachedColumnLabelMap = cachedColumnLabelMap;
}
/** /**
* Executes a query (select statement) and returns the result set. If * Executes a query (select statement) and returns the result set. If
* another result set exists for this statement, this will be closed (even * another result set exists for this statement, this will be closed (even
...@@ -97,7 +108,7 @@ public class JdbcPreparedStatement extends JdbcStatement implements PreparedStat ...@@ -97,7 +108,7 @@ public class JdbcPreparedStatement extends JdbcStatement implements PreparedStat
} finally { } finally {
setExecutingStatement(null); setExecutingStatement(null);
} }
resultSet = new JdbcResultSet(conn, this, result, id, closedByResultSet, scrollable, updatable); resultSet = new JdbcResultSet(conn, this, result, id, closedByResultSet, scrollable, updatable, cachedColumnLabelMap);
} }
return resultSet; return resultSet;
} catch (Exception e) { } catch (Exception e) {
......
...@@ -91,6 +91,7 @@ public class JdbcResultSet extends TraceObject implements ResultSet { ...@@ -91,6 +91,7 @@ public class JdbcResultSet extends TraceObject implements ResultSet {
private Value[] updateRow; private Value[] updateRow;
private HashMap<String, Integer> columnLabelMap; private HashMap<String, Integer> columnLabelMap;
private HashMap<Integer, Value[]> patchedRows; private HashMap<Integer, Value[]> patchedRows;
private JdbcPreparedStatement preparedStatement = null;
JdbcResultSet(JdbcConnection conn, JdbcStatement stat, ResultInterface result, int id, JdbcResultSet(JdbcConnection conn, JdbcStatement stat, ResultInterface result, int id,
boolean closeStatement, boolean scrollable, boolean updatable) { boolean closeStatement, boolean scrollable, boolean updatable) {
...@@ -104,6 +105,16 @@ public class JdbcResultSet extends TraceObject implements ResultSet { ...@@ -104,6 +105,16 @@ public class JdbcResultSet extends TraceObject implements ResultSet {
this.updatable = updatable; this.updatable = updatable;
} }
/**
* Overload used to explicitly set the column-mapping.
*/
JdbcResultSet(JdbcConnection conn, JdbcPreparedStatement preparedStatement, ResultInterface result, int id,
boolean closeStatement, boolean scrollable, boolean updatable, HashMap<String, Integer> columnLabelMap) {
this(conn, preparedStatement, result, id, closeStatement, scrollable, updatable);
this.columnLabelMap = columnLabelMap;
this.preparedStatement = preparedStatement;
}
/** /**
* Moves the cursor to the next row of the result set. * Moves the cursor to the next row of the result set.
* *
...@@ -2876,6 +2887,9 @@ public class JdbcResultSet extends TraceObject implements ResultSet { ...@@ -2876,6 +2887,9 @@ public class JdbcResultSet extends TraceObject implements ResultSet {
} }
// assign at the end so concurrent access is supported // assign at the end so concurrent access is supported
columnLabelMap = map; columnLabelMap = map;
if (preparedStatement != null) {
preparedStatement.setCachedColumnLabelMap(columnLabelMap);
}
} }
Integer index = columnLabelMap.get(StringUtils.toUpperEnglish(columnLabel)); Integer index = columnLabelMap.get(StringUtils.toUpperEnglish(columnLabel));
if (index == null) { if (index == null) {
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论