public static void Delete <T>(string connectionString, T entity) where T : new()
        {
            var(condition, wheres) = KeyAttribute.GetKeysQueryWhere(entity);

            var deleteQueryBuilder = new DeleteQueryBuilder()
                                     .WithWhere(condition, wheres.ToArray());

            var query = deleteQueryBuilder.Build <T>();

            DataBaseDataReader.ExecuteNonQuery(connectionString, query);
        }
Beispiel #2
0
        public void GenerateSimpleQueryTest()
        {
            // Arrange
            const string queryExpected      = "DELETE FROM `books`";
            var          deleteQueryBuilder = new DeleteQueryBuilder();

            // Act
            var query = deleteQueryBuilder.Build <Book>();

            // Assert
            Assert.AreEqual(queryExpected, query);
        }
Beispiel #3
0
        public void GenerateQuerySimpleWhereTest()
        {
            // Arrange
            const string queryExpected      = "DELETE FROM `books` WHERE `Id` = 1";
            var          deleteQueryBuilder = new DeleteQueryBuilder()
                                              .WithWhere(new WhereQueryEquals(GetColumnNameWithQuotes <Book>(nameof(Book.Id)), 1));

            // Act
            var query = deleteQueryBuilder.Build <Book>();

            // Assert
            Assert.AreEqual(queryExpected, query);
        }
Beispiel #4
0
        public void GenerateQueryWith2WhereConditionsTest()
        {
            // Arrange
            const string queryExpected      = "DELETE FROM `books` WHERE `Id` = 1 OR `Id` = 2";
            var          deleteQueryBuilder = new DeleteQueryBuilder()
                                              .WithWhere(
                new WhereQueryEquals(GetColumnNameWithQuotes <Book>(nameof(Book.Id)), 1),
                (WhereQuerySyntaxEnum.Or, new WhereQueryEquals(GetColumnNameWithQuotes <Book>(nameof(Book.Id)), 2)));

            // Act
            var query = deleteQueryBuilder.Build <Book>();

            // Assert
            Assert.AreEqual(queryExpected, query);
        }
Beispiel #5
0
        public void GenerateQueryWithJoinTest()
        {
            // Arrange
            const string queryExpected      = "DELETE FROM `books` JOIN `publishers` ON `books`.`PublisherId` = `publishers`.`Id` WHERE `publishers`.`Name` LIKE '%Scholastic%'";
            var          deleteQueryBuilder = new DeleteQueryBuilder()
                                              .WithJoin(
                JoinEnum.Join,
                GetTableNameWithQuotes <Book>(),
                GetTableNameWithQuotes <Publisher>(),
                (GetColumnNameWithQuotes <Book>(nameof(Book.PublisherId)), GetColumnNameWithQuotes <Publisher>(nameof(Publisher.Id))))
                                              .WithWhere <Publisher>(new WhereQueryLike(GetColumnNameWithQuotes <Publisher>(nameof(Publisher.Name)), "%Scholastic%"));

            // Act
            var query = deleteQueryBuilder.Build <Book>();

            // Assert
            Assert.AreEqual(queryExpected, query);
        }