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

📄 tds.java

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

                switch (nativeType) {
                    case SYBCHAR:
                    {
                        String val;
                        try {
                            val = (String)actualParameterList[i].value;
                        }
                        catch (ClassCastException e) {
                            if( actualParameterList[i].value instanceof Boolean )
                                val = ((Boolean)actualParameterList[i].value).booleanValue() ? "1" : "0";
                            else
                                val = actualParameterList[i].value.toString();
                        }
                        if( tdsVer<TDS70 && val!=null && val.length()==0 )
                            val = " ";
                        int len = val != null ? val.length() : 0;
                        int max = formalParameterList[i].maxLength;

                        // MJH - Do not use the formalType from the actual parameters as
                        // this will be null where a cached stored procedure is reused by a
                        // different PreparedStatement from the one that created it in the fist
                        // place. Use the formalParameter data instead.
                        //
                        //if (actualParameterList[i].formalType != null &&
                        //   actualParameterList[i].formalType.startsWith("n")) {
                        //
                        if( formalParameterList[i].formalType != null &&
                             formalParameterList[i].formalType.startsWith("n") )
                        {
                            /*
                             * This is a Unicode column, save to assume TDS 7.0
                             */

                            if (max > 4000) {
                                comm.appendByte(SYBNTEXT);
                                comm.appendTdsInt(max * 2);
                                if (val == null) {
                                    comm.appendTdsInt(0xFFFFFFFF);
                                }
                                else {
                                    comm.appendTdsInt(len * 2);
                                    comm.appendChars(val);
                                }
                            }
                            else {
                                comm.appendByte((byte) (SYBNVARCHAR | 0x80));
                                comm.appendTdsShort((short) (max * 2));
                                if (val == null) {
                                    comm.appendTdsShort((short) 0xFFFF);
                                }
                                else {
                                    // MJH - Trap silent truncation problem
                                    if( val.length() > 4000 )
                                        throw new java.io.IOException("Field too long");

                                    comm.appendTdsShort((short) (len * 2));
                                    comm.appendChars(val);
                                }
                            }

                        }
                        else {
                            /*
                             * Either VARCHAR or TEXT, TEXT can not happen
                             * with TDS 7.0 as we would always use NTEXT there
                             */
                            if (tdsVer != TDS70 && max > 255) {
                                // TEXT
                                comm.appendByte(SYBTEXT);
                                if( actualParameterList[i].value instanceof byte[] )
                                    sendSybImage((byte[]) actualParameterList[i].value);
                                else
                                    sendSybImage(encoder.getBytes(val));
                            }
                            else {
                                // VARCHAR
                                sendSybChar(val, formalParameterList[i].maxLength);
                            }
                        }
                        break;
                    }

                    case SYBINTN:
                      comm.appendByte(SYBINTN);
                      comm.appendByte((byte)4); // maximum length of the field,

                      if (actualParameterList[i].value == null)
                      {
                         comm.appendByte((byte)0); // actual length
                      }
                      else
                      {
                         comm.appendByte((byte)4); // actual length
                         comm.appendTdsInt(((Number)(actualParameterList[i].value)).intValue());
                      }
                      break;

                    case SYBINT4:
                      comm.appendByte(SYBINT4);
                      comm.appendTdsInt(((Number)(actualParameterList[i].value)).intValue());
                      break;

                    case SYBINT2:
                      comm.appendByte(SYBINT2);
                      comm.appendTdsShort(((Number)(actualParameterList[i].value)).shortValue());
                      break;

                    case SYBINT1:
                      comm.appendByte(SYBINT1);
                      comm.appendByte(((Number)(actualParameterList[i].value)).byteValue());
                      break;

                    case SYBFLT8:
                    case SYBFLTN:
                    {
                      if (actualParameterList[i].value == null)
                      {
                         comm.appendByte(SYBFLTN);
                         comm.appendByte((byte)8);
                         comm.appendByte((byte)0);
                      }
                      else
                      {
                         Number n = (Number)(actualParameterList[i].value);
                         Double d = new Double(n.doubleValue());

                         comm.appendByte(SYBFLT8);
                         comm.appendFlt8(d);
                      }
                      break;
                    }
                    case SYBDATETIMN:
                    {
                      writeDatetimeValue(actualParameterList[i].value, SYBDATETIMN);
                      break;
                    }
                    case SYBIMAGE:
                    {
                        comm.appendByte(nativeType);

                        sendSybImage((byte[]) actualParameterList[i].value);
                        break;
                    }
                    case SYBTEXT:
                    {
                        comm.appendByte(SYBTEXT);
                        sendSybImage(encoder.getBytes((String) actualParameterList[i].
                                value));
                        break;
                    }
                    case SYBBIT:
                    case SYBBITN:
                    {
                      if (actualParameterList[i].value == null)
                      {
                         comm.appendByte(SYBBITN);  //
                         comm.appendByte((byte)1);
                         comm.appendByte((byte)0);
                      }
                      else
                      {
                         comm.appendByte(SYBBIT);
                         if( actualParameterList[i].value.equals(Boolean.TRUE) )
                            comm.appendByte((byte)1);
                         else
                            comm.appendByte((byte)0);
                      }
                      break;
                    }
                    case SYBNUMERIC:
                    case SYBDECIMAL:
                    {
                      writeDecimalValue(nativeType, actualParameterList[i].value, 0, -1);
                      break;
                    }
                    case SYBBINARY:
                    case SYBVARBINARY:
                    {
                      byte[] value = (byte[])actualParameterList[i].value;
                      int maxLength = formalParameterList[i].maxLength;
                      if (value == null)
                      {
                         comm.appendByte(SYBVARBINARY);
                         comm.appendByte((byte)(maxLength));
                         comm.appendByte((byte)0);
                      }
                      else
                      {
                         if (value.length > 255)
                         {
                            if (tdsVer != TDS70)
                            {
                               throw new java.io.IOException("Field too long");
                            }
                            comm.appendByte(SYBBIGVARBINARY);
                            if (maxLength < 0 || maxLength > 8000) {
                              comm.appendTdsShort((short)8000);
                            }
                            else
                              comm.appendTdsShort((short)maxLength);
                            comm.appendTdsShort((short)(value.length));
                         }
                         else
                         {
                            comm.appendByte(SYBVARBINARY);
                            comm.appendByte((byte)(maxLength));
                            comm.appendByte((byte)(value.length));
                         }
                         comm.appendBytes(value);
                      }
                      break;
                    }
                    case SYBVOID:
                    case SYBVARCHAR:
                    case SYBDATETIME4:
                    case SYBREAL:
                    case SYBMONEY:
                    case SYBDATETIME:
                    case SYBMONEYN:
                    case SYBMONEY4:
                    default:
                    {
                        throw new SQLException("Not implemented for nativeType 0x"
                                 + Integer.toHexString(nativeType));
                    }
                }
            }
            // mark that we are performing a query
            cancelController.setQueryInProgressFlag();

            comm.sendPacket();
            waitForDataOrTimeout(wChain, timeout);
        }
        catch (java.io.IOException e) {
            throw new SQLException("Network error-  " + e.getMessage());
        }
        finally
        {
            comm.packetType = 0;
        }
    }


    void checkMaxRows(java.sql.Statement stmt)
             throws java.sql.SQLException, TdsException
    {
      if (stmt == null) // internal usage
        return;
      int maxRows = stmt.getMaxRows();
      if ( maxRows != this.maxRows) {
        submitProcedure("set rowcount " + maxRows, new SQLWarningChain());
        this.maxRows = maxRows;
      }
    }
    /**
     *  send a query to the SQLServer for execution. <p>
     *
     *
     *
     *@param  sql                                        sql statement to
     *      execute.
     *@param  stmt
     *@param  timeout
     *@exception  java.io.IOException
     *@exception  java.sql.SQLException                  Description of
     *      Exception
     *@exception  TdsException                           Description of
     *      Exception
     */
    public synchronized void executeQuery(String sql, TdsStatement stmt, SQLWarningChain wChain, int timeout)
        throws java.io.IOException, java.sql.SQLException, TdsException
    {
        // 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 )
        {
            waitForDataOrTimeout(wChain, timeout);
            processSubPacket();
            if( cancelController.outstandingCancels() > 0 )
                throw new SQLException("Something went completely wrong. A cancel request was lost.");
        }

        checkMaxRows(stmt);

        // If the query has length 0, the server doesn't answer, so we'll hang
        if( sql.length() > 0 )
        {
            try
            {
                comm.startPacket(TdsComm.QUERY);
                if (tdsVer == Tds.TDS70) {
                    comm.appendChars(sql);
                }
                else {
                    byte[] sqlBytes = encoder.getBytes(sql);
                    comm.appendBytes(sqlBytes, sqlBytes.length, (byte) 0);
                }
                moreResults2 = true;
                cancelController.setQueryInProgressFlag();
                //JJ 1999-01-10
                comm.sendPacket();

                waitForDataOrTimeout(wChain, timeout);
            }
            finally
            {
                comm.packetType = 0;
            }
        }
    }


    /**
     * Skip over and discard any remaining data from a result set.
     *
     * @param  row  Description of Parameter
     * @exception  net.sourceforge.jtds.jdbc.TdsException
     * @exception  java.io.IOException
     */
    public synchronized void discardResultSet( PacketRowResult row )
             throws SQLException, java.io.IOException, TdsException
    {

        while ( isResultRow()) {
            comm.skip(1);
            if ( row != null ) {
                loadRow( row );
            }
            if ( Logger.isActive() ) {
                Logger.println( "Discarded row." );
            }
        }

        // SAfe Process everything up to the next TDS_DONE or TDS_DONEINPROC
        //      packet (there must be one, so we won't check the end of the
        //      stream.
        while( !isEndOfResults() )
            processSubPacket();

        // SAfe Then, eat up any uninteresting packets.
        goToNextResult(new SQLWarningChain());
    }

    public synchronized void discardResultSetOld( Context context )
             throws SQLException, java.io.IOException, TdsException
    {
        discardResultSet( new PacketRowResult( context ) );
    }

    public synchronized byte peek()
             throws java.io.IOException, net.sourceforge.jtds.jdbc.TdsException
    {
        return comm.peek();
    }

    /**
     * Eats up all tokens until the next relevant token (TDS_COLMETADATA or
     * TDS_DONE for regular Statements or TDS_COLMETADATA, TDS_DONE or
     * TDS_DONEINPROC for PreparedStatements).
     */
    protected synchronized void goToNextResult(SQLWarningChain warningChain)
        throws TdsException, java.io.IOException, SQLException
    {
        // SAfe Need to process messages, output parameters and return values
        //      here, or even better, in the processXXX methods.
        while( moreResults() )
        {
            byte next = peek();

            // SAfe If the next packet is a rowcount or ResultSet, break
            if( next==TDS_DONE || next==TDS_COLMETADATA || next==TDS_COL_NAME_TOKEN ||
                (next==TDS_DONEINPROC && statement instanceof PreparedStatement &&
                !(statement instanceof CallableStatement)) )
            {
                break;

⌨️ 快捷键说明

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