/// <summary> Updates this object. </summary> /// /// <remarks> Asim Naeem, 7/20/2017. </remarks> /// /// <returns> A string. </returns> public string Update() { List <Object> setValues = new List <Object>(); List <Object> whereClause = new List <Object>(); foreach (PropertyInfo propertyInfo in TT.GetType().GetProperties(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance | BindingFlags.IgnoreCase | BindingFlags.SetProperty).Where(x => x.GetValue(TT, null) != null).Where(x => x.GetMethod.IsVirtual == false).Where(x => x.GetCustomAttribute <CustomAttribute>() == null || x.GetCustomAttribute <CustomAttribute>().NotMapped == false)) { object value = propertyInfo.GetValue(TT, null); switch (propertyInfo.PropertyType.ToString()) { case "System.DateTime": if ((DateTime)value != DateTime.MinValue && (DateTime)value != DateTime.MaxValue) { if (!HelperUtility.IsPrimary(propertyInfo)) { setValues.Add(HelperUtility.ColumnName(propertyInfo) + "='" + value + "'"); } else { whereClause.Add(HelperUtility.ColumnName(propertyInfo) + "='" + value + "'"); } } break; case "System.String": case "System.Byte": case "System.Char": if (!HelperUtility.IsPrimary(propertyInfo)) { setValues.Add(HelperUtility.ColumnName(propertyInfo) + "='" + value + "'"); } else { whereClause.Add(HelperUtility.ColumnName(propertyInfo) + "='" + value + "'"); } break; default: if (!HelperUtility.IsPrimary(propertyInfo)) { setValues.Add(HelperUtility.ColumnName(propertyInfo) + "=" + value); } else { whereClause.Add(HelperUtility.ColumnName(propertyInfo) + "=" + value); } break; } } return(string.Format("UPDATE {0} SET {1} WHERE {2}", HelperUtility.GetTableName <T>(), string.Join(",", setValues.ToArray()), string.Join(" AND ", whereClause.ToArray()) )); }
/// <summary> Gets the insert. </summary> /// /// <remarks> Asim Naeem, 7/20/2017. </remarks> /// /// <returns> A string. </returns> public string Insert() { List <String> columnList = new List <string>(); List <Object> valueList = new List <Object>(); foreach (PropertyInfo propertyInfo in TT.GetType().GetProperties(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance | BindingFlags.IgnoreCase | BindingFlags.SetProperty).Where(x => x.GetValue(TT, null) != null).Where(x => x.GetMethod.IsVirtual == false).Where(x => x.GetCustomAttribute <CustomAttribute>() == null || x.GetCustomAttribute <CustomAttribute>().NotMapped == false)) { object value = propertyInfo.GetValue(TT, null); switch (propertyInfo.PropertyType.ToString()) { case "System.DateTime": if ((DateTime)value != DateTime.MinValue && (DateTime)value != DateTime.MaxValue) { columnList.Add(HelperUtility.ColumnName(propertyInfo)); valueList.Add("'" + value + "'"); } break; case "System.String": case "System.Byte": case "System.Char": columnList.Add(HelperUtility.ColumnName(propertyInfo)); valueList.Add("'" + value + "'"); break; default: columnList.Add(HelperUtility.ColumnName(propertyInfo)); valueList.Add(value); break; } } return(string.Format("INSERT INTO {0} ({1}) VALUES ({2})", HelperUtility.GetTableName <T>(), string.Join(",", columnList.ToArray()), string.Join(",", valueList.ToArray()) )); }