public static T ExecuteScalar <T>( this IDbCmd source) { var obj = source.ExecuteScalar(); return(ConvertTo <object, T> .From(obj)); }
public void AddParameters() { SetupAndAssert(dbCmd => { Assert.NotNull(dbCmd.Parameters); Assert.Empty(dbCmd.Parameters); List <IDbDataParameter> list = new List <IDbDataParameter>() { dbCmd.Connection.CreateParameter(), dbCmd.Connection.CreateParameter(), }; IDbCmd dbCmd2 = dbCmd.AddParameter(list.ToArray()); Assert.Same(dbCmd, dbCmd2); Assert.NotNull(dbCmd.Parameters); Assert.Equal(list.Count, dbCmd.Parameters.Count); for (int i = 0; i < list.Count; i++) { Assert.Same(list[i], dbCmd.Parameters.ElementAt(i)); } dbCmd.Parameters.Clear(); Assert.Empty(dbCmd.Parameters); }); }
public static async Task <DbDataReader> ExecuteReaderAsync( this IDbCommand source, IDbCmd dbCmd) { if (source == null) { throw new ArgumentNullException(nameof(source)); } if (dbCmd == null) { throw new ArgumentNullException(nameof(dbCmd)); } if (source is DbCommand dbCommand) { Task <DbDataReader> task; if (dbCmd.CommandBehavior.HasValue) { task = dbCmd.CancellationToken.HasValue ? dbCommand.ExecuteReaderAsync(dbCmd.CommandBehavior.Value, dbCmd.CancellationToken.Value) : dbCommand.ExecuteReaderAsync(dbCmd.CommandBehavior.Value); } else { task = dbCmd.CancellationToken.HasValue ? dbCommand.ExecuteReaderAsync(dbCmd.CancellationToken.Value) : dbCommand.ExecuteReaderAsync(); } return(await task.ConfigureAwait(false)); } throw new InvalidOperationException("Async operations require use of a DbConnection or an IDbConnection where .CreateCommand() returns a DbCommand"); }
private static IDbCommand CreateCommand( this IDbCmd source) { var cmd = source.Connection.CreateCommand(); var init = GetInit(cmd.GetType()); init?.Invoke(cmd); if (source.CommandTimeout.HasValue) { cmd.CommandTimeout = source.CommandTimeout.Value; } cmd.CommandType = source.CommandType ?? System.Data.CommandType.Text; cmd.Transaction = source.Transaction; cmd.CommandText = source.CommandText; cmd.Connection = source.Connection; cmd.Parameters.Clear(); foreach (var p in source.Parameters) { cmd.Parameters.Add(p); } return(cmd); }
public static async Task <T> ExecuteScalarAsync <T>( this IDbCmd source) { var obj = await source.ExecuteScalarAsync().ConfigureAwait(false); return(ConvertTo <object, T> .From(obj)); }
private static IDbCommand CreateAndOpen( this IDbCmd source) { var cmd = source.CreateCommand(); cmd.Connection.EnsureOpen(); return(cmd); }
public ManagedEnumerable( IDbCmd dbCmd, IDbCommand dbCommand, IDataReader reader) { _dbCmd = dbCmd; _dbCommand = dbCommand; _reader = reader; }
public void SetConnection() { SetupAndAssert(dbCmd => { Assert.NotNull(dbCmd.Connection); IDbCmd dbCmd2 = dbCmd.Connection(null); Assert.Same(dbCmd, dbCmd2); Assert.Null(dbCmd.Connection); }); }
public static IDbCmd CommandText( this IDbCmd source, string text) { if (source == null) { throw new ArgumentNullException(nameof(source)); } source.CommandText = text; return(source); }
public static IDbCmd Connection( this IDbCmd source, IDbConnection con) { if (source == null) { throw new ArgumentNullException(nameof(source)); } source.Connection = con; return(source); }
public static IDbCmd CommandType( this IDbCmd source, CommandType?type) { if (source == null) { throw new ArgumentNullException(nameof(source)); } source.CommandType = type; return(source); }
public static IDbCmd CommandTimeout( this IDbCmd source, int?timeout) { if (source == null) { throw new ArgumentNullException(nameof(source)); } source.CommandTimeout = timeout; return(source); }
public static IDbCmd CancellationToken( this IDbCmd source, CancellationToken?token) { if (source == null) { throw new ArgumentNullException(nameof(source)); } source.CancellationToken = token; return(source); }
public static IDbCmd Transaction( this IDbCmd source, IDbTransaction trans) { if (source == null) { throw new ArgumentNullException(nameof(source)); } source.Transaction = trans; return(source); }
public void SetCommandTimeout() { SetupAndAssert(dbCmd => { Assert.Null(dbCmd.CommandTimeout); IDbCmd dbCmd2 = dbCmd.CommandTimeout(1000); Assert.Same(dbCmd, dbCmd2); Assert.Equal(1000, dbCmd.CommandTimeout); dbCmd.CommandTimeout(null); Assert.Null(dbCmd.CommandTimeout); }); }
public void SetCommandBehavior() { SetupConnection(dbCon => { Assert.Equal(ConnectionState.Closed, dbCon.State); IDbCmd cmd = dbCon.CommandBehavior(CommandBehavior.CloseConnection); Assert.NotNull(cmd); Assert.Equal(CommandBehavior.CloseConnection, cmd.CommandBehavior); Assert.Same(dbCon, cmd.Connection); Assert.Equal(ConnectionState.Closed, dbCon.State); }); }
public void Dispose() { if (!Diposed) { _dbCmd = null; _dbCommand?.Dispose(); _dbCommand = null; _reader?.Dispose(); _reader = null; Diposed = true; } }
public void SetCommandText() { SetupAndAssert(dbCmd => { Assert.True(string.IsNullOrEmpty(dbCmd.CommandText)); IDbCmd dbCmd2 = dbCmd.CommandText("Bill"); Assert.Same(dbCmd, dbCmd2); Assert.Equal("Bill", dbCmd.CommandText); dbCmd.CommandText(null); Assert.Null(dbCmd.CommandText); }); }
public void SetCommandBehavior() { SetupAndAssert(dbCmd => { Assert.Null(dbCmd.CommandBehavior); IDbCmd dbCmd2 = dbCmd.CommandBehavior(CommandBehavior.SingleRow); Assert.Same(dbCmd, dbCmd2); Assert.Equal(CommandBehavior.SingleRow, dbCmd.CommandBehavior); dbCmd.CommandBehavior(null); Assert.Null(dbCmd.CommandBehavior); }); }
public void SetCommandType() { SetupAndAssert(dbCmd => { Assert.Null(dbCmd.CommandType); IDbCmd dbCmd2 = dbCmd.CommandType(CommandType.StoredProcedure); Assert.Same(dbCmd, dbCmd2); Assert.Equal(CommandType.StoredProcedure, dbCmd.CommandType); dbCmd.CommandType(null); Assert.Null(dbCmd.CommandType); }); }
public void SetCommandType() { SetupConnection(dbCon => { Assert.Equal(ConnectionState.Closed, dbCon.State); IDbCmd cmd = dbCon.CommandType(CommandType.TableDirect); Assert.NotNull(cmd); Assert.Equal(CommandType.TableDirect, cmd.CommandType); Assert.Same(dbCon, cmd.Connection); Assert.Equal(ConnectionState.Closed, dbCon.State); }); }
public void SetCommandTimeout() { SetupConnection(dbCon => { Assert.Equal(ConnectionState.Closed, dbCon.State); IDbCmd cmd = dbCon.CommandTimeout(21); Assert.NotNull(cmd); Assert.Equal(21, cmd.CommandTimeout); Assert.Same(dbCon, cmd.Connection); Assert.Equal(ConnectionState.Closed, dbCon.State); }); }
public void SetCommandText() { SetupConnection(dbCon => { Assert.Equal(ConnectionState.Closed, dbCon.State); IDbCmd cmd = dbCon.CommandText("TestText"); Assert.NotNull(cmd); Assert.Equal("TestText", cmd.CommandText); Assert.Same(dbCon, cmd.Connection); Assert.Equal(ConnectionState.Closed, dbCon.State); }); }
public void SetCancellationToken() { SetupConnection(dbCon => { Assert.Equal(ConnectionState.Closed, dbCon.State); var token = new CancellationToken(); IDbCmd cmd = dbCon.CancellationToken(token); Assert.NotNull(cmd); Assert.Equal(token, cmd.CancellationToken); Assert.Same(dbCon, cmd.Connection); Assert.Equal(ConnectionState.Closed, dbCon.State); }); }
/// <summary> /// Either read all items or use a using block to ensure all /// resources are fully cleaned up. /// </summary> /// <param name="source"></param> /// <returns></returns> public static IManagedEnumerable <T> AsEnumerable <T>( this IDbCmd source) where T : new() { if (source == null) { throw new ArgumentNullException(nameof(source)); } source.CommandBehavior(CmdBehavior.SingleResult); var cmd = source.CreateAndOpen(); var reader = cmd.ExecuteReader(source); return(new ManagedEnumerable <T>(source, cmd, reader)); }
public static int ExecuteNonQuery( this IDbCmd source) { if (source == null) { throw new ArgumentNullException(nameof(source)); } using (var cmd = source.CreateAndOpen()) { return(cmd.ExecuteNonQuery()); } }
public void SetTransaction() { SetupAndAssert(dbCmd => { TestTransaction trans = new TestTransaction(); Assert.Null(dbCmd.Transaction); IDbCmd dbCmd2 = dbCmd.Transaction(trans); Assert.Same(dbCmd, dbCmd2); Assert.Same(trans, dbCmd.Transaction); dbCmd.Transaction(null); Assert.Null(dbCmd.Transaction); }); }
private static Task <T> CreateTask <T>( this IDbCmd source, Func <DbCommand, T> func, DbCommand dbCmd) { return(Task.Factory.StartNew(() => { return func(dbCmd); }, source.CancellationToken ?? System.Threading.CancellationToken.None, TaskCreationOptions.None, TaskScheduler.Default)); }
public void SetTransaction() { SetupConnection(dbCon => { Assert.Equal(ConnectionState.Closed, dbCon.State); var trans = new TestTransaction(); IDbCmd cmd = dbCon.Transaction(trans); Assert.NotNull(cmd); Assert.Same(trans, cmd.Transaction); Assert.Same(dbCon, cmd.Connection); Assert.Equal(ConnectionState.Closed, dbCon.State); }); }
public void SetCancellationToken() { SetupAndAssert(dbCmd => { var token = new CancellationToken(); Assert.Null(dbCmd.CancellationToken); IDbCmd dbCmd2 = dbCmd.CancellationToken(token); Assert.Same(dbCmd, dbCmd2); Assert.Equal(token, dbCmd.CancellationToken); dbCmd.CancellationToken(null); Assert.Null(dbCmd.CancellationToken); }); }