Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions src/main/java/org/mybatis/dynamic/sql/delete/DeleteDSL.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.mybatis.dynamic.sql.common.OrderByModel;
import org.mybatis.dynamic.sql.configuration.StatementConfiguration;
import org.mybatis.dynamic.sql.util.Buildable;
import org.mybatis.dynamic.sql.util.Utilities;
import org.mybatis.dynamic.sql.where.AbstractWhereFinisher;
import org.mybatis.dynamic.sql.where.AbstractWhereStarter;
import org.mybatis.dynamic.sql.where.WhereModel;
Expand All @@ -50,9 +51,7 @@ private DeleteDSL(SqlTable table, String tableAlias, Function<DeleteModel, R> ad

@Override
public DeleteWhereBuilder where() {
if (whereBuilder == null) {
whereBuilder = new DeleteWhereBuilder();
}
whereBuilder = Utilities.buildIfNecessary(whereBuilder, DeleteWhereBuilder::new);
return whereBuilder;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import org.mybatis.dynamic.sql.select.join.JoinSpecification;
import org.mybatis.dynamic.sql.select.join.JoinType;
import org.mybatis.dynamic.sql.util.Buildable;
import org.mybatis.dynamic.sql.util.Utilities;
import org.mybatis.dynamic.sql.where.AbstractWhereFinisher;
import org.mybatis.dynamic.sql.where.AbstractWhereStarter;
import org.mybatis.dynamic.sql.where.WhereModel;
Expand Down Expand Up @@ -68,9 +69,7 @@ protected QueryExpressionDSL(FromGatherer<R> fromGatherer, SqlTable table, Strin

@Override
public QueryExpressionWhereBuilder where() {
if (whereBuilder == null) {
whereBuilder = new QueryExpressionWhereBuilder();
}
whereBuilder = Utilities.buildIfNecessary(whereBuilder, QueryExpressionWhereBuilder::new);
return whereBuilder;
}

Expand Down
6 changes: 2 additions & 4 deletions src/main/java/org/mybatis/dynamic/sql/update/UpdateDSL.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import org.mybatis.dynamic.sql.util.NullMapping;
import org.mybatis.dynamic.sql.util.SelectMapping;
import org.mybatis.dynamic.sql.util.StringConstantMapping;
import org.mybatis.dynamic.sql.util.Utilities;
import org.mybatis.dynamic.sql.util.ValueMapping;
import org.mybatis.dynamic.sql.util.ValueOrNullMapping;
import org.mybatis.dynamic.sql.util.ValueWhenPresentMapping;
Expand Down Expand Up @@ -70,10 +71,7 @@ public <T> SetClauseFinisher<T> set(SqlColumn<T> column) {

@Override
public UpdateWhereBuilder where() {
if (whereBuilder == null) {
whereBuilder = new UpdateWhereBuilder();
}

whereBuilder = Utilities.buildIfNecessary(whereBuilder, UpdateWhereBuilder::new);
return whereBuilder;
}

Expand Down
28 changes: 28 additions & 0 deletions src/main/java/org/mybatis/dynamic/sql/util/Utilities.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Copyright 2016-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.mybatis.dynamic.sql.util;

import java.util.function.Supplier;

public interface Utilities {
static <T> T buildIfNecessary(T current, Supplier<T> builder) {
return current == null ? builder.get() : current;
}

static long safelyUnbox(Long l) {
return l == null ? 0 : l;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import org.mybatis.dynamic.sql.update.UpdateModel;
import org.mybatis.dynamic.sql.update.render.UpdateStatementProvider;
import org.mybatis.dynamic.sql.util.Buildable;
import org.mybatis.dynamic.sql.util.Utilities;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
Expand All @@ -57,7 +58,7 @@ public long count(SelectStatementProvider countStatement) {
Long answer = template.queryForObject(countStatement.getSelectStatement(),
countStatement.getParameters(), Long.class);

return answer == null ? 0L : answer;
return Utilities.safelyUnbox(answer);
}

public int delete(Buildable<DeleteModel> deleteStatement) {
Expand Down
28 changes: 28 additions & 0 deletions src/test/java/org/mybatis/dynamic/sql/util/UtilitiesTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Copyright 2016-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.mybatis.dynamic.sql.util;

import static org.assertj.core.api.Assertions.assertThat;

import org.junit.jupiter.api.Test;

class UtilitiesTest {

@Test
void testUnboxWithNull() {
assertThat(Utilities.safelyUnbox(null)).isZero();
}
}