You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@geode.apache.org by ds...@apache.org on 2017/10/26 17:57:04 UTC
[geode] branch feature/GEODE-3781 updated: implemented get primary
key
This is an automated email from the ASF dual-hosted git repository.
dschneider 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 ba2ad22 implemented get primary key
ba2ad22 is described below
commit ba2ad2260d8f4018514bfb0b35ab925fb6f09673
Author: Darrel Schneider <ds...@pivotal.io>
AuthorDate: Thu Oct 26 10:56:45 2017 -0700
implemented get primary key
---
.../apache/geode/connectors/jdbc/JDBCManager.java | 38 ++++++++++++++++++----
1 file changed, 32 insertions(+), 6 deletions(-)
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 fceb8dc..d9a64f6 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
@@ -15,8 +15,10 @@
package org.apache.geode.connectors.jdbc;
import java.sql.Connection;
+import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
+import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
@@ -79,8 +81,7 @@ public class JDBCManager {
}
pstmt.execute();
} catch (SQLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
+ handleSQLException(e);
} finally {
clearStatement(pstmt);
}
@@ -192,16 +193,16 @@ public class JDBCManager {
// try {
// return con.prepareStatement(k);
// } catch (SQLException e) {
- // throw new IllegalStateException("TODO handle exception", e);
+ // handleSQLException(e);
// }
// });
String query = getQueryString(tableName, columnList, operation);
- System.out.println("query=" + query);
Connection con = getConnection();
try {
return con.prepareStatement(query);
} catch (SQLException e) {
- throw new IllegalStateException("TODO handle exception", e);
+ handleSQLException(e);
+ return null; // this line is never reached
}
}
@@ -242,8 +243,33 @@ public class JDBCManager {
return fieldName;
}
+ private final ConcurrentMap<String, String> tableToPrimaryKeyMap = new ConcurrentHashMap<>();
+
private String getKeyColumnName(String tableName) {
- return "id"; // TODO: do not hard code this!
+ return tableToPrimaryKeyMap.computeIfAbsent(tableName, k -> {
+ // TODO: check config for key column
+ Connection con = getConnection();
+ try {
+ DatabaseMetaData metaData = con.getMetaData();
+ ResultSet primaryKeys = metaData.getPrimaryKeys(null, null, k);
+ if (!primaryKeys.next()) {
+ throw new IllegalStateException(
+ "The table " + k + " does not have a primary key column.");
+ }
+ if (!primaryKeys.isLast()) {
+ throw new IllegalStateException(
+ "The table " + k + " has more than one primary key column.");
+ }
+ return primaryKeys.getString("COLUMN_NAME");
+ } catch (SQLException e) {
+ handleSQLException(e);
+ return null; // never reached
+ }
+ });
+ }
+
+ private void handleSQLException(SQLException e) {
+ throw new IllegalStateException("NYI: handleSQLException", e);
}
private String getTableName(Region region) {
--
To stop receiving notification emails like this one, please contact
['"commits@geode.apache.org" <co...@geode.apache.org>'].