postgresmodule.java
来自「RESIN 3.2 最新源码」· Java 代码 · 共 2,483 行 · 第 1/5 页
JAVA
2,483 行
LongValue.MINUS_ONE, LongValue.ZERO); if (value.isLongConvertible()) { return LongValue.create(value.toLong()); } } catch (Exception ex) { log.log(Level.FINE, ex.toString(), ex); } return null; } /** * Returns the type name for the corresponding field number */ @ReturnNullAsFalse public static StringValue pg_field_type(Env env, @NotNull PostgresResult result, int fieldNumber) { try { ResultSetMetaData metaData = result.getMetaData(); fieldNumber++; String typeName = metaData.getColumnTypeName(fieldNumber); return (StringValue) StringValue.create(typeName); } catch (Exception ex) { log.log(Level.FINE, ex.toString(), ex); return null; } } /** * pg_field_type() alias. */ @ReturnNullAsFalse public static StringValue pg_fieldtype(Env env, @NotNull PostgresResult result, int fieldNumber) { return pg_field_type(env, result, fieldNumber); } /** * Free result memory */ public static boolean pg_free_result(Env env, @NotNull PostgresResult result) { try { result.close(); return true; } catch (Exception ex) { log.log(Level.FINE, ex.toString(), ex); return false; } } /** * pg_free_result() alias. */ public static boolean pg_freeresult(Env env, @NotNull PostgresResult result) { return pg_free_result(env, result); } /** * Gets SQL NOTIFY message */ @ReturnNullAsFalse public static ArrayValue pg_get_notify(Env env, @NotNull Postgres conn, @Optional("-1") int resultType) { try { if (resultType > 0) { throw new UnimplementedException("pg_get_notify with result type"); } // org.postgresql.PGConnection Class cl = Class.forName("org.postgresql.PGConnection"); // public PGNotification[] getNotifications() throws SQLException; Method method = cl.getDeclaredMethod("getNotifications", null); Connection pgconn = conn.getJavaConnection(); // getNotifications() Object notifications[] = (Object[]) method.invoke(pgconn, new Object[] {}); // org.postgresql.PGNotification cl = Class.forName("org.postgresql.PGNotification"); // public String getName(); Method methodGetName = cl.getDeclaredMethod("getName", null); // public int getPID(); Method methodGetPID = cl.getDeclaredMethod("getPID", null); ArrayValueImpl arrayValue = new ArrayValueImpl(); int n = notifications.length; StringValue k; LongValue v; for (int i=0; i<n; i++) { // getName() k = (StringValue) StringValue.create(methodGetName.invoke(notifications[i], new Object[] {})); // getPID() v = (LongValue) LongValue.create((Integer) methodGetPID.invoke(notifications[i], new Object[] {})); arrayValue.put(k, v); } return arrayValue; } catch (Exception ex) { log.log(Level.FINE, ex.toString(), ex); return null; } } /** * Gets the backend's process ID */ public static int pg_get_pid(Env env, @NotNull Postgres conn) { try { // @todo create a random string String randomLabel = "caucho_pg_get_pid_random_label"; pg_query(env, conn, "LISTEN "+randomLabel); pg_query(env, conn, "NOTIFY "+randomLabel); ArrayValue arrayValue = pg_get_notify(env, conn, -1); LongValue pid = (LongValue) arrayValue.get(StringValue.create(randomLabel)); return pid.toInt(); } catch (Exception ex) { log.log(Level.FINE, ex.toString(), ex); return -1; } } /** * Get asynchronous query result */ @ReturnNullAsFalse public static PostgresResult pg_get_result(Env env, @Optional Postgres conn) { // Three different scenarios for pg_get_result: // // 1. pg_send_prepare/pg_send_execute - php/431m // // pg_send_prepare($conn, "my_query", 'SELECT * FROM test WHERE data = $1'); // $res1 = pg_get_result($conn); // // pg_send_execute($conn, "my_query", array("Joe's Widgets")); // $res2 = pg_get_result($conn); // // pg_send_execute($conn, "my_query", array("Clothes Clothes Clothes")); // $res3 = pg_get_result($conn); // // 2. Multiquery with pg_send_query - php/430y // // pg_send_query($conn, "select * from test; select count(*) from test;"); // // // select * from test // $res = pg_get_result($conn); // $rows = pg_num_rows($res); // // // select count(*) from test // $res = pg_get_result($conn); // $rows = pg_num_rows($res); // // 3. Individual pg_send_query - php/431g // // $res = pg_send_query($conn, "select * from test;"); // var_dump($res); // $res = pg_get_result($conn); // var_dump($res); // // $res = pg_send_query($conn, "select * from doesnotexist;"); // var_dump($res); // $res = pg_get_result($conn); // var_dump($res); try { if (conn == null) conn = getConnection(env); PostgresResult result = (PostgresResult) conn.getResultResource(); // 1. pg_send_prepare/pg_send_execute if (conn.getAsynchronousStatement() != null) { if (conn.getAsynchronousResult() != null) { conn.setAsynchronousResult(null); return result; } return null; } // 2. pg_send_query if (conn.getAsynchronousResult() != null) { // Check for next result // Ex: pg_send_query($conn, "select * from test; select count(*) from test;"); Statement stmt = result.getJavaStatement(); if (stmt.getMoreResults()) { result = (PostgresResult) conn.createResult(env, stmt, stmt.getResultSet()); } else { // 3. Individual pg_send_query (clean up; no futher results) conn.setResultResource(null); } } conn.setAsynchronousResult(result); return result; } catch (Exception ex) { log.log(Level.FINE, ex.toString(), ex); return null; } } /** * Returns the host name associated with the connection */ @ReturnNullAsFalse public static String pg_host(Env env, @Optional Postgres conn) { try { if (conn == null) conn = getConnection(env); return conn.getHost(); } catch (Exception ex) { log.log(Level.FINE, ex.toString(), ex); return null; } } /** * Insert array into table */ public static boolean pg_insert(Env env, @NotNull Postgres conn, String tableName, ArrayValue assocArray, @Optional("-1") int options) { try { if (options > 0) { throw new UnimplementedException("pg_insert with options"); } StringBuilder names = new StringBuilder(); StringBuilder values = new StringBuilder(); boolean isFirst = true; for (Map.Entry<Value,Value> entry : assocArray.entrySet()) { Value k = entry.getKey(); Value v = entry.getValue(); if (isFirst) { isFirst = false; } else { values.append("','"); names.append(","); } values.append(v.toString()); names.append(k.toString()); } StringBuilder query = new StringBuilder(); query.append("INSERT INTO "); query.append(tableName); query.append("("); query.append(names); query.append(") VALUES('"); query.append(values); query.append("')"); pg_query(env, conn, query.toString()); return true; } catch (Exception ex) { log.log(Level.FINE, ex.toString(), ex); return false; } } /** * Get the last error message string of a connection */ @ReturnNullAsFalse public static StringValue pg_last_error(Env env, @Optional Postgres conn) { try { if (conn == null) conn = getConnection(env); return conn.error(env); } catch (Exception ex) { log.log(Level.FINE, ex.toString(), ex); return null; } } /** * pg_last_error() alias. */ @ReturnNullAsFalse public static StringValue pg_errormessage(Env env, @Optional Postgres conn) { return pg_last_error(env, conn); } /** * Returns the last notice message from PostgreSQL server */ @ReturnNullAsFalse public static String pg_last_notice(Env env, @NotNull Postgres conn) { try { SQLWarning warning = conn.getWarnings(); if (warning != null) return warning.toString(); else return null; } catch (Exception ex) { log.log(Level.FINE, ex.toString(), ex); return null; } } /** * Returns the last row's OID * * Note that: * - OID is a unique id. It will not work if the table was created with "No oid". * - MySql's "mysql_insert_id" receives the conection handler as argument but * PostgreSQL's "pg_last_oid" uses the result handler. */ @ReturnNullAsFalse public static String pg_last_oid(Env env, PostgresResult result) { try { Statement stmt = result.getJavaStatement(); Class cl = Class.forName("org.postgresql.jdbc2.AbstractJdbc2Statement"); Method method = cl.getDeclaredMethod("getLastOID", null); int oid = Integer.parseInt(method.invoke(stmt, new Object[] {}).toString()); if (oid > 0) return ""+oid; } catch (Exception ex) { log.log(Level.FINE, ex.toString(), ex); } return null; } @ReturnNullAsFalse public static String pg_getlastoid(Env env, PostgresResult result) { return pg_last_oid(env, result); } /** * Close a large object */ public static boolean pg_lo_close(Env env, Object largeObject) { try { Class cl = Class.forName("org.postgresql.largeobject.LargeObject"); Method method = cl.getDeclaredMethod("close", null); method.invoke(largeObject, new Object[] {}); // largeObject.close(); return true; } catch (Exception ex) { log.log(Level.FINE, ex.toString(), ex); return false; } } /** * pg_lo_close() alias. */ public static boolean pg_loclose(Env env, Object largeObject) { return pg_lo_close(env, largeObject); } /** * Create a large object */ @ReturnNullAsFalse public static LongValue pg_lo_create(Env env, @Optional Postgres conn) { try { int oid = -1; if (conn == null) conn = getConnection(env); // LargeObjectManager lobManager; Object lobManager; // org.postgresql.PGConnection Class cl = Class.forName("org.postgresql.PGConnection"); Method method = cl.getDeclaredMethod("getLargeObjectAPI", null); Connection pgconn = conn.getJavaConnection(); // Large Objects may not be used in auto-commit mode. pgconn.setAutoCommit(false); lobManager = method.invoke(pgconn, new Object[] {}); // lobManager = ((org.postgresql.PGConnection)conn).getLargeObjectAPI(); // org.postgresql.largeobject.LargeObjectManager cl = Class.forName("org.postgresql.largeobject.LargeObjectManager"); method = cl.getDeclaredMethod("create", null); Object oidObj = method.invoke(lobManager, new Object[] {}); oid = Integer.parseInt(oidObj.toString()); // oid = lobManager.create(); return LongValue.create(oid); } catch (Exception ex) { log.log(Level.FINE, ex.toString(), ex); return null; } } /** * pg_lo_create() alias */ @ReturnNullAsFalse public static LongValue pg_locreate(Env env, @Optional Postgres conn)
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?