String query = "SELECT count(*) FROM pg_attribute a, pg_class c WHERE a.attnum > 0 and a.attrelid =  c.oid and c.relname=?"; PreparedStatement pstmt = conn.getJavaConnection().prepareStatement(query); pstmt.setString(1, tableName); if (!pstmt.execute()) return null; ResultSet rs = pstmt.getResultSet(); rs.next(); int n = rs.getInt(1); pstmt.close();
Connection jdbcConn = conn.getJavaConnection(); DatabaseMetaData dbMetaData = jdbcConn.getMetaData();
  public static ArrayValue pg_convert(Env env,                                      @NotNull Postgres conn,                                      String tableName,                                      ArrayValue assocArray,                                      @Optional("0") int options)  {    try {      // XXX: options has not been implemented yet.      // XXX: the following PHP note has not been implemented yet.      // Note:  If there are boolean fields in table_name don't use      // the constant TRUE in assoc_array. It will be converted to the      // string 'TRUE' which is no valid entry for boolean fields in      // PostgreSQL. Use one of t, true, 1, y, yes instead.      if (options > 0) {        throw new UnimplementedException("pg_convert with options");      }      PostgresResult result;      // XXX: need to review and prevent SQL injection.      String query = "SELECT count(*) FROM pg_attribute a, pg_class c WHERE a.attnum > 0 and a.attrelid =  c.oid and c.relname=?";      PreparedStatement pstmt = conn.getJavaConnection().prepareStatement(query);      pstmt.setString(1, tableName);      if (!pstmt.execute())        return null;      ResultSet rs = pstmt.getResultSet();      rs.next();      int n = rs.getInt(1);      pstmt.close();      if (n < assocArray.getSize())        return null;      ArrayValueImpl newArray = new ArrayValueImpl();      for (Map.Entry<Value,Value> entry : assocArray.entrySet()) {        Value k = entry.getKey();        Value v = entry.getValue();        if (v instanceof StringValue) {          StringBuilderValue sb = new StringBuilderValue();          sb.append("'").append(v).append("'");          newArray.put(k, sb);        } else if (v instanceof NullValue) {          newArray.put(k, StringValue.create("NULL"));        } else {          newArray.put(k, v);        }      }      return newArray;    } catch (Exception ex) {      log.log(Level.FINE, ex.toString(), ex);      return null;    }  }