oraclemodule.java
来自「RESIN 3.2 最新源码」· Java 代码 · 共 2,223 行 · 第 1/5 页
JAVA
2,223 行
* Used with oci_bind_by_name() to bind RAW values. * */ public static boolean oci_bind_by_name(Env env, @NotNull OracleStatement stmt, @NotNull String placeholderName, Value variable, @Optional("0") int maxLength, @Optional("0") int type) { if ((type == OCI_B_CFILEE) || (type == SQLT_CFILE) || (type == SQLT_CFILEE)) { throw new UnimplementedException("oci_bind_by_name with CFILE"); } try { if (placeholderName == null) { return false; } if (!placeholderName.startsWith(":")) { placeholderName = ":" + placeholderName; } if (placeholderName.length() < 2) { return false; } Integer index = stmt.getBindingVariable(placeholderName); if (index == null) return false; int i = index.intValue(); PreparedStatement pstmt = stmt.getPreparedStatement(); CallableStatement callableStmt = (CallableStatement) pstmt; // XXX: We could use ParameterMetaData.getParameterMode // to figure out which parameters are IN and/or OUT and // then setObject and/or registerOutParameter according // to the parameter mode. However, getParameterMode() // is unsupported from Oracle JDBC drivers (Jun-2006). // // ParameterMetaData metaData = pstmt.getParameterMetaData(); // // int paramMode = metaData.getParameterMode(i); // // switch (paramMode) { // case ParameterMetaData.parameterModeInOut: // { // int oracleType = arrayPhpToOracleType[type]; // callableStmt.registerOutParameter(i, oracleType); // pstmt.setObject(i, variable.toJavaObject()); // break; // } // case ParameterMetaData.parameterModeOut: // { // int oracleType = arrayPhpToOracleType[type]; // callableStmt.registerOutParameter(i, oracleType); // break; // } // default: // case ParameterMetaData.parameterModeIn: // { // pstmt.setObject(i, variable.toJavaObject()); // break; // } // } switch (type) { case OCI_B_CURSOR: case SQLT_RSET: { // Assume the most common scenario: OUT parameter mode. int oracleType = arrayPhpToOracleType[type]; callableStmt.registerOutParameter(i, oracleType); // Set the cursor's underlying statement (see php/4404). Object cursor = variable.toJavaObject(); if ((cursor == null) || !(cursor instanceof OracleStatement)) { return false; } ((OracleStatement) cursor).setPreparedStatement(callableStmt); break; } case OCI_B_BFILE: // BFILE case SQLT_BFILEE: // ... case SQLT_FILE: // ... case SQLT_BLOB: // BLOB case OCI_B_BLOB: // ... case SQLT_CLOB: // CLOB case OCI_B_CLOB: // ... case OCI_B_ROWID: // ROWID case SQLT_RDD: // ... { // Assume the most common scenario: OUT parameter mode. int oracleType = arrayPhpToOracleType[type]; callableStmt.registerOutParameter(i, oracleType); Object ociLob = variable.toJavaObject(); if ((ociLob == null) || !(ociLob instanceof OracleOciLob)) { return false; } stmt.setOutParameter((OracleOciLob) ociLob); break; } default: { // Assume the most common scenario: IN parameter mode. // XXX: Check the spec. if there is a case where the // variable would not be initialized yet // stmt.putByNameVariable(placeholderName, variable); Object object = variable.toJavaObject(); if (object instanceof OracleOciCollection) { object = ((OracleOciCollection) object).getCollection(); } pstmt.setObject(i, object); } } return true; } catch (Exception ex) { log.log(Level.FINE, ex.toString(), ex); try { stmt.resetBindingVariables(); stmt.resetByNameVariables(); } catch (Exception ex2) { log.log(Level.FINE, ex2.toString(), ex2); } } return false; } /** * Cancels reading from cursor */ public static boolean oci_cancel(Env env, @NotNull OracleStatement stmt) { return oci_free_statement(env, stmt); } /** * Closes Oracle connection */ public static boolean oci_close(Env env, @NotNull Oracle conn) { if (conn == null) conn = getConnection(env); if (conn != null) { if (conn == getConnection(env)) env.removeSpecialValue("caucho.oracle"); conn.close(env); return true; } else return false; } /** * Commits outstanding statements */ public static boolean oci_commit(Env env, @NotNull Oracle conn) { try { return conn.commit(); } catch (Exception ex) { log.log(Level.FINE, ex.toString(), ex); return false; } } /** * Establishes a connection to the Oracle server */ public static Value oci_connect(Env env, @NotNull String username, @NotNull String password, @Optional String db, @Optional String charset, @Optional("0") int sessionMode) { // Note: The second and subsequent calls to oci_connect() with the // same parameters will return the connection handle returned from // the first call. This means that queries issued against one handle // are also applied to the other handles, because they are the same // handle. (source: php.net) if (!((charset == null) || charset.length() == 0)) { throw new UnimplementedException("oci_connect with charset"); } if (sessionMode == OCI_DEFAULT || sessionMode == OCI_SYSOPER || sessionMode == OCI_SYSDBA) { throw new UnimplementedException("oci_connect with session mode"); } return connectInternal(env, true, username, password, db, charset, sessionMode); } /** * Uses a PHP variable for the define-step during a SELECT */ public static boolean oci_define_by_name(Env env, @NotNull OracleStatement stmt, @NotNull String columnName, @NotNull @Reference Value variable, @Optional("0") int type) { // Example: // // $stmt = oci_parse($conn, "SELECT id, data FROM test"); // // /* the define MUST be done BEFORE oci_execute! */ // // oci_define_by_name($stmt, "ID", $myid); // oci_define_by_name($stmt, "DATA", $mydata); // // oci_execute($stmt, OCI_DEFAULT); // // while ($row = oci_fetch($stmt)) { // echo "id:" . $myid . "\n"; // echo "data:" . $mydata . "\n"; // } try { stmt.putByNameVariable(columnName, variable); return true; } catch (Exception ex) { log.log(Level.FINE, ex.toString(), ex); return false; } } /** * Returns the last error found */ @ReturnNullAsFalse public static String oci_error(Env env, @Optional Value resource) { if (resource instanceof DefaultValue) return null; JdbcConnectionResource conn = null; if (resource == null) { ConnectionInfo connectionInfo = (ConnectionInfo) env.getSpecialValue("caucho.oracle"); if (connectionInfo != null) { conn = connectionInfo.getConnection(); } } else { Object object = resource.toJavaObject(); if (object instanceof Oracle) { conn = ((Oracle) object).validateConnection(); } else { conn = ((OracleStatement) object).validateConnection(); } } return conn.getErrorMessage(); } /** * Executes a statement */ public static boolean oci_execute(Env env, @NotNull OracleStatement stmt, @Optional("0") int mode) { try { // Scenarios for oci_execute. // // // 1. Simple query: oci_parse / oci_execute // // $query = 'SELECT * FROM TEST'; // // $stmt = oci_parse($conn, $query); // // oci_execute($stmt, OCI_DEFAULT); // // $result = oci_fetch_array($stid, OCI_BOTH); // // // 2. Define by name: oci_parse / oci_define_by_name / oci_execute // // $stmt = oci_parse($conn, "SELECT id, data FROM test"); // // /* the define MUST be done BEFORE oci_execute! */ // // oci_define_by_name($stmt, "ID", $myid); // oci_define_by_name($stmt, "DATA", $mydata); // // oci_execute($stmt, OCI_DEFAULT); // // while ($row = oci_fetch($stmt)) { // echo "id:" . $myid . "\n"; // echo "data:" . $mydata . "\n"; // } // // // 3. Cursors: oci_new_cursor / oci_parse / // oci_bind_by_name / oci_execute($stmt) / oci_execute($cursor) // // $stmt = oci_parse($conn, // "CREATE OR REPLACE PACKAGE cauchopkgtestoci AS ". // "TYPE refcur IS REF CURSOR; ". // "PROCEDURE testproc (var_result out cauchopkgtestoci.refcur); ". // "END cauchopkgtestoci; "); // // oci_execute($stmt); // // $stmt = oci_parse($conn, // "CREATE OR REPLACE PACKAGE BODY cauchopkgtestoci IS ". // "PROCEDURE testproc (var_result out cauchopkgtestoci.refcur) IS ". // "BEGIN ". // "OPEN var_result FOR SELECT data FROM caucho.test; ". // "END testproc; ". // "END; "); // // oci_execute($stmt); // // $curs = oci_new_cursor($conn); // // $stmt = oci_parse($conn, "begin cauchopkgtestoci.testproc(:dataCursor); end;"); // // oci_bind_by_name($stmt, "dataCursor", $curs, 255, SQLT_RSET); // // oci_execute($stmt); // // oci_execute($curs); // // while ($data = oci_fetch_row($curs)) { // var_dump($data); // } // Get the underlying JDBC connection. Connection conn = stmt.getJavaConnection(); // Large Objects can not be used in auto-commit mode. conn.setAutoCommit(false); // Use the underlying callable statement to check different scenarios. CallableStatement callableStatement = stmt.getCallableStatement(); // Check for case (3) oci_execute($cursor); // A previous statement has been executed and holds the result set. Object cursorResult = null; try { cursorResult = callableStatement.getObject(1); if ((cursorResult != null) && (cursorResult instanceof ResultSet)) { ResultSet rs = (ResultSet) cursorResult; stmt.setResultSet(rs); return true; } } catch (Exception e) { // We assume this is not case (3). No error. } // Case (1) or executing a more complex query. stmt.execute(env); OracleOciLob ociLob = stmt.getOutParameter(); if (ociLob != null) { // Ex: java.sql.Clob outParameter = callableStatement.getClob(1); ociLob.setLob(callableStatement.getObject(1)); } // Fetch and assign values to corresponding binded variables // This is necessary for LOB support and // should be reworked calling a private fetch method instead. // oci_fetch(env, stmt); if (mode == OCI_COMMIT_ON_SUCCESS) { conn.commit(); } return true; } catch (Exception ex) { log.log(Level.FINE, ex.toString(), ex); try { stmt.resetBindingVariables(); stmt.resetByNameVariables(); } catch (Exception ex2) { log.log(Level.FINE, ex2.toString(), ex2); } return false; } } /** * Fetches all rows of result data into an array */ @ReturnNullAsFalse public static ArrayValue oci_fetch_all(Env env, @NotNull OracleStatement stmt, @NotNull Value output, @Optional int skip, @Optional int maxrows, @Optional int flags) { JdbcResultResource resource = null; ArrayValueImpl newArray = new ArrayValueImpl(); try { if (stmt == null) return null; resource = new JdbcResultResource(env, null, stmt.getResultSet(), null); ArrayValue value = resource.fetchArray(env, JdbcResultResource.FETCH_ASSOC); int curr = 0; if (maxrows == 0) maxrows = Integer.MAX_VALUE / 2; while (value != null && curr < maxrows) { newArray.put(LongValue.create(curr), value); curr++; value = resource.fetchArray(env, JdbcResultResource.FETCH_ASSOC); }
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?