Thread: Patches for DatabaseMetaData.java for Postgresql 7.1.3 (sorry, here is the diff -c with explanations)

This patch fixes a null pointer exception (because the return type of 'v' is
not in the switch statement causing the variable relKind to be assigned a
null value and a subsequent call to relKind.getBytes() throwing the
exception) when invoking method ResultSetMetaData.getTables().
In addition, this fix will properly recognize both the schema pattern and
the table name pattern with both "%" and "_" SQL wildcard characters.

*** DatabaseMetaData.java.orig Fri Feb 16 11:45:00 2001
--- DatabaseMetaData.java Sat Jan  5 13:46:01 2002
***************
*** 1635,1641 ****
      if(types==null)
        types = defaultTableTypes;

!     if(tableNamePattern==null)
        tableNamePattern="%";

      // the field descriptors for the new ResultSet
--- 1635,1644 ----
      if(types==null)
        types = defaultTableTypes;

!     if((schemaPattern==null) || (schemaPattern.length()==0))
!       schemaPattern="%";
!
!     if((tableNamePattern==null) || (tableNamePattern.length()==0))
        tableNamePattern="%";

      // the field descriptors for the new ResultSet
***************
*** 1650,1656 ****
      f[4] = new Field(connection, "REMARKS", iVarcharOid, 32);

      // Now form the query
