|
| 1 | +namespace Belin.Sql; |
| 2 | + |
| 3 | +using Belin.Sql.Reflection; |
| 4 | +using System.Data; |
| 5 | + |
| 6 | +/// <summary> |
| 7 | +/// Provides extension members for database connections. |
| 8 | +/// </summary> |
| 9 | +public static partial class ConnectionExtensions { |
| 10 | + |
| 11 | + /// <summary> |
| 12 | + /// Deletes the specified entity. |
| 13 | + /// </summary> |
| 14 | + /// <typeparam name="T">The entity type.</typeparam> |
| 15 | + /// <param name="connection">The connection to the data source.</param> |
| 16 | + /// <param name="instance">The entity to delete.</param> |
| 17 | + /// <param name="options">The command options.</param> |
| 18 | + /// <returns><see langword="true"/> if the specified entity has been deleted, otherwise <see langword="false"/>.</returns> |
| 19 | + public static bool Delete<T>(this IDbConnection connection, T instance, CommandOptions? options = null) where T: new() { |
| 20 | + var (text, parameters) = new CommandBuilder(connection).GetDeleteCommand(instance); |
| 21 | + return Execute(connection, text, parameters, options) > 0; |
| 22 | + } |
| 23 | + |
| 24 | + /// <summary> |
| 25 | + /// Deletes the specified entity. |
| 26 | + /// </summary> |
| 27 | + /// <typeparam name="T">The entity type.</typeparam> |
| 28 | + /// <param name="connection">The connection to the data source.</param> |
| 29 | + /// <param name="instance">The entity to delete.</param> |
| 30 | + /// <param name="options">The command options.</param> |
| 31 | + /// <returns><see langword="true"/> if the specified entity has been deleted, otherwise <see langword="false"/>.</returns> |
| 32 | + public static async Task<bool> DeleteAsync<T>(this IDbConnection connection, T instance, CommandOptions? options = null) where T: new() { |
| 33 | + var (text, parameters) = new CommandBuilder(connection).GetDeleteCommand(instance); |
| 34 | + return await ExecuteAsync(connection, text, parameters, options) > 0; |
| 35 | + } |
| 36 | + |
| 37 | + /// <summary> |
| 38 | + /// Checks whether an entity with the specified primary key exists. |
| 39 | + /// </summary> |
| 40 | + /// <typeparam name="T">The entity type.</typeparam> |
| 41 | + /// <param name="connection">The connection to the data source.</param> |
| 42 | + /// <param name="id">The primary key value.</param> |
| 43 | + /// <param name="options">The command options.</param> |
| 44 | + /// <returns><see langword="true"/> if an entity with the specified primary key exists, otherwise <see langword="false"/>.</returns> |
| 45 | + public static bool Exists<T>(this IDbConnection connection, object id, CommandOptions? options = null) where T: new() { |
| 46 | + var (text, parameters) = new CommandBuilder(connection).GetExistsCommand<T>(id); |
| 47 | + return ExecuteScalar<bool>(connection, text, parameters, options); |
| 48 | + } |
| 49 | + |
| 50 | + /// <summary> |
| 51 | + /// Checks whether an entity with the specified primary key exists. |
| 52 | + /// </summary> |
| 53 | + /// <typeparam name="T">The entity type.</typeparam> |
| 54 | + /// <param name="connection">The connection to the data source.</param> |
| 55 | + /// <param name="id">The primary key value.</param> |
| 56 | + /// <param name="options">The command options.</param> |
| 57 | + /// <returns><see langword="true"/> if an entity with the specified primary key exists, otherwise <see langword="false"/>.</returns> |
| 58 | + public static async Task<bool> ExistsAsync<T>(this IDbConnection connection, object id, CommandOptions? options = null) where T: new() { |
| 59 | + var (text, parameters) = new CommandBuilder(connection).GetExistsCommand<T>(id); |
| 60 | + return await ExecuteScalarAsync<bool>(connection, text, parameters, options); |
| 61 | + } |
| 62 | + |
| 63 | + /// <summary> |
| 64 | + /// Finds an entity with the specified primary key. |
| 65 | + /// </summary> |
| 66 | + /// <typeparam name="T">The entity type.</typeparam> |
| 67 | + /// <param name="connection">The connection to the data source.</param> |
| 68 | + /// <param name="id">The primary key value.</param> |
| 69 | + /// <param name="columns">The list of columns to select. By default, all columns.</param> |
| 70 | + /// <param name="options">The command options.</param> |
| 71 | + /// <returns>The entity with the specified primary key, or <see langword="null"/> if not found.</returns> |
| 72 | + public static T? Find<T>(this IDbConnection connection, object id, string[]? columns = null, CommandOptions? options = null) where T: new() { |
| 73 | + var (text, parameters) = new CommandBuilder(connection).GetFindCommand<T>(id, columns ?? []); |
| 74 | + return QuerySingleOrDefault<T>(connection, text, parameters, options); |
| 75 | + } |
| 76 | + |
| 77 | + /// <summary> |
| 78 | + /// Finds an entity with the specified primary key. |
| 79 | + /// </summary> |
| 80 | + /// <typeparam name="T">The entity type.</typeparam> |
| 81 | + /// <param name="connection">The connection to the data source.</param> |
| 82 | + /// <param name="id">The primary key value.</param> |
| 83 | + /// <param name="columns">The list of columns to select. By default, all columns.</param> |
| 84 | + /// <param name="options">The command options.</param> |
| 85 | + /// <returns>The entity with the specified primary key, or <see langword="null"/> if not found.</returns> |
| 86 | + public static async Task<T?> FindAsync<T>(this IDbConnection connection, object id, string[]? columns = null, CommandOptions? options = null) where T: new() { |
| 87 | + var (text, parameters) = new CommandBuilder(connection).GetFindCommand<T>(id, columns ?? []); |
| 88 | + return await QuerySingleOrDefaultAsync<T>(connection, text, parameters, options); |
| 89 | + } |
| 90 | + |
| 91 | + /// <summary> |
| 92 | + /// Inserts the specified entity. |
| 93 | + /// </summary> |
| 94 | + /// <typeparam name="T">The entity type.</typeparam> |
| 95 | + /// <param name="connection">The connection to the data source.</param> |
| 96 | + /// <param name="instance">The entity to insert.</param> |
| 97 | + /// <param name="options">The command options.</param> |
| 98 | + /// <returns>The generated primary key value.</returns> |
| 99 | + public static long Insert<T>(this IDbConnection connection, T instance, CommandOptions? options = null) where T: new() { |
| 100 | + var (text, parameters) = new CommandBuilder(connection).GetInsertCommand(instance); |
| 101 | + var id = ExecuteScalar<long>(connection, text, parameters, options); |
| 102 | + if (Mapper.Instance.GetTable<T>().IdentityColumn is ColumnInfo column) column.SetValue(instance, Mapper.ChangeType(id, column)); |
| 103 | + return id; |
| 104 | + } |
| 105 | + |
| 106 | + /// <summary> |
| 107 | + /// Inserts the specified entity. |
| 108 | + /// </summary> |
| 109 | + /// <typeparam name="T">The entity type.</typeparam> |
| 110 | + /// <param name="connection">The connection to the data source.</param> |
| 111 | + /// <param name="instance">The entity to insert.</param> |
| 112 | + /// <param name="options">The command options.</param> |
| 113 | + /// <returns>The generated primary key value.</returns> |
| 114 | + public static async Task<long> InsertAsync<T>(this IDbConnection connection, T instance, CommandOptions? options = null) where T: new() { |
| 115 | + var (text, parameters) = new CommandBuilder(connection).GetInsertCommand(instance); |
| 116 | + var id = await ExecuteScalarAsync<long>(connection, text, parameters, options); |
| 117 | + if (Mapper.Instance.GetTable<T>().IdentityColumn is ColumnInfo column) column.SetValue(instance, Mapper.ChangeType(id, column)); |
| 118 | + return id; |
| 119 | + } |
| 120 | + |
| 121 | + /// <summary> |
| 122 | + /// Updates the specified entity. |
| 123 | + /// </summary> |
| 124 | + /// <typeparam name="T">The entity type.</typeparam> |
| 125 | + /// <param name="connection">The connection to the data source.</param> |
| 126 | + /// <param name="instance">The entity to update.</param> |
| 127 | + /// <param name="columns">The list of columns to update. By default, all columns.</param> |
| 128 | + /// <param name="options">The command options.</param> |
| 129 | + /// <returns>The number of rows affected.</returns> |
| 130 | + public static int Update<T>(this IDbConnection connection, T instance, string[]? columns = null, CommandOptions? options = null) where T: new() { |
| 131 | + var (text, parameters) = new CommandBuilder(connection).GetUpdateCommand(instance, columns ?? []); |
| 132 | + return Execute(connection, text, parameters, options); |
| 133 | + } |
| 134 | + |
| 135 | + /// <summary> |
| 136 | + /// Updates the specified entity. |
| 137 | + /// </summary> |
| 138 | + /// <typeparam name="T">The entity type.</typeparam> |
| 139 | + /// <param name="connection">The connection to the data source.</param> |
| 140 | + /// <param name="instance">The entity to update.</param> |
| 141 | + /// <param name="columns">The list of columns to update. By default, all columns.</param> |
| 142 | + /// <param name="options">The command options.</param> |
| 143 | + /// <returns>The number of rows affected.</returns> |
| 144 | + public static async Task<int> UpdateAsync<T>(this IDbConnection connection, T instance, string[]? columns = null, CommandOptions? options = null) where T: new() { |
| 145 | + var (text, parameters) = new CommandBuilder(connection).GetUpdateCommand(instance, columns ?? []); |
| 146 | + return await ExecuteAsync(connection, text, parameters, options); |
| 147 | + } |
| 148 | +} |
0 commit comments