You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by "zeroshade (via GitHub)" <gi...@apache.org> on 2023/04/25 18:44:57 UTC

[GitHub] [arrow-adbc] zeroshade commented on a diff in pull request #607: feat(java/driver/jdbc): create AdbcDatabase from javax.sql.DataSource

zeroshade commented on code in PR #607:
URL: https://github.com/apache/arrow-adbc/pull/607#discussion_r1176891409


##########
java/driver/jdbc-validation-derby/src/test/java/org/apache/arrow/adbc/driver/jdbc/derby/DerbyConnectionTest.java:
##########
@@ -28,4 +39,31 @@ public class DerbyConnectionTest extends AbstractConnectionTest {
   static void beforeAll() {
     quirks = new DerbyQuirks(tempDir);
   }
+
+  @Test
+  void newUrlOption() throws Exception {
+    try (final BufferAllocator allocator = new RootAllocator()) {
+      AdbcDriver driver = new JdbcDriver(allocator);
+      Map<String, Object> parameters = new HashMap<>();
+      parameters.put("url", "jdbc:derby:memory:newUrlOption;create=true");

Review Comment:
   should we standardize on `"uri"` or make this a constant?



##########
java/driver/jdbc/src/main/java/org/apache/arrow/adbc/driver/jdbc/JdbcDriver.java:
##########
@@ -47,20 +50,64 @@ public JdbcDriver(BufferAllocator allocator) {
 
   @Override
   public AdbcDatabase open(Map<String, Object> parameters) throws AdbcException {
-    Object target = parameters.get(PARAM_URL);
-    if (!(target instanceof String)) {
-      throw AdbcException.invalidArgument("[JDBC] Must provide String " + PARAM_URL + " parameter");
+    DataSource dataSource = getParam(DataSource.class, parameters, PARAM_DATASOURCE);
+    // XXX(apache/arrow-adbc#316): allow "url" to align with C/Go
+    String target = getParam(String.class, parameters, "url", PARAM_URL);
+    if (dataSource != null && target != null) {
+      throw AdbcException.invalidArgument(
+          "[JDBC] Can only provide one of " + PARAM_URL + " and " + PARAM_DATASOURCE);
     }
-    Object quirks = parameters.get(PARAM_SQL_QUIRKS);
-    if (quirks != null) {
-      Preconditions.checkArgument(
-          quirks instanceof SqlQuirks,
-          String.format(
-              "[JDBC] %s must be a SqlQuirks instance, not %s",
-              PARAM_SQL_QUIRKS, quirks.getClass().getName()));
-    } else {
+
+    SqlQuirks quirks = getParam(SqlQuirks.class, parameters, PARAM_SQL_QUIRKS);
+    if (quirks == null) {
       quirks = new SqlQuirks();
     }
-    return new JdbcDatabase(allocator, (String) target, (SqlQuirks) quirks);
+
+    String username = getParam(String.class, parameters, "username");
+    String password = getParam(String.class, parameters, "password");
+    if ((username != null && password == null) || (username == null && password != null)) {
+      throw AdbcException.invalidArgument(
+          "[JDBC] Must provide both or neither of username and password");
+    }
+
+    if (target != null) {
+      dataSource = new UrlDataSource(target);
+    }

Review Comment:
   Is it a bad thing that this could completely replace the existing `dataSource` object based on `PARAM_DATASOURCE`? Should that be an error?



##########
java/driver/jdbc/src/main/java/org/apache/arrow/adbc/driver/jdbc/JdbcDataSourceDatabase.java:
##########
@@ -0,0 +1,90 @@
+/*
+ * 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.adbc.driver.jdbc;
+
+import java.sql.Connection;
+import java.sql.SQLException;
+import java.util.Objects;
+import java.util.concurrent.atomic.AtomicInteger;
+import javax.sql.DataSource;
+import org.apache.arrow.adbc.core.AdbcConnection;
+import org.apache.arrow.adbc.core.AdbcDatabase;
+import org.apache.arrow.adbc.core.AdbcException;
+import org.apache.arrow.adbc.sql.SqlQuirks;
+import org.apache.arrow.memory.BufferAllocator;
+
+/** An instance of a database based on a {@link DataSource}. */
+public final class JdbcDataSourceDatabase implements AdbcDatabase {
+  private final BufferAllocator allocator;
+  private final DataSource dataSource;
+  private final String username;
+  private final String password;
+  private final SqlQuirks quirks;
+  private final Connection connection;
+  private final AtomicInteger counter;
+
+  JdbcDataSourceDatabase(
+      BufferAllocator allocator,
+      DataSource dataSource,
+      String username,
+      String password,
+      SqlQuirks quirks)
+      throws AdbcException {
+    this.allocator = Objects.requireNonNull(allocator);
+    this.dataSource = Objects.requireNonNull(dataSource);
+    this.username = username;
+    this.password = password;
+    this.quirks = Objects.requireNonNull(quirks);
+    try {
+      this.connection = dataSource.getConnection();
+    } catch (SQLException e) {

Review Comment:
   I'm surprised the username and password aren't part of the datasource. You don't need them to create an initial connection on the datasource? Any reason to not just call `this.connect()` here?



-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org