using System; using System.Data; namespace Csla.Data { /// /// This is a DataReader that 'fixes' any null values before /// they are returned to our business code. /// public class SafeDataReader : IDataReader { private IDataReader _dataReader; /// /// Get a reference to the underlying data reader /// object that actually contains the data from /// the data source. /// protected IDataReader DataReader { get { return _dataReader; } } /// /// Initializes the SafeDataReader object to use data from /// the provided DataReader object. /// /// The source DataReader object containing the data. public SafeDataReader(IDataReader dataReader) { _dataReader = dataReader; } /// /// Gets a string value from the datareader. /// /// /// Returns empty string for null. /// /// Name of the column containing the value. public string GetString(string name) { return GetString(_dataReader.GetOrdinal(name)); } /// /// Gets a string value from the datareader. /// /// /// Returns empty string for null. /// /// Ordinal column position of the value. public virtual string GetString(int i) { if (_dataReader.IsDBNull(i)) return string.Empty; else return _dataReader.GetString(i); } /// /// Gets a value of type from the datareader. /// /// Name of the column containing the value. public object GetValue(string name) { return GetValue(_dataReader.GetOrdinal(name)); } /// /// Gets a value of type from the datareader. /// /// Ordinal column position of the value. public virtual object GetValue(int i) { if (_dataReader.IsDBNull(i)) return null; else return _dataReader.GetValue(i); } /// /// Gets an integer from the datareader. /// /// /// Returns 0 for null. /// /// Name of the column containing the value. public int GetInt32(string name) { return GetInt32(_dataReader.GetOrdinal(name)); } /// /// Gets an integer from the datareader. /// /// /// Returns 0 for null. /// /// Ordinal column position of the value. public virtual int GetInt32(int i) { if (_dataReader.IsDBNull(i)) return 0; else return _dataReader.GetInt32(i); } /// /// Gets a double from the datareader. /// /// /// Returns 0 for null. /// /// Name of the column containing the value. public double GetDouble(string name) { return GetDouble(_dataReader.GetOrdinal(name)); } /// /// Gets a double from the datareader. /// /// /// Returns 0 for null. /// /// Ordinal column position of the value. public virtual double GetDouble(int i) { if (_dataReader.IsDBNull(i)) return 0; else return _dataReader.GetDouble(i); } /// /// Gets a from the datareader. /// /// /// A null is converted into min possible date /// See Chapter 5 for more details on the SmartDate class. /// /// Name of the column containing the value. public Csla.SmartDate GetSmartDate(string name) { return GetSmartDate(_dataReader.GetOrdinal(name), true); } /// /// Gets a from the datareader. /// /// /// A null is converted into the min possible date /// See Chapter 5 for more details on the SmartDate class. /// /// Ordinal column position of the value. public virtual Csla.SmartDate GetSmartDate(int i) { return GetSmartDate(i, true); } /// /// Gets a from the datareader. /// /// /// A null is converted into either the min or max possible date /// depending on the MinIsEmpty parameter. See Chapter 5 for more /// details on the SmartDate class. /// /// Name of the column containing the value. /// /// A flag indicating whether the min or max /// value of a data means an empty date. public Csla.SmartDate GetSmartDate(string name, bool minIsEmpty) { return GetSmartDate(_dataReader.GetOrdinal(name), minIsEmpty); } /// /// Gets a from the datareader. /// /// Ordinal column position of the value. /// /// A flag indicating whether the min or max /// value of a data means an empty date. public virtual Csla.SmartDate GetSmartDate( int i, bool minIsEmpty) { if (_dataReader.IsDBNull(i)) return new Csla.SmartDate(minIsEmpty); else return new Csla.SmartDate( _dataReader.GetDateTime(i), minIsEmpty); } /// /// Gets a Guid value from the datareader. /// /// /// Returns Guid.Empty for null. /// /// Name of the column containing the value. public System.Guid GetGuid(string name) { return GetGuid(_dataReader.GetOrdinal(name)); } /// /// Gets a Guid value from the datareader. /// /// /// Returns Guid.Empty for null. /// /// Ordinal column position of the value. public virtual System.Guid GetGuid(int i) { if (_dataReader.IsDBNull(i)) return Guid.Empty; else return _dataReader.GetGuid(i); } /// /// Reads the next row of data from the datareader. /// public bool Read() { return _dataReader.Read(); } /// /// Moves to the next result set in the datareader. /// public bool NextResult() { return _dataReader.NextResult(); } /// /// Closes the datareader. /// public void Close() { _dataReader.Close(); } /// /// Returns the depth property value from the datareader. /// public int Depth { get { return _dataReader.Depth; } } /// /// Returns the FieldCount property from the datareader. /// public int FieldCount { get { return _dataReader.FieldCount; } } /// /// Gets a boolean value from the datareader. /// /// /// Returns for null. /// /// Name of the column containing the value. public bool GetBoolean(string name) { return GetBoolean(_dataReader.GetOrdinal(name)); } /// /// Gets a boolean value from the datareader. /// /// /// Returns for null. /// /// Ordinal column position of the value. public virtual bool GetBoolean(int i) { if (_dataReader.IsDBNull(i)) return false; else return _dataReader.GetBoolean(i); } /// /// Gets a byte value from the datareader. /// /// /// Returns 0 for null. /// /// Name of the column containing the value. public byte GetByte(string name) { return GetByte(_dataReader.GetOrdinal(name)); } /// /// Gets a byte value from the datareader. /// /// /// Returns 0 for null. /// /// Ordinal column position of the value. public virtual byte GetByte(int i) { if (_dataReader.IsDBNull(i)) return 0; else return _dataReader.GetByte(i); } /// /// Invokes the GetBytes method of the underlying datareader. /// /// /// Returns 0 for null. /// /// Name of the column containing the value. /// Array containing the data. /// Offset position within the buffer. /// Offset position within the field. /// Length of data to read. public Int64 GetBytes(string name, Int64 fieldOffset, byte[] buffer, int bufferOffset, int length) { return GetBytes(_dataReader.GetOrdinal(name), fieldOffset, buffer, bufferOffset, length); } /// /// Invokes the GetBytes method of the underlying datareader. /// /// /// Returns 0 for null. /// /// Ordinal column position of the value. /// Array containing the data. /// Offset position within the buffer. /// Offset position within the field. /// Length of data to read. public virtual Int64 GetBytes(int i, Int64 fieldOffset, byte[] buffer, int bufferOffset, int length) { if (_dataReader.IsDBNull(i)) return 0; else return _dataReader.GetBytes(i, fieldOffset, buffer, bufferOffset, length); } /// /// Gets a char value from the datareader. /// /// /// Returns Char.MinValue for null. /// /// Name of the column containing the value. public char GetChar(string name) { return GetChar(_dataReader.GetOrdinal(name)); } /// /// Gets a char value from the datareader. /// /// /// Returns Char.MinValue for null. /// /// Ordinal column position of the value. public virtual char GetChar(int i) { if (_dataReader.IsDBNull(i)) return char.MinValue; else { char[] myChar = new char[1]; _dataReader.GetChars(i, 0, myChar, 0, 1); return myChar[0]; } } /// /// Invokes the GetChars method of the underlying datareader. /// /// /// Returns 0 for null. /// /// Name of the column containing the value. /// Array containing the data. /// Offset position within the buffer. /// Offset position within the field. /// Length of data to read. public Int64 GetChars(string name, Int64 fieldOffset, char[] buffer, int bufferOffset, int length) { return GetChars(_dataReader.GetOrdinal(name), fieldOffset, buffer, bufferOffset, length); } /// /// Invokes the GetChars method of the underlying datareader. /// /// /// Returns 0 for null. /// /// Ordinal column position of the value. /// Array containing the data. /// Offset position within the buffer. /// Offset position within the field. /// Length of data to read. public virtual Int64 GetChars(int i, Int64 fieldOffset, char[] buffer, int bufferOffset, int length) { if (_dataReader.IsDBNull(i)) return 0; else return _dataReader.GetChars(i, fieldOffset, buffer, bufferOffset, length); } /// /// Invokes the GetData method of the underlying datareader. /// /// Name of the column containing the value. public IDataReader GetData(string name) { return GetData(_dataReader.GetOrdinal(name)); } /// /// Invokes the GetData method of the underlying datareader. /// /// Ordinal column position of the value. public virtual IDataReader GetData(int i) { return _dataReader.GetData(i); } /// /// Invokes the GetDataTypeName method of the underlying datareader. /// /// Name of the column containing the value. public string GetDataTypeName(string name) { return GetDataTypeName(_dataReader.GetOrdinal(name)); } /// /// Invokes the GetDataTypeName method of the underlying datareader. /// /// Ordinal column position of the value. public virtual string GetDataTypeName(int i) { return _dataReader.GetDataTypeName(i); } /// /// Gets a date value from the datareader. /// /// /// Returns DateTime.MinValue for null. /// /// Name of the column containing the value. public virtual DateTime GetDateTime(string name) { return GetDateTime(_dataReader.GetOrdinal(name)); } /// /// Gets a date value from the datareader. /// /// /// Returns DateTime.MinValue for null. /// /// Ordinal column position of the value. public virtual DateTime GetDateTime(int i) { if (_dataReader.IsDBNull(i)) return DateTime.MinValue; else return _dataReader.GetDateTime(i); } /// /// Gets a decimal value from the datareader. /// /// /// Returns 0 for null. /// /// Name of the column containing the value. public decimal GetDecimal(string name) { return GetDecimal(_dataReader.GetOrdinal(name)); } /// /// Gets a decimal value from the datareader. /// /// /// Returns 0 for null. /// /// Ordinal column position of the value. public virtual decimal GetDecimal(int i) { if (_dataReader.IsDBNull(i)) return 0; else return _dataReader.GetDecimal(i); } /// /// Invokes the GetFieldType method of the underlying datareader. /// /// Name of the column containing the value. public Type GetFieldType(string name) { return GetFieldType(_dataReader.GetOrdinal(name)); } /// /// Invokes the GetFieldType method of the underlying datareader. /// /// Ordinal column position of the value. public virtual Type GetFieldType(int i) { return _dataReader.GetFieldType(i); } /// /// Gets a Single value from the datareader. /// /// /// Returns 0 for null. /// /// Name of the column containing the value. public float GetFloat(string name) { return GetFloat(_dataReader.GetOrdinal(name)); } /// /// Gets a Single value from the datareader. /// /// /// Returns 0 for null. /// /// Ordinal column position of the value. public virtual float GetFloat(int i) { if (_dataReader.IsDBNull(i)) return 0; else return _dataReader.GetFloat(i); } /// /// Gets a Short value from the datareader. /// /// /// Returns 0 for null. /// /// Name of the column containing the value. public short GetInt16(string name) { return GetInt16(_dataReader.GetOrdinal(name)); } /// /// Gets a Short value from the datareader. /// /// /// Returns 0 for null. /// /// Ordinal column position of the value. public virtual short GetInt16(int i) { if (_dataReader.IsDBNull(i)) return 0; else return _dataReader.GetInt16(i); } /// /// Gets a Long value from the datareader. /// /// /// Returns 0 for null. /// /// Name of the column containing the value. public Int64 GetInt64(string name) { return GetInt64(_dataReader.GetOrdinal(name)); } /// /// Gets a Long value from the datareader. /// /// /// Returns 0 for null. /// /// Ordinal column position of the value. public virtual Int64 GetInt64(int i) { if (_dataReader.IsDBNull(i)) return 0; else return _dataReader.GetInt64(i); } /// /// Invokes the GetName method of the underlying datareader. /// /// Ordinal column position of the value. public virtual string GetName(int i) { return _dataReader.GetName(i); } /// /// Gets an ordinal value from the datareader. /// /// Name of the column containing the value. public int GetOrdinal(string name) { return _dataReader.GetOrdinal(name); } /// /// Invokes the GetSchemaTable method of the underlying datareader. /// public DataTable GetSchemaTable() { return _dataReader.GetSchemaTable(); } /// /// Invokes the GetValues method of the underlying datareader. /// /// An array of System.Object to /// copy the values into. public int GetValues(object[] values) { return _dataReader.GetValues(values); } /// /// Returns the IsClosed property value from the datareader. /// public bool IsClosed { get { return _dataReader.IsClosed; } } /// /// Invokes the IsDBNull method of the underlying datareader. /// /// Ordinal column position of the value. public virtual bool IsDBNull(int i) { return _dataReader.IsDBNull(i); } /// /// Returns a value from the datareader. /// /// Name of the column containing the value. public object this[string name] { get { object val = _dataReader[name]; if (DBNull.Value.Equals(val)) return null; else return val; } } /// /// Returns a value from the datareader. /// /// Ordinal column position of the value. public virtual object this[int i] { get { if (_dataReader.IsDBNull(i)) return null; else return _dataReader[i]; } } /// /// Returns the RecordsAffected property value from the underlying datareader. /// public int RecordsAffected { get { return _dataReader.RecordsAffected; } } #region IDisposable Support private bool _disposedValue; // To detect redundant calls /// /// Disposes the object. /// /// True if called by /// the public Dispose method. protected virtual void Dispose(bool disposing) { if (!_disposedValue) { if (disposing) { // free unmanaged resources when explicitly called _dataReader.Dispose(); } // free shared unmanaged resources } _disposedValue = true; } /// /// Disposes the object. /// public void Dispose() { // Do not change this code. Put cleanup code in Dispose(bool disposing) above. Dispose(true); GC.SuppressFinalize(this); } /// /// Object finalizer. /// ~SafeDataReader() { Dispose(false); } #endregion } }