postgresmodule.java

来自「RESIN 3.2 最新源码」· Java 代码 · 共 2,483 行 · 第 1/5 页

JAVA
2,483
字号
  {    return pg_lo_create(env, conn);  }  /**   * Export a large object to a file   */  public static boolean pg_lo_export(Env env,                                     @NotNull Postgres conn,                                     int oid,                                     Path path)  {    try {      //@todo conn should be optional      // LargeObjectManager lobManager;      Object lobManager;      //org.postgresql.largeobject.LargeObjectManager      Class cl = Class.forName("org.postgresql.PGConnection");      Method method = cl.getDeclaredMethod("getLargeObjectAPI", null);      Connection pgconn = conn.getJavaConnection();      lobManager = method.invoke(pgconn, new Object[] {});      // lobManager = ((org.postgresql.PGConnection)conn).getLargeObjectAPI();      cl = Class.forName("org.postgresql.largeobject.LargeObjectManager");      method = cl.getDeclaredMethod("open", new Class[] {Integer.TYPE});      Object lobj = method.invoke(lobManager, new Object[] {oid});      cl = Class.forName("org.postgresql.largeobject.LargeObject");      method = cl.getDeclaredMethod("getInputStream", null);      Object isObj = method.invoke(lobj, new Object[] {});      InputStream is = (InputStream)isObj;      // Open the file      WriteStream os = path.openWrite();      // copy the data from the large object to the file      os.writeStream(is);      os.close();      is.close();      // Close the large object      method = cl.getDeclaredMethod("close", null);      method.invoke(lobj, new Object[] {});      return true;    } catch (Exception ex) {      log.log(Level.FINE, ex.toString(), ex);      return false;    }  }  /**   * pg_lo_export() alias.   */  public static boolean pg_loexport(Env env,                                     @NotNull Postgres conn,                                     int oid,                                     Path path)  {    return pg_lo_export(env, conn, oid, path);  }    /**   * Import a large object from file   */  @ReturnNullAsFalse  public static LongValue pg_lo_import(Env env,                                       @NotNull Postgres conn,                                       Path path)  {    try {      //@todo conn should be optional      LongValue value = pg_lo_create(env, conn);      if (value != null) {        int oid = value.toInt();        Object largeObject = pg_lo_open(env, conn, oid, "w");        String data = "";        // Open the file        ReadStream is = path.openRead();        writeLobInternal(largeObject, is, Integer.MAX_VALUE);        pg_lo_close(env, largeObject);        is.close();        return LongValue.create(oid);      }    } catch (Exception ex) {      log.log(Level.FINE, ex.toString(), ex);    }    return null;  }  /**   * pg_lo_import() alias.   */  @ReturnNullAsFalse  public static LongValue pg_loimport(Env env,                                      @NotNull Postgres conn,                                      Path path)  {    return pg_lo_import(env, conn, path);  }    /**   * Open a large object   */  @ReturnNullAsFalse  public static Object pg_lo_open(Env env,                                  @NotNull Postgres conn,                                  int oid,                                  String mode)  {    try {      Object largeObject = null;      // LargeObjectManager lobManager;      Object lobManager;      //org.postgresql.largeobject.LargeObjectManager      Class cl = Class.forName("org.postgresql.PGConnection");      Method method = cl.getDeclaredMethod("getLargeObjectAPI", null);      Connection pgconn = conn.getJavaConnection();      lobManager = method.invoke(pgconn, new Object[] {});      cl = Class.forName("org.postgresql.largeobject.LargeObjectManager");      method = cl.getDeclaredMethod("open", new Class[] {Integer.TYPE, Integer.TYPE});      boolean write = mode.indexOf("w") >= 0;      boolean read = mode.indexOf("r") >= 0;      int modeREAD = cl.getDeclaredField("READ").getInt(null);      int modeREADWRITE = cl.getDeclaredField("READWRITE").getInt(null);      int modeWRITE = cl.getDeclaredField("WRITE").getInt(null);      int intMode = modeREAD;      if (read) {        if (write) {          intMode = modeREADWRITE;        }      } else if (write) {        intMode = modeWRITE;      }      largeObject = method.invoke(lobManager, new Object[] {oid, intMode});      return largeObject;    } catch (Exception ex) {      env.warning(L.l("Unable to open PostgreSQL large object"));      log.log(Level.FINE, ex.toString(), ex);      return null;    }  }  /**   * pg_lo_open() alias.   */  @ReturnNullAsFalse  public static Object pg_loopen(Env env,                                 @NotNull Postgres conn,                                 int oid,                                 String mode)  {    return pg_lo_open(env, conn, oid, mode);  }    /**   * Reads an entire large object and send straight to browser   */  @ReturnNullAsFalse  public static LongValue pg_lo_read_all(Env env,                                         Object largeObject)  {    try {      StringValue contents = pg_lo_read(env, largeObject, -1);            if (contents != null) {        env.getOut().print(contents);      }    } catch (Exception ex) {      log.log(Level.FINE, ex.toString(), ex);    }    return null;  }  /**   * pg_lo_read_all() alias.   */  @ReturnNullAsFalse  public static LongValue pg_loreadall(Env env,                                       Object largeObject)  {    return pg_lo_read_all(env, largeObject);  }    /**   * Read a large object   */  @ReturnNullAsFalse  public static StringValue pg_lo_read(Env env,				       Object largeObject,				       @Optional("-1") int len)  {    try {      if (len < 0) {        len = Integer.MAX_VALUE;      }      Class cl = Class.forName("org.postgresql.largeobject.LargeObject");      Method method = cl.getDeclaredMethod("getInputStream", null);      InputStream is = (InputStream) method.invoke(largeObject, new Object[] {});      try {	StringValue bb = env.createBinaryBuilder();	bb.appendReadAll(is, len);	return bb;      } finally {	is.close();      }    } catch (Exception ex) {      log.log(Level.FINE, ex.toString(), ex);      return null;    }  }  /**   * pg_lo_read() alias.   */  @ReturnNullAsFalse  public static StringValue pg_loread(Env env,				      Object largeObject,				      @Optional("-1") int len)  {    return pg_lo_read(env, largeObject, len);  }    /**   * Seeks position within a large object   */  public static boolean pg_lo_seek(Env env,                                   Object largeObject,                                   int offset,                                   @Optional int whence)  {    try {      Class cl = Class.forName("org.postgresql.largeobject.LargeObject");      int seekSET = cl.getDeclaredField("SEEK_SET").getInt(null);      int seekEND = cl.getDeclaredField("SEEK_END").getInt(null);      int seekCUR = cl.getDeclaredField("SEEK_CUR").getInt(null);      switch (whence) {      case PGSQL_SEEK_SET:        whence = seekSET;        break;      case PGSQL_SEEK_END:        whence = seekEND;        break;      default:        whence = seekCUR;      }      Method method = cl.getDeclaredMethod("seek", new Class[]{Integer.TYPE,Integer.TYPE});      method.invoke(largeObject, new Object[] {offset, whence});      return true;    } catch (Exception ex) {      log.log(Level.FINE, ex.toString(), ex);      return false;    }  }  /**   * Returns current seek position a of large object   */  public static int pg_lo_tell(Env env,                               Object largeObject)  {    try {      Class cl = Class.forName("org.postgresql.largeobject.LargeObject");      Method method = cl.getDeclaredMethod("tell", null);      Object obj = method.invoke(largeObject, new Object[] {});      return Integer.parseInt(obj.toString());    } catch (Exception ex) {      log.log(Level.FINE, ex.toString(), ex);      return -1;    }  }  /**   * Delete a large object   */  public static boolean pg_lo_unlink(Env env,                                     @NotNull Postgres conn,                                     int oid)  {    try {      // LargeObjectManager lobManager;      Object lobManager;      //org.postgresql.largeobject.LargeObjectManager      Class cl = Class.forName("org.postgresql.PGConnection");      Method method = cl.getDeclaredMethod("getLargeObjectAPI", null);      Connection pgconn = conn.getJavaConnection();      lobManager = method.invoke(pgconn, new Object[] {});      cl = Class.forName("org.postgresql.largeobject.LargeObjectManager");      method = cl.getDeclaredMethod("unlink", new Class[] {Integer.TYPE});      method.invoke(lobManager, new Object[] {oid});      return true;    } catch (Exception ex) {      log.log(Level.FINE, ex.toString(), ex);      return false;    }  }  /**   * pg_lo_unlink() alias.   */  public static boolean pg_lounlink(Env env,                                    @NotNull Postgres conn,                                    int oid)  {    return pg_lo_unlink(env, conn, oid);  }    /**   * Write to a large object   */  @ReturnNullAsFalse  public static LongValue pg_lo_write(Env env,                                      @NotNull Object largeObject,                                      String data,                                      @Optional int len)  {    try {      if (len <= 0) {        len = data.length();      }      int written = len;      Class cl = Class.forName("org.postgresql.largeobject.LargeObject");      Method method = cl.getDeclaredMethod("write",                                           new Class[] {byte[].class,                                                        Integer.TYPE,                                                        Integer.TYPE});      method.invoke(largeObject, new Object[] {data.getBytes(), 0, len});      return LongValue.create(written);    } catch (Exception ex) {      log.log(Level.FINE, ex.toString(), ex);      return null;    }  }  /**   * pg_lo_write() alias.   */  @ReturnNullAsFalse  public static LongValue pg_lowrite(Env env,                                     @NotNull Object largeObject,                                     String data,                                     @Optional int len)  {    return pg_lo_write(env, largeObject, data, len);  }    /**   * Get meta data for table   */  @ReturnNullAsFalse  public static ArrayValue pg_meta_data(Env env,                                        @NotNull Postgres conn,                                        String tableName)  {    env.stub("pg_meta_data");    return null;  }  /**   * Returns the number of fields in a result   */  public static int pg_num_fields(Env env,                                  @NotNull PostgresResult result)  {    try {      return result.getFieldCount();    } catch (Exception ex) {      log.log(Level.FINE, ex.toString(), ex);      return -1;    }  }  /**   * pg_num_fields() alias.   */  public static int pg_numfields(Env env,                                 @NotNull PostgresResult result)  {    return pg_num_fields(env, result);  }    /**   * Returns the number of rows in a result   */  public static LongValue pg_num_rows(Env env,                                      @NotNull PostgresResult result)  {    int numRows = -1;    try {      if ((result != null) && (result.getResultSet() != null)) {        numRows = result.getNumRows();      }      if (numRows < 0) {        env.warning(L.l("supplied argument is not a valid PostgreSQL result resource"));      }    } catch (Exception ex) {      log.log(Level.FINE, ex.toString(), ex);    }    return LongValue.create(numRows);  }  /**   * pg_num_rows() alias.   */  public static LongValue pg_numrows(Env env,              

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?