Example #1
0
 private MySqlValue GetFieldValue(int index, bool checkNull)
 {
     if (index >= 0 && index < this.fields.Length)
     {
         try
         {
             MySqlValue mySqlValue = this.currentResult.ReadColumnValue(index);
             if (mySqlValue.IsNull && checkNull)
             {
                 throw new SqlNullValueException();
             }
             if (this.readCount == 0)
             {
                 throw new MySqlException("Invalid attempt to access a field before calling Read()");
             }
             return(mySqlValue);
         }
         catch (MySqlException ex)
         {
             if (ex.IsFatal)
             {
                 this.connection.Reader = null;
                 this.connection.Terminate();
             }
             throw;
         }
     }
     throw new ArgumentException("You have specified an invalid column ordinal.");
 }
Example #2
0
        public object GetValue(int i)
        {
            if (!this.isOpen)
            {
                throw new Exception("No current query in data reader");
            }
            if (i >= this.fields.Length)
            {
                throw new IndexOutOfRangeException();
            }
            MySqlValue fieldValue = this.GetFieldValue(i, false);

            if (fieldValue.IsNull)
            {
                return(DBNull.Value);
            }
            if (!(fieldValue is MySqlDateTime))
            {
                return(fieldValue.ValueAsObject);
            }
            MySqlDateTime mySqlDateTime = (MySqlDateTime)fieldValue;

            if (!mySqlDateTime.IsValidDateTime && this.connection.Settings.ConvertZeroDateTime)
            {
                return(DateTime.MinValue);
            }
            if (this.connection.Settings.AllowZeroDateTime)
            {
                return(fieldValue);
            }
            return(mySqlDateTime.GetDateTime());
        }
Example #3
0
        private MySqlValue GetFieldValue(int index)
        {
            if (index < 0 || index >= fields.Length)
            {
                throw new ArgumentException("You have specified an invalid column ordinal.");
            }

            try
            {
                MySqlValue val = currentResult.ReadColumnValue(index);
                if (readCount == 0)
                {
                    throw new MySqlException("Invalid attempt to access a field before calling Read()");
                }

                return(val);
            }
            catch (MySqlException ex)
            {
                if (ex.IsFatal)
                {
                    connection.Reader = null;
                    connection.Terminate();
                }
                throw;
            }
        }
Example #4
0
        public ushort GetUInt16(int index)
        {
            MySqlValue fieldValue = this.GetFieldValue(index, true);

            if (fieldValue is MySqlUInt16)
            {
                return(((MySqlUInt16)fieldValue).Value);
            }
            return(Convert.ToUInt16(fieldValue.ValueAsObject));
        }
Example #5
0
        public decimal GetDecimal(int index)
        {
            MySqlValue fieldValue = this.GetFieldValue(index, true);

            if (fieldValue is MySqlDecimal)
            {
                return(((MySqlDecimal)fieldValue).Value);
            }
            return(Convert.ToDecimal(fieldValue.ValueAsObject));
        }
Example #6
0
        public int GetInt32(int index)
        {
            MySqlValue fieldValue = this.GetFieldValue(index, true);

            if (fieldValue is MySqlInt32)
            {
                return(((MySqlInt32)fieldValue).Value);
            }
            return(Convert.ToInt32(fieldValue.ValueAsObject));
        }
Example #7
0
        public float GetFloat(int index)
        {
            MySqlValue fieldValue = this.GetFieldValue(index, true);

            if (fieldValue is MySqlFloat)
            {
                return(((MySqlFloat)fieldValue).Value);
            }
            return(Convert.ToSingle(fieldValue.ValueAsObject));
        }
Example #8
0
        /// <include file='docs/MySqlDataReader.xml' path='docs/GetUInt64/*'/>
        public UInt64 GetUInt64(int index)
        {
            MySqlValue v = GetFieldValue(index);

            if (v is MySqlUInt64)
            {
                return(((MySqlUInt64)v).Value);
            }
            return(Convert.ToUInt64(v.ValueAsObject));
        }
Example #9
0
        /// <include file='docs/MySqlDataReader.xml' path='docs/GetDecimal/*'/>
        public Decimal GetDecimal(int index)
        {
            MySqlValue v = GetFieldValue(index);

            if (v is MySqlDecimal)
            {
                return(((MySqlDecimal)v).Value);
            }
            return(Convert.ToDecimal(v.ValueAsObject));
        }
Example #10
0
        public ulong GetUInt64(int index)
        {
            MySqlValue fieldValue = this.GetFieldValue(index, true);

            if (fieldValue is MySqlUInt64)
            {
                return(((MySqlUInt64)fieldValue).Value);
            }
            return(Convert.ToUInt64(fieldValue.ValueAsObject));
        }
