/// <summary> /// Returns a single entity of type 'T'. /// </summary> /// <typeparam name="T">Type of entity</typeparam> /// <param name="connection">Open SqlConnection</param> /// <param name="entityToGet">Entity to Retrieve with keys populated</param> /// <param name="transaction">The transaction to run under, null (the default) if none</param> /// <param name="commandTimeout">Number of seconds before command execution timeout</param> /// <returns>the entity, else null</returns> public static async Task <T> GetAsync <T>(this IDbConnection connection, T entityToGet, IDbTransaction transaction = null, int?commandTimeout = null) where T : class { if (entityToGet == null) { throw new ArgumentException("Cannot Get null Object", nameof(entityToGet)); } var sqlHelper = new SqlQueryHelper(typeof(T), connection); var getQuery = sqlHelper.GenerateCompositeKeyQuery(entityToGet, (ti, sql) => sqlHelper.Adapter.GetQuery(ti, sql)); return((await connection.QueryAsync <T>(getQuery.SqlStatement, getQuery.Parameters, transaction, commandTimeout)).SingleOrDefault()); }
/// <summary> /// Check if a record exists /// </summary> /// <typeparam name="T">Type of entity</typeparam> /// <param name="connection">Open SqlConnection</param> /// <param name="entityToExists">Entity to delete</param> /// <param name="transaction">The transaction to run under, null (the default) if none</param> /// <param name="commandTimeout">Number of seconds before command execution timeout</param> /// <returns>true if record is found</returns> public static async Task <bool> ExistsAsync <T>(this IDbConnection connection, T entityToExists, IDbTransaction transaction = null, int?commandTimeout = null) where T : class { if (entityToExists == null) { throw new ArgumentException("Cannot Exists null Object", nameof(entityToExists)); } var sqlHelper = new SqlQueryHelper(typeof(T), connection); var existsQuery = sqlHelper.GenerateCompositeKeyQuery(entityToExists, (ti, sql) => sqlHelper.Adapter.ExistsQuery(ti, sql)); return(await connection.ExecuteScalarAsync <bool>(existsQuery.SqlStatement, existsQuery.Parameters, transaction, commandTimeout)); }
/// <summary> /// Delete entity in table "Ts" that match the key values of the entity (T) passed in /// </summary> /// <typeparam name="T">Type of entity</typeparam> /// <param name="connection">Open SqlConnection</param> /// <param name="entityToDelete"> /// Entity to delete. If Keys are specified, they will be used as the WHERE condition to /// delete. /// </param> /// <param name="transaction">The transaction to run under, null (the default) if none</param> /// <param name="commandTimeout">Number of seconds before command execution timeout</param> /// <returns>true if deleted, false if not found</returns> public static bool Delete <T>(this IDbConnection connection, T entityToDelete, IDbTransaction transaction = null, int?commandTimeout = null) where T : class { if (entityToDelete == null) { throw new ArgumentNullException(nameof(entityToDelete), "Cannot Delete null Object"); } var sqlHelper = new SqlQueryHelper(typeof(T), connection); var deleteQuery = sqlHelper.GenerateCompositeKeyQuery(entityToDelete, (ti, sql) => sqlHelper.Adapter.DeleteQuery(ti, sql)); return(connection.Execute(deleteQuery.SqlStatement, deleteQuery.Parameters, transaction, commandTimeout) > 0); }