use of org.h2.result.ResultInterface in project h2database by h2database.
the class ConditionInSelect method getValue.
@Override
public Value getValue(Session session) {
query.setSession(session);
if (!query.hasOrder()) {
query.setDistinct(true);
}
ResultInterface rows = query.query(0);
Value l = left.getValue(session);
if (!rows.hasNext()) {
return ValueBoolean.get(all);
} else if (l == ValueNull.INSTANCE) {
return l;
}
if (!session.getDatabase().getSettings().optimizeInSelect) {
return getValueSlow(rows, l);
}
if (all || (compareType != Comparison.EQUAL && compareType != Comparison.EQUAL_NULL_SAFE)) {
return getValueSlow(rows, l);
}
int dataType = rows.getColumnType(0);
if (dataType == Value.NULL) {
return ValueBoolean.FALSE;
}
l = l.convertTo(dataType);
if (rows.containsDistinct(new Value[] { l })) {
return ValueBoolean.TRUE;
}
if (rows.containsDistinct(new Value[] { ValueNull.INSTANCE })) {
return ValueNull.INSTANCE;
}
return ValueBoolean.FALSE;
}
use of org.h2.result.ResultInterface in project h2database by h2database.
the class ConditionInSelect method getValueSlow.
private Value getValueSlow(ResultInterface rows, Value l) {
// this only returns the correct result if the result has at least one
// row, and if l is not null
boolean hasNull = false;
boolean result = all;
while (rows.next()) {
boolean value;
Value r = rows.currentRow()[0];
if (r == ValueNull.INSTANCE) {
value = false;
hasNull = true;
} else {
value = Comparison.compareNotNull(database, l, r, compareType);
}
if (!value && all) {
result = false;
break;
} else if (value && !all) {
result = true;
break;
}
}
if (!result && hasNull) {
return ValueNull.INSTANCE;
}
return ValueBoolean.get(result);
}
use of org.h2.result.ResultInterface in project h2database by h2database.
the class JdbcPreparedStatement method execute.
/**
* Executes an arbitrary statement. If another result set exists for this
* statement, this will be closed (even if this statement fails). If auto
* commit is on, and the statement is not a select, this statement will be
* committed.
*
* @return true if a result set is available, false if not
* @throws SQLException if this object is closed or invalid
*/
@Override
public boolean execute() throws SQLException {
try {
int id = getNextId(TraceObject.RESULT_SET);
if (isDebugEnabled()) {
debugCodeCall("execute");
}
checkClosedForWrite();
try {
boolean returnsResultSet;
synchronized (conn.getSession()) {
closeOldResultSet();
boolean lazy = false;
try {
setExecutingStatement(command);
if (command.isQuery()) {
returnsResultSet = true;
boolean scrollable = resultSetType != ResultSet.TYPE_FORWARD_ONLY;
boolean updatable = resultSetConcurrency == ResultSet.CONCUR_UPDATABLE;
ResultInterface result = command.executeQuery(maxRows, scrollable);
lazy = result.isLazy();
resultSet = new JdbcResultSet(conn, this, command, result, id, closedByResultSet, scrollable, updatable, cachedColumnLabelMap);
} else {
returnsResultSet = false;
ResultWithGeneratedKeys result = command.executeUpdate(generatedKeysRequest);
updateCount = result.getUpdateCount();
ResultInterface gk = result.getGeneratedKeys();
if (gk != null) {
generatedKeys = new JdbcResultSet(conn, this, command, gk, id, false, true, false);
}
}
} finally {
if (!lazy) {
setExecutingStatement(null);
}
}
}
return returnsResultSet;
} finally {
afterWriting();
}
} catch (Throwable e) {
throw logAndConvert(e);
}
}
use of org.h2.result.ResultInterface in project h2database by h2database.
the class JdbcPreparedStatement method executeQuery.
/**
* Executes a query (select statement) and returns the result set. If
* another result set exists for this statement, this will be closed (even
* if this statement fails).
*
* @return the result set
* @throws SQLException if this object is closed or invalid
*/
@Override
public ResultSet executeQuery() throws SQLException {
try {
int id = getNextId(TraceObject.RESULT_SET);
if (isDebugEnabled()) {
debugCodeAssign("ResultSet", TraceObject.RESULT_SET, id, "executeQuery()");
}
batchIdentities = null;
synchronized (session) {
checkClosed();
closeOldResultSet();
ResultInterface result;
boolean lazy = false;
boolean scrollable = resultSetType != ResultSet.TYPE_FORWARD_ONLY;
boolean updatable = resultSetConcurrency == ResultSet.CONCUR_UPDATABLE;
try {
setExecutingStatement(command);
result = command.executeQuery(maxRows, scrollable);
lazy = result.isLazy();
} finally {
if (!lazy) {
setExecutingStatement(null);
}
}
resultSet = new JdbcResultSet(conn, this, command, result, id, closedByResultSet, scrollable, updatable, cachedColumnLabelMap);
}
return resultSet;
} catch (Exception e) {
throw logAndConvert(e);
}
}
use of org.h2.result.ResultInterface in project h2database by h2database.
the class JdbcStatement method executeInternal.
private boolean executeInternal(String sql, Object generatedKeysRequest) throws SQLException {
int id = getNextId(TraceObject.RESULT_SET);
checkClosedForWrite();
try {
closeOldResultSet();
sql = JdbcConnection.translateSQL(sql, escapeProcessing);
CommandInterface command = conn.prepareCommand(sql, fetchSize);
boolean lazy = false;
boolean returnsResultSet;
synchronized (session) {
setExecutingStatement(command);
try {
if (command.isQuery()) {
returnsResultSet = true;
boolean scrollable = resultSetType != ResultSet.TYPE_FORWARD_ONLY;
boolean updatable = resultSetConcurrency == ResultSet.CONCUR_UPDATABLE;
ResultInterface result = command.executeQuery(maxRows, scrollable);
lazy = result.isLazy();
resultSet = new JdbcResultSet(conn, this, command, result, id, closedByResultSet, scrollable, updatable);
} else {
returnsResultSet = false;
ResultWithGeneratedKeys result = command.executeUpdate(conn.scopeGeneratedKeys() ? false : generatedKeysRequest);
updateCount = result.getUpdateCount();
ResultInterface gk = result.getGeneratedKeys();
if (gk != null) {
generatedKeys = new JdbcResultSet(conn, this, command, gk, id, false, true, false);
}
}
} finally {
if (!lazy) {
setExecutingStatement(null);
}
}
}
if (!lazy) {
command.close();
}
return returnsResultSet;
} finally {
afterWriting();
}
}
Aggregations