Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Licensed to Crate.io GmbH ("Crate") under one or more contributor
* license agreements. See the NOTICE file distributed with this work for
* additional information regarding copyright ownership. Crate licenses
* this file to you under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. You may
* obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
* However, if you have executed another commercial license agreement
* with Crate these terms will supersede the license and you may use the
* software solely pursuant to the terms of the relevant commercial agreement.
*/
package org.finos.legend.engine.postgres;
import io.opentelemetry.api.trace.Span;
import io.opentelemetry.api.trace.StatusCode;
import io.opentelemetry.api.trace.Tracer;
import io.opentelemetry.context.Context;
import io.opentelemetry.context.Scope;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutorService;
import java.util.function.Function;
import java.util.function.Supplier;
import org.finos.legend.engine.language.sql.grammar.from.SQLGrammarParser;
import org.finos.legend.engine.language.sql.grammar.from.antlr4.SqlBaseParser;
import org.finos.legend.engine.postgres.handler.PostgresPreparedStatement;
import org.finos.legend.engine.postgres.handler.PostgresResultSet;
import org.finos.legend.engine.postgres.handler.PostgresStatement;
import org.finos.legend.engine.postgres.handler.SessionHandler;
import org.finos.legend.engine.postgres.utils.OpenTelemetryUtil;
import org.finos.legend.engine.shared.core.identity.Identity;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class Session implements AutoCloseable
{
private static final Logger LOGGER = LoggerFactory.getLogger(Session.class);
public static final String FAILED_TO_EXECUTE = "Failed to execute";
private final Map parsed = new ConcurrentHashMap<>();
private final Map portals = new ConcurrentHashMap<>();
private final ExecutionDispatcher dispatcher;
private final ExecutorService executorService;
private final Identity identity;
private CompletableFuture> activeExecution;
public Session(SessionHandler dataSessionHandler, SessionHandler metaDataSessionHandler, ExecutorService executorService, Identity identity)
{
this.executorService = executorService;
this.dispatcher = new ExecutionDispatcher(dataSessionHandler, metaDataSessionHandler);
this.identity = identity;
OpenTelemetryUtil.ACTIVE_SESSIONS.add(1);
OpenTelemetryUtil.TOTAL_SESSIONS.add(1);
activeExecution = CompletableFuture.completedFuture(null);
}
public Identity getIdentity()
{
return identity;
}
public void setActiveExecution(CompletableFuture> activeExecution)
{
this.activeExecution = activeExecution;
}
public CompletableFuture> sync()
{
//TODO do we need to handle batch requests?
LOGGER.info("Sync");
return activeExecution;
}
public CompletableFuture> parseAsync(String statementName, String query, List paramTypes)
{
LOGGER.debug("got request to parse");
activeExecution = activeExecution.thenRunAsync(() -> parse(statementName, query, paramTypes), executorService);
LOGGER.debug("done queuing parse");
return activeExecution;
}
private void parse(String statementName, String query, List paramTypes)
{
if (LOGGER.isDebugEnabled())
{
LOGGER.debug("method=parse stmtName={} query={} paramTypes={}", statementName, query,
paramTypes);
}
Prepared p = new Prepared();
p.name = statementName;
p.sql = query;
p.paramType = paramTypes.toArray(new Integer[]{});
if (query != null)
{
try
{
SessionHandler sessionHandler;
if (query.isEmpty())
{
// Parser can't handle empty string, but postgres requires support for it
// Using an empty session handler for empty queries
sessionHandler = ExecutionDispatcher.getEmptySessionHandler();
}
else
{
sessionHandler = getSessionHandler(query);
}
p.prep = sessionHandler.prepareStatement(query);
}
catch (Exception e)
{
throw PostgresServerException.wrapException(e);
}
}
parsed.put(p.name, p);
}
/**
* Identify type of query and return appropriate session handler
* based on schema of the query.
*
* @param query SQL query to be executed
* @return session handler for the given query
*/
private SessionHandler getSessionHandler(String query)
{
SqlBaseParser parser = SQLGrammarParser.getSqlBaseParser(query, "query");
SqlBaseParser.SingleStatementContext singleStatementContext = parser.singleStatement();
SessionHandler sessionHandler = singleStatementContext.accept(dispatcher);
if (sessionHandler == null)
{
throw new PostgresServerException(String.format("Unable to determine session handler for query[%s]", query));
}
return sessionHandler;
}
public int getParamType(String statementName, int idx)
{
Prepared stmt = getSafeStmt(statementName);
return stmt.paramType[idx];
}
public CompletableFuture> bindAsync(String portalName, String statementName, List