abstractjdbc1statement.java
来自「PostgreSQL7.4.6 for Linux」· Java 代码 · 共 2,147 行 · 第 1/5 页
JAVA
2,147 行
if (l_hours < 10) sbuf.append('0'); sbuf.append(l_hours); sbuf.append(':'); int l_minutes = x.getMinutes(); if (l_minutes < 10) sbuf.append('0'); sbuf.append(l_minutes); sbuf.append(':'); int l_seconds = x.getSeconds(); if (l_seconds < 10) sbuf.append('0'); sbuf.append(l_seconds); // Make decimal from nanos. char[] l_decimal = {'0', '0', '0', '0', '0', '0', '0', '0', '0'}; char[] l_nanos = Integer.toString(x.getNanos()).toCharArray(); System.arraycopy(l_nanos, 0, l_decimal, l_decimal.length - l_nanos.length, l_nanos.length); sbuf.append('.'); if (connection.haveMinimumServerVersion("7.2")) { sbuf.append(l_decimal, 0, 6); } else { // Because 7.1 include bug that "hh:mm:59.999" becomes "hh:mm:60.00". sbuf.append(l_decimal, 0, 2); } //add timezone offset int l_offset = -(x.getTimezoneOffset()); int l_houros = l_offset / 60; if (l_houros >= 0) { sbuf.append('+'); } else { sbuf.append('-'); } if (l_houros > -10 && l_houros < 10) sbuf.append('0'); if (l_houros >= 0) { sbuf.append(l_houros); } else { sbuf.append(-l_houros); } int l_minos = l_offset - (l_houros * 60); if (l_minos != 0) { if (l_minos > -10 && l_minos < 10) sbuf.append('0'); if (l_minos >= 0) { sbuf.append(l_minos); } else { sbuf.append(-l_minos); } } sbuf.append("'"); bind(parameterIndex, sbuf.toString(), PG_TIMESTAMPTZ); } } } private void setCharacterStreamPost71(int parameterIndex, InputStream x, int length, String encoding) throws SQLException { if (x == null) { setNull(parameterIndex, Types.VARCHAR); return; } //Version 7.2 supports AsciiStream for all PG text types (char, varchar, text) //As the spec/javadoc for this method indicate this is to be used for //large String values (i.e. LONGVARCHAR) PG doesn't have a separate //long varchar datatype, but with toast all text datatypes are capable of //handling very large values. Thus the implementation ends up calling //setString() since there is no current way to stream the value to the server try { InputStreamReader l_inStream = new InputStreamReader(x, encoding); char[] l_chars = new char[length]; int l_charsRead = 0; while (true) { int n = l_inStream.read(l_chars, l_charsRead, length - l_charsRead); if (n == -1) break; l_charsRead += n; if (l_charsRead == length) break; } setString(parameterIndex, new String(l_chars, 0, l_charsRead), PG_TEXT); } catch (UnsupportedEncodingException l_uee) { throw new PSQLException("postgresql.unusual", PSQLState.UNEXPECTED_ERROR, l_uee); } catch (IOException l_ioe) { throw new PSQLException("postgresql.unusual", PSQLState.UNEXPECTED_ERROR, l_ioe); } } /* * 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. * * @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 (connection.haveMinimumCompatibleVersion("7.2")) { setCharacterStreamPost71(parameterIndex, x, length, "ASCII"); } else { //Version 7.1 supported only LargeObjects by treating everything //as binary data setBinaryStream(parameterIndex, x, length); } } /* * When a very large Unicode 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 * UNICODE 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. * * @param parameterIndex the first parameter is 1... * @param x the parameter value * @exception SQLException if a database access error occurs */ public void setUnicodeStream(int parameterIndex, InputStream x, int length) throws SQLException { if (connection.haveMinimumCompatibleVersion("7.2")) { setCharacterStreamPost71(parameterIndex, x, length, "UTF-8"); } else { //Version 7.1 supported only LargeObjects by treating everything //as binary data setBinaryStream(parameterIndex, x, length); } } /* * 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. * * @param parameterIndex the first parameter is 1... * @param x the parameter value * @exception SQLException if a database access error occurs */ public void setBinaryStream(int parameterIndex, InputStream x, int length) throws SQLException { if (connection.haveMinimumCompatibleVersion("7.2")) { if (x == null) { setNull(parameterIndex, Types.VARBINARY); return; } //Version 7.2 supports BinaryStream for for the PG bytea type //As the spec/javadoc for this method indicate this is to be used for //large binary values (i.e. LONGVARBINARY) PG doesn't have a separate //long binary datatype, but with toast the bytea datatype is capable of //handling very large values. Thus the implementation ends up calling //setBytes() since there is no current way to stream the value to the server byte[] l_bytes = new byte[length]; int l_bytesRead = 0; try { while (true) { int n = x.read(l_bytes, l_bytesRead, length - l_bytesRead); if (n == -1) break; l_bytesRead += n; if (l_bytesRead == length) break; } } catch (IOException l_ioe) { throw new PSQLException("postgresql.unusual", PSQLState.UNEXPECTED_ERROR, l_ioe); } if (l_bytesRead == length) { setBytes(parameterIndex, l_bytes); } else { //the stream contained less data than they said byte[] l_bytes2 = new byte[l_bytesRead]; System.arraycopy(l_bytes, 0, l_bytes2, 0, l_bytesRead); setBytes(parameterIndex, l_bytes2); } } else { //Version 7.1 only supported streams for LargeObjects //but the jdbc spec indicates that streams should be //available for LONGVARBINARY instead LargeObjectManager lom = connection.getLargeObjectAPI(); int oid = lom.create(); LargeObject lob = lom.open(oid); OutputStream los = lob.getOutputStream(); try { // could be buffered, but then the OutputStream returned by LargeObject // is buffered internally anyhow, so there would be no performance // boost gained, if anything it would be worse! int c = x.read(); int p = 0; while (c > -1 && p < length) { los.write(c); c = x.read(); p++; } los.close(); } catch (IOException se) { throw new PSQLException("postgresql.unusual", PSQLState.UNEXPECTED_ERROR, se); } // lob is closed by the stream so don't call lob.close() setInt(parameterIndex, oid); } } /* * In general, parameter values remain in force for repeated used of a * Statement. Setting a parameter value automatically clears its * previous value. However, in coms cases, it is useful to immediately * release the resources used by the current parameter values; this * can be done by calling clearParameters * * @exception SQLException if a database access error occurs */ public void clearParameters() throws SQLException { int i; for (i = 0 ; i < m_binds.length ; i++) { m_binds[i] = null; m_bindTypes[i] = null; } } // Helper method that extracts numeric values from an arbitary Object. private String numericValueOf(Object x) { if (x instanceof Boolean) return ((Boolean)x).booleanValue() ? "1" :"0"; else if (x instanceof Integer || x instanceof Long || x instanceof Double || x instanceof Short || x instanceof Number || x instanceof Float) return x.toString(); else //ensure the value is a valid numeric value to avoid //sql injection attacks return new BigDecimal(x.toString()).toString(); } /* * Set the value of a parameter using an object; use the java.lang * equivalent objects for integral values. * * <P>The given Java object will be converted to the targetSqlType before * being sent to the database. * * <P>note that this method may be used to pass database-specific * abstract data types. This is done by using a Driver-specific * Java type and using a targetSqlType of java.sql.Types.OTHER * * @param parameterIndex the first parameter is 1... * @param x the object containing the input parameter value * @param targetSqlType The SQL type to be send to the database * @param scale For java.sql.Types.DECIMAL or java.sql.Types.NUMERIC * * types this is the number of digits after the decimal. For * * all other types this value will be ignored. * @exception SQLException if a database access error occurs */ public void setObject(int parameterIndex, Object x, int targetSqlType, int scale) throws SQLException { if (x == null) { setNull(parameterIndex, targetSqlType); return ; } switch (targetSqlType) { case Types.INTEGER: bind(parameterIndex, numericValueOf(x), PG_INTEGER); break; case Types.TINYINT: case Types.SMALLINT: bind(parameterIndex, numericValueOf(x), PG_INT2); break; case Types.BIGINT: bind(parameterIndex, numericValueOf(x), PG_INT8); break; case Types.REAL: case Types.FLOAT: bind(parameterIndex, numericValueOf(x), PG_FLOAT); break; case Types.DOUBLE: bind(parameterIndex, numericValueOf(x), PG_DOUBLE); break; case Types.DECIMAL: case Types.NUMERIC: bind(parameterIndex, numericValueOf(x), PG_NUMERIC); break; case Types.CHAR: case Types.VARCHAR: case Types.LONGVARCHAR: setString(parameterIndex, x.toString()); break; case Types.DATE: if (x instanceof java.sql.Date) setDate(parameterIndex, (java.sql.Date)x); else { java.sql.Date tmpd = (x instanceof java.util.Date) ? new java.sql.Date(((java.util.Date)x).getTime()) : dateFromString(x.toString()); setDate(parameterIndex, tmpd); } break; case Types.TIME: if (x instanceof java.sql.Time) setTime(parameterIndex, (java.sql.Time)x); else { java.sql.Time tmpt = (x instanceof java.util.Date) ? new java.sql.Time(((java.util.Date)x).getTime()) : timeFromString(x.toString()); setTime(parameterIndex, tmpt); } break; case Types.TIMESTAMP: if (x instanceof java.sql.Timestamp) setTimestamp(parameterIndex ,(java.sql.Timestamp)x); else { java.sql.Timestamp tmpts = (x instanceof java.util.Date) ? new java.sql.Timestamp(((java.util.Date)x).getTime()) : timestampFromString(x.toString()); setTimestamp(parameterIndex, tmpts); } break; case Types.BIT: if (x instanceof Boolean) { bind(parameterIndex, ((Boolean)x).booleanValue() ? "'1'" : "'0'", PG_BOOLEAN); } else if (x instanceof String) { bind(parameterIndex, Boolean.valueOf(x.toString()).booleanValue() ? "'1'" : "'0'", PG_BOOLEAN); } else if (x instanceof Number) { bind(parameterIndex, ((Number)x).intValue()!=0 ? "'1'" : "'0'", PG_BOOLEAN); } else { throw new PSQLException("postgresql.prep.type", PSQLState.INVALID_PARAMETER_TYPE); } break; case Types.BINARY: case Types.VARBINARY: case Types.LONGVARBINARY: setObject(parameterIndex, x); break; case Types.OTHER: if (x instanceof PGobject) setString(parameterIndex, ((PGobject)x).getValue(), ((PGobject)x).getType()); else throw new PSQLException("postgresql.prep.type", PSQLState.INVALID_PARAMETER_TYPE); break; default: throw new PSQLException("postgresql.prep.type", PSQLState.INVALID_PARAMETER_TYPE); } } public void setObject(int parameterIndex, Object x, int targetSqlType) throws SQLException { setObject(parameterIndex, x, targetSqlType, 0); } /* * This stores an Object into a parameter. */ public void setObject(int parameterIndex, Object x) throws SQLException { if (x == null)
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?