Skip to content
Permalink
Browse files
IGNITE-16201 JDBC: Batch execution (#639)
  • Loading branch information
vladErmakov07 committed Feb 21, 2022
1 parent 12e1c38 commit 3eb87bf14d54d4e21af73c5bd6ee9ceba60e5b07
Showing 14 changed files with 1,143 additions and 248 deletions.
@@ -20,6 +20,7 @@
import java.util.concurrent.CompletableFuture;
import org.apache.ignite.client.proto.query.event.BatchExecuteRequest;
import org.apache.ignite.client.proto.query.event.BatchExecuteResult;
import org.apache.ignite.client.proto.query.event.BatchPreparedStmntRequest;
import org.apache.ignite.client.proto.query.event.JdbcMetaColumnsRequest;
import org.apache.ignite.client.proto.query.event.JdbcMetaColumnsResult;
import org.apache.ignite.client.proto.query.event.JdbcMetaPrimaryKeysRequest;
@@ -64,6 +65,14 @@ public interface JdbcQueryEventHandler {
*/
CompletableFuture<BatchExecuteResult> batchAsync(BatchExecuteRequest req);

/**
* {@link BatchPreparedStmntRequest} command handler.
*
* @param req Batch query request.
* @return Result future.
*/
CompletableFuture<BatchExecuteResult> batchPrepStatementAsync(BatchPreparedStmntRequest req);

/**
* {@link QueryCloseRequest} command handler.
*
@@ -32,11 +32,8 @@ public class BatchExecuteRequest implements ClientMessage {
/** Schema name. */
private String schemaName;

/** Sql query. */
private List<Query> queries;

/** Client auto commit flag state. */
private boolean autoCommit;
/** Sql queries. */
private List<String> queries;

/**
* Default constructor.
@@ -49,14 +46,12 @@ public BatchExecuteRequest() {
*
* @param schemaName Schema name.
* @param queries Queries.
* @param autoCommit Client auto commit flag state.
*/
public BatchExecuteRequest(String schemaName, List<Query> queries, boolean autoCommit) {
public BatchExecuteRequest(String schemaName, List<String> queries) {
assert !CollectionUtils.nullOrEmpty(queries);

this.schemaName = schemaName;
this.queries = queries;
this.autoCommit = autoCommit;
}

/**
@@ -73,28 +68,19 @@ public String schemaName() {
*
* @return Queries.
*/
public List<Query> queries() {
public List<String> queries() {
return queries;
}

/**
* Get the auto commit flag.
*
* @return Auto commit flag.
*/
boolean autoCommit() {
return autoCommit;
}

/** {@inheritDoc} */
@Override
public void writeBinary(ClientMessagePacker packer) {
ClientMessageUtils.writeStringNullable(packer, schemaName);

packer.packArrayHeader(queries.size());

for (Query q : queries) {
q.writeBinary(packer);
for (String q : queries) {
packer.packString(q);
}
}

@@ -108,11 +94,7 @@ public void readBinary(ClientMessageUnpacker unpacker) {
queries = new ArrayList<>(n);

for (int i = 0; i < n; ++i) {
Query qry = new Query();

qry.readBinary(unpacker);

queries.add(qry);
queries.add(unpacker.unpackString());
}
}

@@ -30,12 +30,30 @@ public class BatchExecuteResult extends Response {
/** Update counts. */
private int[] updateCnts;

/** Error code. */
private int errorCode;

/**
* Constructor.
*/
public BatchExecuteResult() {
}

/**
* Constructor.
*
* @param status Status code.
* @param errorCode Product error code.
* @param errorMessage Error message.
* @param updateCnts Array with success and error markers.
*/
public BatchExecuteResult(int status, int errorCode, String errorMessage, int[] updateCnts) {
super(status, errorMessage);

this.errorCode = errorCode;
this.updateCnts = updateCnts;
}

/**
* Constructor.
*
@@ -68,14 +86,21 @@ public int[] updateCounts() {
return updateCnts == null ? ArrayUtils.INT_EMPTY_ARRAY : updateCnts;
}

/**
* Get the error code.
*
* @return Error code.
*/
public int getErrorCode() {
return errorCode;
}

/** {@inheritDoc} */
@Override
public void writeBinary(ClientMessagePacker packer) {
super.writeBinary(packer);

if (!hasResults) {
return;
}
packer.packInt(errorCode);

packer.packIntArray(updateCnts);
}
@@ -85,11 +110,13 @@ public void writeBinary(ClientMessagePacker packer) {
public void readBinary(ClientMessageUnpacker unpacker) {
super.readBinary(unpacker);

if (!hasResults) {
return;
}
errorCode = unpacker.unpackInt();

updateCnts = unpacker.unpackIntArray();
if (unpacker.tryUnpackNil()) {
updateCnts = ArrayUtils.INT_EMPTY_ARRAY;
} else {
updateCnts = unpacker.unpackIntArray();
}
}

/** {@inheritDoc} */
@@ -0,0 +1,125 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF 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.
*/

package org.apache.ignite.client.proto.query.event;

import io.netty.util.internal.StringUtil;
import java.util.ArrayList;
import java.util.List;
import org.apache.ignite.client.proto.query.ClientMessage;
import org.apache.ignite.internal.client.proto.ClientMessagePacker;
import org.apache.ignite.internal.client.proto.ClientMessageUnpacker;
import org.apache.ignite.internal.tostring.S;
import org.apache.ignite.internal.util.CollectionUtils;

/**
* JDBC prepared statement query batch execute request.
*/
public class BatchPreparedStmntRequest implements ClientMessage {
/** Schema name. */
private String schemaName;

/** Sql query. */
private String query;

/** Batch of query arguments. */
private List<Object[]> args;

/**
* Default constructor.
*/
public BatchPreparedStmntRequest() {
}

/**
* Constructor.
*
* @param schemaName Schema name.
* @param query Sql query string.
* @param args Sql query arguments.
*/
public BatchPreparedStmntRequest(String schemaName, String query, List<Object[]> args) {
assert !StringUtil.isNullOrEmpty(query);
assert !CollectionUtils.nullOrEmpty(args);

this.query = query;
this.args = args;
this.schemaName = schemaName;
}

/**
* Get the schema name.
*
* @return Schema name.
*/
public String schemaName() {
return schemaName;
}

/**
* Get the sql query string.
*
* @return Query string.
*/
public String getQuery() {
return query;
}

/**
* Get the query arguments batch.
*
* @return query arguments batch.
*/
public List<Object[]> getArgs() {
return args;
}

/** {@inheritDoc} */
@Override
public void writeBinary(ClientMessagePacker packer) {
ClientMessageUtils.writeStringNullable(packer, schemaName);

packer.packString(query);
packer.packArrayHeader(args.size());

for (Object[] arg : args) {
packer.packObjectArray(arg);
}
}

/** {@inheritDoc} */
@Override
public void readBinary(ClientMessageUnpacker unpacker) {
schemaName = ClientMessageUtils.readStringNullable(unpacker);

query = unpacker.unpackString();

int n = unpacker.unpackArrayHeader();

args = new ArrayList<>(n);

for (int i = 0; i < n; ++i) {
args.add(unpacker.unpackObjectArray());
}
}

/** {@inheritDoc} */
@Override
public String toString() {
return S.toString(BatchPreparedStmntRequest.class, this);
}
}

This file was deleted.

0 comments on commit 3eb87bf

Please sign in to comment.