|
| 1 | +using System; |
| 2 | +using System.Linq; |
| 3 | +using System.Linq.Expressions; |
| 4 | + |
| 5 | +namespace NHibernate.Linq |
| 6 | +{ |
| 7 | + /// <summary> |
| 8 | + /// NHibernate LINQ DML extension methods. They are meant to work with <see cref="NhQueryable{T}"/>. Supplied <see cref="IQueryable{T}"/> parameters |
| 9 | + /// should at least have an <see cref="INhQueryProvider"/> <see cref="IQueryable.Provider"/>. <see cref="LinqExtensionMethods.Query{T}(ISession)"/> and |
| 10 | + /// its overloads supply such queryables. |
| 11 | + /// </summary> |
| 12 | + public static class DmlExtensionMethods |
| 13 | + { |
| 14 | + /// <summary> |
| 15 | + /// Delete all entities selected by the specified query. The delete operation is performed in the database without reading the entities out of it. |
| 16 | + /// </summary> |
| 17 | + /// <typeparam name="TSource">The type of the elements of <paramref name="source" />.</typeparam> |
| 18 | + /// <param name="source">The query matching the entities to delete.</param> |
| 19 | + /// <returns>The number of deleted entities.</returns> |
| 20 | + public static int Delete<TSource>(this IQueryable<TSource> source) |
| 21 | + { |
| 22 | + var provider = source.GetNhProvider(); |
| 23 | + return provider.ExecuteDml<TSource>(QueryMode.Delete, source.Expression); |
| 24 | + } |
| 25 | + |
| 26 | + /// <summary> |
| 27 | + /// Update all entities selected by the specified query. The update operation is performed in the database without reading the entities out of it. |
| 28 | + /// </summary> |
| 29 | + /// <typeparam name="TSource">The type of the elements of <paramref name="source" />.</typeparam> |
| 30 | + /// <param name="source">The query matching the entities to update.</param> |
| 31 | + /// <param name="expression">The update setters expressed as a member initialization of updated entities, e.g. |
| 32 | + /// <c>x => new Dog { Name = x.Name, Age = x.Age + 5 }</c>. Unset members are ignored and left untouched.</param> |
| 33 | + /// <returns>The number of updated entities.</returns> |
| 34 | + public static int Update<TSource>(this IQueryable<TSource> source, Expression<Func<TSource, TSource>> expression) |
| 35 | + { |
| 36 | + return ExecuteUpdate(source, DmlExpressionRewriter.PrepareExpression(source.Expression, expression), false); |
| 37 | + } |
| 38 | + |
| 39 | + /// <summary> |
| 40 | + /// Update all entities selected by the specified query, using an anonymous initializer for specifying setters. The update operation is performed |
| 41 | + /// in the database without reading the entities out of it. |
| 42 | + /// </summary> |
| 43 | + /// <typeparam name="TSource">The type of the elements of <paramref name="source" />.</typeparam> |
| 44 | + /// <param name="source">The query matching the entities to update.</param> |
| 45 | + /// <param name="expression">The assignments expressed as an anonymous object, e.g. |
| 46 | + /// <c>x => new { Name = x.Name, Age = x.Age + 5 }</c>. Unset members are ignored and left untouched.</param> |
| 47 | + /// <returns>The number of updated entities.</returns> |
| 48 | + public static int Update<TSource>(this IQueryable<TSource> source, Expression<Func<TSource, object>> expression) |
| 49 | + { |
| 50 | + return ExecuteUpdate(source, DmlExpressionRewriter.PrepareExpressionFromAnonymous(source.Expression, expression), false); |
| 51 | + } |
| 52 | + |
| 53 | + /// <summary> |
| 54 | + /// Perform an <c>update versioned</c> on all entities selected by the specified query. The update operation is performed in the database without |
| 55 | + /// reading the entities out of it. |
| 56 | + /// </summary> |
| 57 | + /// <typeparam name="TSource">The type of the elements of <paramref name="source" />.</typeparam> |
| 58 | + /// <param name="source">The query matching the entities to update.</param> |
| 59 | + /// <param name="expression">The update setters expressed as a member initialization of updated entities, e.g. |
| 60 | + /// <c>x => new Dog { Name = x.Name, Age = x.Age + 5 }</c>. Unset members are ignored and left untouched.</param> |
| 61 | + /// <returns>The number of updated entities.</returns> |
| 62 | + public static int UpdateVersioned<TSource>(this IQueryable<TSource> source, Expression<Func<TSource, TSource>> expression) |
| 63 | + { |
| 64 | + return ExecuteUpdate(source, DmlExpressionRewriter.PrepareExpression(source.Expression, expression), true); |
| 65 | + } |
| 66 | + |
| 67 | + /// <summary> |
| 68 | + /// Perform an <c>update versioned</c> on all entities selected by the specified query, using an anonymous initializer for specifying setters. |
| 69 | + /// The update operation is performed in the database without reading the entities out of it. |
| 70 | + /// </summary> |
| 71 | + /// <typeparam name="TSource">The type of the elements of <paramref name="source" />.</typeparam> |
| 72 | + /// <param name="source">The query matching the entities to update.</param> |
| 73 | + /// <param name="expression">The assignments expressed as an anonymous object, e.g. |
| 74 | + /// <c>x => new { Name = x.Name, Age = x.Age + 5 }</c>. Unset members are ignored and left untouched.</param> |
| 75 | + /// <returns>The number of updated entities.</returns> |
| 76 | + public static int UpdateVersioned<TSource>(this IQueryable<TSource> source, Expression<Func<TSource, object>> expression) |
| 77 | + { |
| 78 | + return ExecuteUpdate(source, DmlExpressionRewriter.PrepareExpressionFromAnonymous(source.Expression, expression), true); |
| 79 | + } |
| 80 | + |
| 81 | + /// <summary> |
| 82 | + /// Initiate an update for the entities selected by the query. Return |
| 83 | + /// a builder allowing to set properties and allowing to execute the update. |
| 84 | + /// </summary> |
| 85 | + /// <typeparam name="TSource">The type of the elements of <paramref name="source" />.</typeparam> |
| 86 | + /// <param name="source">The query matching the entities to update.</param> |
| 87 | + /// <returns>An update builder.</returns> |
| 88 | + public static UpdateBuilder<TSource> UpdateBuilder<TSource>(this IQueryable<TSource> source) |
| 89 | + { |
| 90 | + return new UpdateBuilder<TSource>(source); |
| 91 | + } |
| 92 | + |
| 93 | + internal static int ExecuteUpdate<TSource>(this IQueryable<TSource> source, Expression updateExpression, bool versioned) |
| 94 | + { |
| 95 | + var provider = source.GetNhProvider(); |
| 96 | + return provider.ExecuteDml<TSource>(versioned ? QueryMode.UpdateVersioned : QueryMode.Update, updateExpression); |
| 97 | + } |
| 98 | + |
| 99 | + /// <summary> |
| 100 | + /// Insert all entities selected by the specified query. The insert operation is performed in the database without reading the entities out of it. |
| 101 | + /// </summary> |
| 102 | + /// <typeparam name="TSource">The type of the elements of <paramref name="source" />.</typeparam> |
| 103 | + /// <typeparam name="TTarget">The type of the entities to insert.</typeparam> |
| 104 | + /// <param name="source">The query matching entities source of the data to insert.</param> |
| 105 | + /// <param name="expression">The expression projecting a source entity to the entity to insert.</param> |
| 106 | + /// <returns>The number of inserted entities.</returns> |
| 107 | + public static int InsertInto<TSource, TTarget>(this IQueryable<TSource> source, Expression<Func<TSource, TTarget>> expression) |
| 108 | + { |
| 109 | + return ExecuteInsert<TSource, TTarget>(source, DmlExpressionRewriter.PrepareExpression(source.Expression, expression)); |
| 110 | + } |
| 111 | + |
| 112 | + /// <summary> |
| 113 | + /// Insert all entities selected by the specified query, using an anonymous initializer for specifying setters. <typeparamref name="TTarget"/> |
| 114 | + /// must be explicitly provided, e.g. <c>source.InsertInto<Cat, Dog>(c => new {...})</c>. The insert operation is performed in the |
| 115 | + /// database without reading the entities out of it. |
| 116 | + /// </summary> |
| 117 | + /// <typeparam name="TSource">The type of the elements of <paramref name="source" />.</typeparam> |
| 118 | + /// <typeparam name="TTarget">The type of the entities to insert. Must be explicitly provided.</typeparam> |
| 119 | + /// <param name="source">The query matching entities source of the data to insert.</param> |
| 120 | + /// <param name="expression">The expression projecting a source entity to an anonymous object representing |
| 121 | + /// the entity to insert.</param> |
| 122 | + /// <returns>The number of inserted entities.</returns> |
| 123 | + public static int InsertInto<TSource, TTarget>(this IQueryable<TSource> source, Expression<Func<TSource, object>> expression) |
| 124 | + { |
| 125 | + return ExecuteInsert<TSource, TTarget>(source, DmlExpressionRewriter.PrepareExpressionFromAnonymous(source.Expression, expression)); |
| 126 | + } |
| 127 | + |
| 128 | + /// <summary> |
| 129 | + /// Initiate an insert using selected entities as a source. Return |
| 130 | + /// a builder allowing to set properties to insert and allowing to execute the update. |
| 131 | + /// </summary> |
| 132 | + /// <typeparam name="TSource">The type of the elements of <paramref name="source" />.</typeparam> |
| 133 | + /// <param name="source">The query matching the entities to update.</param> |
| 134 | + /// <returns>An update builder.</returns> |
| 135 | + public static InsertBuilder<TSource> InsertBuilder<TSource>(this IQueryable<TSource> source) |
| 136 | + { |
| 137 | + return new InsertBuilder<TSource>(source); |
| 138 | + } |
| 139 | + |
| 140 | + internal static int ExecuteInsert<TSource, TTarget>(this IQueryable<TSource> source, Expression insertExpression) |
| 141 | + { |
| 142 | + var provider = source.GetNhProvider(); |
| 143 | + return provider.ExecuteDml<TTarget>(QueryMode.Insert, insertExpression); |
| 144 | + } |
| 145 | + } |
| 146 | +} |
0 commit comments