Example #1
0
		void IMySqlValue.SkipValue(MySqlStream stream)
		{
			int len = stream.ReadByte();
			stream.SkipBytes(len);
		}
Example #2
0
		void IMySqlValue.SkipValue(MySqlStream stream)
		{
			stream.ReadByte();
		}
Example #3
0
		IMySqlValue IMySqlValue.ReadValue(MySqlStream stream, long length, bool nullVal)
		{
			if (nullVal) return new MySqlTimeSpan(true);

			if (length >= 0)
			{
				string value = stream.ReadString(length);
				ParseMySql(value, stream.Version.isAtLeast(4, 1, 0));
				return this;
			}

			long bufLength = stream.ReadByte();
			int negate = 0;
			if (bufLength > 0)
				negate = stream.ReadByte();

			isNull = false;
			if (bufLength == 0)
				isNull = true;
			else if (bufLength == 5)
				mValue = new TimeSpan(stream.ReadInteger(4), 0, 0, 0);
			else if (bufLength == 8)
				mValue = new TimeSpan(stream.ReadInteger(4),
					 stream.ReadByte(), stream.ReadByte(), stream.ReadByte());
			else
				mValue = new TimeSpan(stream.ReadInteger(4),
					 stream.ReadByte(), stream.ReadByte(), stream.ReadByte(),
					 stream.ReadInteger(4) / 1000000);

			if (negate == 1)
				mValue = mValue.Negate();
			return this;
		}
		void IMySqlValue.SkipValue(MySqlStream stream)
		{
			long len = stream.ReadByte();
			stream.SkipBytes((int)len);
		}
Example #5
0
		IMySqlValue IMySqlValue.ReadValue(MySqlStream stream, long length, bool nullVal)
		{
			if (nullVal)
				return new MySqlByte(true);

			if (length == -1)
				return new MySqlByte((sbyte)stream.ReadByte());
			else
			{
				string s = stream.ReadString(length);
				MySqlByte b = new MySqlByte(SByte.Parse(s, NumberStyles.Any, CultureInfo.InvariantCulture));
                b.TreatAsBoolean = TreatAsBoolean;
                return b;
			}
		}
