📄 preparedstatement.java
字号:
vec.add(nv); } else if (c == 'Y') { c = 'M'; nv = new Object[3]; nv[1] = (new StringBuffer(((StringBuffer) v[1]) .toString())).append('d'); nv[0] = new Character('d'); nv[2] = new Integer(1); vec.add(nv); } ((StringBuffer) v[1]).append(c); if (c == ((Character) v[0]).charValue()) { v[2] = new Integer(n + 1); } else { v[0] = new Character(c); v[2] = new Integer(1); } } } int size = vecRemovelist.size(); for (int i = 0; i < size; i++) { v = (Object[]) vecRemovelist.get(i); vec.remove(v); } vecRemovelist.clear(); } int size = vec.size(); for (int i = 0; i < size; i++) { v = (Object[]) vec.get(i); c = ((Character) v[0]).charValue(); n = ((Integer) v[2]).intValue(); boolean bk = getSuccessor(c, n) != c; boolean atEnd = (((c == 's') || (c == 'm') || ((c == 'h') && toTime)) && bk); boolean finishesAtDate = (bk && (c == 'd') && !toTime); boolean containsEnd = (((StringBuffer) v[1]).toString() .indexOf('W') != -1); if ((!atEnd && !finishesAtDate) || (containsEnd)) { vecRemovelist.add(v); } } size = vecRemovelist.size(); for (int i = 0; i < size; i++) { vec.remove(vecRemovelist.get(i)); } vecRemovelist.clear(); v = (Object[]) vec.get(0); // might throw exception StringBuffer format = (StringBuffer) v[1]; format.setLength(format.length() - 1); return format.toString(); } /** * The number, types and properties of a ResultSet's columns are provided by * the getMetaData method. * * @return the description of a ResultSet's columns * * @exception SQLException * if a database-access error occurs. */ public java.sql.ResultSetMetaData getMetaData() throws SQLException { // // We could just tack on a LIMIT 0 here no matter what the // statement, and check if a result set was returned or not, // but I'm not comfortable with that, myself, so we take // the "safer" road, and only allow metadata for _actual_ // SELECTS (but not SHOWs). // // CALL's are trapped further up and you end up with a // CallableStatement anyway. // if (!isSelectQuery()) { return null; } PreparedStatement mdStmt = null; java.sql.ResultSet mdRs = null; if (this.pstmtResultMetaData == null) { try { mdStmt = new PreparedStatement(this.connection, this.originalSql, this.currentCatalog, this.parseInfo); mdStmt.setMaxRows(0); int paramCount = this.parameterValues.length; for (int i = 1; i <= paramCount; i++) { mdStmt.setString(i, ""); //$NON-NLS-1$ } boolean hadResults = mdStmt.execute(); if (hadResults) { mdRs = mdStmt.getResultSet(); this.pstmtResultMetaData = mdRs.getMetaData(); } else { this.pstmtResultMetaData = new ResultSetMetaData( new Field[0], this.connection.getUseOldAliasMetadataBehavior()); } } finally { SQLException sqlExRethrow = null; if (mdRs != null) { try { mdRs.close(); } catch (SQLException sqlEx) { sqlExRethrow = sqlEx; } mdRs = null; } if (mdStmt != null) { try { mdStmt.close(); } catch (SQLException sqlEx) { sqlExRethrow = sqlEx; } mdStmt = null; } if (sqlExRethrow != null) { throw sqlExRethrow; } } } return this.pstmtResultMetaData; } protected boolean isSelectQuery() { return StringUtils.startsWithIgnoreCaseAndWs( StringUtils.stripComments(this.originalSql, "'\"", "'\"", true, false, true, true), "SELECT"); } /** * @see PreparedStatement#getParameterMetaData() */ public ParameterMetaData getParameterMetaData() throws SQLException { if (this.parameterMetaData == null) { if (this.connection.getGenerateSimpleParameterMetadata()) { this.parameterMetaData = new MysqlParameterMetadata(this.parameterCount); } else { this.parameterMetaData = new MysqlParameterMetadata( null, this.parameterCount); } } return this.parameterMetaData; } ParseInfo getParseInfo() { return this.parseInfo; } private final char getSuccessor(char c, int n) { return ((c == 'y') && (n == 2)) ? 'X' : (((c == 'y') && (n < 4)) ? 'y' : ((c == 'y') ? 'M' : (((c == 'M') && (n == 2)) ? 'Y' : (((c == 'M') && (n < 3)) ? 'M' : ((c == 'M') ? 'd' : (((c == 'd') && (n < 2)) ? 'd' : ((c == 'd') ? 'H' : (((c == 'H') && (n < 2)) ? 'H' : ((c == 'H') ? 'm' : (((c == 'm') && (n < 2)) ? 'm' : ((c == 'm') ? 's' : (((c == 's') && (n < 2)) ? 's' : 'W')))))))))))); } /** * Used to escape binary data with hex for mb charsets * * @param buf * @param packet * @param size * @throws SQLException */ private final void hexEscapeBlock(byte[] buf, Buffer packet, int size) throws SQLException { for (int i = 0; i < size; i++) { byte b = buf[i]; int lowBits = (b & 0xff) / 16; int highBits = (b & 0xff) % 16; packet.writeByte(HEX_DIGITS[lowBits]); packet.writeByte(HEX_DIGITS[highBits]); } } private void initializeFromParseInfo() throws SQLException { this.staticSqlStrings = this.parseInfo.staticSql; this.hasLimitClause = this.parseInfo.foundLimitClause; this.isLoadDataQuery = this.parseInfo.foundLoadData; this.firstCharOfStmt = this.parseInfo.firstStmtChar; this.parameterCount = this.staticSqlStrings.length - 1; this.parameterValues = new byte[this.parameterCount][]; this.parameterStreams = new InputStream[this.parameterCount]; this.isStream = new boolean[this.parameterCount]; this.streamLengths = new int[this.parameterCount]; this.isNull = new boolean[this.parameterCount]; clearParameters(); for (int j = 0; j < this.parameterCount; j++) { this.isStream[j] = false; } this.statementAfterCommentsPos = this.parseInfo.statementStartPos; } boolean isNull(int paramIndex) { return this.isNull[paramIndex]; } private final int readblock(InputStream i, byte[] b) throws SQLException { try { return i.read(b); } catch (Throwable E) { throw SQLError.createSQLException(Messages.getString("PreparedStatement.56") //$NON-NLS-1$ + E.getClass().getName(), SQLError.SQL_STATE_GENERAL_ERROR); } } private final int readblock(InputStream i, byte[] b, int length) throws SQLException { try { int lengthToRead = length; if (lengthToRead > b.length) { lengthToRead = b.length; } return i.read(b, 0, lengthToRead); } catch (Throwable E) { throw SQLError.createSQLException(Messages.getString("PreparedStatement.55") //$NON-NLS-1$ + E.getClass().getName(), SQLError.SQL_STATE_GENERAL_ERROR); } } /** * Closes this statement, releasing all resources * * @param calledExplicitly * was this called by close()? * * @throws SQLException * if an error occurs */ protected void realClose(boolean calledExplicitly, boolean closeOpenResults) throws SQLException { if (this.useUsageAdvisor) { if (this.numberOfExecutions <= 1) { String message = Messages.getString("PreparedStatement.43"); //$NON-NLS-1$ this.eventSink.consumeEvent(new ProfilerEvent( ProfilerEvent.TYPE_WARN, "", this.currentCatalog, //$NON-NLS-1$ this.connectionId, this.getId(), -1, System .currentTimeMillis(), 0, Constants.MILLIS_I18N, null, this.pointOfOrigin, message)); } } super.realClose(calledExplicitly, closeOpenResults); this.dbmd = null; this.originalSql = null; this.staticSqlStrings = null; this.parameterValues = null; this.parameterStreams = null; this.isStream = null; this.streamLengths = null; this.isNull = null; this.streamConvertBuf = null; } /** * JDBC 2.0 Set an Array parameter. * * @param i * the first parameter is 1, the second is 2, ... * @param x * an object representing an SQL array * * @throws SQLException * because this method is not implemented. * @throws NotImplemented * DOCUMENT ME! */ public void setArray(int i, Array x) throws SQLException { throw new NotImplemented(); } /** * When a very large ASCII value is input to a LONGVARCHAR parameter, it may * be more practical to send it via a java.io.InputStream. JDBC will read * the data from the stream as needed, until it reaches end-of-file. The * JDBC driver will do any necessary conversion from ASCII to the database * char format. * * <P> * <B>Note:</B> This stream object can either be a standard Java stream * object or your own subclass that implements the standard interface. * </p> * * @param parameterIndex * the first parameter is 1... * @param x * the parameter value * @param length * the number of bytes in the stream * * @exception SQLException * if a database access error occurs */ public void setAsciiStream(int parameterIndex, InputStream x, int length) throws SQLException { if (x == null) { setNull(parameterIndex, java.sql.Types.VARCHAR); } else { setBinaryStream(parameterIndex, x, length); } } /** * Set a parameter to a java.math.BigDecimal value. The driver converts this * to a SQL NUMERIC value when it sends it to the database. * * @param parameterIndex * the first parameter is 1... * @param x * the parameter value * * @exception SQLException * if a database access error occurs */ public void setBigDecimal(int parameterIndex, BigDecimal x) throws SQLException { if (x == null) { setNull(parameterIndex, java.sql.Types.DECIMAL); } else { setInternal(parameterIndex, StringUtils .fixDecimalExponent(StringUtils.consistentToString(x))); } } /** * When a very large binary value is input to a LONGVARBINARY parameter, it * may be more practical to send it via a java.io.InputStream. JDBC will * read the data from the stream as needed, until it reaches end-of-file. * * <P> * <B>Note:</B> This stream object can either be a standard Java stream * object or your own subclass that implements the standard interface. * </p> * * @param parameterIndex * the first parameter is 1... * @param x * the parameter value * @param length * the number of bytes to read from the stream (ignored) * * @throws SQLException * if a database access error occurs */ public void setBinaryStream(int parameterIndex, InputStream x, int length) throws SQLException { if (x == null) { setNull(parameterIndex, java.sql.Types.BINARY); } else { int parameterIndexOffset = getParameterIndexOffset(); if ((parameterIndex < 1) || (parameterIndex > this.staticSqlStrings.length)) { throw SQLError.createSQLException( Messages.getString("PreparedStatement.2") //$NON-NLS-1$ + parameterIndex + Messages.getString("PreparedStatement.3") + this.staticSqlStrings.length + Messages.getString("PreparedStatement.4"), //$NON-NLS-1$ //$NON-NLS-2$ SQLError.SQL_STATE_ILLEGAL_ARGUMENT); } else if (parameterIndexOffset == -1 && parameterIndex == 1) { throw SQLError.createSQLException("Can't set IN parameter for return value of stored function call.", SQLError.SQL_STATE_ILLEGAL_ARGUMENT); } this.parameterStreams[parameterIndex - 1 + parameterIndexOffset] = x; this.isStream[parameterIndex - 1 + parameterIndexOffset] = true; this.streamLengths[parameterIndex - 1 + parameterIndexOffset] = length; this.isNull[parameterIndex - 1 + parameterIndexOffset] = false; } } /** * JDBC 2.0 Set a BLOB parameter. * * @param i * the first parameter is 1, the second is 2, ... * @param x * an object representing a BLOB * * @throws SQLException * if a database error occurs */ public void setBlob(int i, java.sql.Blob x) throws SQLException { if (x == null) { setNull(i, Types.BLOB); } else { ByteArrayOutputStream bytesOut = new ByteArrayOutputStream(); bytesOut.write('\''); escapeblockFast(x.getBytes(1, (int) x.length()), bytesOut, (int) x .length()); bytesOut.write('\''); setInternal(i, bytesOut.toByteArray()); } } /** * Set a parameter to a Java boolean value. The driver converts this to a * SQL BIT value when it sends it to the database. * * @param parameterIndex * the first parameter is 1... * @param x * the parameter value * * @throws SQLException * if a database access error occurs */ public void setBoolean(int parameterIndex, boolean x) throws SQLException { if (this.useTrueBoolean) { setInternal(parameterIndex, x ? "1" : "0"); //$NON-NLS-1$ //$NON-NLS-2$ } else { setInternal(parameterIndex, x ? "'t'" : "'f'"); //$NON-NLS-1$ //$NON-NLS-2$ } } /** * Set a parameter to a Java byte value. Th
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -