Skip to content

Update GetPrimaryKeys.java #132

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/main/java/cn/hashdata/bireme/Bireme.java
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ protected void getTableInfo() throws BiremeException {
}

strArray = fullname.split("\\.");
cxt.tablesInfo.put(fullname, new Table(strArray[1], tableInfoMap, conn));
cxt.tablesInfo.put(fullname, new Table(fullname, tableInfoMap, conn));
}

try {
Expand Down
7 changes: 3 additions & 4 deletions src/main/java/cn/hashdata/bireme/GetPrimaryKeys.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,11 @@ public static Map<String, List<String>> getPrimaryKeys(
sb.append("(");

for (String fullname : tableMap.values()) {
strArray = fullname.split("\\.");
sb.append("'").append(strArray[1]).append("',");
sb.append("'").append(fullname).append("',");
}

String tableList = sb.toString().substring(0, sb.toString().length() - 1) + ")";
String tableSql = "select tablename from pg_tables where schemaname='public' and tablename in "
String tableSql = "select tablename from pg_tables where concat_ws('.',schemaname,tablename) in "

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

GP5.x does not support concat_ws.

+ tableList + "";
String prSql = "SELECT NULL AS TABLE_CAT, "
+ "n.nspname AS TABLE_SCHEM, "
Expand All @@ -45,7 +44,7 @@ public static Map<String, List<String>> getPrimaryKeys(
+ "FROM pg_catalog.pg_class ct JOIN pg_catalog.pg_attribute a ON (ct.oid = a.attrelid) "
+ "JOIN pg_catalog.pg_namespace n ON (ct.relnamespace = n.oid) "
+ "JOIN ( SELECT i.indexrelid, i.indrelid, i.indisprimary, information_schema._pg_expandarray(i.indkey) AS KEYS FROM pg_catalog.pg_index i) i ON (a.attnum = (i.keys).x AND a.attrelid = i.indrelid) "
+ "JOIN pg_catalog.pg_class ci ON (ci.oid = i.indexrelid) WHERE TRUE AND n.nspname = 'public' AND ct.relname in "
+ "JOIN pg_catalog.pg_class ci ON (ci.oid = i.indexrelid) WHERE TRUE AND concat_ws('.',n.nspname,ct.relname) in "
+ tableList + " AND i.indisprimary ORDER BY TABLE_NAME, pk_name, key_seq";
try {
statement = conn.createStatement();
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/cn/hashdata/bireme/Table.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,15 @@ public Table(String tableName, Map<String, List<String>> tableMap, Connection co
ResultSetMetaData rsMetaData = null;

try {
List<String> mapList = tableMap.get(tableName);
String[] strArray = tableName.split("\\.");
List<String> mapList = tableMap.get(strArray[1]);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The table name should include schema info. So you should modify the input parameters of getPrimaryKeys

String tableName = resultSet.getString("TABLE_SCHEM") + '.' + resultSet.getString("TABLE_NAME");

for (int i = 0; i < mapList.size(); i++) {
this.keyNames.add(mapList.get(i));
}

statement = conn.createStatement();

String queryTableInfo = "select * from public." + tableName + " where 1=2";
String queryTableInfo = "select * from " + tableName + " where 1=2";
rs = statement.executeQuery(queryTableInfo);
rsMetaData = rs.getMetaData();
this.ncolumns = rsMetaData.getColumnCount();
Expand Down