Skip to content

DATAJDBC-157 - Replaced dots int mybatis statement ids with dashes. #30

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
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
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jdbc</artifactId>
<version>1.0.0.BUILD-SNAPSHOT</version>
<version>1.0.0.DATAJDBC-157-SNAPSHOT</version>

<name>Spring Data JDBC</name>
<description>Spring Data module for JDBC repositories.</description>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2017 the original author or authors.
* Copyright 2018 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.
Expand All @@ -25,17 +25,12 @@
import org.springframework.data.mapping.PropertyPath;

/**
* {@link DataAccessStrategy} implementation based on MyBatis.
*
* Each method gets mapped to a statement. The name of the statement gets constructed as follows:
*
* The namespace is based on the class of the entity plus the suffix "Mapper". This is then followed by the method name separated by a dot.
*
* For methods taking a {@link PropertyPath} as argument, the relevant entity is that of the root of the path, and the path itself gets as dot separated String appended to the statement name.
*
* Each statement gets an instance of {@link MyBatisContext}, which at least has the entityType set.
*
* For methods taking a {@link PropertyPath} the entityTyoe if the context is set to the class of the leaf type.
* {@link DataAccessStrategy} implementation based on MyBatis. Each method gets mapped to a statement. The name of the
* statement gets constructed as follows: The namespace is based on the class of the entity plus the suffix "Mapper".
* This is then followed by the method name separated by a dot. For methods taking a {@link PropertyPath} as argument,
* the relevant entity is that of the root of the path, and the path itself gets as dot separated String appended to the
* statement name. Each statement gets an instance of {@link MyBatisContext}, which at least has the entityType set. For
* methods taking a {@link PropertyPath} the entityTyoe if the context is set to the class of the leaf type.
*
* @author Jens Schauder
*/
Expand Down Expand Up @@ -73,7 +68,7 @@ public void delete(Object id, Class<?> domainType) {
@Override
public void delete(Object rootId, PropertyPath propertyPath) {

sqlSession().delete(mapper(propertyPath.getOwningType().getType()) + ".delete." + propertyPath.toDotPath(),
sqlSession().delete(mapper(propertyPath.getOwningType().getType()) + ".delete-" + toDashPath(propertyPath),
new MyBatisContext(rootId, null, propertyPath.getLeafProperty().getTypeInformation().getType(),
Collections.emptyMap()));
}
Expand All @@ -94,7 +89,7 @@ public <T> void deleteAll(PropertyPath propertyPath) {
Class leaveType = propertyPath.getLeafProperty().getTypeInformation().getType();

sqlSession().delete( //
mapper(baseType) + ".deleteAll." + propertyPath.toDotPath(), //
mapper(baseType) + ".deleteAll-" + toDashPath(propertyPath), //
new MyBatisContext(null, null, leaveType, Collections.emptyMap()) //
);
}
Expand All @@ -119,7 +114,7 @@ public <T> Iterable<T> findAllById(Iterable<?> ids, Class<T> domainType) {

@Override
public <T> Iterable<T> findAllByProperty(Object rootId, JdbcPersistentProperty property) {
return sqlSession().selectList(mapper(property.getOwner().getType()) + ".findAllByProperty." + property.getName(),
return sqlSession().selectList(mapper(property.getOwner().getType()) + ".findAllByProperty-" + property.getName(),
new MyBatisContext(rootId, null, property.getType(), Collections.emptyMap()));
}

Expand All @@ -131,7 +126,8 @@ public <T> boolean existsById(Object id, Class<T> domainType) {

@Override
public long count(Class<?> domainType) {
return sqlSession().selectOne(mapper(domainType) + ".count");
return sqlSession().selectOne(mapper(domainType) + ".count",
new MyBatisContext(null, null, domainType, Collections.emptyMap()));
}

private String mapper(Class<?> domainType) {
Expand All @@ -141,4 +137,8 @@ private String mapper(Class<?> domainType) {
private SqlSession sqlSession() {
return sqlSessionFactory.openSession();
}

private String toDashPath(PropertyPath propertyPath) {
return propertyPath.toDotPath().replaceAll("\\.", "-");
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2017 the original author or authors.
* Copyright 2018 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.
Expand Down Expand Up @@ -123,7 +123,7 @@ public void deleteAllByPath() {

accessStrategy.deleteAll(PropertyPath.from("class.name.bytes", String.class));

verify(session).delete(eq("java.lang.StringMapper.deleteAll.class.name.bytes"), captor.capture());
verify(session).delete(eq("java.lang.StringMapper.deleteAll-class-name-bytes"), captor.capture());

assertThat(captor.getValue()) //
.isNotNull() //
Expand Down Expand Up @@ -167,7 +167,7 @@ public void deleteByPath() {

accessStrategy.delete("rootid", PropertyPath.from("class.name.bytes", String.class));

verify(session).delete(eq("java.lang.StringMapper.delete.class.name.bytes"), captor.capture());
verify(session).delete(eq("java.lang.StringMapper.delete-class-name-bytes"), captor.capture());

assertThat(captor.getValue()) //
.isNotNull() //
Expand Down Expand Up @@ -248,6 +248,7 @@ public void findAllById() {
);
}

@SuppressWarnings("unchecked")
@Test // DATAJDBC-123
public void findAllByProperty() {

Expand All @@ -259,7 +260,7 @@ public void findAllByProperty() {

accessStrategy.findAllByProperty("id", property);

verify(session).selectList(eq("java.lang.StringMapper.findAllByProperty.propertyName"), captor.capture());
verify(session).selectList(eq("java.lang.StringMapper.findAllByProperty-propertyName"), captor.capture());

assertThat(captor.getValue()) //
.isNotNull() //
Expand Down Expand Up @@ -298,4 +299,29 @@ public void existsById() {
);
}

@Test // DATAJDBC-157
public void count() {

doReturn(0L).when(session).selectOne(anyString(), any());

accessStrategy.count(String.class);


verify(session).selectOne(eq("java.lang.StringMapper.count"), captor.capture());

assertThat(captor.getValue()) //
.isNotNull() //
.extracting( //
MyBatisContext::getInstance, //
MyBatisContext::getId, //
MyBatisContext::getDomainType, //
c -> c.get("key") //
).containsExactly( //
null, //
null, //
String.class, //
null //
);
}

}