//public void Fill<T>(DataSet dataSet, Expression<Func<T, bool>> filterExpression) //{ // AdsEntityMapping mapping = AdsEntityMapping.GetForType(typeof(T)); // Fill<T>(mapping, dataSet, CreateQuery<T>(mapping, filterExpression)); //} public void Delete <T>(string sqlFilterExpression) where T : class { AdsEntityMapping mapping = AdsEntityMapping.GetForType(typeof(T)); AdsCommand command = new AdsCommand(); command.Connection = connection; command.CommandText = CreateDeleteQuery <T>(mapping, sqlFilterExpression); LastSqlCommandText = command.CommandText; command.ExecuteNonQuery(); }
public object GetAutoIncPrimaryKeyValue(object obj) { AdsEntityMapping mapping = AdsEntityMapping.GetForType(obj.GetType()); foreach (AdsEntityMemberMapping memberMapping in mapping.Members) { if (memberMapping.ColumnAttribute.IsPrimaryKey && memberMapping.ColumnAttribute.IsAutoGenerated) { return(memberMapping.GetValue(obj)); } } return(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 void Fill <T>(DataSet dataSet, int?topCount, string sqlFilterExpression, string sqlOrderExpression) where T : class { AdsEntityMapping mapping = AdsEntityMapping.GetForType(typeof(T)); Fill <T>(mapping, dataSet, topCount, sqlFilterExpression, sqlOrderExpression); }
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); }