!     StringBuffer sql = new StringBuffer("select relname,oid,relkind from
pg_class where (");
      boolean notFirst=false;
      for(int i=0;i<types.length;i++) {
        for(int j=0;j<getTableTypes.length;j++)
--- 1653,1660 ----
      f[4] = new Field(connection, "REMARKS", iVarcharOid, 32);

      // Now form the query
!     StringBuffer sql = new StringBuffer(
!         "select relname,pg_class.oid,relkind from pg_class, pg_user where
(");
      boolean notFirst=false;
      for(int i=0;i<types.length;i++) {
        for(int j=0;j<getTableTypes.length;j++)
***************
*** 1663,1673 ****
      }

      // Added by Stefan Andreasen <stefan@linux.kapow.dk>
      // Now take the pattern into account
!     sql.append(") and relname like '");
!     sql.append(tableNamePattern.toLowerCase());
!     sql.append("'");

      // Now run the query
      r = connection.ExecSQL(sql.toString());

--- 1667,1693 ----
      }

      // Added by Stefan Andreasen <stefan@linux.kapow.dk>
+     // Modified by Ed Yu <ekyu@asgnet.psc.sc.edu>
      // Now take the pattern into account
!     sql.append(") and relname");
!     if ((tableNamePattern.indexOf("%") >= 0) ||
!             (tableNamePattern.indexOf("_") >= 0))
!         sql.append(" like ");
!     else
!         sql.append(" = ");
!     sql.append("'" + tableNamePattern.toLowerCase() + "'");

+     // Added by Ed Yu <ekyu@asgnet.psc.sc.edu>
+     // Now take the schemaPattern into account
+     sql.append(" and pg_class.relowner = pg_user.usesysid");
+     sql.append(" and pg_user.usename");
+     if ((schemaPattern.indexOf("%") >= 0) ||
+             (schemaPattern.indexOf("_") >= 0))
+         sql.append(" like ");
+     else
+         sql.append(" = ");
+     sql.append("'" + schemaPattern + "'");
+
      // Now run the query
      r = connection.ExecSQL(sql.toString());

***************
*** 1686,1691 ****
--- 1706,1713 ----
     remarks = defaultRemarks;
   dr.close();

+  // JDBC definition for TABLE_TYPE - "TABLE", "VIEW", "SYSTEM TABLE",
+  // "GLOBAL TEMPORARY", "LOCAL TEMPORARY", "ALIAS", "SYNONYM".
   String relKind;
   switch (r.getBytes(3)[0]) {
   case 'r':
***************
*** 1697,1702 ****
--- 1719,1727 ----
   case 'S':
       relKind = "SEQUENCE";
       break;
+  case 'v':
+             relKind = "VIEW";
+             break;
   default:
       relKind = null;
   }
***************
*** 1704,1710 ****
   tuple[0] = null;  // Catalog name
   tuple[1] = null;  // Schema name
   tuple[2] = r.getBytes(1); // Table name
!  tuple[3] = relKind.getBytes(); // Table type
   tuple[4] = remarks;  // Remarks
   v.addElement(tuple);
        }
--- 1729,1743 ----
   tuple[0] = null;  // Catalog name
   tuple[1] = null;  // Schema name
   tuple[2] = r.getBytes(1); // Table name
!
!         // Added by Ed Yu <ekyu@asgnet.psc.sc.edu>
!  // Fix NullPointerException if return type is not handled in the
!  // above switch statement.
!  if (relKind==null)
!   tuple[3] = null;
!  else
!   tuple[3] = relKind.getBytes(); // Table type
!
   tuple[4] = remarks;  // Remarks
   v.addElement(tuple);
        }




Re: Patches for DatabaseMetaData.java for Postgresql 7.1.3 (sorry,

From
Bruce Momjian
Date:
This has been saved for the 7.3 release:

    http://candle.pha.pa.us/cgi-bin/pgpatches2

---------------------------------------------------------------------------

Ed Yu wrote:
> This patch fixes a null pointer exception (because the return type of 'v' is
> not in the switch statement causing the variable relKind to be assigned a
> null value and a subsequent call to relKind.getBytes() throwing the
> exception) when invoking method ResultSetMetaData.getTables().
> In addition, this fix will properly recognize both the schema pattern and
> the table name pattern with both "%" and "_" SQL wildcard characters.
>
> *** DatabaseMetaData.java.orig Fri Feb 16 11:45:00 2001
> --- DatabaseMetaData.java Sat Jan  5 13:46:01 2002
> ***************
> *** 1635,1641 ****
>       if(types==null)
>         types = defaultTableTypes;
>
> !     if(tableNamePattern==null)
>         tableNamePattern="%";
>
>       // the field descriptors for the new ResultSet
> --- 1635,1644 ----
>       if(types==null)
>         types = defaultTableTypes;
>
> !     if((schemaPattern==null) || (schemaPattern.length()==0))
> !       schemaPattern="%";
> !
> !     if((tableNamePattern==null) || (tableNamePattern.length()==0))
>         tableNamePattern="%";
>
>       // the field descriptors for the new ResultSet
> ***************
> *** 1650,1656 ****
>       f[4] = new Field(connection, "REMARKS", iVarcharOid, 32);
>
>       // Now form the query
> !     StringBuffer sql = new StringBuffer("select relname,oid,relkind from
> pg_class where (");
>       boolean notFirst=false;
>       for(int i=0;i<types.length;i++) {
>         for(int j=0;j<getTableTypes.length;j++)
> --- 1653,1660 ----
>       f[4] = new Field(connection, "REMARKS", iVarcharOid, 32);
>
>       // Now form the query
> !     StringBuffer sql = new StringBuffer(
> !         "select relname,pg_class.oid,relkind from pg_class, pg_user where
> (");
>       boolean notFirst=false;
>       for(int i=0;i<types.length;i++) {
>         for(int j=0;j<getTableTypes.length;j++)
> ***************
> *** 1663,1673 ****
>       }
>
>       // Added by Stefan Andreasen <stefan@linux.kapow.dk>
>       // Now take the pattern into account
> !     sql.append(") and relname like '");
> !     sql.append(tableNamePattern.toLowerCase());
> !     sql.append("'");
>
>       // Now run the query
>       r = connection.ExecSQL(sql.toString());
>
> --- 1667,1693 ----
>       }
>
>       // Added by Stefan Andreasen <stefan@linux.kapow.dk>
> +     // Modified by Ed Yu <ekyu@asgnet.psc.sc.edu>
>       // Now take the pattern into account
> !     sql.append(") and relname");
> !     if ((tableNamePattern.indexOf("%") >= 0) ||
> !             (tableNamePattern.indexOf("_") >= 0))
> !         sql.append(" like ");
> !     else
> !         sql.append(" = ");
> !     sql.append("'" + tableNamePattern.toLowerCase() + "'");
>
> +     // Added by Ed Yu <ekyu@asgnet.psc.sc.edu>
> +     // Now take the schemaPattern into account
> +     sql.append(" and pg_class.relowner = pg_user.usesysid");
> +     sql.append(" and pg_user.usename");
> +     if ((schemaPattern.indexOf("%") >= 0) ||
> +             (schemaPattern.indexOf("_") >= 0))
> +         sql.append(" like ");
> +     else
> +         sql.append(" = ");
> +     sql.append("'" + schemaPattern + "'");
> +
>       // Now run the query
>       r = connection.ExecSQL(sql.toString());
>
> ***************
> *** 1686,1691 ****
> --- 1706,1713 ----
>      remarks = defaultRemarks;
>    dr.close();
>
> +  // JDBC definition for TABLE_TYPE - "TABLE", "VIEW", "SYSTEM TABLE",
> +  // "GLOBAL TEMPORARY", "LOCAL TEMPORARY", "ALIAS", "SYNONYM".
>    String relKind;
>    switch (r.getBytes(3)[0]) {
>    case 'r':
> ***************
> *** 1697,1702 ****
> --- 1719,1727 ----
>    case 'S':
>        relKind = "SEQUENCE";
>        break;
> +  case 'v':
> +             relKind = "VIEW";
> +             break;
>    default:
>        relKind = null;
>    }
> ***************
> *** 1704,1710 ****
>    tuple[0] = null;  // Catalog name
>    tuple[1] = null;  // Schema name
>    tuple[2] = r.getBytes(1); // Table name
> !  tuple[3] = relKind.getBytes(); // Table type
>    tuple[4] = remarks;  // Remarks
>    v.addElement(tuple);
>         }
> --- 1729,1743 ----
>    tuple[0] = null;  // Catalog name
>    tuple[1] = null;  // Schema name
>    tuple[2] = r.getBytes(1); // Table name
> !
> !         // Added by Ed Yu <ekyu@asgnet.psc.sc.edu>
> !  // Fix NullPointerException if return type is not handled in the
> !  // above switch statement.
> !  if (relKind==null)
> !   tuple[3] = null;
> !  else
> !   tuple[3] = relKind.getBytes(); // Table type
> !
>    tuple[4] = remarks;  // Remarks
>    v.addElement(tuple);
>         }
>
>
>
>
> ---------------------------(end of broadcast)---------------------------
> TIP 4: Don't 'kill -9' the postmaster
>

--
  Bruce Momjian                        |  http://candle.pha.pa.us
  pgman@candle.pha.pa.us               |  (610) 853-3000
  +  If your life is a hard drive,     |  830 Blythe Avenue
  +  Christ can be your backup.        |  Drexel Hill, Pennsylvania 19026