abstractjdbc1statement.java

来自「PostgreSQL7.4.6 for Linux」· Java 代码 · 共 2,147 行 · 第 1/5 页

JAVA
2,147
字号
		{			setNull(parameterIndex, Types.OTHER);			return ;		}		if (x instanceof String)			setString(parameterIndex, (String)x);		else if (x instanceof BigDecimal)			setBigDecimal(parameterIndex, (BigDecimal)x);		else if (x instanceof Short)			setShort(parameterIndex, ((Short)x).shortValue());		else if (x instanceof Integer)			setInt(parameterIndex, ((Integer)x).intValue());		else if (x instanceof Long)			setLong(parameterIndex, ((Long)x).longValue());		else if (x instanceof Float)			setFloat(parameterIndex, ((Float)x).floatValue());		else if (x instanceof Double)			setDouble(parameterIndex, ((Double)x).doubleValue());		else if (x instanceof byte[])			setBytes(parameterIndex, (byte[])x);		else if (x instanceof java.sql.Date)			setDate(parameterIndex, (java.sql.Date)x);		else if (x instanceof Time)			setTime(parameterIndex, (Time)x);		else if (x instanceof Timestamp)			setTimestamp(parameterIndex, (Timestamp)x);		else if (x instanceof Boolean)			setBoolean(parameterIndex, ((Boolean)x).booleanValue());		else if (x instanceof PGobject)			setString(parameterIndex, ((PGobject)x).getValue(), PG_TEXT);		else			// Try to store as a string in database			setString(parameterIndex, x.toString(), PG_TEXT);	}	/*	 * Before executing a stored procedure call you must explicitly	 * call registerOutParameter to register the java.sql.Type of each	 * out parameter.	 *	 * <p>Note: When reading the value of an out parameter, you must use	 * the getXXX method whose Java type XXX corresponds to the	 * parameter's registered SQL type.	 *	 * ONLY 1 RETURN PARAMETER if {?= call ..} syntax is used	 *	 * @param parameterIndex the first parameter is 1, the second is 2,...	 * @param sqlType SQL type code defined by java.sql.Types; for	 * parameters of type Numeric or Decimal use the version of	 * registerOutParameter that accepts a scale value	 * @exception SQLException if a database-access error occurs.	 */	public void registerOutParameter(int parameterIndex, int sqlType) throws SQLException	{		if (parameterIndex != 1)			throw new PSQLException ("postgresql.call.noinout", PSQLState.STATEMENT_NOT_ALLOWED_IN_FUNCTION_CALL);		if (!isFunction)			throw new PSQLException ("postgresql.call.procasfunc", PSQLState.STATEMENT_NOT_ALLOWED_IN_FUNCTION_CALL,originalSql);		// functionReturnType contains the user supplied value to check		// testReturn contains a modified version to make it easier to		// check the getXXX methods..		functionReturnType = sqlType;		testReturn = sqlType;		if (functionReturnType == Types.CHAR ||				functionReturnType == Types.LONGVARCHAR)			testReturn = Types.VARCHAR;		else if (functionReturnType == Types.FLOAT)			testReturn = Types.REAL; // changes to streamline later error checking		returnTypeSet = true;	}	/*	 * You must also specify the scale for numeric/decimal types:	 *	 * <p>Note: When reading the value of an out parameter, you must use	 * the getXXX method whose Java type XXX corresponds to the	 * parameter's registered SQL type.	 *	 * @param parameterIndex the first parameter is 1, the second is 2,...	 * @param sqlType use either java.sql.Type.NUMERIC or java.sql.Type.DECIMAL	 * @param scale a value greater than or equal to zero representing the	 * desired number of digits to the right of the decimal point	 * @exception SQLException if a database-access error occurs.	 */	public void registerOutParameter(int parameterIndex, int sqlType,									 int scale) throws SQLException	{		registerOutParameter (parameterIndex, sqlType); // ignore for now..	}	/*	 * An OUT parameter may have the value of SQL NULL; wasNull	 * reports whether the last value read has this special value.	 *	 * <p>Note: You must first call getXXX on a parameter to read its	 * value and then call wasNull() to see if the value was SQL NULL.	 * @return true if the last parameter read was SQL NULL	 * @exception SQLException if a database-access error occurs.	 */	public boolean wasNull() throws SQLException	{		// check to see if the last access threw an exception		return (callResult == null);	}	/*	 * Get the value of a CHAR, VARCHAR, or LONGVARCHAR parameter as a	 * Java String.	 *	 * @param parameterIndex the first parameter is 1, the second is 2,...	 * @return the parameter value; if the value is SQL NULL, the result is null	 * @exception SQLException if a database-access error occurs.	 */	public String getString(int parameterIndex) throws SQLException	{		checkIndex (parameterIndex, Types.VARCHAR, "String");		return (String)callResult;	}	/*	 * Get the value of a BIT parameter as a Java boolean.	 *	 * @param parameterIndex the first parameter is 1, the second is 2,...	 * @return the parameter value; if the value is SQL NULL, the result is false	 * @exception SQLException if a database-access error occurs.	 */	public boolean getBoolean(int parameterIndex) throws SQLException	{		checkIndex (parameterIndex, Types.BIT, "Boolean");		if (callResult == null)			return false;		return ((Boolean)callResult).booleanValue ();	}	/*	 * Get the value of a TINYINT parameter as a Java byte.	 *	 * @param parameterIndex the first parameter is 1, the second is 2,...	 * @return the parameter value; if the value is SQL NULL, the result is 0	 * @exception SQLException if a database-access error occurs.	 */	public byte getByte(int parameterIndex) throws SQLException	{		checkIndex (parameterIndex, Types.TINYINT, "Byte");		// We expect the above checkIndex call to fail because		// we don't have an equivalent pg type for TINYINT.		// Possibly "char" (not char(N)), could be used, but		// for the moment we just bail out.		//		throw new PSQLException("postgresql.unusual", PSQLState.UNEXPECTED_ERROR);	}	/*	 * Get the value of a SMALLINT parameter as a Java short.	 *	 * @param parameterIndex the first parameter is 1, the second is 2,...	 * @return the parameter value; if the value is SQL NULL, the result is 0	 * @exception SQLException if a database-access error occurs.	 */	public short getShort(int parameterIndex) throws SQLException	{		checkIndex (parameterIndex, Types.SMALLINT, "Short");		if (callResult == null)			return 0;		return (short)((Short)callResult).intValue ();	}	/*	 * Get the value of an INTEGER parameter as a Java int.	 *	 * @param parameterIndex the first parameter is 1, the second is 2,...	 * @return the parameter value; if the value is SQL NULL, the result is 0	 * @exception SQLException if a database-access error occurs.	 */	public int getInt(int parameterIndex) throws SQLException	{		checkIndex (parameterIndex, Types.INTEGER, "Int");		if (callResult == null)			return 0;		return ((Integer)callResult).intValue ();	}	/*	 * Get the value of a BIGINT parameter as a Java long.	 *	 * @param parameterIndex the first parameter is 1, the second is 2,...	 * @return the parameter value; if the value is SQL NULL, the result is 0	 * @exception SQLException if a database-access error occurs.	 */	public long getLong(int parameterIndex) throws SQLException	{		checkIndex (parameterIndex, Types.BIGINT, "Long");		if (callResult == null)			return 0;		return ((Long)callResult).longValue ();	}	/*	 * Get the value of a FLOAT parameter as a Java float.	 *	 * @param parameterIndex the first parameter is 1, the second is 2,...	 * @return the parameter value; if the value is SQL NULL, the result is 0	 * @exception SQLException if a database-access error occurs.	 */	public float getFloat(int parameterIndex) throws SQLException	{		checkIndex (parameterIndex, Types.REAL, "Float");		if (callResult == null)			return 0;		return ((Float)callResult).floatValue ();	}	/*	 * Get the value of a DOUBLE parameter as a Java double.	 *	 * @param parameterIndex the first parameter is 1, the second is 2,...	 * @return the parameter value; if the value is SQL NULL, the result is 0	 * @exception SQLException if a database-access error occurs.	 */	public double getDouble(int parameterIndex) throws SQLException	{		checkIndex (parameterIndex, Types.DOUBLE, "Double");		if (callResult == null)			return 0;		return ((Double)callResult).doubleValue ();	}	/*	 * Get the value of a NUMERIC parameter as a java.math.BigDecimal	 * object.	 *	 * @param parameterIndex the first parameter is 1, the second is 2,...	 * @param scale a value greater than or equal to zero representing the	 * desired number of digits to the right of the decimal point	 * @return the parameter value; if the value is SQL NULL, the result is null	 * @exception SQLException if a database-access error occurs.	 * @deprecated in Java2.0	 */	public BigDecimal getBigDecimal(int parameterIndex, int scale)	throws SQLException	{		checkIndex (parameterIndex, Types.NUMERIC, "BigDecimal");		return ((BigDecimal)callResult);	}	/*	 * Get the value of a SQL BINARY or VARBINARY parameter as a Java	 * byte[]	 *	 * @param parameterIndex the first parameter is 1, the second is 2,...	 * @return the parameter value; if the value is SQL NULL, the result is null	 * @exception SQLException if a database-access error occurs.	 */	public byte[] getBytes(int parameterIndex) throws SQLException	{		checkIndex (parameterIndex, Types.VARBINARY, Types.BINARY, "Bytes");		return ((byte [])callResult);	}	/*	 * Get the value of a SQL DATE parameter as a java.sql.Date object	 *	 * @param parameterIndex the first parameter is 1, the second is 2,...	 * @return the parameter value; if the value is SQL NULL, the result is null	 * @exception SQLException if a database-access error occurs.	 */	public java.sql.Date getDate(int parameterIndex) throws SQLException	{		checkIndex (parameterIndex, Types.DATE, "Date");		return (java.sql.Date)callResult;	}	/*	 * Get the value of a SQL TIME parameter as a java.sql.Time object.	 *	 * @param parameterIndex the first parameter is 1, the second is 2,...	 * @return the parameter value; if the value is SQL NULL, the result is null	 * @exception SQLException if a database-access error occurs.	 */	public java.sql.Time getTime(int parameterIndex) throws SQLException	{		checkIndex (parameterIndex, Types.TIME, "Time");		return (java.sql.Time)callResult;	}	/*	 * Get the value of a SQL TIMESTAMP parameter as a java.sql.Timestamp object.	 *	 * @param parameterIndex the first parameter is 1, the second is 2,...	 * @return the parameter value; if the value is SQL NULL, the result is null	 * @exception SQLException if a database-access error occurs.	 */	public java.sql.Timestamp getTimestamp(int parameterIndex)	throws SQLException	{		checkIndex (parameterIndex, Types.TIMESTAMP, "Timestamp");		return (java.sql.Timestamp)callResult;	}	// getObject returns a Java object for the parameter.	// See the JDBC spec's "Dynamic Programming" chapter for details.	/*	 * Get the value of a parameter as a Java object.	 *	 * <p>This method returns a Java object whose type coresponds to the	 * SQL type that was registered for this parameter using	 * registerOutParameter.	 *	 * <P>Note that this method may be used to read datatabase-specific,	 * abstract data types. This is done by specifying a targetSqlType	 * of java.sql.types.OTHER, which allows the driver to return a	 * database-specific Java type.	 *	 * <p>See the JDBC spec's "Dynamic Programming" chapter for details.	 *	 * @param parameterIndex the first parameter is 1, the second is 2,...	 * @return A java.lang.Object holding the OUT parameter value.	 * @exception SQLException if a database-access error occurs.	 */	public Object getObject(int parameterIndex)	throws SQLException	{		checkIndex (parameterIndex);		return callResult;	}	//This method is implemeted in jdbc2	public int getResultSetConcurrency() throws SQLException	{		return 0;	}	/*	 * Returns the SQL statement with the current template values	 * substituted.	 */	public String toString()	{		if (m_sqlFragments == null)			return super.toString();		synchronized (sbuf)		{			sbuf.setLength(0);			int i;			for (i = 0 ; i < m_binds.length ; ++i)			{				sbuf.append (m_sqlFragments[i]);				if (m_binds[i] == null)					sbuf.append( '?' );				else					sbuf.append (m_binds[i]);			}			sbuf.append(m_sqlFragments[m_binds.length]);			return sbuf.toString();		}	}	/*     * Note if s is a String it should be escaped by the caller to avoid SQL     * injection attacks.  It is not done here for efficency reasons as     * most calls to this method do not require escaping as the source     * of the string is known safe (i.e. Integer.toString())	 */	private void bind(int paramIndex, Object s, String type) throws SQLException	{		if (paramIndex < 1 || paramIndex > m_binds.length)			throw new PSQLException("postgresql.prep.range", PSQLState.INVALID_PARAMETER_VALUE);		if (paramIndex == 1 && isFunction) // need to registerOut instead			throw new PSQLException ("postgresql.call.funcover");		m_binds[paramIndex - 1] = s;		m_bindTypes[paramIndex - 1] = type;	}	/**	 * this method will turn a string of the form	 * {? = call <some_function> (?, [?,..]) }	 * into the PostgreSQL format which is	 * select <some_function> (?, [?, ...]) as result	 * or select * from <some_function> (?, [?, ...]) as result (7.3)	 *	 */	private String modifyJdbcCall(String p_sql) throws SQLException	{		//Check that this is actually a call which should start with a {        //if not do nothing and treat this as a standard prepared sql		if (!p_sql.trim().startsWith("{")) {			return p_sql;		}		// syntax checking is not complete only a few basics :(		originalSql = p_sql; // save for error msgs..		String l_sql = p_sql;		int index = l_sql.indexOf ("="); // is implied func or proc?		boolean isValid = true;		if (index > -1)		{			isFunction = true;			isValid = l_sql.indexOf ("?") < index; // ? before =		}		l_sql = l_sql.trim ();		if (l_sql.startsWith ("{") && l_sql.endsWith ("}"))		{			l_sql = l_sql.substring (1, l_sql.length() - 1);		}		else			isValid = false;		index = l_sql.indexOf ("call");		if (index == -1 || !isValid)			throw new PSQLException ("postgresql.call.malformed",PSQLState.STATEMENT_NOT_ALLOWED_IN_FUNCTION_CALL,									 new Object[]{l_sql, JDBC_SYNTAX});		l_sql = l_sql.replace ('{', ' '); // replace these characters		l_sql = l_sql.replace ('}', ' ');		l_sql = l_sql.replace (';', ' ');		// this removes the 'call' string and also puts a hidden '?'		// at the front of the line for functions, this will		// allow the registerOutParameter to work correctly		// because in the source sql there was one more ? for the return		// value that is not needed by the postgres syntax.  But to make		// sure that the parameter numbers are the same as in the original		// sql we add a dummy parameter i

⌨️ 快捷键说明

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