public static AdsEntityMapping GetForType(Type type) { if (cachedMappings.ContainsKey(type)) { return(cachedMappings[type]); } object[] attributes = type.GetCustomAttributes(typeof(AdsTableAttribute), true); if (attributes != null && attributes.Length > 0) { AdsEntityMapping mapping = new AdsEntityMapping(); mapping.EntityType = type; mapping.TableAttribute = (AdsTableAttribute)attributes[0]; foreach (MemberInfo memberInfo in type.GetMembers()) { object[] memberAttributes = memberInfo.GetCustomAttributes(typeof(AdsColumnAttribute), true); if (memberAttributes != null && memberAttributes.Length > 0) { AdsEntityMemberMapping memberMapping = new AdsEntityMemberMapping(memberInfo, (AdsColumnAttribute)memberAttributes[0]); mapping.Members.Add(memberMapping); } } cachedMappings.Add(type, mapping); return(mapping); } else { throw new AdsContextException("The class sent to SaveOrUpdate did not have the necessary AdsTable attribute", null); } }
public void SaveOrUpdate(object obj) { AdsEntityMapping mapping = AdsEntityMapping.GetForType(obj.GetType()); AdsEntityMemberMapping autoIncPrimaryKeyMapping = null; bool isInsert = !mapping.IsPrimaryKeySet(obj); string list1 = string.Empty; string list2 = string.Empty; AdsCommand command = new AdsCommand(); command.Connection = connection; foreach (AdsEntityMemberMapping memberMapping in mapping.Members) { if (memberMapping.ColumnAttribute.IsPrimaryKey && memberMapping.ColumnAttribute.IsAutoGenerated) { autoIncPrimaryKeyMapping = memberMapping; } if (list1.Length > 0) { list1 += ","; } if (isInsert) { if (!memberMapping.ColumnAttribute.IsPrimaryKey) { list1 += memberMapping.ResolvedName; if (list2.Length > 0) { list2 += ","; } list2 += ":p_" + memberMapping.ResolvedName; } } else { if (memberMapping.ColumnAttribute.IsPrimaryKey) { if (list2.Length > 0) { list2 += " and "; } list2 += string.Format("{0} = :p_{0}", memberMapping.ResolvedName); } else { list1 += string.Format("{0} = :p_{0}", memberMapping.ResolvedName); } } if (!isInsert || (isInsert && !(memberMapping.ColumnAttribute.IsPrimaryKey && memberMapping.ColumnAttribute.IsAutoGenerated))) { command.Parameters.Add(":p_" + memberMapping.ResolvedName, memberMapping.GetValue(obj)); } } string sql; if (isInsert) { sql = string.Format("insert into {0} ({1}) values ({2})", mapping.ResolvedName, list1, list2); } else { sql = string.Format("update {0} set {1} where {2}", mapping.ResolvedName, list1, list2); } command.CommandText = sql; LastSqlCommandText = command.CommandText; command.ExecuteNonQuery(); if (isInsert && autoIncPrimaryKeyMapping != null) { object lastAutoInc; if (autoIncPrimaryKeyMapping.MemberType == typeof(int)) { lastAutoInc = command.LastAutoinc; } else if (autoIncPrimaryKeyMapping.MemberType == typeof(uint)) { lastAutoInc = (uint)command.LastAutoinc; } else if (autoIncPrimaryKeyMapping.MemberType == typeof(long)) { lastAutoInc = (long)command.LastAutoinc; } else if (autoIncPrimaryKeyMapping.MemberType == typeof(ulong)) { lastAutoInc = (ulong)command.LastAutoinc; } else { throw new NotImplementedException("The type of the primary key is not supported for auto-incrementation"); } autoIncPrimaryKeyMapping.SetValue(obj, lastAutoInc); } }
public T Get <T>(object key) where T : class { AdsEntityMapping mapping = AdsEntityMapping.GetForType(typeof(T)); var primaryKeyMappings = new List <AdsEntityMemberMapping>(); foreach (AdsEntityMemberMapping memberMapping in mapping.Members) { if (memberMapping.ColumnAttribute.IsPrimaryKey) { primaryKeyMappings.Add(memberMapping); } } if (primaryKeyMappings.Count == 0) { throw new AdsContextException("No primary key is defined for the class"); } if (primaryKeyMappings.Count > 1) { throw new AdsContextException("Multiple primary key columns are not supported"); } AdsEntityMemberMapping primaryKeyMapping = primaryKeyMappings[0]; string selectFilter = string.Format("{0} = :p_key", primaryKeyMapping.ResolvedName);; string query = CreateSelectQuery <T>(mapping, null, selectFilter, null); AdsCommand command = new AdsCommand(query, connection); command.Parameters.Add(":p_key", key); AdsDataReader reader = command.ExecuteReader(); if (reader.HasRows && reader.Read()) { T obj = (T)Activator.CreateInstance(typeof(T)); foreach (AdsEntityMemberMapping memberMapping in mapping.Members) { int resolvedNameOrdinal = reader.GetOrdinal(memberMapping.ResolvedName); object value = null; if (!reader.IsDBNull(resolvedNameOrdinal)) { value = reader.GetValue(resolvedNameOrdinal); if (memberMapping.MemberType == typeof(uint) && value is int) { value = (uint)(int)value; } if (memberMapping.MemberType == typeof(ushort) && value is short) { value = (ushort)(short)value; } } memberMapping.SetValue(obj, value); } return(obj); } return(null); }