Exemple #1
0
        public static T ExecuteScalar <T>(
            this IDbCmd source)
        {
            var obj = source.ExecuteScalar();

            return(ConvertTo <object, T> .From(obj));
        }
Exemple #2
0
        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");
        }
Exemple #4
0
        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);
        }
Exemple #5
0
        public static async Task <T> ExecuteScalarAsync <T>(
            this IDbCmd source)
        {
            var obj = await source.ExecuteScalarAsync().ConfigureAwait(false);

            return(ConvertTo <object, T> .From(obj));
        }
Exemple #6
0
        private static IDbCommand CreateAndOpen(
            this IDbCmd source)
        {
            var cmd = source.CreateCommand();

            cmd.Connection.EnsureOpen();
            return(cmd);
        }
Exemple #7
0
 public ManagedEnumerable(
     IDbCmd dbCmd,
     IDbCommand dbCommand,
     IDataReader reader)
 {
     _dbCmd     = dbCmd;
     _dbCommand = dbCommand;
     _reader    = reader;
 }
Exemple #8
0
 public void SetConnection()
 {
     SetupAndAssert(dbCmd =>
     {
         Assert.NotNull(dbCmd.Connection);
         IDbCmd dbCmd2 = dbCmd.Connection(null);
         Assert.Same(dbCmd, dbCmd2);
         Assert.Null(dbCmd.Connection);
     });
 }
Exemple #9
0
 public static IDbCmd CommandText(
     this IDbCmd source,
     string text)
 {
     if (source == null)
     {
         throw new ArgumentNullException(nameof(source));
     }
     source.CommandText = text;
     return(source);
 }
Exemple #10
0
 public static IDbCmd Connection(
     this IDbCmd source,
     IDbConnection con)
 {
     if (source == null)
     {
         throw new ArgumentNullException(nameof(source));
     }
     source.Connection = con;
     return(source);
 }
Exemple #11
0
 public static IDbCmd CommandType(
     this IDbCmd source,
     CommandType?type)
 {
     if (source == null)
     {
         throw new ArgumentNullException(nameof(source));
     }
     source.CommandType = type;
     return(source);
 }
Exemple #12
0
 public static IDbCmd CommandTimeout(
     this IDbCmd source,
     int?timeout)
 {
     if (source == null)
     {
         throw new ArgumentNullException(nameof(source));
     }
     source.CommandTimeout = timeout;
     return(source);
 }
Exemple #13
0
 public static IDbCmd CancellationToken(
     this IDbCmd source,
     CancellationToken?token)
 {
     if (source == null)
     {
         throw new ArgumentNullException(nameof(source));
     }
     source.CancellationToken = token;
     return(source);
 }
Exemple #14
0
 public static IDbCmd Transaction(
     this IDbCmd source,
     IDbTransaction trans)
 {
     if (source == null)
     {
         throw new ArgumentNullException(nameof(source));
     }
     source.Transaction = trans;
     return(source);
 }
Exemple #15
0
 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);
     });
 }
Exemple #16
0
 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);
     });
 }
Exemple #17
0
 public void Dispose()
 {
     if (!Diposed)
     {
         _dbCmd = null;
         _dbCommand?.Dispose();
         _dbCommand = null;
         _reader?.Dispose();
         _reader = null;
         Diposed = true;
     }
 }
Exemple #18
0
 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);
     });
 }
Exemple #19
0
 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);
     });
 }
Exemple #20
0
 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);
     });
 }
Exemple #21
0
 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);
     });
 }
Exemple #22
0
 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);
     });
 }
Exemple #23
0
 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);
     });
 }
Exemple #24
0
 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);
     });
 }
Exemple #25
0
        /// <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));
        }
Exemple #26
0
        public static int ExecuteNonQuery(
            this IDbCmd source)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            using (var cmd = source.CreateAndOpen())
            {
                return(cmd.ExecuteNonQuery());
            }
        }
Exemple #27
0
 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);
     });
 }
Exemple #28
0
 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));
 }
Exemple #29
0
 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);
     });
 }
Exemple #30
0
 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);
     });
 }