lidavidm commented on a change in pull request #10906:
URL: https://github.com/apache/arrow/pull/10906#discussion_r687169846



##########
File path: 
java/flight/flight-sql/src/main/java/org/apache/arrow/flight/sql/FlightSqlClient.java
##########
@@ -0,0 +1,504 @@
+/*
+ * 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.arrow.flight.sql;
+
+import static 
org.apache.arrow.flight.sql.impl.FlightSql.ActionClosePreparedStatementRequest;
+import static 
org.apache.arrow.flight.sql.impl.FlightSql.ActionCreatePreparedStatementRequest;
+import static org.apache.arrow.flight.sql.impl.FlightSql.CommandGetCatalogs;
+import static 
org.apache.arrow.flight.sql.impl.FlightSql.CommandGetExportedKeys;
+import static 
org.apache.arrow.flight.sql.impl.FlightSql.CommandGetImportedKeys;
+import static org.apache.arrow.flight.sql.impl.FlightSql.CommandGetPrimaryKeys;
+import static org.apache.arrow.flight.sql.impl.FlightSql.CommandGetSchemas;
+import static org.apache.arrow.flight.sql.impl.FlightSql.CommandGetSqlInfo;
+import static org.apache.arrow.flight.sql.impl.FlightSql.CommandGetTableTypes;
+import static org.apache.arrow.flight.sql.impl.FlightSql.CommandGetTables;
+import static org.apache.arrow.flight.sql.impl.FlightSql.CommandStatementQuery;
+import static 
org.apache.arrow.flight.sql.impl.FlightSql.CommandStatementUpdate;
+import static org.apache.arrow.flight.sql.impl.FlightSql.DoPutUpdateResult;
+
+import java.io.Closeable;
+import java.io.IOException;
+import java.nio.ByteBuffer;
+import java.sql.SQLException;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Objects;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.atomic.AtomicLong;
+
+import javax.annotation.Nullable;
+
+import org.apache.arrow.flight.Action;
+import org.apache.arrow.flight.CallOption;
+import org.apache.arrow.flight.FlightClient;
+import org.apache.arrow.flight.FlightDescriptor;
+import org.apache.arrow.flight.FlightInfo;
+import org.apache.arrow.flight.FlightStream;
+import org.apache.arrow.flight.PutResult;
+import org.apache.arrow.flight.Result;
+import org.apache.arrow.flight.SchemaResult;
+import org.apache.arrow.flight.SyncPutListener;
+import org.apache.arrow.flight.Ticket;
+import org.apache.arrow.flight.sql.impl.FlightSql;
+import 
org.apache.arrow.flight.sql.impl.FlightSql.ActionCreatePreparedStatementResult;
+import 
org.apache.arrow.flight.sql.impl.FlightSql.CommandPreparedStatementQuery;
+import org.apache.arrow.memory.ArrowBuf;
+import org.apache.arrow.vector.VectorSchemaRoot;
+import org.apache.arrow.vector.types.pojo.Schema;
+
+import com.google.protobuf.Any;
+import com.google.protobuf.ByteString;
+import com.google.protobuf.InvalidProtocolBufferException;
+import com.google.protobuf.StringValue;
+
+import io.grpc.Status;
+
+/**
+ * Flight client with Flight SQL semantics.
+ */
+public class FlightSqlClient {
+  private FlightClient client;
+
+  public FlightSqlClient(FlightClient client) {
+    this.client = client;
+  }
+
+  /**
+   * Execute a query on the server.
+   *
+   * @param query The query to execute.
+   * @return a FlightInfo object representing the stream(s) to fetch.
+   */
+  public FlightInfo execute(String query) {
+    final CommandStatementQuery.Builder builder = 
CommandStatementQuery.newBuilder();
+    builder.setQuery(query);
+    final FlightDescriptor descriptor = 
FlightDescriptor.command(Any.pack(builder.build()).toByteArray());
+    return client.getInfo(descriptor);
+  }
+
+  /**
+   * Execute an update query on the server.
+   *
+   * @param query The query to execute.
+   * @return a FlightInfo object representing the stream(s) to fetch.
+   */
+  public long executeUpdate(String query) {
+    final CommandStatementUpdate.Builder builder = 
CommandStatementUpdate.newBuilder();
+    builder.setQuery(query);
+
+    final FlightDescriptor descriptor = 
FlightDescriptor.command(Any.pack(builder.build()).toByteArray());
+    final SyncPutListener putListener = new SyncPutListener();
+    client.startPut(descriptor, VectorSchemaRoot.of(), putListener);
+
+    try {
+      final PutResult read = putListener.read();
+      try (final ArrowBuf metadata = read.getApplicationMetadata()) {
+        final DoPutUpdateResult doPutUpdateResult = 
DoPutUpdateResult.parseFrom(metadata.nioBuffer());
+        return doPutUpdateResult.getRecordCount();
+      }
+    } catch (InterruptedException | ExecutionException | 
InvalidProtocolBufferException e) {

Review comment:
       I think it comes from SyncPutListener though: 
https://github.com/apache/arrow/blob/4fa983281bdabc3d6eb78b23261809e2b245b4f2/java/flight/flight-core/src/main/java/org/apache/arrow/flight/SyncPutListener.java#L63




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to