public void ExecuteInvalidSqlBatch()
        {
            using (var connection = new MySqlConnection(AppConfig.ConnectionString))
            {
                connection.Open();
                using (var batch = new MySqlBatch(connection)
                {
                    BatchCommands =
                    {
                        new MySqlBatchCommand("SELECT 1;"),
                        new MySqlBatchCommand("SELECT 2 /* incomplete"),
                        new MySqlBatchCommand("SELECT 3;"),
                    },
                })
                    using (var reader = batch.ExecuteReader())
                    {
                        Assert.True(reader.Read());
                        Assert.Equal(1, reader.GetInt32(0));
                        Assert.False(reader.Read());

                        try
                        {
                            reader.NextResult();
                            Assert.True(false, "Shouldn't get here");
                        }
                        catch (MySqlException ex)
                        {
                            Assert.Equal(MySqlErrorCode.ParseError, (MySqlErrorCode)ex.Number);
                        }
                    }
            }
        }
Beispiel #2
0
 public void NeedsCommands()
 {
     using var connection = new MySqlConnection(AppConfig.ConnectionString);
     connection.Open();
     using var batch = new MySqlBatch(connection);
     Assert.Throws <InvalidOperationException>(() => batch.ExecuteNonQuery());
 }
Beispiel #3
0
        public void ExecuteBatch(string suffix)
        {
            using var connection = new MySqlConnection(AppConfig.ConnectionString);
            connection.Open();
            using var batch = new MySqlBatch(connection)
                  {
                      BatchCommands =
                      {
                          new MySqlBatchCommand("SELECT 1" + suffix),
                          new MySqlBatchCommand("SELECT 2" + suffix),
                          new MySqlBatchCommand("SELECT 3" + suffix),
                      },
                  };
            using var reader = batch.ExecuteReader();
            var total = 0;

            Assert.True(reader.Read());
            total += reader.GetInt32(0);
            Assert.False(reader.Read());
            Assert.True(reader.NextResult());

            Assert.True(reader.Read());
            total += reader.GetInt32(0);
            Assert.False(reader.Read());
            Assert.True(reader.NextResult());

            Assert.True(reader.Read());
            total += reader.GetInt32(0);
            Assert.False(reader.Read());
            Assert.False(reader.NextResult());

            Assert.Equal(6, total);
        }
 public void NotDisposed()
 {
     using (var batch = new MySqlBatch())
     {
         batch.Dispose();
         Assert.Throws <ObjectDisposedException>(() => batch.ExecuteNonQuery());
     }
 }
Beispiel #5
0
 public void PrepareNeedsCommandsWithText()
 {
     using var connection = new MySqlConnection(AppConfig.ConnectionString);
     connection.Open();
     using var batch = new MySqlBatch(connection)
           {
               BatchCommands = { new MySqlBatchCommand() },
           };
     Assert.Throws <InvalidOperationException>(() => batch.Prepare());
 }
 public void PrepareNeedsCommands()
 {
     using (var connection = new MySqlConnection(AppConfig.ConnectionString))
     {
         connection.Open();
         using (var batch = new MySqlBatch(connection))
         {
             Assert.Throws <InvalidOperationException>(() => batch.Prepare());
         }
     }
 }
Beispiel #7
0
 public void PrepareNeedsConnection()
 {
     using var batch = new MySqlBatch
           {
               BatchCommands =
               {
                   new MySqlBatchCommand("SELECT 1;"),
               },
           };
     Assert.Throws <InvalidOperationException>(() => batch.Prepare());
 }
Beispiel #8
0
 public void PrepareNeedsOpenConnection()
 {
     using var connection = new MySqlConnection(AppConfig.ConnectionString);
     using var batch      = new MySqlBatch(connection)
           {
               BatchCommands =
               {
                   new MySqlBatchCommand("SELECT 1;"),
               },
           };
     Assert.Throws <InvalidOperationException>(() => batch.Prepare());
 }
 public void NeedsConnection()
 {
     using (var batch = new MySqlBatch
     {
         BatchCommands =
         {
             new MySqlBatchCommand("SELECT 1;"),
         },
     })
     {
         Assert.Throws <InvalidOperationException>(() => batch.ExecuteNonQuery());
     }
 }
Beispiel #10
0
 public void NeedsOpenConnection()
 {
     using (var connection = new MySqlConnection(AppConfig.ConnectionString))
         using (var batch = new MySqlBatch(connection)
         {
             BatchCommands =
             {
                 new MySqlBatchCommand("SELECT 1;"),
             },
         })
         {
             Assert.Throws <InvalidOperationException>(() => batch.ExecuteNonQuery());
         }
 }
Beispiel #11
0
 public void NoCloseConnection()
 {
     using var connection = new MySqlConnection(AppConfig.ConnectionString);
     connection.Open();
     using var batch = new MySqlBatch(connection)
           {
               BatchCommands =
               {
                   new MySqlBatchCommand("SELECT 1;")
                   {
                       CommandBehavior = CommandBehavior.CloseConnection
                   },
               },
           };
     Assert.Throws <NotSupportedException>(() => batch.ExecuteNonQuery());
 }
Beispiel #12
0
        public void SingleRow()
        {
            using (var connection = new MySqlConnection(AppConfig.ConnectionString))
            {
                connection.Open();
                using (var command = new MySqlCommand(@"drop table if exists batch_single_row;
create table batch_single_row(id integer not null primary key);
insert into batch_single_row(id) values(1),(2),(3);", connection))
                {
                    command.ExecuteNonQuery();
                }

                using (var batch = new MySqlBatch(connection)
                {
                    BatchCommands =
                    {
                        new MySqlBatchCommand("SELECT id FROM batch_single_row ORDER BY id"),
                        new MySqlBatchCommand("SELECT id FROM batch_single_row ORDER BY id")
                        {
                            CommandBehavior = CommandBehavior.SingleRow
                        },
                    },
                })
                    using (var reader = batch.ExecuteReader())
                    {
                        Assert.True(reader.Read());
                        Assert.Equal(1, reader.GetInt32(0));
                        Assert.True(reader.Read());
                        Assert.Equal(2, reader.GetInt32(0));
                        Assert.True(reader.Read());
                        Assert.Equal(3, reader.GetInt32(0));
                        Assert.False(reader.Read());

                        Assert.True(reader.NextResult());
                        Assert.True(reader.Read());
                        Assert.Equal(1, reader.GetInt32(0));
                        Assert.False(reader.Read());

                        Assert.False(reader.NextResult());
                    }
            }
        }