⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tds.java

📁 Java写的TDS协议(JDBC/ODBC)实现
💻 JAVA
📖 第 1 页 / 共 5 页
字号:

    /**
     *  Determine if the next subpacket is a result row or a computed result row.
     *  <p>
     *  This does not eat any input.
     *
     *@return true if the next piece of data to read is a result row.
     *@exception  net.sourceforge.jtds.jdbc.TdsException
     *@exception  java.io.IOException
     */
    public synchronized boolean isResultRow()
             throws net.sourceforge.jtds.jdbc.TdsException, java.io.IOException
    {
        byte type = comm.peek();

        return type == TDS_ROW /*|| type == TDS_CMP_ROW_TOKEN*/;
    }


    /**
     *  Determine if the next subpacket is an end of result set marker. <p>
     *
     *  This does not eat any input.
     *
     *@return                                            true if the next piece
     *      of data to read is end of result set marker.
     *@exception  net.sourceforge.jtds.jdbc.TdsException
     *@exception  java.io.IOException
     */
    public synchronized boolean isEndOfResults()
             throws net.sourceforge.jtds.jdbc.TdsException, java.io.IOException
    {
        byte type = comm.peek();

        return type == TDS_DONE || type == TDS_DONEPROC || type == TDS_DONEINPROC;
    }


    /**
     *  Determine if the next subpacket is a DONEINPROC marker <p>
     *
     *  This does not eat any input.
     *
     *@return    <code>true</code> if the next packet is a DONEINPROC packet
     *@exception net.sourceforge.jtds.jdbc.TdsException
     *@exception java.io.IOException
     */
    public synchronized boolean isDoneInProc()
             throws net.sourceforge.jtds.jdbc.TdsException, java.io.IOException
    {
        byte type = comm.peek();

        return type == TDS_DONEINPROC;
    }


    /**
     *  Determine if the next subpacket is a message packet <p>
     *
     *  This does not eat any input.
     *
     *@return                                            true if the next piece
     *      of data to read is message
     *@exception  net.sourceforge.jtds.jdbc.TdsException
     *@exception  java.io.IOException
     */
    public synchronized boolean isMessagePacket()
             throws net.sourceforge.jtds.jdbc.TdsException, java.io.IOException
    {
        byte type = comm.peek();
        return type == TDS_INFO;
    }


     /**
      * Determine if the next subpacket is an output parameter from a stored proc
      * <p>
      * This does not eat any input.
      *
      * @return true if the next piece of data to read is an output parameter
      *
      * @exception net.sourceforge.jtds.jdbc.TdsException
      * @exception java.io.IOException
      */
     synchronized public boolean isParamResult()
        throws net.sourceforge.jtds.jdbc.TdsException, java.io.IOException
     {
        byte  type = comm.peek();
        return type==TDS_PARAM;
     }

    /**
     *  Determine if the next subpacket is a text update packet <p>
     *
     *  This does not eat any input.
     *
     *@return                                            true if the next piece
     *      of data to read is text update
     *@exception  net.sourceforge.jtds.jdbc.TdsException
     *@exception  java.io.IOException
     */
     /* strange replaced by paramToken
    public synchronized boolean isTextUpdate()
             throws net.sourceforge.jtds.jdbc.TdsException, java.io.IOException
    {
        byte type = comm.peek();
        return type == TDS_TEXT_UPD_TOKEN;
    }
      */


    /**
     *  Determine if the next subpacket is an error packet <p>
     *
     *  This does not eat any input.
     *
     *@return                                            true if the next piece
     *      of data to read is an error
     *@exception  net.sourceforge.jtds.jdbc.TdsException
     *@exception  java.io.IOException
     */
    public synchronized boolean isErrorPacket()
             throws net.sourceforge.jtds.jdbc.TdsException, java.io.IOException
    {
        byte type = comm.peek();
        return type == TDS_ERROR;
    }


    /**
     *  Determine if the next subpacket is an procid subpacket <p>
     *
     *  This does not eat any input.
     *
     *@return                                            true if the next piece
     *      of data to read is end of result set marker.
     *@exception  net.sourceforge.jtds.jdbc.TdsException
     *@exception  java.io.IOException
     */
    public synchronized boolean isProcId()
             throws net.sourceforge.jtds.jdbc.TdsException, java.io.IOException
    {
        byte type = comm.peek();

        return type == TDS_PROCID;
    }

    /**
     * Determine if the next subpacket is an environment change subpacket <p>
     *
     * This does not eat any input.
     *
     *@return     true if the next piece of data to read is end of result set
     *            marker.
     *@exception  net.sourceforge.jtds.jdbc.TdsException
     *@exception  java.io.IOException
     */
    public synchronized boolean isEnvChange()
        throws net.sourceforge.jtds.jdbc.TdsException, java.io.IOException
    {
        return comm.peek() == TDS_ENVCHANGE;
    }


    public void close()
    {
        comm.close();
        try {
            sock.close();
        }
        catch (java.io.IOException e) {
            // XXX should do something here
        }
    }


    public String toString()
    {
        return ""
                 + database + ", "
                 + sock.getLocalAddress() + ":" + sock.getLocalPort()
                 + " -> " + sock.getInetAddress() + ":" + sock.getPort();
    }


    /**
     * Change the connection level database.
     *
     * @param   _database  the database name to change to
     * @return             <code>true</code> if the database accepted the changes, false if rejected.
     * @exception  java.sql.SQLException  if an exception occured
     */
    private synchronized boolean initSettings(String _database)
             throws java.sql.SQLException
    {
        boolean isOkay = true;

        try
        {
            if( _database.length() > 0 )
                isOkay = changeDB(_database);

            if( isOkay )
                isOkay = changeSettings(sqlStatementToInitialize());
        }
        catch (net.sourceforge.jtds.jdbc.TdsUnknownPacketSubType e) {
            throw new SQLException("Unknown response. " + e.getMessage());
        }
        catch (java.io.IOException e) {
            throw new SQLException("Network problem. " + e.getMessage());
        }
        catch (net.sourceforge.jtds.jdbc.TdsException e) {
            throw new SQLException(e.getMessage());
        }
        return isOkay;
    }


    public void cancel()
             throws java.io.IOException, net.sourceforge.jtds.jdbc.TdsException
    {
        // XXX How should this be synchronized?  What sort of deadlock
        // conditions do we need to consider?

        cancelController.doCancel(comm);
    }


    public boolean moreResults()
    {
        return moreResults2;
    }
    // getUniqueProcedureName()


    /**
     * @param  sql               Description of Parameter
     * @param  chain             Description of Parameter
     * @exception  SQLException  Description of Exception
     */
    public synchronized void submitProcedure(String sql, SQLWarningChain chain) throws SQLException
    {
        PacketResult tmp = null;

        try {
            executeQuery(sql, null, null, 0);

            boolean done;
            do  // skip to end, why not ?
            {
               tmp = processSubPacket();
               if( tmp instanceof PacketMsgResult )
                   chain.addOrReturn((PacketMsgResult)tmp);
               done = (tmp instanceof PacketEndTokenResult)
                  && (! ((PacketEndTokenResult)tmp).moreResults());
            } while (! done);
        }
        catch (java.io.IOException e) {
            throw new SQLException("Network error" + e.getMessage());
        }
        catch (net.sourceforge.jtds.jdbc.TdsUnknownPacketSubType e) {
            throw new SQLException(e.getMessage());
        }
        catch (net.sourceforge.jtds.jdbc.TdsException e) {
            throw new SQLException(e.getMessage());
        }

        chain.checkForExceptions();
    }


    /**
     *  Execute a stored procedure on the SQLServer <p>
     *
     *
     *
     *@param  procedureName                              Description of
     *      Parameter
     *@param  formalParameterList                        Description of
     *      Parameter
     *@param  actualParameterList                        Description of
     *      Parameter
     *@param  stmt                                       Description of
     *      Parameter
     *@param  timeout                                    Description of
     *      Parameter
     *@exception  java.sql.SQLException
     *@exception  net.sourceforge.jtds.jdbc.TdsException
     */
    public synchronized void executeProcedure(
            String procedureName,
            ParameterListItem[] formalParameterList,
            ParameterListItem[] actualParameterList,
            TdsStatement stmt,
            SQLWarningChain wChain,
            int timeout)
             throws java.sql.SQLException, net.sourceforge.jtds.jdbc.TdsException, java.io.IOException
    {
        // SAfe We need to check if all cancel requests were processed and wait
        //      for any outstanding cancel. It could happen that we sent the
        //      CANCEL after the server sent us all the data, so the answer is
        //      going to come in a packet of its own.
        if( cancelController.outstandingCancels() > 0 )
        {
            processSubPacket();
            if( cancelController.outstandingCancels() > 0 )
                throw new SQLException("Something went completely wrong. A cancel request was lost.");
        }

        // A stored procedure has a packet type of 0x03 in the header packet.
        // for non-image date the packets consists of
        // offset         length        desc.
        // 0                1           The length of the name of the stored proc
        // 1                N1          The name of the stored proc
        // N1 + 1           2           unknown (filled with zeros?)
        // N1 + 3           2           unknown prefix for param 1 (zero filled?)
        // N1 + 5           1           datatype for param 1
        // N1 + 6           1           max length of param 1
        // N1 + 7           N2          parameter 1 data
        // ...
        //
        // For image data (datatype 0x22) the packet consists of
        // 0                1           The length of the name of the stored proc
        // 1                N1          The name of the stored proc
        // N1 + 1           2           unknown (filled with zeros?)
        // N1 + 3           2           unknown prefix for param 1 (zero filled?)
        // N1 + 5           1           datatype for param 1
        // N1 + 6           4           length of param 1
        // N1 + 10          4           length of param 1 (duplicated?)
        // N1 + 7           N2          parameter 1 data
        // ...
        checkMaxRows(stmt);

        int i;

        try {
            moreResults2 = true;

            // Start sending the procedure execute packet.
            comm.startPacket(TdsComm.PROC);
            if (tdsVer == Tds.TDS70) {
                comm.appendTdsShort((short) (procedureName.length()));
                comm.appendChars(procedureName);
            }
            else {
                byte[] nameBytes = encoder.getBytes(procedureName);
                comm.appendByte((byte) nameBytes.length);
                comm.appendBytes(nameBytes,
                        nameBytes.length,
                        (byte) 0);
            }
            // SAfe I think if this is set to 2 it means "more requests"
            //      More requests can be separated with 0x80
            comm.appendByte((byte) 0);
            comm.appendByte((byte) 0);

            // Now handle the parameters
            for( i=0; i<formalParameterList.length; i++ ) {
                byte nativeType = cvtJdbcTypeToNativeType(formalParameterList[i].type);

                comm.appendByte((byte) 0);
                if (actualParameterList[i].isOutput)
                {
                   comm.appendByte((byte)1);

                   if (nativeType == SYBBIT && actualParameterList[i].value == null)
                   {
                      actualParameterList[i].value = Boolean.FALSE;
                   }
                }
                else
                {
                  comm.appendByte((byte) 0);
                }

                if (actualParameterList[i].value == null &&
                   (nativeType == SYBINT1 || nativeType == SYBINT2 || nativeType == SYBINT4))
                {
                   nativeType = SYBINTN;

⌨️ 快捷键说明

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