geode-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From aging...@apache.org
Subject [geode] branch feature/GEODE-3781 updated: Support for getting key column info from database.
Date Thu, 26 Oct 2017 19:57:47 GMT
This is an automated email from the ASF dual-hosted git repository.

agingade pushed a commit to branch feature/GEODE-3781
in repository https://gitbox.apache.org/repos/asf/geode.git


The following commit(s) were added to refs/heads/feature/GEODE-3781 by this push:
     new c9b89a2  Support for getting key column info from database.
c9b89a2 is described below

commit c9b89a25f3d970cda8867745da049ac371ebfe8c
Author: Anil <agingade@pivotal.io>
AuthorDate: Thu Oct 26 12:57:13 2017 -0700

    Support for getting key column info from database.
---
 .../geode/connectors/jdbc/JDBCAsyncWriter.java     |  8 ++-
 .../apache/geode/connectors/jdbc/JDBCManager.java  | 57 +++++++++++++++++++---
 .../jdbc/JDBCAsyncWriterIntegrationTest.java       | 30 +++++++++---
 3 files changed, 79 insertions(+), 16 deletions(-)

diff --git a/geode-connectors/src/main/java/org/apache/geode/connectors/jdbc/JDBCAsyncWriter.java
b/geode-connectors/src/main/java/org/apache/geode/connectors/jdbc/JDBCAsyncWriter.java
index 97709cb..804301b 100644
--- a/geode-connectors/src/main/java/org/apache/geode/connectors/jdbc/JDBCAsyncWriter.java
+++ b/geode-connectors/src/main/java/org/apache/geode/connectors/jdbc/JDBCAsyncWriter.java
@@ -20,7 +20,9 @@ import java.util.Properties;
 import org.apache.geode.cache.asyncqueue.AsyncEvent;
 import org.apache.geode.cache.asyncqueue.AsyncEventListener;
 import org.apache.geode.cache.query.internal.DefaultQuery;
+import org.apache.geode.internal.logging.LogService;
 import org.apache.geode.pdx.PdxInstance;
