private void AdjustOutputTypes() { // since MySQL likes to return user variables as strings // we reset the types of the readers internal value objects // this will allow those value objects to parse the string based // return values for (int i = 0; i < FieldCount; i++) { string fieldName = GetName(i); fieldName = fieldName.Remove(0, StoredProcedure.ParameterPrefix.Length + 1); MyCatParameter parameter = command.Parameters.GetParameterFlexible(fieldName, true); IMyCatValue v = MyCatField.GetIMyCatValue(parameter.MyCatDbType); if (v is MyCatBit) { MyCatBit bit = (MyCatBit)v; bit.ReadAsString = true; resultSet.SetValueObject(i, bit); } else { resultSet.SetValueObject(i, v); } } }
/// <summary> /// Gets the Type that is the data type of the object. /// </summary> /// <param name="i"></param> /// <returns></returns> public override Type GetFieldType(int i) { if (!isOpen) { Throw(new Exception("No current query in data reader")); } if (i >= FieldCount) { Throw(new IndexOutOfRangeException()); } // we have to use the values array directly because we can't go through // GetValue IMyCatValue v = resultSet.Values[i]; if (v is MyCatDateTime) { if (!connection.Settings.AllowZeroDateTime) { return(typeof(DateTime)); } return(typeof(MyCatDateTime)); } return(v.SystemType); }
/// <include file='docs/MyCatDataReader.xml' path='docs/GetDateTime/*'/> public override DateTime GetDateTime(int i) { IMyCatValue val = GetFieldValue(i, true); MyCatDateTime dt; if (val is MyCatDateTime) { dt = (MyCatDateTime)val; } else { // we need to do this because functions like date_add return string string s = GetString(i); dt = MyCatDateTime.Parse(s); } dt.TimezoneOffset = driver.timeZoneOffset; if (connection.Settings.ConvertZeroDateTime && !dt.IsValidDateTime) { return(DateTime.MinValue); } else { return(dt.GetDateTime()); } }
/// <include file='docs/MyCatDataReader.xml' path='docs/GetTimeSpan/*'/> public TimeSpan GetTimeSpan(int column) { IMyCatValue val = GetFieldValue(column, true); MyCatTimeSpan ts = (MyCatTimeSpan)val; return(ts.Value); }
/// <include file='docs/MyCatDataReader.xml' path='docs/GetFloat/*'/> public override float GetFloat(int i) { IMyCatValue v = GetFieldValue(i, true); if (v is MyCatSingle) { return(((MyCatSingle)v).Value); } return(Convert.ToSingle(v.Value)); }
/// <include file='docs/MyCatDataReader.xml' path='docs/GetDecimal/*'/> public override Decimal GetDecimal(int i) { IMyCatValue v = GetFieldValue(i, true); if (v is MyCatDecimal) { return(((MyCatDecimal)v).Value); } return(Convert.ToDecimal(v.Value)); }
/// <include file='docs/MyCatDataReader.xml' path='docs/GetDouble/*'/> public override double GetDouble(int i) { IMyCatValue v = GetFieldValue(i, true); if (v is MyCatDouble) { return(((MyCatDouble)v).Value); } return(Convert.ToDouble(v.Value)); }
/// <include file='docs/MyCatDataReader.xml' path='docs/GetUInt32/*'/> public UInt32 GetUInt32(int column) { IMyCatValue v = GetFieldValue(column, true); if (v is MyCatUInt32) { return(((MyCatUInt32)v).Value); } return((uint)ChangeType(v, column, typeof(UInt32))); }
/// <include file='docs/MyCatDataReader.xml' path='docs/GetUInt64/*'/> public UInt64 GetUInt64(int column) { IMyCatValue v = GetFieldValue(column, true); if (v is MyCatUInt64) { return(((MyCatUInt64)v).Value); } return((UInt64)ChangeType(v, column, typeof(UInt64))); }
/// <include file='docs/MyCatDataReader.xml' path='docs/GetInt64/*'/> public override Int64 GetInt64(int i) { IMyCatValue v = GetFieldValue(i, true); if (v is MyCatInt64) { return(((MyCatInt64)v).Value); } return((Int64)ChangeType(v, i, typeof(Int64))); }
/// <include file='docs/MyCatDataReader.xml' path='docs/GetInt16/*'/> public override Int16 GetInt16(int i) { IMyCatValue v = GetFieldValue(i, true); if (v is MyCatInt16) { return(((MyCatInt16)v).Value); } return((short)ChangeType(v, i, typeof(short))); }
/// <include file='docs/MyCatDataReader.xml' path='docs/GetString/*'/> public override String GetString(int i) { IMyCatValue val = GetFieldValue(i, true); if (val is MyCatBinary) { byte[] v = ((MyCatBinary)val).Value; return(resultSet.Fields[i].Encoding.GetString(v, 0, v.Length)); } return(val.Value.ToString()); }
/// <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="fieldOffset">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="bufferoffset">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/MyCatDataReader.xml' path='MyDocs/MyMembers[@name="GetBytes"]/*'/> public override long GetBytes(int i, long fieldOffset, byte[] buffer, int bufferoffset, int length) { if (i >= FieldCount) { Throw(new IndexOutOfRangeException()); } IMyCatValue val = GetFieldValue(i, false); if (!(val is MyCatBinary) && !(val is MyCatGuid)) { Throw(new MyCatException("GetBytes can only be called on binary or guid columns")); } byte[] bytes = null; if (val is MyCatBinary) { bytes = ((MyCatBinary)val).Value; } else { bytes = ((MyCatGuid)val).Bytes; } if (buffer == null) { return(bytes.Length); } if (bufferoffset >= buffer.Length || bufferoffset < 0) { Throw(new IndexOutOfRangeException("Buffer index must be a valid index in buffer")); } if (buffer.Length < (bufferoffset + length)) { Throw(new ArgumentException("Buffer is not large enough to hold the requested data")); } if (fieldOffset < 0 || ((ulong)fieldOffset >= (ulong)bytes.Length && (ulong)bytes.Length > 0)) { Throw(new IndexOutOfRangeException("Data index must be a valid index in the field")); } // adjust the length so we don't run off the end if ((ulong)bytes.Length < (ulong)(fieldOffset + length)) { length = (int)((ulong)bytes.Length - (ulong)fieldOffset); } Buffer.BlockCopy(bytes, (int)fieldOffset, buffer, (int)bufferoffset, (int)length); return(length); }
/// <summary> /// Gets the value of the specified column as a byte. /// </summary> /// <param name="i"></param> /// <returns></returns> public override byte GetByte(int i) { IMyCatValue v = GetFieldValue(i, false); if (v is MyCatUByte) { return(((MyCatUByte)v).Value); } else { return((byte)((MyCatByte)v).Value); } }
/// <summary> /// Gets the value of the specified column as a sbyte. /// </summary> /// <param name="i"></param> /// <returns></returns> public sbyte GetSByte(int i) { IMyCatValue v = GetFieldValue(i, false); if (v is MyCatByte) { return(((MyCatByte)v).Value); } else { return((sbyte)((MyCatByte)v).Value); } }
/// <summary> /// Gets a value indicating whether the column contains non-existent or missing values. /// </summary> /// <param name="i"></param> /// <returns></returns> public override bool IsDBNull(int i) { if (!isOpen) { Throw(new Exception("No current query in data reader")); } if (i >= FieldCount) { Throw(new IndexOutOfRangeException()); } IMyCatValue val = GetFieldValue(i, false); return(val.IsNull); }
public MyCatGeometry GetMyCatGeometry(int i) { try { IMyCatValue v = GetFieldValue(i, false); if (v is MyCatGeometry || v is MyCatBinary) { return(new MyCatGeometry(MyCatDbType.Geometry, (Byte[])v.Value)); } } catch { Throw(new Exception("Can't get MyCatGeometry from value")); } return(new MyCatGeometry(true)); }
/// <summary> /// Gets the name of the source data type. /// </summary> /// <param name="i"></param> /// <returns></returns> public override String GetDataTypeName(int i) { if (!isOpen) { Throw(new Exception("No current query in data reader")); } if (i >= FieldCount) { Throw(new IndexOutOfRangeException()); } // return the name of the type used on the backend IMyCatValue v = resultSet.Values[i]; return(v.MyCatTypeName); }
private IMyCatValue GetFieldValue(int index, bool checkNull) { if (index < 0 || index >= FieldCount) { Throw(new ArgumentException(Resources.InvalidColumnOrdinal)); } IMyCatValue v = resultSet[index]; if (checkNull && v.IsNull) #if NETSTANDARD1_3 { throw new MyCatNullValueException(); } #else { throw new System.Data.SqlTypes.SqlNullValueException(); } #endif return(v); }
public IMyCatValue GetValueObject() { IMyCatValue v = GetIMyCatValue(Type); if (v is MyCatByte && ColumnLength == 1 && driver.Settings.TreatTinyAsBoolean) { MyCatByte b = (MyCatByte)v; b.TreatAsBoolean = true; v = b; } else if (v is MyCatGuid) { MyCatGuid g = (MyCatGuid)v; g.OldGuids = driver.Settings.OldGuids; v = g; } return(v); }
/// <summary> /// Gets the value of the specified column in its native format. /// </summary> /// <param name="i"></param> /// <returns></returns> public override object GetValue(int i) { if (!isOpen) { Throw(new Exception("No current query in data reader")); } if (i >= FieldCount) { Throw(new IndexOutOfRangeException()); } IMyCatValue val = GetFieldValue(i, false); if (val.IsNull) { return(DBNull.Value); } // if the column is a date/time, then we return a MyCatDateTime // so .ToString() will print '0000-00-00' correctly if (val is MyCatDateTime) { MyCatDateTime dt = (MyCatDateTime)val; if (!dt.IsValidDateTime && connection.Settings.ConvertZeroDateTime) { return(DateTime.MinValue); } else if (connection.Settings.AllowZeroDateTime) { return(val); } else { return(dt.GetDateTime()); } } if (val is MyCatJson) { return(new JsonObject(val.Value.ToString())); } return(val.Value); }
public IMyCatValue ReadColumnValue(int index, MyCatField field, IMyCatValue valObject) { long length = -1; bool isNull; if (nullMap != null) { isNull = nullMap[index + 2]; } else { length = packet.ReadFieldLength(); isNull = length == -1; } packet.Encoding = field.Encoding; packet.Version = version; return(valObject.ReadValue(packet, length, isNull)); }
public void SkipColumnValue(IMyCatValue valObject) { int length = -1; if (nullMap == null) { length = (int)packet.ReadFieldLength(); if (length == -1) { return; } } if (length > -1) { packet.Position += length; } else { valObject.SkipValue(packet); } }
public void SetValueObject(int i, IMyCatValue valueObject) { Debug.Assert(values != null); Debug.Assert(i < values.Length); values[i] = valueObject; }
public void SkipColumnValue(IMyCatValue valObject) { handler.SkipColumnValue(valObject); }
public IMyCatValue ReadColumnValue(int index, MyCatField field, IMyCatValue value) { return(handler.ReadColumnValue(index, field, value)); }
private object ChangeType(IMyCatValue value, int fieldIndex, Type newType) { resultSet.Fields[fieldIndex].AddTypeConversion(newType); return(Convert.ChangeType(value.Value, newType, CultureInfo.InvariantCulture)); }