Example #11
0
        /// <include file='docs/MySqlDataReader.xml' path='docs/GetInt32/*'/>
        public Int32 GetInt32(int index)
        {
            MySqlValue v = GetFieldValue(index);

            if (v is MySqlInt32)
            {
                return(((MySqlInt32)v).Value);
            }
            return(Convert.ToInt32(v.ValueAsObject));
        }
Example #12
0
        public string GetString(int index)
        {
            MySqlValue fieldValue = this.GetFieldValue(index, true);

            if (fieldValue is MySqlBinary)
            {
                return((this.currentResult[index] as MySqlBinary).ToString(this.fields[index].Encoding));
            }
            return(fieldValue.ToString());
        }
Example #13
0
        /// <include file='docs/MySqlDataReader.xml' path='docs/GetFloat/*'/>
        public float GetFloat(int index)
        {
            MySqlValue v = GetFieldValue(index);

            if (v is MySqlFloat)
            {
                return(((MySqlFloat)v).Value);
            }
            return(Convert.ToSingle(v.ValueAsObject));
        }
Example #14
0
        /// <include file='docs/MySqlDataReader.xml' path='docs/GetDouble/*'/>
        public double GetDouble(int index)
        {
            MySqlValue v = GetFieldValue(index);

            if (v is MySqlDouble)
            {
                return(((MySqlDouble)v).Value);
            }
            return(Convert.ToDouble(v.ValueAsObject));
        }
Example #15
0
        public double GetDouble(int index)
        {
            MySqlValue fieldValue = this.GetFieldValue(index, true);

            if (fieldValue is MySqlDouble)
            {
                return(((MySqlDouble)fieldValue).Value);
            }
            return(Convert.ToDouble(fieldValue.ValueAsObject));
        }
Example #16
0
        public byte GetByte(int i)
        {
            MySqlValue fieldValue = this.GetFieldValue(i, false);

            if (fieldValue is MySqlUByte)
            {
                return(((MySqlUByte)fieldValue).Value);
            }
            return((byte)((MySqlByte)fieldValue).Value);
        }
Example #17
0
        /// <summary>
        /// Gets the value of the specified column as a byte.
        /// </summary>
        /// <param name="i"></param>
        /// <returns></returns>
        public byte GetByte(int i)
        {
            MySqlValue v = GetFieldValue(i);

            if (v is MySqlUByte)
            {
                return(((MySqlUByte)v).Value);
            }
            else
            {
                return((byte)((MySqlByte)v).Value);
            }
        }
Example #18
0
 internal MySqlValue GetValueObject()
 {
     if (this.valueObject == null)
     {
         this.valueObject = MySqlValue.GetMySqlValue(this.mySqlDbType);
         MySqlDecimal mySqlDecimal = this.valueObject as MySqlDecimal;
         if (mySqlDecimal != null)
         {
             mySqlDecimal.Precision = this.precision;
             mySqlDecimal.Scale     = this.scale;
         }
     }
     return(this.valueObject);
 }
Example #19
0
        internal MySqlValue GetValueObject()
        {
            if (valueObject == null)
            {
                valueObject = MySqlValue.GetMySqlValue(mySqlDbType, IsUnsigned, true);

                MySqlDecimal dec = (valueObject as MySqlDecimal);
                if (dec != null)
                {
                    dec.Precision = precision;
                    dec.Scale     = scale;
                }
            }
            return(valueObject);
        }
Example #20
0
        private void SetMySqlDbType(MySqlDbType mySqlDbType)
        {
            if (this.mySqlDbType != mySqlDbType)
            {
                valueObject = null;
            }
            this.mySqlDbType = mySqlDbType;
            switch (mySqlDbType)
            {
            case MySqlDbType.Decimal: dbType = DbType.Decimal; break;

            case MySqlDbType.Byte: dbType = isUnsigned ? DbType.Byte : DbType.SByte; break;

            case MySqlDbType.Int16: dbType = isUnsigned ? DbType.UInt16 : DbType.Int16; break;

            case MySqlDbType.Int24: dbType = isUnsigned ? DbType.UInt32 : DbType.Int32; break;

            case MySqlDbType.Int32: dbType = isUnsigned ? DbType.UInt32 : DbType.Int32; break;

            case MySqlDbType.Int64: dbType = isUnsigned ? DbType.UInt64 : DbType.Int64; break;

            case MySqlDbType.Bit: dbType = DbType.UInt64; break;

            case MySqlDbType.Float: dbType = DbType.Single; break;

            case MySqlDbType.Double: dbType = DbType.Double; break;

            case MySqlDbType.Timestamp:
            case MySqlDbType.Datetime: dbType = DbType.DateTime; break;

            case MySqlDbType.Date:
            case MySqlDbType.Newdate:
            case MySqlDbType.Year: dbType = DbType.Date; break;

            case MySqlDbType.Time: dbType = DbType.Time; break;

            case MySqlDbType.Enum:
            case MySqlDbType.Set:
            case MySqlDbType.VarChar: dbType = DbType.String;  break;

            case MySqlDbType.TinyBlob:
            case MySqlDbType.MediumBlob:
            case MySqlDbType.LongBlob:
            case MySqlDbType.Blob: dbType = DbType.Object; break;

            case MySqlDbType.String: dbType = DbType.StringFixedLength; break;
            }
        }