Example #6
0
        public override void Open()
        {
            base.Open();

            // connect to one of our specified hosts
            try
            {
#if !CF
                if (Settings.ConnectionProtocol == MySqlConnectionProtocol.SharedMemory)
                {
                    SharedMemoryStream str = new SharedMemoryStream(Settings.SharedMemoryName);
                    str.Open(Settings.ConnectionTimeout);
                    baseStream = str;
                }
                else
                {
#endif
                    string pipeName = Settings.PipeName;
                    if (Settings.ConnectionProtocol != MySqlConnectionProtocol.NamedPipe)
                        pipeName = null;
                    StreamCreator sc = new StreamCreator(Settings.Server, Settings.Port, pipeName);
                    baseStream = sc.GetStream(Settings.ConnectionTimeout);
#if !CF
                }
#endif
                if (baseStream == null)
                    throw new Exception();
            }
            catch (Exception ex)
            {
                throw new MySqlException(Resources.UnableToConnectToHost, 
                    (int) MySqlErrorCode.UnableToConnectToHost, ex);
            }

            if (baseStream == null)
                throw new MySqlException("Unable to connect to any of the specified MySQL hosts");

            int maxSinglePacket = 255*255*255;
            stream = new MySqlStream(baseStream, encoding, false);

            // read off the welcome packet and parse out it's values
            stream.OpenPacket();
            protocol = stream.ReadByte();
            string versionString = stream.ReadString();
            version = DBVersion.Parse(versionString);
            threadId = stream.ReadInteger(4);
            encryptionSeed = stream.ReadString();

            if (version.isAtLeast(4, 0, 8))
                maxSinglePacket = (256*256*256) - 1;

            // read in Server capabilities if they are provided
            serverCaps = 0;
            if (stream.HasMoreData)
                serverCaps = (ClientFlags) stream.ReadInteger(2);
            if (version.isAtLeast(4, 1, 1))
            {
                /* New protocol with 16 bytes to describe server characteristics */
                serverCharSetIndex = stream.ReadInteger(1);

                serverStatus = (ServerStatusFlags) stream.ReadInteger(2);
                stream.SkipBytes(13);
                string seedPart2 = stream.ReadString();
                encryptionSeed += seedPart2;
            }

            // based on our settings, set our connection flags
            SetConnectionFlags();

            stream.StartOutput(0, false);
            stream.WriteInteger((int) connectionFlags,
                                version.isAtLeast(4, 1, 0) ? 4 : 2);

#if !CF
            if (connectionString.UseSSL && (serverCaps & ClientFlags.SSL) != 0)
            {
                stream.Flush();

                StartSSL();

                stream.StartOutput(0, false);
                stream.WriteInteger((int) connectionFlags,
                                    version.isAtLeast(4, 1, 0) ? 4 : 2);
            }
#endif

            stream.WriteInteger(maxSinglePacket,
                                version.isAtLeast(4, 1, 0) ? 4 : 3);

            if (version.isAtLeast(4, 1, 1))
            {
                stream.WriteByte(8);
                stream.Write(new byte[23]);
            }

            Authenticate();

            // if we are using compression, then we use our CompressedStream class
            // to hide the ugliness of managing the compression
            if ((connectionFlags & ClientFlags.COMPRESS) != 0)
                stream = new MySqlStream(baseStream, encoding, true);

            // give our stream the server version we are connected to.  
            // We may have some fields that are read differently based 
            // on the version of the server we are connected to.
            stream.Version = version;
            stream.MaxBlockSize = maxSinglePacket;

            isOpen = true;
        }
		IMySqlValue IMySqlValue.ReadValue(MySqlStream stream, long length, bool nullVal)
		{
			if (nullVal) return new MySqlDateTime(type, true);

			if (length >= 0)
			{
				string value = stream.ReadString(length);
				return ParseMySql(value, stream.Version.isAtLeast(4, 1, 0));
			}

			long bufLength = stream.ReadByte();
			int year = 0, month = 0, day = 0;
			int hour = 0, minute = 0, second = 0;

			if (bufLength >= 4)
			{
				year = stream.ReadInteger(2);
				month = stream.ReadByte();
				day = stream.ReadByte();
			}

			if (bufLength > 4)
			{
				hour = stream.ReadByte();
				minute = stream.ReadByte();
				second = stream.ReadByte();
			}

			if (bufLength > 7)
				stream.ReadInteger(4);

			return new MySqlDateTime(type, year, month, day, hour, minute, second);
		}
Example #8
0
		IMySqlValue IMySqlValue.ReadValue(MySqlStream stream, long length, bool nullVal)
		{
			if (nullVal)
				return new MySqlUByte(true);

			if (length == -1)
				return new MySqlUByte((byte)stream.ReadByte());
			else
				return new MySqlUByte(Byte.Parse(stream.ReadString(length)));
		}
