Skip to content

Commit 1466000

Browse files
committed
Add the ConnectionExtensions.Update() method
1 parent 5764045 commit 1466000

4 files changed

Lines changed: 35 additions & 7 deletions

File tree

src/Sql/CommandBuilder.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -120,13 +120,13 @@ SELECT 1
120120
public Command GetFindCommand<T>(object id, params string[] columns) where T: new() {
121121
var table = Mapper.Instance.GetTable<T>();
122122
var identityColumn = table.IdentityColumn ?? throw new InvalidOperationException("The identity column could not be found.");
123-
var sql = $"""
123+
var text = $"""
124124
SELECT {(columns is null || columns.Length == 0 ? "*" : string.Join(", ", columns.Select(QuoteIdentifier)))}
125125
FROM {QuoteIdentifier(table.Name)}
126126
WHERE {QuoteIdentifier(identityColumn.Name)} = {(UsePositionalParameters ? "?" : GetParameterName(identityColumn.Name))}
127127
""";
128128

129-
return new(sql, new(UsePositionalParameters ? "?1" : GetParameterName(identityColumn.Name), id));
129+
return new(text, new(UsePositionalParameters ? "?1" : GetParameterName(identityColumn.Name), id));
130130
}
131131

132132
/// <summary>
@@ -138,12 +138,12 @@ SELECT 1
138138
public Command GetInsertCommand<T>(T instance) where T: new() {
139139
var table = Mapper.Instance.GetTable<T>();
140140
var fields = table.Columns.Values.Where(column => column.CanRead && !column.IsComputed).ToArray();
141-
var sql = $"""
141+
var text = $"""
142142
INSERT INTO {QuoteIdentifier(table.Name)} ({string.Join(", ", fields.Select(field => QuoteIdentifier(field.Name)))})
143143
VALUES ({string.Join(", ", fields.Select(field => UsePositionalParameters ? "?" : GetParameterName(field.Name)))})
144144
""";
145145

146-
return new(sql, [.. fields.Select((field, index) => (UsePositionalParameters ? $"?{index}" : GetParameterName(field.Name), field.GetValue(instance)))]);
146+
return new(text, [.. fields.Select((field, index) => (UsePositionalParameters ? $"?{index}" : GetParameterName(field.Name), field.GetValue(instance)))]);
147147
}
148148

149149
/// <summary>
@@ -161,13 +161,13 @@ SELECT 1
161161
.Where(column => column.CanRead && !column.IsComputed)
162162
.ToArray();
163163

164-
var sql = $"""
164+
var text = $"""
165165
UPDATE {QuoteIdentifier(table.Name)}
166166
SET {string.Join(", ", fields.Select(field => $"{QuoteIdentifier(field.Name)} = {(UsePositionalParameters ? "?" : GetParameterName(field.Name))}"))}
167167
WHERE {QuoteIdentifier(identityColumn.Name)} = {(UsePositionalParameters ? "?" : GetParameterName(identityColumn.Name))}
168168
""";
169169

170-
return new(sql, [
170+
return new(text, [
171171
.. fields.Select((field, index) => (UsePositionalParameters ? $"?{index}" : GetParameterName(field.Name), field.GetValue(instance))),
172172
(GetParameterName(identityColumn.Name), identityColumn.GetValue(instance))
173173
]);

src/Sql/ConnectionExtensions.Async.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,4 +297,18 @@ record = Mapper.Instance.CreateInstance<T>(reader);
297297

298298
return rowCount == 1 ? record : default;
299299
}
300+
301+
/// <summary>
302+
/// Updates the specified entity.
303+
/// </summary>
304+
/// <typeparam name="T">The entity type.</typeparam>
305+
/// <param name="connection">The connection to the data source.</param>
306+
/// <param name="instance">The entity to update.</param>
307+
/// <param name="columns">The list of columns to update. By default, all columns.</param>
308+
/// <param name="options">The command options.</param>
309+
/// <returns>The number of rows affected.</returns>
310+
public static async Task<int> UpdateAsync<T>(this IDbConnection connection, T instance, string[]? columns = null, CommandOptions? options = null) where T: new() {
311+
var (text, parameters) = new CommandBuilder(connection).GetUpdateCommand(instance, columns ?? []);
312+
return await ExecuteAsync(connection, text, parameters, options);
313+
}
300314
}

src/Sql/ConnectionExtensions.Sync.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,4 +281,18 @@ record = Mapper.Instance.CreateInstance<T>(reader);
281281

282282
return rowCount == 1 ? record : default;
283283
}
284+
285+
/// <summary>
286+
/// Updates the specified entity.
287+
/// </summary>
288+
/// <typeparam name="T">The entity type.</typeparam>
289+
/// <param name="connection">The connection to the data source.</param>
290+
/// <param name="instance">The entity to update.</param>
291+
/// <param name="columns">The list of columns to update. By default, all columns.</param>
292+
/// <param name="options">The command options.</param>
293+
/// <returns>The number of rows affected.</returns>
294+
public static int Update<T>(this IDbConnection connection, T instance, string[]? columns = null, CommandOptions? options = null) where T: new() {
295+
var (text, parameters) = new CommandBuilder(connection).GetUpdateCommand(instance, columns ?? []);
296+
return Execute(connection, text, parameters, options);
297+
}
284298
}

src/Sql/ConnectionExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public static partial class ConnectionExtensions {
1818
/// <returns>The newly created command.</returns>
1919
public static IDbCommand CreateCommand(this IDbConnection connection, string text, ParameterCollection? parameters = null, CommandOptions? options = null) {
2020
var command = connection.CreateCommand();
21-
command.CommandText = sql;
21+
command.CommandText = text;
2222
command.CommandTimeout = options?.Timeout ?? 30;
2323
command.CommandType = options?.Type ?? CommandType.Text;
2424
command.Transaction = options?.Transaction;

0 commit comments

Comments
 (0)