use of org.teiid.client.ResultsMessage in project teiid by teiid.
the class RequestWorkItem method createResultsMessage.
public ResultsMessage createResultsMessage(List<? extends List<?>> batch, List<? extends Expression> columnSymbols) {
String[] columnNames = new String[columnSymbols.size()];
String[] dataTypes = new String[columnSymbols.size()];
byte clientSerializationVersion = this.dqpWorkContext.getClientVersion().getClientSerializationVersion();
for (int i = 0; i < columnSymbols.size(); i++) {
Expression symbol = columnSymbols.get(i);
columnNames[i] = Symbol.getShortName(Symbol.getOutputName(symbol));
dataTypes[i] = DataTypeManager.getDataTypeName(symbol.getType());
if (dataTypes[i].equals(DataTypeManager.DefaultDataTypes.GEOMETRY) && !this.dqpWorkContext.getSession().isEmbedded() && clientSerializationVersion < BatchSerializer.VERSION_GEOMETRY) {
dataTypes[i] = DataTypeManager.DefaultDataTypes.BLOB;
}
}
ResultsMessage result = new ResultsMessage(batch, columnNames, dataTypes);
result.setClientSerializationVersion(clientSerializationVersion);
result.setDelayDeserialization(this.requestMsg.isDelaySerialization() && this.originalCommand.returnsResultSet());
return result;
}
use of org.teiid.client.ResultsMessage in project teiid by teiid.
the class DQPCore method executeRequest.
public ResultsFuture<ResultsMessage> executeRequest(long reqID, RequestMessage requestMsg, Long queryTimeout) throws TeiidProcessingException {
DQPWorkContext workContext = DQPWorkContext.getWorkContext();
checkActive(workContext);
RequestID requestID = workContext.getRequestID(reqID);
requestMsg.setFetchSize(Math.min(requestMsg.getFetchSize(), this.config.getMaxRowsFetchSize()));
Request request = null;
if (requestMsg.isPreparedStatement() || requestMsg.isCallableStatement() || requestMsg.getRequestOptions().isContinuous()) {
request = new PreparedStatementRequest(prepPlanCache);
} else {
request = new Request();
}
ClientState state = this.getClientState(workContext.getSessionId(), true);
if (state.session == null) {
state.session = workContext.getSession();
}
request.initialize(requestMsg, bufferManager, dataTierMgr, transactionService, state.sessionTables, workContext, this.prepPlanCache);
request.setOptions(options);
request.setExecutor(this.processWorkerPool);
request.setResultSetCacheEnabled(this.rsCache != null);
request.setAuthorizationValidator(this.authorizationValidator);
final PreParser preparser = workContext.getVDB().getAttachment(PreParser.class);
if (preparser != null) {
if (this.config.getPreParser() != null) {
// chain the preparsing effect
request.setPreParser(new PreParser() {
@Override
public String preParse(String command, org.teiid.CommandContext context) {
String preParse = config.getPreParser().preParse(command, context);
return preparser.preParse(preParse, context);
}
});
} else {
request.setPreParser(preparser);
}
} else {
request.setPreParser(this.config.getPreParser());
}
request.setUserRequestConcurrency(this.getUserRequestSourceConcurrency());
ResultsFuture<ResultsMessage> resultsFuture = new ResultsFuture<ResultsMessage>();
final RequestWorkItem workItem = new RequestWorkItem(this, requestMsg, request, resultsFuture.getResultsReceiver(), requestID, workContext);
logMMCommand(workItem, Event.NEW, null, null);
addRequest(requestID, workItem, state);
long timeout = workContext.getVDB().getQueryTimeout();
timeout = Math.min(timeout > 0 ? timeout : Long.MAX_VALUE, config.getQueryTimeout() > 0 ? config.getQueryTimeout() : Long.MAX_VALUE);
if (queryTimeout != null && queryTimeout > 0) {
timeout = Math.min(timeout > 0 ? timeout : Long.MAX_VALUE, queryTimeout);
}
if (timeout < Long.MAX_VALUE) {
final long finalTimeout = timeout;
workItem.setCancelTask(this.cancellationTimer.add(new Runnable() {
WeakReference<RequestWorkItem> workItemRef = new WeakReference<RequestWorkItem>(workItem);
@Override
public void run() {
try {
RequestWorkItem wi = workItemRef.get();
if (wi != null) {
String reason = QueryPlugin.Util.gs(QueryPlugin.Event.TEIID31096, finalTimeout);
wi.requestCancel(reason);
}
} catch (TeiidComponentException e) {
LogManager.logError(LogConstants.CTX_DQP, e, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30018));
}
}
}, timeout));
}
boolean runInThread = requestMsg.isSync();
synchronized (waitingPlans) {
if (runInThread || currentlyActivePlans < maxActivePlans) {
startActivePlan(workItem, !runInThread);
} else {
if (LogManager.isMessageToBeRecorded(LogConstants.CTX_DQP, MessageLevel.DETAIL)) {
// $NON-NLS-1$
LogManager.logDetail(LogConstants.CTX_DQP, workItem.requestID, "Queuing plan, since max plans has been reached.");
}
waitingPlans.add(workItem);
maxWaitingPlans = Math.max(this.maxWaitingPlans, waitingPlans.size());
}
}
if (runInThread) {
workItem.useCallingThread = true;
workItem.run();
}
return resultsFuture;
}
use of org.teiid.client.ResultsMessage in project teiid by teiid.
the class DQPCore method executeQuery.
/**
* Execute the given query asynchly. Has a hard limit of only returning max rows fetch size rows.
* @param command
* @param vdb
* @param user
* @param app
* @param timeoutInMilli
* @param engine
* @param listener
* @return
* @throws Throwable
*/
public static ResultsFuture<?> executeQuery(final Object command, final VDBMetaData vdb, final String user, final String app, final long timeoutInMilli, final DQPCore engine, final ResultsListener listener) throws Throwable {
final SessionMetadata session = TempTableDataManager.createTemporarySession(user, app, vdb);
final long requestID = 0L;
DQPWorkContext workContext = new DQPWorkContext();
if (engine.localProfile != null) {
workContext.setConnectionProfile(engine.localProfile);
}
workContext.setUseCallingThread(true);
workContext.setSession(session);
workContext.setAdmin(true);
final ResultsFuture<Void> resultFuture = new ResultsFuture<Void>();
resultFuture.addCompletionListener(new ResultsFuture.CompletionListener<Void>() {
@SuppressWarnings("unchecked")
@Override
public void onCompletion(ResultsFuture<Void> future) {
ResultsFuture<?> response;
try {
response = engine.closeRequest(requestID);
response.addCompletionListener(new ResultsFuture.CompletionListener() {
@Override
public void onCompletion(ResultsFuture future) {
engine.terminateSession(session.getSessionId());
}
});
} catch (Exception e) {
engine.terminateSession(session.getSessionId());
}
}
});
workContext.runInContext(new Callable<Void>() {
@Override
public Void call() throws Exception {
RequestMessage request = new RequestMessage();
if (command instanceof String) {
request.setCommands((String) command);
} else {
request.setCommands(command.toString());
request.setCommand(command);
}
request.setExecutionId(requestID);
// this would limit the number of rows that are returned.
request.setRowLimit(engine.getMaxRowsFetchSize());
ResultsFuture<ResultsMessage> message = engine.executeRequest(requestID, request, timeoutInMilli);
message.addCompletionListener(new ResultsFuture.CompletionListener<ResultsMessage>() {
@Override
public void onCompletion(ResultsFuture<ResultsMessage> future) {
try {
ResultsMessage rm = future.get();
if (rm.getException() != null) {
throw rm.getException();
}
if (rm.isUpdateResult()) {
// $NON-NLS-1$
listener.onResults(Arrays.asList("update-count"), rm.getResultsList());
resultFuture.getResultsReceiver().receiveResults(null);
} else {
processResult(rm);
}
} catch (Exception e) {
resultFuture.getResultsReceiver().exceptionOccurred(e);
}
}
private void processResult(ResultsMessage rm) throws Exception {
if (rm.getException() != null) {
throw rm.getException();
}
listener.onResults(Arrays.asList(rm.getColumnNames()), rm.getResultsList());
if (rm.getFinalRow() == -1 || rm.getLastRow() < rm.getFinalRow()) {
ResultsFuture<ResultsMessage> next = engine.processCursorRequest(requestID, rm.getLastRow() + 1, 1024);
next.addCompletionListener(new ResultsFuture.CompletionListener<ResultsMessage>() {
@Override
public void onCompletion(ResultsFuture<ResultsMessage> future) {
try {
processResult(future.get());
} catch (Exception e) {
resultFuture.getResultsReceiver().exceptionOccurred(e);
}
}
});
} else {
resultFuture.getResultsReceiver().receiveResults(null);
}
}
});
return null;
}
});
return resultFuture;
}
use of org.teiid.client.ResultsMessage in project teiid by teiid.
the class StatementImpl method executeSql.
@SuppressWarnings("unchecked")
protected ResultsFuture<Boolean> executeSql(String[] commands, boolean isBatchedCommand, ResultsMode resultsMode, boolean synch, RequestOptions options, boolean autoGenerateKeys) throws SQLException {
checkStatement();
resetExecutionState();
if (options != null) {
if (options.isContinuous()) {
if (!this.driverConnection.getServerConnection().supportsContinuous()) {
// $NON-NLS-1$
throw new TeiidSQLException(JDBCPlugin.Util.getString("JDBC.continuous"));
}
if (this.getResultSetType() != ResultSet.TYPE_FORWARD_ONLY) {
// $NON-NLS-1$
String msg = JDBCPlugin.Util.getString("JDBC.forward_only_resultset");
throw new TeiidSQLException(msg);
}
if (resultsMode == ResultsMode.EITHER) {
resultsMode = ResultsMode.RESULTSET;
} else if (resultsMode == ResultsMode.UPDATECOUNT) {
// $NON-NLS-1$
String msg = JDBCPlugin.Util.getString("JDBC.forward_only_resultset");
throw new TeiidSQLException(msg);
}
}
}
if (logger.isLoggable(Level.FINER)) {
// $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
logger.finer("Executing: requestID " + getCurrentRequestID() + " commands: " + Arrays.toString(commands) + " expecting: " + resultsMode);
}
if (commands.length == 1) {
Matcher match = SET_STATEMENT.matcher(commands[0]);
if (match.matches()) {
if (resultsMode == ResultsMode.RESULTSET) {
// $NON-NLS-1$
throw new TeiidSQLException(JDBCPlugin.Util.getString("StatementImpl.set_result_set"));
}
String val = match.group(2);
String key = unescapeId(val);
String value = match.group(3);
if (value != null && value.startsWith("\'") && value.endsWith("\'")) {
// $NON-NLS-1$ //$NON-NLS-2$
value = value.substring(1, value.length() - 1);
// $NON-NLS-1$ //$NON-NLS-2$
value = StringUtil.replaceAll(value, "''", "'");
}
if (match.group(1) != null) {
// payload case
Properties p = this.getMMConnection().getPayload();
if (p == null) {
p = new Properties();
this.getMMConnection().setPayload(p);
}
p.setProperty(key, value);
} else if (val == key && "SESSION AUTHORIZATION".equalsIgnoreCase(key)) {
// $NON-NLS-1$
this.getMMConnection().changeUser(value, this.getMMConnection().getPassword());
} else if (key.equalsIgnoreCase(TeiidURL.CONNECTION.PASSWORD)) {
this.getMMConnection().setPassword(value);
} else if (ExecutionProperties.NEWINSTANCE.equalsIgnoreCase(key)) {
if (Boolean.valueOf(value)) {
this.getMMConnection().getServerConnection().cleanUp();
}
} else {
this.driverConnection.setExecutionProperty(key, value);
}
this.updateCounts = new int[] { 0 };
return booleanFuture(false);
}
match = SET_CHARACTERISTIC_STATEMENT.matcher(commands[0]);
if (match.matches()) {
String value = match.group(1);
if (StringUtil.endsWithIgnoreCase(value, "uncommitted")) {
// $NON-NLS-1$
this.getMMConnection().setTransactionIsolation(Connection.TRANSACTION_READ_UNCOMMITTED);
} else if (StringUtil.endsWithIgnoreCase(value, "committed")) {
// $NON-NLS-1$
this.getMMConnection().setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);
} else if (StringUtil.startsWithIgnoreCase(value, "repeatable")) {
// $NON-NLS-1$
this.getMMConnection().setTransactionIsolation(Connection.TRANSACTION_REPEATABLE_READ);
} else if ("serializable".equalsIgnoreCase(value)) {
// $NON-NLS-1$
this.getMMConnection().setTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE);
}
this.updateCounts = new int[] { 0 };
return booleanFuture(false);
}
match = TRANSACTION_STATEMENT.matcher(commands[0]);
if (match.matches()) {
// $NON-NLS-1$
logger.finer("Executing as transaction statement");
if (resultsMode == ResultsMode.RESULTSET) {
// $NON-NLS-1$
throw new TeiidSQLException(JDBCPlugin.Util.getString("StatementImpl.set_result_set"));
}
String command = match.group(1);
Boolean commit = null;
if (StringUtil.startsWithIgnoreCase(command, "start")) {
// $NON-NLS-1$
// TODO: this should force a start and through an exception if we're already in a txn
this.getConnection().setAutoCommit(false);
} else if (command.equalsIgnoreCase("commit")) {
// $NON-NLS-1$
commit = true;
if (synch) {
this.getConnection().setAutoCommit(true);
}
} else if (command.equalsIgnoreCase("rollback")) {
// $NON-NLS-1$
commit = false;
if (synch || !this.getConnection().isInLocalTxn()) {
this.getConnection().rollback(false);
}
}
this.updateCounts = new int[] { 0 };
if (commit != null && !synch) {
ResultsFuture<?> pending = this.getConnection().submitSetAutoCommitTrue(commit);
final ResultsFuture<Boolean> result = new ResultsFuture<Boolean>();
pending.addCompletionListener(new ResultsFuture.CompletionListener() {
@Override
public void onCompletion(ResultsFuture future) {
try {
future.get();
result.getResultsReceiver().receiveResults(false);
} catch (Throwable t) {
result.getResultsReceiver().exceptionOccurred(t);
}
}
});
return result;
}
return booleanFuture(false);
}
match = SHOW_STATEMENT.matcher(commands[0]);
if (match.matches()) {
// $NON-NLS-1$
logger.finer("Executing as show statement");
if (resultsMode == ResultsMode.UPDATECOUNT) {
// $NON-NLS-1$
throw new TeiidSQLException(JDBCPlugin.Util.getString("StatementImpl.show_update_count"));
}
return executeShow(match);
}
}
final RequestMessage reqMessage = createRequestMessage(commands, isBatchedCommand, resultsMode);
reqMessage.setReturnAutoGeneratedKeys(autoGenerateKeys);
reqMessage.setRequestOptions(options);
ResultsFuture<ResultsMessage> pendingResult = execute(reqMessage, synch);
final ResultsFuture<Boolean> result = new ResultsFuture<Boolean>();
pendingResult.addCompletionListener(new ResultsFuture.CompletionListener<ResultsMessage>() {
@Override
public void onCompletion(ResultsFuture<ResultsMessage> future) {
try {
postReceiveResults(reqMessage, future.get());
result.getResultsReceiver().receiveResults(hasResultSet());
} catch (Throwable t) {
result.getResultsReceiver().exceptionOccurred(t);
}
}
});
if (synch) {
try {
pendingResult.get(queryTimeoutMS == 0 ? Integer.MAX_VALUE : queryTimeoutMS, TimeUnit.MILLISECONDS);
// throw an exception if needed
result.get();
return result;
} catch (ExecutionException e) {
if (e.getCause() instanceof SQLException) {
throw (SQLException) e.getCause();
}
if (e.getCause() != null) {
throw TeiidSQLException.create(e.getCause());
}
throw TeiidSQLException.create(e);
} catch (InterruptedException e) {
timeoutOccurred();
} catch (TimeoutException e) {
timeoutOccurred();
}
// $NON-NLS-1$
throw new TeiidSQLException(JDBCPlugin.Util.getString("MMStatement.Timeout_before_complete"));
}
return result;
}
use of org.teiid.client.ResultsMessage in project teiid by teiid.
the class StatementImpl method createDummyResultsMessage.
static ResultsMessage createDummyResultsMessage(String[] columnNames, String[] dataTypes, List records) {
ResultsMessage resultsMsg = new ResultsMessage();
resultsMsg.setColumnNames(columnNames);
resultsMsg.setDataTypes(dataTypes);
resultsMsg.setFirstRow(1);
resultsMsg.setLastRow(records.size());
resultsMsg.setFinalRow(records.size());
resultsMsg.setResults((List[]) records.toArray(new List[records.size()]));
return resultsMsg;
}
Aggregations