Example #9
0
        public override void Open()
        {
            base.Open();

            // connect to one of our specified hosts
            try
            {
#if !CF
                if (Settings.ConnectionProtocol == MySqlConnectionProtocol.SharedMemory)
                {
                    SharedMemoryStream str = new SharedMemoryStream(Settings.SharedMemoryName);
                    str.Open(Settings.ConnectionTimeout);
                    baseStream = str;
                }
                else
                {
#endif
                string pipeName = Settings.PipeName;
                if (Settings.ConnectionProtocol != MySqlConnectionProtocol.NamedPipe)
                {
                    pipeName = null;
                }
                StreamCreator sc = new StreamCreator(Settings.Server, Settings.Port, pipeName);
                baseStream = sc.GetStream(Settings.ConnectionTimeout);
#if !CF
            }
#endif
                if (baseStream == null)
                {
                    throw new Exception();
                }
            }
            catch (Exception ex)
            {
                throw new MySqlException(Resources.UnableToConnectToHost,
                                         (int)MySqlErrorCode.UnableToConnectToHost, ex);
            }

            if (baseStream == null)
            {
                throw new MySqlException("Unable to connect to any of the specified MySQL hosts");
            }

            int maxSinglePacket = 255 * 255 * 255;
            stream = new MySqlStream(baseStream, encoding, false);

            // read off the welcome packet and parse out it's values
            stream.OpenPacket();
            protocol = stream.ReadByte();
            string versionString = stream.ReadString();
            version        = DBVersion.Parse(versionString);
            threadId       = stream.ReadInteger(4);
            encryptionSeed = stream.ReadString();

            if (version.isAtLeast(4, 0, 8))
            {
                maxSinglePacket = (256 * 256 * 256) - 1;
            }

            // read in Server capabilities if they are provided
            serverCaps = 0;
            if (stream.HasMoreData)
            {
                serverCaps = (ClientFlags)stream.ReadInteger(2);
            }
            if (version.isAtLeast(4, 1, 1))
            {
                /* New protocol with 16 bytes to describe server characteristics */
                serverCharSetIndex = stream.ReadInteger(1);

                serverStatus = (ServerStatusFlags)stream.ReadInteger(2);
                stream.SkipBytes(13);
                string seedPart2 = stream.ReadString();
                encryptionSeed += seedPart2;
            }

            // based on our settings, set our connection flags
            SetConnectionFlags();

            stream.StartOutput(0, false);
            stream.WriteInteger((int)connectionFlags,
                                version.isAtLeast(4, 1, 0) ? 4 : 2);

#if !CF
            if (connectionString.UseSSL && (serverCaps & ClientFlags.SSL) != 0)
            {
                stream.Flush();

                StartSSL();

                stream.StartOutput(0, false);
                stream.WriteInteger((int)connectionFlags,
                                    version.isAtLeast(4, 1, 0) ? 4 : 2);
            }
#endif

            stream.WriteInteger(maxSinglePacket,
                                version.isAtLeast(4, 1, 0) ? 4 : 3);

            if (version.isAtLeast(4, 1, 1))
            {
                stream.WriteByte(8);
                stream.Write(new byte[23]);
            }

            Authenticate();

            // if we are using compression, then we use our CompressedStream class
            // to hide the ugliness of managing the compression
            if ((connectionFlags & ClientFlags.COMPRESS) != 0)
            {
                stream = new MySqlStream(baseStream, encoding, true);
            }

            // give our stream the server version we are connected to.
            // We may have some fields that are read differently based
            // on the version of the server we are connected to.
            stream.Version      = version;
            stream.MaxBlockSize = maxSinglePacket;

            isOpen = true;
        }
Example #10
0
        private void ReadOk(bool read)
        {
            try
            {
                if (read)
                {
                    stream.OpenPacket();
                }
                byte marker = (byte)stream.ReadByte();
                if (marker != 0)
                {
                    throw new MySqlException("Out of sync with server", true, null);
                }

                stream.ReadFieldLength(); /* affected rows */
                stream.ReadFieldLength(); /* last insert id */
                if (stream.HasMoreData)
                {
                    serverStatus = (ServerStatusFlags)stream.ReadInteger(2);
                    stream.ReadInteger(2);  /* warning count */
                    if (stream.HasMoreData)
                    {
                        stream.ReadLenString();  /* message */
                    }
                }
            }
            catch (MySqlException ex)
            {
                if (ex.IsFatal)
                {
                    isOpen = false;
                    Close();
                }
                throw;
            }
        }
Example #11
0
        private void ReadOk(bool read)
        {
            try
            {
                if (read)
                {
                    stream.OpenPacket();
                }
                byte marker = (byte)stream.ReadByte();
                if (marker != 0)
                {
                    throw new MySqlException("Out of sync with server", true, null);
                }

                long affectedRows = stream.ReadFieldLength();
                long lastInsertId = stream.ReadFieldLength();
                if (stream.HasMoreData)
                {
                    serverStatus = (ServerStatusFlags)stream.ReadInteger(2);
                    int warningCount = stream.ReadInteger(2);
                    if (stream.HasMoreData)
                    {
                        string msg = stream.ReadLenString();
                    }
                }
            }
            catch (MySqlException ex)
            {
                if (ex.IsFatal)
                {
                    isOpen = false;
                    Close();
                }
                throw;
            }
        }
Example #12
0
		public IMySqlValue ReadValue(MySqlStream stream, long length, bool isNull)
		{
            this.isNull = isNull;
			if (isNull)
				return this;

			if (buffer == null)
				buffer = new byte[8];
			if (length == -1)
			{
				length = stream.ReadFieldLength();
			}
			Array.Clear(buffer, 0, buffer.Length);
			for (long i = length - 1; i >= 0; i--)
				buffer[i] = (byte)stream.ReadByte();
			mValue = BitConverter.ToUInt64(buffer, 0);
			return this;
		}