changeset 311:0f55802721dd

Add check on null to prevent NPE and added missing tbl.close(); statements to close ResultSet resources.
author Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
date Thu, 22 Aug 2019 18:30:08 +0200 (2019-08-22)
parents 5ddab9d8d3dd
children df815a403bf4
files src/main/java/nl/cwi/monetdb/util/SQLExporter.java src/main/java/nl/cwi/monetdb/util/XMLExporter.java
diffstat 2 files changed, 37 insertions(+), 20 deletions(-) [+]
line wrap: on
line diff
--- a/src/main/java/nl/cwi/monetdb/util/SQLExporter.java
+++ b/src/main/java/nl/cwi/monetdb/util/SQLExporter.java
@@ -63,20 +63,27 @@ public final class SQLExporter extends E
 			changeSchema(schema);
 
 		// handle views directly
-		if (type.indexOf("VIEW") != -1) {
+		if (type.indexOf("VIEW") != -1) {	// for types: VIEW and SYSTEM VIEW
 			final String[] types = new String[1];
 			types[0] = type;
 			final ResultSet tbl = dbmd.getTables(catalog, schema, name, types);
-			if (!tbl.next())
-				throw new SQLException("Whoops no meta data for view " + fqname);
+			if (tbl != null) {
+				if (!tbl.next()) {
+					tbl.close();
+					throw new SQLException("Whoops no meta data for view " + fqname);
+				}
 
-			// This will probably only work for MonetDB
-			final String remarks = tbl.getString("REMARKS");	// for MonetDB driver this contains the view definition
-			if (remarks == null) {
-				out.println("-- invalid " + type + " " + fqname + ": no definition found");
-			} else {
-				out.print("CREATE " + type + " " + fqname + " AS ");
-				out.println(remarks.replaceFirst("create view [^ ]+ as", "").trim());
+				// This will only work for MonetDB JDBC driver
+				final String remarks = tbl.getString("REMARKS");	// for MonetDB driver this contains the view definition (if no comment is set) or else the comment
+				if (remarks == null) {
+					out.println("-- invalid " + type + " " + fqname + ": no definition found");
+				} else {
+					// TODO when it does not contain the  create view ...  command, but a comment, we need to use query:
+					// "select query from sys.tables where name = '" + name + "' and schema_id in (select id from sys.schemas where name = '" + schema + "')"
+					out.print("CREATE " + type + " " + fqname + " AS ");
+					out.println(remarks.replaceFirst("create view [^ ]+ as", "").trim());
+				}
+				tbl.close();
 			}
 			return;
 		}
--- a/src/main/java/nl/cwi/monetdb/util/XMLExporter.java
+++ b/src/main/java/nl/cwi/monetdb/util/XMLExporter.java
@@ -34,20 +34,30 @@ public final class XMLExporter extends E
 		throws SQLException
 	{
 		// handle views directly
-		if (type.indexOf("VIEW") != -1) {
+		if (type.indexOf("VIEW") != -1) {	// for types: VIEW and SYSTEM VIEW
 			final String[] types = new String[1];
 			types[0] = type;
-
 			final ResultSet tbl = dbmd.getTables(catalog, schema, name, types);
-			if (!tbl.next())
-				throw new SQLException("Whoops no data for " + name);
+			if (tbl != null) {
+				final String fqname = (!useSchema ? dq(schema) + "." : "") + dq(name);
+				if (!tbl.next()) {
+					tbl.close();
+					throw new SQLException("Whoops no meta data for view " + fqname);
+				}
 
-			// This will probably only work for MonetDB
-			out.print("<!-- unable to represent: CREATE " + type + " " +
-				(!useSchema ? dq(schema) + "." : "") + dq(name));
-			out.print(" AS ");
-			out.print(tbl.getString("REMARKS").trim());
-			out.print(" -->");
+				// This will only work for MonetDB JDBC driver
+				final String remarks = tbl.getString("REMARKS");	// for MonetDB driver this contains the view definition (if no comment is set) or else the comment
+				if (remarks == null) {
+					out.print("<!-- unable to represent: CREATE " + type + " " + fqname + " AS ? -->");
+				} else {
+					// TODO when it does not contain the  create view ...  command, but a comment, we need to use query:
+					// "select query from sys.tables where name = '" + name + "' and schema_id in (select id from sys.schemas where name = '" + schema + "')"
+					out.print("<!-- CREATE " + type + " " + fqname + " AS ");
+					out.print(remarks.replaceFirst("create view [^ ]+ as", "").trim());
+					out.println(" -->");
+				}
+				tbl.close();
+			}
 			return;
 		}