Example #21
0
        /// <summary>
        /// Reads a stream of bytes from the specified column offset into the buffer an array starting at the given buffer offset.
        /// </summary>
        /// <param name="i">The zero-based column ordinal. </param>
        /// <param name="dataIndex">The index within the field from which to begin the read operation. </param>
        /// <param name="buffer">The buffer into which to read the stream of bytes. </param>
        /// <param name="bufferIndex">The index for buffer to begin the read operation. </param>
        /// <param name="length">The maximum length to copy into the buffer. </param>
        /// <returns>The actual number of bytes read.</returns>
        /// <include file='docs/MySqlDataReader.xml' path='MyDocs/MyMembers[@name="GetBytes"]/*'/>
        public long GetBytes(int i, long dataIndex, byte[] buffer, int bufferIndex, int length)
        {
            if (i >= fields.Length)
            {
                throw new IndexOutOfRangeException();
            }

            MySqlValue val = GetFieldValue(i);

            if (!(val is MySqlBinary))
            {
                throw new MySqlException("GetBytes can only be called on binary columns");
            }

            MySqlBinary binary = (MySqlBinary)val;

            if (buffer == null)
            {
                return((long)binary.Value.Length);
            }

            if (bufferIndex >= buffer.Length || bufferIndex < 0)
            {
                throw new IndexOutOfRangeException("Buffer index must be a valid index in buffer");
            }
            if (buffer.Length < (bufferIndex + length))
            {
                throw new ArgumentException("Buffer is not large enough to hold the requested data");
            }
            if (dataIndex < 0 ||
                ((ulong)dataIndex >= (ulong)binary.Value.Length && (ulong)binary.Value.Length > 0))
            {
                throw new IndexOutOfRangeException("Data index must be a valid index in the field");
            }

            byte[] bytes = (byte[])binary.Value;

            // adjust the length so we don't run off the end
            if ((ulong)binary.Value.Length < (ulong)(dataIndex + length))
            {
                length = (int)((ulong)binary.Value.Length - (ulong)dataIndex);
            }

            Array.Copy(bytes, (int)dataIndex, buffer, (int)bufferIndex, (int)length);

            return(length);
        }
Example #22
0
        /// <include file='docs/MySqlDataReader.xml' path='docs/GetString/*'/>
        public String GetString(int index)
        {
            MySqlValue  val = GetFieldValue(index);
            MySqlString str = (val as MySqlString);

            if (str != null)
            {
                return(str.Value);
            }

            if (val is MySqlBinary)
            {
                return((currentResult[index] as MySqlBinary).ToString(fields[index].Encoding));
            }

            return(val.ToString());
        }
Example #23
0
        public override void SkipField(MySqlValue valObject)
        {
            long num = -1L;

            if (this.nullMap == null)
            {
                num = this.reader.GetFieldLength();
                if (num == -1L)
                {
                    return;
                }
            }
            if (num > -1L)
            {
                this.reader.Skip((long)((int)num));
                return;
            }
            valObject.Skip(this.reader);
        }
Example #24
0
        public override MySqlValue ReadFieldValue(int index, MySqlField field, MySqlValue valObject)
        {
            long num = -1L;

            if (this.nullMap != null)
            {
                valObject.IsNull = this.nullMap[index + 2];
            }
            else
            {
                num = this.reader.GetFieldLength();
                valObject.IsNull = (num == -1L);
            }
            if (valObject.IsNull)
            {
                return(valObject);
            }
            this.reader.Encoding = field.Encoding;
            return(valObject.ReadValue(this.reader, num));
        }
Example #25
0
        public override void SkipField(MySqlValue valObject)
        {
            long length = -1;

            if (nullMap == null)
            {
                length = reader.GetFieldLength();
                if (length == -1)
                {
                    return;
                }
            }
            if (length > -1)
            {
                reader.Skip((int)length);
            }
            else
            {
                valObject.Skip(reader);
            }
        }