+import org.apache.logging.log4j.Logger;
 
 /*
  * This class provides write behind cache semantics for a JDBC data source using AsyncEventListener.
@@ -29,6 +31,8 @@ import org.apache.geode.pdx.PdxInstance;
  */
 public class JDBCAsyncWriter implements AsyncEventListener {
 
+  static final Logger logger = LogService.getLogger();
+
   private long totalEvents = 0;
 
   private long successfulEvents = 0;
@@ -55,8 +59,8 @@ public class JDBCAsyncWriter implements AsyncEventListener {
           this.manager.write(event.getRegion(), event.getOperation(), event.getKey(), value);
           successfulEvents += 1;
         } catch (RuntimeException ex) {
-          // TODO: need to log exceptions here
-          throw ex;
+          // TODO improve the following logging
+          logger.error("Exception processing event " + event, ex);
         }
       }
     } finally {
diff --git a/geode-connectors/src/main/java/org/apache/geode/connectors/jdbc/JDBCManager.java
b/geode-connectors/src/main/java/org/apache/geode/connectors/jdbc/JDBCManager.java
index d9a64f6..86980e0 100644
--- a/geode-connectors/src/main/java/org/apache/geode/connectors/jdbc/JDBCManager.java
+++ b/geode-connectors/src/main/java/org/apache/geode/connectors/jdbc/JDBCManager.java
@@ -19,14 +19,11 @@ import java.sql.DatabaseMetaData;
 import java.sql.DriverManager;
 import java.sql.PreparedStatement;
 import java.sql.ResultSet;
+import java.sql.ResultSetMetaData;
 import java.sql.SQLException;
-import java.sql.Statement;
 import java.util.ArrayList;
 import java.util.Collections;
-import java.util.HashSet;
 import java.util.List;
-import java.util.Map;
-import java.util.Set;
 import java.util.concurrent.ConcurrentHashMap;
 import java.util.concurrent.ConcurrentMap;
 
@@ -197,6 +194,7 @@ public class JDBCManager {
     // }
     // });
     String query = getQueryString(tableName, columnList, operation);
+    System.out.println("query=" + query);
     Connection con = getConnection();
     try {
       return con.prepareStatement(query);
@@ -221,7 +219,7 @@ public class JDBCManager {
         continue;
       }
       String columnName = mapFieldNameToColumnName(fieldName, tableName);
-      if (columnName.equals(keyColumnName)) {
+      if (columnName.equalsIgnoreCase(keyColumnName)) {
         continue;
       }
       Object columnValue = value.getField(fieldName);
@@ -251,16 +249,31 @@ public class JDBCManager {
       Connection con = getConnection();
       try {
         DatabaseMetaData metaData = con.getMetaData();
-        ResultSet primaryKeys = metaData.getPrimaryKeys(null, null, k);
+        ResultSet tablesRS = metaData.getTables(null, null, "%", null);
+        String realTableName = null;
+        while (tablesRS.next()) {
+          String name = tablesRS.getString("TABLE_NAME");
+          if (name.equalsIgnoreCase(k)) {
+            if (realTableName != null) {
+              throw new IllegalStateException("Duplicate tables that match region name");
+            }
+            realTableName = name;
+          }
+        }
+        if (realTableName == null) {
+          throw new IllegalStateException("no table was found that matches " + k);
+        }
+        ResultSet primaryKeys = metaData.getPrimaryKeys(null, null, realTableName);
         if (!primaryKeys.next()) {
           throw new IllegalStateException(
               "The table " + k + " does not have a primary key column.");
         }
-        if (!primaryKeys.isLast()) {
+        String key = primaryKeys.getString("COLUMN_NAME");
+        if (primaryKeys.next()) {
           throw new IllegalStateException(
               "The table " + k + " has more than one primary key column.");
         }
-        return primaryKeys.getString("COLUMN_NAME");
+        return key;
       } catch (SQLException e) {
         handleSQLException(e);
         return null; // never reached
@@ -276,4 +289,32 @@ public class JDBCManager {
     // TODO: check config for mapping
     return region.getName();
   }
+
+  private void printResultSet(ResultSet rs) {
+    System.out.println("Printing ResultSet:");
+    try {
+      int size = 0;
+      ResultSetMetaData rsmd = rs.getMetaData();
+      int columnsNumber = rsmd.getColumnCount();
+      while (rs.next()) {
+        size++;
+        for (int i = 1; i <= columnsNumber; i++) {
+          if (i > 1)
+            System.out.print(",  ");
+          String columnValue = rs.getString(i);
+          System.out.print(rsmd.getColumnName(i) + ": " + columnValue);
+        }
+        System.out.println("");
+      }
+      System.out.println("size=" + size);
+    } catch (SQLException ex) {
+      System.out.println("Exception while printing result set" + ex);
+    } finally {
+      try {
+        rs.beforeFirst();
+      } catch (SQLException e) {
+        System.out.println("Exception while calling beforeFirst" + e);
+      }
+    }
+  }
 }
diff --git a/geode-connectors/src/test/java/org/apache/geode/connectors/jdbc/JDBCAsyncWriterIntegrationTest.java
b/geode-connectors/src/test/java/org/apache/geode/connectors/jdbc/JDBCAsyncWriterIntegrationTest.java
index 2152ad9..88ef4b9 100644
--- a/geode-connectors/src/test/java/org/apache/geode/connectors/jdbc/JDBCAsyncWriterIntegrationTest.java
+++ b/geode-connectors/src/test/java/org/apache/geode/connectors/jdbc/JDBCAsyncWriterIntegrationTest.java
@@ -17,11 +17,7 @@ package org.apache.geode.connectors.jdbc;
 import static org.apache.geode.distributed.ConfigurationProperties.MCAST_PORT;
 import static org.assertj.core.api.Assertions.*;
 
-import java.sql.Connection;
-import java.sql.DriverManager;
-import java.sql.ResultSet;
-import java.sql.ResultSetMetaData;
-import java.sql.Statement;
+import java.sql.*;
 import java.util.Properties;
 import java.util.concurrent.TimeUnit;
 
@@ -85,7 +81,7 @@ public class JDBCAsyncWriterIntegrationTest {
     conn = DriverManager.getConnection(connectionURL);
     stmt = conn.createStatement();
     stmt.execute("Create Table " + regionTableName
-        + " (id varchar(10) primary key, name varchar(10), age int)");
+        + " (id varchar(10) primary key not null, name varchar(10), age int)");
   }
 
   public void closeDB() throws Exception {
@@ -108,6 +104,28 @@ public class JDBCAsyncWriterIntegrationTest {
   }
 
   @Test
+  public void canExecuteSQLOnDataBase() throws Exception {
+    stmt.execute("Insert into " + regionTableName + " values ('1', 'emp1', 10)");
+    stmt.execute("Select * from " + regionTableName);
+    DatabaseMetaData metaData = conn.getMetaData();
+    ResultSet tablesRS = metaData.getTables(null, null, "%", null);
+    String realTableName = null;
+    while (tablesRS.next()) {
+      String name = tablesRS.getString("TABLE_NAME");
+      if (name.equalsIgnoreCase(this.regionTableName)) {
+        if (realTableName != null) {
+          throw new IllegalStateException("Duplicate tables that match region name");
+        }
+        realTableName = name;
+      }
+    }
+    if (realTableName == null) {
+      throw new IllegalStateException("no table was found that matches " + regionTableName);
+    }
+    ResultSet primaryKeys = metaData.getPrimaryKeys(null, null, realTableName);
+  }
+
+  @Test
   public void canInstallJDBCAsyncWriterOnRegion() {
     Region employees = createRegionWithJDBCAsyncWriter(regionTableName, getRequiredProperties());
     PdxInstance pdx1 = cache.createPdxInstanceFactory("Employee").writeString("name", "Emp1")

-- 
To stop receiving notification emails like this one, please contact
['"commits@geode.apache.org" <commits@geode.apache.org>'].

Mime
View raw message