Exemple #1
0
        public static MultiPartTableName?ReadMultiPartTableName(this TdsPackageReader reader)
        {
            // Find out how many parts in the TDS stream
            var nParts = reader.ReadByte();

            if (nParts == 0)
            {
                return(null);
            }

            var mpt = new MultiPartTableName();

            if (nParts == 4)
            {
                mpt.ServerName = reader.ReadString(reader.ReadUInt16());
            }

            if (nParts >= 3)
            {
                mpt.CatalogName = reader.ReadString(reader.ReadUInt16());
            }

            if (nParts >= 2)
            {
                mpt.SchemaName = reader.ReadString(reader.ReadUInt16());
            }

            mpt.TableName = reader.ReadString(reader.ReadUInt16());

            return(mpt);
        }
Exemple #2
0
        public static int GetTokenLength(this TdsPackageReader reader, byte token)
        {
            switch (token & TdsEnums.SQLLenMask)
            {
            case TdsEnums.SQLZeroLen:
                return(0);

            case TdsEnums.SQLFixedLen:
                return(0x01 << ((token & 0x0c) >> 2));

            default:
                switch (token)
                {
                case TdsEnums.SQLFEATUREEXTACK:
                    return(-1);

                case TdsEnums.SQLSESSIONSTATE:
                    return(reader.ReadInt32());

                    ;

                case TdsEnums.SQLRETURNVALUE:
                    return(-1);        // In Yukon, the RETURNVALUE token stream no longer has length

                default:
                    return(reader.ReadUInt16());
                }
            }
        }
Exemple #3
0
 internal static Udt ReadUdtMetadata(this TdsPackageReader reader) =>
 new Udt
 {
     DatabaseName          = reader.ReadString(reader.ReadByte()),
     SchemaName            = reader.ReadString(reader.ReadByte()),
     TypeName              = reader.ReadString(reader.ReadByte()),
     AssemblyQualifiedName = reader.ReadString(reader.ReadUInt16())
 };
Exemple #4
0
 public static int ReadTdsTypeLen(this TdsPackageReader reader, int len) =>
 len == 0
         ? 0
         : len == 1
             ? reader.ReadByte()
             : len == 2
                 ? reader.ReadUInt16()
                 : reader.ReadInt32();
Exemple #5
0
        public static XmlSchema?ReadXmlSchema(this TdsPackageReader reader)
        {
            var schemaPresent = reader.ReadByte();

            if ((schemaPresent & 1) != 0)
            {
                return new XmlSchema
                       {
                           CollectionDatabase     = reader.ReadString(reader.ReadByte()),
                           CollectionOwningSchema = reader.ReadString(reader.ReadByte()),
                           CollectionName         = reader.ReadString(reader.ReadUInt16())
                       }
            }
            ;
            return(null);
        }
Exemple #6
0
        public static void SqlErrorAndInfo(this TdsPackageReader reader, byte token, int tokenLength)
        {
            var start = reader.GetReadPos();
            var error = new SqlInfoAndError
            {
                Number    = reader.ReadInt32(),
                State     = reader.ReadByte(),
                Class     = reader.ReadByte(),
                Message   = reader.ReadString(reader.ReadUInt16()),
                Server    = reader.ReadString(reader.ReadByte()),
                Procedure = reader.ReadString(reader.ReadByte())
            };
            var current = reader.GetReadPos();

            error.LineNumber = tokenLength - (current - start) > 2 ? reader.ReadInt32() : reader.ReadInt16();
            if (error.Class >= TdsEnums.MIN_ERROR_CLASS)
            {
                throw new Exception(error.Message);
            }
            reader.CurrentSession.Errors.Add(error);
        }
Exemple #7
0
        public static bool SqlDone(this TdsPackageReader reader)
        {
            // status
            // command
            // rowcount (valid only if DONE_COUNT bit is set)
            bool attentionReceived;
            var  status = reader.ReadUInt16();
            var  curCmd = reader.ReadUInt16();
            var  count  = (int)reader.ReadInt64();


            // We get a done token with the attention bit set
            if (TdsEnums.DONE_ATTN == (status & TdsEnums.DONE_ATTN))
            {
                attentionReceived = true;
            }

            //if (cmd != null && TdsEnums.DONE_COUNT == (status & TdsEnums.DONE_COUNT))
            //{
            //    if (curCmd != TdsEnums.SELECT) cmd.InternalRecordsAffected = count;
            //    // Skip the bogus DONE counts sent by the server
            //    if (curCmd != TdsEnums.SELECT) cmd.OnStatementCompleted(count);
            //}


            // Surface exception for DONE_ERROR in the case we did not receive an error token
            // in the stream, but an error occurred.  In these cases, we throw a general server error.  The
            // situations where this can occur are: an invalid buffer received from client, login error
            // and the server refused our connection, and the case where we are trying to log in but
            // the server has reached its max connection limit.  Bottom line, we need to throw general
            // error in the cases where we did not receive an error token along with the DONE_ERROR.
            if (TdsEnums.DONE_ERROR == (TdsEnums.DONE_ERROR & status))
            {
            }

            // Similar to above, only with a more severe error.  In this case, if we received
            // the done_srverror, this exception will be added to the collection regardless.
            // The server will always break the connection in this case.
            if (TdsEnums.DONE_SRVERROR == (TdsEnums.DONE_SRVERROR & status))
            {
            }


            // stop if the DONE_MORE bit isn't set (see above for attention handling)
            var _pendingData   = false;
            var _hasOpenResult = false;

            if (TdsEnums.DONE_MORE != (status & TdsEnums.DONE_MORE))
            {
                return(true);
            }

            // _pendingData set by e.g. 'TdsExecuteSQLBatch'
            // _hasOpenResult always set to true by 'WriteMarsHeader'
            //
            if (!_pendingData && _hasOpenResult)
            {
                ;
            }
            return(false);
        }