public static void Update <T>(this IHop hopper, IEnumerable <T> instances) where T : new() { SchemaVerifierService.AddTypeToCache <T>(hopper.Connection); IIdExtractorService idExtractorService = HopBase.GetIdExtractorService(); List <T> instanceList = instances as List <T> ?? instances.ToList(); var sb = new StringBuilder(); int paramCounter = 0; using (var dbCommand = new SqlCommand()) { dbCommand.Connection = (SqlConnection)hopper.Connection; foreach (T inst in instanceList) { sb.AppendLine(string.Format("UPDATE {0} SET ", HopBase.GetTypeToTableNameService(typeof(T)))); sb.AppendLine( TypeCache.Get <T>().PropertiesWithoutId .Select((x, i) => { string paramName = string.Format("@param{0}{1}", paramCounter, i); object value = x.GetValue(inst, null); if (value == null) { return(string.Empty); } dbCommand.Parameters.Add(new SqlParameter(paramName, value)); return(string.Format("{0} = {1}", x.Name, paramName)); }) .Where(x => x != string.Empty) .Aggregate((set1, set2) => set1 + ", " + set2)); object instanceId = idExtractorService.GetId(inst); if (instanceId == null || HopBase.GetDefault(instanceId.GetType()).Equals(instanceId)) { throw new HopUpdateWithoutKeyException(inst); } sb.AppendLine(string.Format(" WHERE {0} = {1}", idExtractorService.GetIdField <T>(), instanceId)); paramCounter++; } dbCommand.CommandText = sb.ToString(); dbCommand.Connection.Open(); dbCommand.ExecuteNonQuery(); dbCommand.Connection.Close(); } }
/// <summary> /// Deletes a collection of instances in the database /// </summary> /// <typeparam name="T"></typeparam> /// <param name="hopper"></param> /// <param name="instances"></param> /// <exception cref="ArgumentNullException"></exception> public static void Delete <T>(this IHop hopper, ICollection <T> instances) { if (instances == null) { throw new ArgumentNullException("instances", "Please provide a non null value for parameter instances"); } var tableName = HopBase.GetTypeToTableNameService(typeof(T)); var idExtractorService = HopBase.GetIdExtractorService(); var idField = idExtractorService.GetIdField <T>(); var inClause = idExtractorService .GetIds <T, int>(instances) .Select(x => x.ToString(CultureInfo.InvariantCulture)) .Aggregate((id1, id2) => id1 + ", " + id2); Delete <T>(hopper, tableName, string.Format("{0} IN({1})", idField, inClause)); }
/// <summary> /// Inserts a collection of entities to the database /// </summary> /// <typeparam name="T"></typeparam> /// <param name="hopper"></param> /// <param name="instances">Collection to insert</param> /// <exception cref="ArgumentNullException"></exception> public static void Insert <T>(this IHop hopper, ICollection <T> instances) where T : new() { if (instances == null) { throw new ArgumentNullException("instances", "Please provide a non null value to parameter instances"); } if (!instances.Any()) { return; } SchemaVerifierService.AddTypeToCache <T>(hopper.Connection); var propertyInfos = TypeCache.Get <T>().PropertiesWithoutId; var objects = instances .Select(objToInsert => propertyInfos .Select(prop => prop.GetValue(objToInsert, null).ToSqlString()) .Aggregate((field1, field2) => field1 + ", " + field2) ); var tableName = HopBase.GetTypeToTableNameService(typeof(T)); var columnList = propertyInfos .Select(x => x.Name) .Aggregate((field1, field2) => field1 + ", " + field2); var intoClause = string.Format("{0} ({1})", tableName, columnList); var valuesClause = objects .Select(x => "(" + x + ")") .Aggregate((obj1, obj2) => obj1 + ", " + obj2); var lastId = hopper.Insert <T>(intoClause, valuesClause); foreach (T source in instances.Reverse()) { HopBase.GetIdExtractorService().SetId(source, lastId--); } }
public static IEnumerable <T> Read <T>(this IHop hopper, IEnumerable <T> instances) where T : new() { if (instances == null) { throw new ArgumentNullException("instances", "Read extension method expects an array of instances to read"); } var listInstances = instances as List <T> ?? instances.ToList(); if (!listInstances.Any()) { return(Enumerable.Empty <T>()); } var idExtractorService = HopBase.GetIdExtractorService(); var ids = idExtractorService.GetIds(listInstances).Select((x, i) => { if (HopBase.GetDefault(x.GetType()).Equals(x)) { throw new HopReadWithoutKeyException(listInstances[i]); } return(new SqlParameter(string.Format("@param{0}", i), x)); }).ToArray(); var idField = idExtractorService.GetIdField <T>(); var whereClause = ids.Select(p => p.ParameterName).Aggregate((p1, p2) => p1 + " , " + p2); var selectFrom = SelectFrom <T>(); var cmdText = string.Format("{0} WHERE {2} IN ( {1} )", selectFrom, whereClause, idField); using (var command = new SqlCommand()) { command.CommandText = cmdText; command.Parameters.AddRange(ids); return(hopper.ReadInternal <T>(command)); } }