All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.yandex.ydb.jdbc.impl.AbstractYdbDataQueryPreparedStatementImpl Maven / Gradle / Ivy

The newest version!
package com.yandex.ydb.jdbc.impl;

import java.sql.SQLException;
import java.util.Map;
import java.util.Objects;
import java.util.function.Supplier;

import com.google.common.base.Suppliers;
import com.yandex.ydb.jdbc.YdbParameterMetaData;
import com.yandex.ydb.table.query.DataQuery;

import static com.yandex.ydb.jdbc.YdbConst.UNSUPPORTED_QUERY_TYPE_IN_PS;

public abstract class AbstractYdbDataQueryPreparedStatementImpl extends AbstractYdbPreparedStatementImpl {

    private final Supplier metaDataSupplier;
    private final DataQuery dataQuery;

    protected AbstractYdbDataQueryPreparedStatementImpl(YdbConnectionImpl connection,
                                                        int resultSetType,
                                                        String query,
                                                        DataQuery dataQuery) throws SQLException {
        super(connection, resultSetType, query);
        this.dataQuery = Objects.requireNonNull(dataQuery);
        this.metaDataSupplier = Suppliers.memoize(() ->
                new YdbParameterMetaDataImpl(getParameterTypes()))::get;
    }

    @Override
    public YdbParameterMetaData getParameterMetaData() {
        return metaDataSupplier.get();
    }

    protected DataQuery getDataQuery() {
        return dataQuery;
    }

    @Override
    protected boolean executeImpl() throws SQLException {
        QueryType queryType = getQueryType();
        switch (queryType) {
            case DATA_QUERY:
                return executeDataQueryImpl(
                        getParams(),
                        params -> QueryType.DATA_QUERY + " [" + dataQuery.getId() + "] >>\n" +
                                dataQuery.getText().orElse("") +
                                "\n\nParams: " + paramsToString(params),
                        dataQuery::execute);
            case SCAN_QUERY:
                return executeScanQueryImpl();
            default:
                throw new SQLException(UNSUPPORTED_QUERY_TYPE_IN_PS + queryType);
        }
    }

    protected abstract Map getParameterTypes();
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy