Skip to content
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

fix: support methods in mapper tags to set space to null. #254

Merged
merged 3 commits into from
Nov 12, 2023
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
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,17 @@ This source code is licensed under Apache 2.0 License.

## Bugfix

- fix: support methods in mapper tags to set space to null.
- Such as:

```xml
<mapper namespace="...">
<create id="createSpace" space="null">
create space new_space ( vid_type = INT64 );
</create>
</mapper>
```

- fix: [#190](https://github.com/nebula-contrib/ngbatis/issues/190) Insert failed when tag has no attributes
- chore: removing and exclude some packages: log4j related or useless.
- fix: [#194](https://github.com/nebula-contrib/ngbatis/issues/194) we can name the interface by `@Component` and `@Resource`, for example:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package ye.weicheng.ngbatis.demo.repository;

// Copyright (c) 2023 All project authors. All rights reserved.
//
// This source code is licensed under Apache 2.0 License.

import java.util.List;

/**
* 方法中指定该语句不使用space,比如说 create space 等语句-DAO
* @author yeweicheng
* @since 2023-11-10 13:18
* <br>Now is history!
*/
public interface DropSpaceDao {

void createSpace(String name);

void dropSpace(String name);

List<String> showTags();

}
20 changes: 20 additions & 0 deletions ngbatis-demo/src/main/resources/mapper/DropSpaceDao.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<!--
Copyright (c) 2023 All project authors. All rights reserved.

This source code is licensed under Apache 2.0 License.
-->
<mapper namespace="ye.weicheng.ngbatis.demo.repository.DropSpaceDao" space="test_drop">

<delete id="dropSpace">
drop space ${ p0 };
</delete>

<select id="createSpace" space="null">
create space ${ p0 } ( vid_type = INT64 );
</select>

<select id="showTags" resultType="java.lang.String">
SHOW TAGS;
</select>

</mapper>
Original file line number Diff line number Diff line change
Expand Up @@ -358,31 +358,6 @@ public void deleteByIdBatch() {
}
// endregion

@Test
public void deleteByIdBatch() {
long now = System.currentTimeMillis();
Person person1 = new Person();
person1.setName("UBB" + now);

Person person2 = new Person();
person2.setName("UBB" + (now + 1));

Person person3 = new Person();
person3.setName("UBB" + (now + 2));

List<Person> people = new ArrayList<>();
people.add(person1);
people.add(person2);
people.add(person3);
repository.insertBatch(people);

List<String> peopleIds = new ArrayList<>();
peopleIds.add(person1.getName());
peopleIds.add(person2.getName());
peopleIds.add(person3.getName());
Assert.equals(repository.deleteByIdBatch(peopleIds),1);
}

// region graph special
@Test
public void insertEdge() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package ye.weicheng.ngbatis.demo.repository;

// Copyright (c) 2023 All project authors. All rights reserved.
//
// This source code is licensed under Apache 2.0 License.

import java.util.List;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

/**
* @author yeweicheng
* @since 2023-11-10 13:26
* <br>Now is history!
*/
@SpringBootTest
class DropSpaceDaoTest {

@Autowired
private DropSpaceDao dao;

@Test
void dropSpace() throws InterruptedException {
String spaceName = "test_drop";
dao.createSpace(spaceName);
Thread.sleep(10 * 1000);

List<String> tags = dao.showTags();
System.out.println(tags);

dao.dropSpace(spaceName);
}

}
19 changes: 17 additions & 2 deletions src/main/java/org/nebula/contrib/ngbatis/proxy/MapperProxy.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
// This source code is licensed under Apache 2.0 License.

import static org.apache.commons.lang3.ObjectUtils.isEmpty;
import static org.apache.commons.lang3.StringUtils.isBlank;
import static org.nebula.contrib.ngbatis.models.ClassModel.PROXY_SUFFIX;

import com.vesoft.nebula.client.graph.SessionPool;
Expand Down Expand Up @@ -231,7 +232,7 @@ public static ResultSet executeWithParameter(ClassModel cm, MethodModel mm, Stri
autoSwitch = qlAndSpace[0] == null ? "" : qlAndSpace[0];
session = localSession.getSession();
result = session.executeWithParameter(gql, params);
localSession.setCurrentSpace(result.getSpaceName());
localSession.setCurrentSpace(getSpace(result));
if (result.isSucceeded()) {
return result;
} else {
Expand Down Expand Up @@ -339,11 +340,25 @@ private static String[] qlWithSpace(LocalSession localSession, String gql, Strin
* @return 目标space
*/
public static String getSpace(ClassModel cm, MethodModel mm) {
return mm != null && mm.getSpace() != null ? mm.getSpace()
String methodSpace;
return (mm != null && (methodSpace = mm.getSpace()) != null)
? (
"null".equals(methodSpace.trim()) ? null : methodSpace
)
: cm != null && cm.getSpace() != null ? cm.getSpace()
: ENV.getSpace();
}

/**
* 从结果集中获取当前的 space
* @param result 脚本执行之后的结果集
* @return 结果集所对应的 space
*/
private static String getSpace(ResultSet result) {
String spaceName = result.getSpaceName();
return isBlank(spaceName) ? null : spaceName;
}

public static Logger getLog() {
return log;
}
Expand Down
Loading