Example #26
0
        public override MySqlValue ReadFieldValue(int index, MySqlField field, MySqlValue valObject)
        {
            long length = -1;

            if (nullMap != null)
            {
                valObject.IsNull = nullMap [index + 2];
            }
            else
            {
                length           = reader.GetFieldLength();
                valObject.IsNull = length == -1;
            }

            if (valObject.IsNull)
            {
                return(valObject);
            }

            reader.Encoding = field.Encoding;
            return(valObject.ReadValue(reader, length));
        }
Example #27
0
        /// <summary>
        /// Gets the value of the specified column in its native format.
        /// </summary>
        /// <param name="i"></param>
        /// <returns></returns>
        public object GetValue(int i)
        {
            if (!isOpen)
            {
                throw new Exception("No current query in data reader");
            }
            if (i >= fields.Length)
            {
                throw new IndexOutOfRangeException();
            }

            MySqlValue val = GetFieldValue(i);

            if (val.IsNull)
            {
                return(DBNull.Value);
            }

            // if the column is a date/time, then we return a MySqlDateTime
            // so .ToString() will print '0000-00-00' correctly
            if (val is MySqlDateTime)
            {
                MySqlDateTime dt = (MySqlDateTime)val;
                if (!dt.IsValidDateTime && connection.Settings.ConvertZeroDateTime)
                {
                    return(DateTime.MinValue);
                }
                else if (connection.Settings.AllowZeroDateTime)
                {
                    return(val);
                }
                else
                {
                    return(dt.GetDateTime());
                }
            }

            return(val.ValueAsObject);
        }
Example #28
0
        public long GetBytes(int i, long dataIndex, byte[] buffer, int bufferIndex, int length)
        {
            if (i >= this.fields.Length)
            {
                throw new IndexOutOfRangeException();
            }
            MySqlValue fieldValue = this.GetFieldValue(i, false);

            if (!(fieldValue is MySqlBinary))
            {
                throw new MySqlException("GetBytes can only be called on binary columns");
            }
            MySqlBinary mySqlBinary = (MySqlBinary)fieldValue;

            if (buffer == null)
            {
                return((long)mySqlBinary.Value.Length);
            }
            if (bufferIndex >= buffer.Length || bufferIndex < 0)
            {
                throw new IndexOutOfRangeException("Buffer index must be a valid index in buffer");
            }
            if (buffer.Length < bufferIndex + length)
            {
                throw new ArgumentException("Buffer is not large enough to hold the requested data");
            }
            if (dataIndex >= 0L && (dataIndex < (long)mySqlBinary.Value.Length || (long)mySqlBinary.Value.Length == 0L))
            {
                byte[] value = mySqlBinary.Value;
                if ((long)mySqlBinary.Value.Length < dataIndex + (long)length)
                {
                    length = (int)((long)mySqlBinary.Value.Length - dataIndex);
                }
                Array.Copy(value, (int)dataIndex, buffer, bufferIndex, length);
                return((long)length);
            }
            throw new IndexOutOfRangeException("Data index must be a valid index in the field");
        }
Example #29
0
        /// <include file='docs/MySqlDataReader.xml' path='docs/GetDateTime/*'/>
        public DateTime GetDateTime(int index)
        {
            MySqlValue val = GetFieldValue(index);

            if (val is MySqlDateTime)
            {
                MySqlDateTime dt = (MySqlDateTime)val;
                if (connection.Settings.ConvertZeroDateTime && !dt.IsValidDateTime)
                {
                    return(DateTime.MinValue);
                }
                else
                {
                    return(dt.GetDateTime());
                }
            }
            else if (val is MySqlString)
            {
                MySqlDateTime d = new MySqlDateTime(MySqlDbType.Datetime);
                d = d.ParseMySql((val as MySqlString).Value, true);
                return(d.GetDateTime());
            }
            throw new NotSupportedException("Unable to convert from type " + val.GetType().ToString() + " to DateTime");
        }
Example #30
0
        public DateTime GetDateTime(int index)
        {
            MySqlValue fieldValue = this.GetFieldValue(index, true);

            if (fieldValue is MySqlDateTime)
            {
                MySqlDateTime mySqlDateTime = (MySqlDateTime)fieldValue;
                if (this.connection.Settings.ConvertZeroDateTime && !mySqlDateTime.IsValidDateTime)
                {
                    return(DateTime.MinValue);
                }
                return(mySqlDateTime.GetDateTime());
            }
            else
            {
                if (!(fieldValue is MySqlString))
                {
                    throw new NotSupportedException("Unable to convert from type " + fieldValue.GetType().ToString() + " to DateTime");
                }
                MySqlDateTime mySqlDateTime2 = new MySqlDateTime(MySqlDbType.Datetime);
                mySqlDateTime2 = mySqlDateTime2.ParseMySql(this.GetString(index), true);
                return(mySqlDateTime2.GetDateTime());
            }
        }