using System; using System.Data; using System.Globalization; namespace Ease.Core.DataAccess { #region DataAccess: Data Handler /// /// Handle the null value /// public class RowReader { private DataRow _row; public RowReader(DataRow row) { _row = row; } #region Get ID public long GetID(Type[] types, params int[] fldIdxs) { if (types == null || fldIdxs == null || types.Length != fldIdxs.Length) throw new Exception("Invalid use of array for ID."); string value = string.Empty; foreach (int fldIdx in fldIdxs) { if (_row[fldIdx] == null || _row[fldIdx] == DBNull.Value) return long.MinValue; value += value.Length > 0 ? "|" : ""; value += this.GetID(types[fldIdx], _row[fldIdx]); } return long.Parse(value); } public long GetID(Type[] types, params string[] names) { if (types == null || names == null || types.Length != names.Length) throw new Exception("Invalid use of array for ID."); int fldIdx = -1; string value = string.Empty; foreach (string name in names) { fldIdx++; if (_row[name] == null || _row[name] == DBNull.Value) return long.MinValue; value += value.Length > 0 ? "|" : ""; value += this.GetID(types[fldIdx], _row[name]); } return long.Parse(value); } private string GetID(Type fldType, object fldValue) { if (fldType == typeof(int) || fldType == typeof(uint)) return string.Format("i:{0}", fldValue); else if (fldType == typeof(long) || fldType == typeof(ulong) || fldType == typeof(decimal)) return string.Format("l:{0}", fldValue); else throw new Exception("Invalid use of type for ID"); } #endregion #region Get Null value public static bool? GetNullValue(bool value, bool nullIfValue) { return (value == nullIfValue) ? null : (bool?)value; } public static byte? GetNullValue(byte value, byte nullIfValue) { return (value == nullIfValue) ? null : (byte?)value; } public static sbyte? GetNullValue(sbyte value, sbyte nullIfValue) { return (value == nullIfValue) ? null : (sbyte?)value; } public static short? GetNullValue(short value, short nullIfValue) { return (value == nullIfValue) ? null : (short?)value; } public static ushort? GetNullValue(ushort value, ushort nullIfValue) { return (value == nullIfValue) ? null : (ushort?)value; } public static int? GetNullValue(int value, int nullIfValue) { return (value == nullIfValue) ? null : (int?)value; } public static uint? GetNullValue(uint value, uint nullIfValue) { return (value == nullIfValue) ? null : (uint?)value; } public static ulong? GetNullValue(ulong value, ulong nullIfValue) { return (value == nullIfValue) ? null : (ulong?)value; } public static long? GetNullValue(long value, long nullIfValue) { return (value == nullIfValue) ? null : (long?)value; } public static decimal? GetNullValue(decimal value, decimal nullIfValue) { return (value == nullIfValue) ? null : (byte?)value; } public static double? GetNullValue(double value, double nullIfValue) { return (value == nullIfValue) ? null : (byte?)value; } public static float? GetNullValue(float value, float nullIfValue) { return (value == nullIfValue) ? null : (float?)value; } public static char? GetNullValue(char value, char nullIfValue) { return (value == nullIfValue) ? null : (char?)value; } public static object GetNullValue(string value, string nullIfValue) { return (value == nullIfValue) ? null : value; } public static DateTime? GetNullValue(DateTime value, DateTime nullIfValue) { return (value == nullIfValue) ? null : (DateTime?)value; } public static Guid? GetNullValue(Guid value, Guid nullIfValue) { return (value == nullIfValue) ? null : (Guid?)value; } public static bool? GetNullValue(bool? value) { return (value == null || value == false) ? null : (bool?)value; } public static byte? GetNullValue(byte? value) { return (value == null || value == 0) ? null : (byte?)value; } public static sbyte? GetNullValue(sbyte? value) { return (value == null || value == 0) ? null : (sbyte?)value; } public static short? GetNullValue(short? value) { return (value == null || value == 0) ? null : (short?)value; } public static ushort? GetNullValue(ushort? value) { return (value == null || value == 0) ? null : (ushort?)value; } public static int? GetNullValue(int? value) { return (value == null || value == 0) ? null : (int?)value; } public static uint? GetNullValue(uint? value) { return (value == null || value == 0) ? null : (uint?)value; } public static ulong? GetNullValue(ulong? value) { return (value == null || value == 0) ? null : (ulong?)value; } public static long? GetNullValue(long? value) { return (value == null || value == 0) ? null : (long?)value; } public static decimal? GetNullValue(decimal? value) { return (value == null || value == 0) ? null : (decimal?)value; } public static double? GetNullValue(double? value) { return (value == null || value == 0) ? null : (double?)value; } public static float? GetNullValue(float? value) { return (value == null || value == 0) ? null : (float?)value; } public static char? GetNullValue(char? value) { return (value == null || (byte)value == 0) ? null : (char?)value; } public static string GetNullValue(string value) { return (string.IsNullOrEmpty(value)) ? null : value; } public static DateTime? GetNullValue(DateTime? value) { return (value == null || value == DateTime.MinValue) ? null : (DateTime?)value; } public static Guid? GetNullValue(Guid? value) { return value; } public static Object GetNullValue(long value) { if (value == null || value == long.MinValue) return null; return value; } #endregion #region Default Null Values public bool? GetBoolean(int i) { IConvertible value = (IConvertible)_row[i]; if (value == null || value == DBNull.Value) return null; if (value.ToString().Trim().Equals("1") || value.ToString().Trim().Equals("-1") || value.ToString().Trim().ToLower().Equals("true") || value.ToString().Trim().ToLower().Equals("yes")) return true; else if (value.ToString().Trim().Equals("0") || value.ToString().Trim().ToLower().Equals("false") || value.ToString().Trim().ToLower().Equals("no")) return false; else return (bool?)value.ToBoolean(null); } public byte? GetByte(int i) { IConvertible value = (IConvertible)_row[i]; return value == DBNull.Value ? null : (byte?)value.ToByte(null); } public sbyte? GetSByte(int i) { IConvertible value = (IConvertible)_row[i]; return value == DBNull.Value ? null : (sbyte?)value.ToSByte(null); } public short? GetInt16(int i) { IConvertible value = (IConvertible)_row[i]; return value == DBNull.Value ? null : (short?)value.ToInt16(null); } public ushort? GetUInt16(int i) { IConvertible value = (IConvertible)_row[i]; return value == DBNull.Value ? null : (ushort?)value.ToUInt16(null); } public int? GetInt32(int i) { IConvertible value = (IConvertible)_row[i]; return value == DBNull.Value ? null : (int?)value.ToInt32(null); } public uint? GetUInt32(int i) { IConvertible value = (IConvertible)_row[i]; return value == DBNull.Value ? null : (uint?)value.ToUInt32(null); } public long? GetInt64(int i) { IConvertible value = (IConvertible)_row[i]; return value == DBNull.Value ? null : (long?)value.ToInt64(null); } public ulong? GetUInt64(int i) { IConvertible value = (IConvertible)_row[i]; return value == DBNull.Value ? null : (ulong?)value.ToUInt64(null); } public decimal? GetDecimal(int i) { IConvertible value = (IConvertible)_row[i]; return value == DBNull.Value ? null : (decimal?)value.ToDecimal(null); } public double? GetDouble(int i) { IConvertible value = (IConvertible)_row[i]; return value == DBNull.Value ? null : (double?)value.ToDouble(null); } public float? GetFloat(int i) { IConvertible value = (IConvertible)_row[i]; return value == DBNull.Value ? null : (float?)value.ToSingle(null); } public char? GetChar(int i) { IConvertible value = (IConvertible)_row[i]; return value == DBNull.Value ? null : (char?)value.ToChar(null); } public string GetString(int i) { IConvertible value = (IConvertible)_row[i]; return value == DBNull.Value ? null : value.ToString(null); } public DateTime? GetDateTime(int i) { IConvertible value = (IConvertible)_row[i]; return value == DBNull.Value ? null : (DateTime?)value.ToDateTime(null); } public Guid? GetGuid(int i) { object value = _row[i]; return value == DBNull.Value ? null : (Guid?)value; } public byte[] GetLob(int i) { return (byte[])(_row.IsNull(i) ? (byte[])null : _row[i]); } public bool? GetBoolean(string name) { IConvertible value = (IConvertible)_row[name]; if (value == null || value == DBNull.Value) return null; if (value.ToString().Trim().Equals("1") || value.ToString().Trim().Equals("-1") || value.ToString().Trim().ToLower().Equals("true") || value.ToString().Trim().ToLower().Equals("yes")) return true; else if (value.ToString().Trim().Equals("0") || value.ToString().Trim().ToLower().Equals("false") || value.ToString().Trim().ToLower().Equals("no")) return false; else return (bool?)value.ToBoolean(null); } public byte? GetByte(string name) { IConvertible value = (IConvertible)_row[name]; return value == DBNull.Value ? null : (byte?)value.ToByte(null); } public sbyte? GetSByte(string name) { IConvertible value = (IConvertible)_row[name]; return value == DBNull.Value ? null : (sbyte?)value.ToSByte(null); } public short? GetInt16(string name) { IConvertible value = (IConvertible)_row[name]; return value == DBNull.Value ? null : (short?)value.ToInt16(null); } public ushort? GetUInt16(string name) { IConvertible value = (IConvertible)_row[name]; return value == DBNull.Value ? null : (ushort?)value.ToUInt16(null); } public int? GetInt32(string name) { IConvertible value = (IConvertible)_row[name]; return value == DBNull.Value ? (int?)null : (int?)value.ToInt32(null); } public uint? GetUInt32(string name) { IConvertible value = (IConvertible)_row[name]; return value == DBNull.Value ? null : (uint?)value.ToUInt32(null); } public long? GetInt64(string name) { IConvertible value = (IConvertible)_row[name]; return value == DBNull.Value ? null : (long?)value.ToInt64(null); } public ulong? GetUInt64(string name) { IConvertible value = (IConvertible)_row[name]; return value == DBNull.Value ? null : (ulong?)value.ToUInt64(null); } public decimal? GetDecimal(string name) { IConvertible value = (IConvertible)_row[name]; return value == DBNull.Value ? null : (decimal?)value.ToDecimal(null); } public double? GetDouble(string name) { IConvertible value = (IConvertible)_row[name]; return value == DBNull.Value ? null : (double?)value.ToDouble(null); } public float? GetFloat(string name) { IConvertible value = (IConvertible)_row[name]; return value == DBNull.Value ? null : (float?)value.ToSingle(null); } public char? GetChar(string name) { IConvertible value = (IConvertible)_row[name]; return value == DBNull.Value ? null : (char?)value.ToChar(null); } public string GetString(string name) { IConvertible value = (IConvertible)_row[name]; return value == DBNull.Value ? null : value.ToString(null); } public DateTime? GetDateTime(string name) { IConvertible value = (IConvertible)_row[name]; return value == DBNull.Value ? null : (DateTime?)value.ToDateTime(null); } public Guid? GetGuid(string name) { object value = _row[name]; return value == DBNull.Value ? null : (Guid?)value; } public byte[] GetLob(string name) { return (byte[])(_row[name] == DBNull.Value ? (byte[])null : _row[name]); } #endregion #region Parameterized Null Value public bool GetBoolean(int i, bool valueIfNull) { IConvertible value = (IConvertible)_row[i]; if (value == null || value == DBNull.Value) return valueIfNull; if (value.ToString().Trim().Equals("1") || value.ToString().Trim().Equals("-1") || value.ToString().Trim().ToLower().Equals("true") || value.ToString().Trim().ToLower().Equals("yes")) return true; else if (value.ToString().Trim().Equals("0") || value.ToString().Trim().ToLower().Equals("false") || value.ToString().Trim().ToLower().Equals("no")) return false; else return value.ToBoolean(null); } public byte GetByte(int i, byte valueIfNull) { IConvertible value = (IConvertible)_row[i]; return value == DBNull.Value ? valueIfNull : value.ToByte(null); } public sbyte GetSByte(int i, sbyte valueIfNull) { IConvertible value = (IConvertible)_row[i]; return value == DBNull.Value ? valueIfNull : value.ToSByte(null); } public short GetInt16(int i, short valueIfNull) { IConvertible value = (IConvertible)_row[i]; return value == DBNull.Value ? valueIfNull : value.ToInt16(null); } public ushort GetUInt16(int i, ushort valueIfNull) { IConvertible value = (IConvertible)_row[i]; return value == DBNull.Value ? valueIfNull : value.ToUInt16(null); } public int GetInt32(int i, int valueIfNull) { IConvertible value = (IConvertible)_row[i]; return value == DBNull.Value ? valueIfNull : value.ToInt32(null); } public uint GetUInt32(int i, uint valueIfNull) { IConvertible value = (IConvertible)_row[i]; return value == DBNull.Value ? valueIfNull : value.ToUInt32(null); } public ulong GetUInt64(int i, ulong valueIfNull) { IConvertible value = (IConvertible)_row[i]; return value == DBNull.Value ? valueIfNull : value.ToUInt64(null); } public long GetInt64(int i, long valueIfNull) { IConvertible value = (IConvertible)_row[i]; return value == DBNull.Value ? valueIfNull : value.ToInt64(null); } public decimal GetDecimal(int i, decimal valueIfNull) { IConvertible value = (IConvertible)_row[i]; return value == DBNull.Value ? valueIfNull : value.ToDecimal(null); } public double GetDouble(int i, double valueIfNull) { IConvertible value = (IConvertible)_row[i]; return value == DBNull.Value ? valueIfNull : value.ToDouble(null); } public float GetFloat(int i, float valueIfNull) { IConvertible value = (IConvertible)_row[i]; return value == DBNull.Value ? valueIfNull : value.ToSingle(null); } public char GetChar(int i, char valueIfNull) { IConvertible value = (IConvertible)_row[i]; return value == DBNull.Value ? valueIfNull : value.ToChar(null); } public string GetString(int i, string valueIfNull) { IConvertible value = (IConvertible)_row[i]; return value == DBNull.Value ? valueIfNull : value.ToString(null); } public DateTime GetDateTime(int i, DateTime valueIfNull) { IConvertible value = (IConvertible)_row[i]; return value == DBNull.Value ? valueIfNull : value.ToDateTime(null); } public Guid GetGuid(int i, Guid valueIfNull) { object value = _row[i]; return value == DBNull.Value ? valueIfNull : (Guid)value; } public bool GetBoolean(string name, bool valueIfNull) { IConvertible value = (IConvertible)_row[name]; if (value == null || value == DBNull.Value) return valueIfNull; if (value.ToString().Trim().Equals("1") || value.ToString().Trim().Equals("-1") || value.ToString().Trim().ToLower().Equals("true") || value.ToString().Trim().ToLower().Equals("yes")) return true; else if (value.ToString().Trim().Equals("0") || value.ToString().Trim().ToLower().Equals("false") || value.ToString().Trim().ToLower().Equals("no")) return false; else return value.ToBoolean(null); } public byte GetByte(string name, byte valueIfNull) { IConvertible value = (IConvertible)_row[name]; return value == DBNull.Value ? valueIfNull : value.ToByte(null); } public sbyte GetSByte(string name, sbyte valueIfNull) { IConvertible value = (IConvertible)_row[name]; return value == DBNull.Value ? valueIfNull : value.ToSByte(null); } public short GetInt16(string name, short valueIfNull) { IConvertible value = (IConvertible)_row[name]; return value == DBNull.Value ? valueIfNull : value.ToInt16(null); } public ushort GetUInt16(string name, ushort valueIfNull) { IConvertible value = (IConvertible)_row[name]; return value == DBNull.Value ? valueIfNull : value.ToUInt16(null); } public int GetInt32(string name, int valueIfNull) { IConvertible value = (IConvertible)_row[name]; return value == DBNull.Value ? valueIfNull : value.ToInt32(null); } public uint GetUInt32(string name, uint valueIfNull) { IConvertible value = (IConvertible)_row[name]; return value == DBNull.Value ? valueIfNull : value.ToUInt32(null); } public ulong GetUInt64(string name, ulong valueIfNull) { IConvertible value = (IConvertible)_row[name]; return value == DBNull.Value ? valueIfNull : value.ToUInt64(null); } public long GetInt64(string name, long valueIfNull) { IConvertible value = (IConvertible)_row[name]; return value == DBNull.Value ? valueIfNull : value.ToInt64(null); } public decimal GetDecimal(string name, decimal valueIfNull) { IConvertible value = (IConvertible)_row[name]; return value == DBNull.Value ? valueIfNull : value.ToDecimal(null); } public double GetDouble(string name, double valueIfNull) { IConvertible value = (IConvertible)_row[name]; return value == DBNull.Value ? valueIfNull : value.ToDouble(null); } public float GetFloat(string name, float valueIfNull) { IConvertible value = (IConvertible)_row[name]; return value == DBNull.Value ? valueIfNull : value.ToSingle(null); } public char GetChar(string name, char valueIfNull) { IConvertible value = (IConvertible)_row[name]; return value == DBNull.Value ? valueIfNull : value.ToChar(null); } public string GetString(string name, string valueIfNull) { IConvertible value = (IConvertible)_row[name]; return value == DBNull.Value ? valueIfNull : value.ToString(null); } public DateTime GetDateTime(string name, DateTime valueIfNull) { IConvertible value = (IConvertible)_row[name]; return value == DBNull.Value ? valueIfNull : value.ToDateTime(null); } public Guid GetGuid(string name, Guid valueIfNull) { object value = _row[name]; return value == DBNull.Value ? valueIfNull : (Guid)value; } #endregion } #endregion }