H2数据库查询源码实现
设置一个查询入口
ResultSet result = stat.executeQuery("select id,name,sex,degree from userInfo where name like '%Hello World%'");
H2中的JdbcStatement实现了Statement
@Override
public ResultSet executeQuery(String sql) throws SQLException {
try {
int id = getNextId(TraceObject.RESULT_SET);
if (isDebugEnabled()) {
debugCodeAssign("ResultSet", TraceObject.RESULT_SET, id,
"executeQuery(" + quote(sql) + ")");
}
synchronized (session) {
checkClosed();
closeOldResultSet();
sql = JdbcConnection.translateSQL(sql, escapeProcessing);
CommandInterface command = conn.prepareCommand(sql, fetchSize);
ResultInterface result;
boolean lazy = false;
boolean scrollable = resultSetType != ResultSet.TYPE_FORWARD_ONLY;
boolean updatable = resultSetConcurrency == ResultSet.CONCUR_UPDATABLE;
setExecutingStatement(command);
try {
result = command.executeQuery(maxRows, scrollable);
lazy = result.isLazy();
} finally {
if (!lazy) {
setExecutingStatement(null);
}
}
if (!lazy) {
command.close();
}
resultSet = new JdbcResultSet(conn, this, command, result, id,
closedByResultSet, scrollable, updatable);
}
return resultSet;
} catch (Exception e) {
throw logAndConvert(e);
}
}
调用了Command中的executeQuery方法
@Override
public ResultInterface executeQuery(int maxrows, boolean scrollable) {
startTimeNanos = 0;
long start = 0;
Database database = session.getDatabase();
Object sync = database.isMultiThreaded() ? (Object) session : (Object) database;
session.waitIfExclusiveModeEnabled();
boolean callStop = true;
boolean writing = !isReadOnly();
if (writing) {
while (!database.beforeWriting()) {
// wait
}
}
synchronized (sync) {
session.setCurrentCommand(this);
try {
while (true) {
database.checkPowerOff();
try {
//返回查询结果
ResultInterface result = query(maxrows);
callStop = !result.isLazy();
return result;
} catch (DbException e) {
start = filterConcurrentUpdate(e, start);
} catch (OutOfMemoryError e) {
callStop = false;
// there is a serious problem:
// the transaction may be applied partially
// in this case we need to panic:
// close the database
database.shutdownImmediately();
throw DbException.convert(e);
} catch (Throwable e) {
throw DbException.convert(e);
}
}
} catch (DbException e) {
e = e.addSQL(sql);
SQLException s = e.getSQLException();
database.exceptionThrown(s, sql);
if (s.getErrorCode() == ErrorCode.OUT_OF_MEMORY) {
callStop = false;
database.shutdownImmediately();
throw e;
}
database.checkPowerOff();
throw e;
} finally {
if (callStop) {
stop();
}
if (writing) {
database.afterWriting();
}
}
}
}
调用CommandContainer中的query方法
```java
@Override
public ResultInterface query(int maxrows) {
recompileIfRequired();
setProgress(DatabaseEventListener.STATE_STATEMENT_START);
start();
prepared.checkParameters();
//没有查询缓存 查询
ResultInterface result = prepared.query(maxrows);
prepared.trace(startTimeNanos, result.isLazy() ? 0 : result.getRowCount());
setProgress(DatabaseEventListener.STATE_STATEMENT_END);
return result;
}
Query中的query方法
public final ResultInterface query(int limit, ResultTarget target) {
if (isUnion()) {
// union doesn't always know the parameter list of the left and
// right queries
return queryWithoutCacheLazyCheck(limit, target);
}
fireBeforeSelectTriggers();
if (noCache || !session.getDatabase().getOptimizeReuseResults() ||
session.isLazyQueryExecution()) {
return queryWithoutCacheLazyCheck(limit, target);
}
Value[] params = getParameterValues();
long now = session.getDatabase().getModificationDataId();
if (isEverything(ExpressionVisitor.DETERMINISTIC_VISITOR)) {
if (lastResult != null && !lastResult.isClosed() &&
limit == lastLimit) {
if (sameResultAsLast(session, params, lastParameters,
lastEvaluated)) {
lastResult = lastResult.createShallowCopy(session);
if (lastResult != null) {
lastResult.reset();
return lastResult;
}
}
}
}
lastParameters = params;
closeLastResult();
//调用queryWithoutCacheLazyCheck方法
ResultInterface r = queryWithoutCacheLazyCheck(limit, target);
lastResult = r;
this.lastEvaluated = now;
lastLimit = limit;
return r;
}
Select 中的queryWithoutCache方法
@Override
protected ResultInterface queryWithoutCache(int maxRows, ResultTarget target) {
int limitRows = maxRows == 0 ? -1 : maxRows;
if (limitExpr != null) {
Value v = limitExpr.getValue(session);
int l = v == ValueNull.INSTANCE ? -1 : v.getInt();
if (limitRows < 0) {
limitRows = l;
} else if (l >= 0) {
limitRows = Math.min(l, limitRows);
}
}
boolean lazy = session.isLazyQueryExecution() &&
target == null && !isForUpdate && !isQuickAggregateQuery &&
limitRows != 0 && offsetExpr == null && isReadOnly();
int columnCount = expressions.size();
LocalResult result = null;
if (!lazy && (target == null ||
!session.getDatabase().getSettings().optimizeInsertFromSelect)) {
//此方法获得表的字段数并没有获得行数
result = createLocalResult(result);
}
createLocalResult方法获得的数据
Select中的createLocalResult方法
//
lazyResult = queryFlat(columnCount, to, limitRows);
private LazyResult queryFlat(int columnCount, ResultTarget result, long limitRows) {
// limitRows must be long, otherwise we get an int overflow
// if limitRows is at or near Integer.MAX_VALUE
// limitRows is never 0 here
if (limitRows > 0 && offsetExpr != null) {
int offset = offsetExpr.getValue(session).getInt();
if (offset > 0) {
limitRows += offset;
}
}
ArrayList<Row> forUpdateRows = null;
if (isForUpdateMvcc) {
forUpdateRows = New.arrayList();
}
int sampleSize = getSampleSizeValue(session);
LazyResultQueryFlat lazyResult = new LazyResultQueryFlat(expressionArray,
sampleSize, columnCount);
if (result == null) {
return lazyResult;
}
//遍历lazyResult 将行值添加到result中
while (lazyResult.next()) {
if (isForUpdateMvcc) {
topTableFilter.lockRowAdd(forUpdateRows);
}
result.addRow(lazyResult.currentRow());
if ((sort == null || sortUsingIndex) && limitRows > 0 &&
result.getRowCount() >= limitRows) {
break;
}
}
if (isForUpdateMvcc) {
topTableFilter.lockRows(forUpdateRows);
}
return null;
}
查询经过的值栈