unikdahal commented on code in PR #4444:
URL: https://github.com/apache/arrow-adbc/pull/4444#discussion_r3523065741


##########
java/driver/flight-sql/src/main/java/org/apache/arrow/adbc/driver/flightsql/FlightSqlConnection.java:
##########
@@ -203,8 +212,115 @@ public void setAutoCommit(boolean enableAutoCommit) 
throws AdbcException {
     }
   }
 
+  @Override
+  public <T> T getOption(TypedKey<T> key) throws AdbcException {
+    final String k = key.getKey();
+
+    if (k.equals(FlightSqlConnectionProperties.SESSION_OPTIONS)) {
+      if (key.getType() != String.class) {
+        return AdbcConnection.super.getOption(key);
+      }
+      return 
key.cast(FlightSqlSessionUtil.toJson(fetchSessionOptionsOrEmpty()));
+    }
+
+    final String prefix;
+    if 
(k.startsWith(FlightSqlConnectionProperties.SESSION_OPTION_BOOL_PREFIX)) {
+      prefix = FlightSqlConnectionProperties.SESSION_OPTION_BOOL_PREFIX;
+    } else if 
(k.startsWith(FlightSqlConnectionProperties.SESSION_OPTION_STRING_LIST_PREFIX)) 
{
+      prefix = FlightSqlConnectionProperties.SESSION_OPTION_STRING_LIST_PREFIX;
+    } else if 
(k.startsWith(FlightSqlConnectionProperties.SESSION_OPTION_PREFIX)) {
+      prefix = FlightSqlConnectionProperties.SESSION_OPTION_PREFIX;
+    } else {
+      return AdbcConnection.super.getOption(key);
+    }
+
+    final String name = k.substring(prefix.length());
+    if (name.isEmpty()) {
+      throw AdbcException.invalidArgument("[Flight SQL] Session option name 
must not be empty");
+    }
+    final Object raw =
+        FlightSqlSessionUtil.require(fetchSessionOptions(), name)
+            .acceptVisitor(FlightSqlSessionUtil.TO_JAVA);
+    if (raw == null) {
+      throw new AdbcException(
+          "[Flight SQL] Session option not found: " + name,
+          null,
+          AdbcStatusCode.NOT_FOUND,
+          null,
+          0);
+    }
+    final T result = FlightSqlSessionUtil.cast(key, raw, name);
+    return result != null ? result : AdbcConnection.super.getOption(key);
+  }
+
+  @Override
+  public <T> void setOption(TypedKey<T> key, T value) throws AdbcException {
+    final String k = key.getKey();
+
+    if (value == null) {
+      throw AdbcException.invalidArgument(
+          "[Flight SQL] null value not allowed for key: "
+              + k
+              + " — use adbc.flight.sql.session.optionerase.<name> to erase an 
option");
+    }
+
+    if 
(k.startsWith(FlightSqlConnectionProperties.SESSION_OPTION_ERASE_PREFIX)) {
+      final String name =
+          
k.substring(FlightSqlConnectionProperties.SESSION_OPTION_ERASE_PREFIX.length());
+      doSetSessionOption(name, 
SessionOptionValueFactory.makeEmptySessionOptionValue());
+
+    } else if 
(k.startsWith(FlightSqlConnectionProperties.SESSION_OPTION_BOOL_PREFIX)) {
+      final String name =
+          
k.substring(FlightSqlConnectionProperties.SESSION_OPTION_BOOL_PREFIX.length());
+      final boolean b;
+      if (value instanceof Boolean) {
+        b = (Boolean) value;
+      } else {
+        b = Boolean.parseBoolean(value.toString());
+      }
+      doSetSessionOption(name, 
SessionOptionValueFactory.makeSessionOptionValue(b));
+
+    } else if 
(k.startsWith(FlightSqlConnectionProperties.SESSION_OPTION_STRING_LIST_PREFIX)) 
{
+      final String name =
+          
k.substring(FlightSqlConnectionProperties.SESSION_OPTION_STRING_LIST_PREFIX.length());
+      final String[] arr;
+      if (value instanceof String[]) {
+        arr = (String[]) value;
+      } else {
+        arr = FlightSqlSessionUtil.parseJsonArray(value.toString());
+      }
+      doSetSessionOption(name, 
SessionOptionValueFactory.makeSessionOptionValue(arr));
+
+    } else if 
(k.startsWith(FlightSqlConnectionProperties.SESSION_OPTION_PREFIX)) {
+      final String name = 
k.substring(FlightSqlConnectionProperties.SESSION_OPTION_PREFIX.length());
+      final SessionOptionValue sv;
+      if (value instanceof Long) {
+        sv = SessionOptionValueFactory.makeSessionOptionValue((Long) value);
+      } else if (value instanceof Double) {
+        sv = SessionOptionValueFactory.makeSessionOptionValue((Double) value);
+      } else {
+        sv = 
SessionOptionValueFactory.makeSessionOptionValue(value.toString());
+      }
+      doSetSessionOption(name, sv);
+
+    } else if (k.equals(FlightSqlConnectionProperties.SESSION_OPTIONS)) {
+      throw AdbcException.notImplemented(
+          "[Flight SQL] adbc.flight.sql.session.options is read-only");
+
+    } else {
+      AdbcConnection.super.setOption(key, value);
+    }
+  }
+
   @Override
   public void close() throws Exception {
+    try {
+      client.closeSession(new CloseSessionRequest());
+    } catch (FlightRuntimeException e) {
+      if (e.status().code() != FlightStatusCode.UNIMPLEMENTED) {
+        throw FlightSqlDriverUtil.fromFlightException(e);
+      }
+    }
     clientCache.invalidateAll();
     AutoCloseables.close(client, allocator);
   }

Review Comment:
   refactored 



-- 
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