From 8cc98a75c5d67020942185fd9105f7f5734e38aa Mon Sep 17 00:00:00 2001 From: CorvusYe Date: Thu, 19 Dec 2024 05:54:25 +0800 Subject: [PATCH 1/2] fix#329: correct the return value type and clear the interface generic. --- .../ye/weicheng/ngbatis/demo/NebulaBasicDaoTests.java | 9 ++++++++- .../nebula/contrib/ngbatis/proxy/NebulaDaoBasic.java | 10 ++++------ .../contrib/ngbatis/proxy/NebulaDaoBasicExt.java | 4 ++-- src/main/resources/NebulaDaoBasic.xml | 2 +- 4 files changed, 15 insertions(+), 10 deletions(-) diff --git a/ngbatis-demo/src/test/java/ye/weicheng/ngbatis/demo/NebulaBasicDaoTests.java b/ngbatis-demo/src/test/java/ye/weicheng/ngbatis/demo/NebulaBasicDaoTests.java index 08694278..3ad5707a 100644 --- a/ngbatis-demo/src/test/java/ye/weicheng/ngbatis/demo/NebulaBasicDaoTests.java +++ b/ngbatis-demo/src/test/java/ye/weicheng/ngbatis/demo/NebulaBasicDaoTests.java @@ -25,6 +25,7 @@ import ye.weicheng.ngbatis.demo.pojo.Like; import ye.weicheng.ngbatis.demo.pojo.LikeWithRank; import ye.weicheng.ngbatis.demo.pojo.Person; +import ye.weicheng.ngbatis.demo.pojo.vertex.Player; import ye.weicheng.ngbatis.demo.repository.TestRepository; /** @@ -555,10 +556,16 @@ public void listStartNodes() { @Test public void listEndNodes() { - List personList = repository.listEndNodes("易小海", Like.class); + List personList = repository.listEndNodes("叶小南", Like.class); System.out.println(JSON.toJSONString(personList)); } + @Test + public void listEndNodesPlayer() { + List playerList = repository.listEndNodes("叶小南", Like.class, Player.class); + System.out.println(JSON.toJSONString(playerList)); + } + @Test public void startNode() { Person whoIsStartForTest = repository.startNode(Like.class, "易小海"); diff --git a/src/main/java/org/nebula/contrib/ngbatis/proxy/NebulaDaoBasic.java b/src/main/java/org/nebula/contrib/ngbatis/proxy/NebulaDaoBasic.java index 13dbda03..8de7c941 100644 --- a/src/main/java/org/nebula/contrib/ngbatis/proxy/NebulaDaoBasic.java +++ b/src/main/java/org/nebula/contrib/ngbatis/proxy/NebulaDaoBasic.java @@ -451,13 +451,12 @@ default List listStartNodes(Class edgeType, I endId) { * @param endId 结束节点的 id * @return 开始节点列表 */ - default List listStartNodes(Class startType, Class edgeType, I endId) { + default List listStartNodes(Class startType, Class edgeType, I endId) { String cqlTpl = getCqlTpl(); String startVertexName = vertexName(startType); String edgeName = edgeName(edgeType); Class daoType = this.getClass(); - Class returnType = entityType(daoType); - return (List) proxy(daoType, returnType, cqlTpl, + return (List) proxy(daoType, startType, cqlTpl, new Class[]{Class.class, Class.class, Serializable.class}, startVertexName, edgeName, endId); } @@ -482,13 +481,12 @@ default List listEndNodes(I startId, Class edgeType) { * @param endType 结束节点的类型 * @return 结束节点列表 */ - default List listEndNodes(I startId, Class edgeType, Class endType) { + default List listEndNodes(I startId, Class edgeType, Class endType) { String cqlTpl = getCqlTpl(); String endVertexName = vertexName(endType); String edgeName = edgeName(edgeType); Class daoType = this.getClass(); - Class returnType = entityType(daoType); - return (List) proxy(daoType, returnType, cqlTpl, + return (List) proxy(daoType, endType, cqlTpl, new Class[]{Serializable.class, Class.class, Class.class}, startId, edgeName, endVertexName); } diff --git a/src/main/java/org/nebula/contrib/ngbatis/proxy/NebulaDaoBasicExt.java b/src/main/java/org/nebula/contrib/ngbatis/proxy/NebulaDaoBasicExt.java index 19d7fde2..d393f33e 100644 --- a/src/main/java/org/nebula/contrib/ngbatis/proxy/NebulaDaoBasicExt.java +++ b/src/main/java/org/nebula/contrib/ngbatis/proxy/NebulaDaoBasicExt.java @@ -123,7 +123,7 @@ public static Class pkType(Class currentType) { * @param args 接口参数 * @return 对结果集进行处理后的 java对象 */ - public static Object proxy(Class currentType, Class returnType, String gql, + public static Object proxy(Class currentType, Class resultType, String gql, Class[] argTypes, Object... args) { Method method = null; try { @@ -135,7 +135,7 @@ public static Object proxy(Class currentType, Class returnType, String gql MethodModel methodModel = new MethodModel(); methodModel.setMethod(method); - methodModel.setResultType(returnType); + methodModel.setResultType(resultType); methodModel.setText(gql); ClassModel classModel = getClassModel(currentType); return MapperProxy.invoke(classModel, methodModel, args); diff --git a/src/main/resources/NebulaDaoBasic.xml b/src/main/resources/NebulaDaoBasic.xml index f53829c2..bdeedd5b 100644 --- a/src/main/resources/NebulaDaoBasic.xml +++ b/src/main/resources/NebulaDaoBasic.xml @@ -398,7 +398,7 @@ From 087e9adbd80585f59c923de2764698bea6d7bf84 Mon Sep 17 00:00:00 2001 From: CorvusYe Date: Thu, 19 Dec 2024 07:23:26 +0800 Subject: [PATCH 2/2] feat#324: [NebulaDaoBasic] add methods: insertForce(v) insertSelectiveForce(v) --- .../demo/NebulaBasicDaoOrderedTests.java | 125 ++++++++++++++++++ .../contrib/ngbatis/proxy/NebulaDaoBasic.java | 34 ++++- src/main/resources/NebulaDaoBasic.xml | 31 +++++ 3 files changed, 188 insertions(+), 2 deletions(-) create mode 100644 ngbatis-demo/src/test/java/ye/weicheng/ngbatis/demo/NebulaBasicDaoOrderedTests.java diff --git a/ngbatis-demo/src/test/java/ye/weicheng/ngbatis/demo/NebulaBasicDaoOrderedTests.java b/ngbatis-demo/src/test/java/ye/weicheng/ngbatis/demo/NebulaBasicDaoOrderedTests.java new file mode 100644 index 00000000..928a07d8 --- /dev/null +++ b/ngbatis-demo/src/test/java/ye/weicheng/ngbatis/demo/NebulaBasicDaoOrderedTests.java @@ -0,0 +1,125 @@ +package ye.weicheng.ngbatis.demo; + +// Copyright (c) 2024 All project authors. All rights reserved. +// +// This source code is licensed under Apache 2.0 License. + +import static org.springframework.util.ObjectUtils.nullSafeEquals; + +import java.util.Date; +import java.util.Random; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.MethodOrderer.OrderAnnotation; +import org.junit.jupiter.api.Order; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestMethodOrder; +import org.locationtech.jts.util.Assert; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import ye.weicheng.ngbatis.demo.pojo.Person; +import ye.weicheng.ngbatis.demo.repository.TestRepository; + +/** + * @author yeweicheng + * @since 2024-12-19 06:24 + *
Now is history! + */ +@SpringBootTest +@TestMethodOrder(OrderAnnotation.class) +public class NebulaBasicDaoOrderedTests { + + @Autowired + private TestRepository dao; + + private static Random random = new Random(); + + private static String nameCache = null; + + private static Integer ageCache = null; + + private static Date birthdayCache = null; + + // 数据库定义的默认年龄 + private static Integer defaultAge = 18; + + @BeforeAll + public static void setVars() { + nameCache = String.valueOf(random.nextInt()); + ageCache = random.nextInt(100); + birthdayCache = new Date(); + } + + + @Test + @Order(1) + public void testInsert() { + Person person = new Person(); + nameCache = String.valueOf(random.nextInt()); + person.setName(nameCache); + person.setAge(ageCache); + person.setBirthday(birthdayCache); + Integer effect = dao.insert(person); + Assert.isTrue(effect == 1); + checkData(); + } + + public void checkData() { + Person person = dao.selectById(nameCache); + Assert.isTrue(person != null); + Assert.isTrue(nullSafeEquals(person.getName(), nameCache)); + Assert.isTrue(nullSafeEquals(person.getAge(), ageCache)); + Assert.isTrue(nullSafeEquals(person.getBirthday(), birthdayCache)); + } + + @Test + @Order(3) + public void testInsertForce() { + Person person = new Person(); + person.setName(nameCache); + ageCache = ageCache + 1; + person.setAge(ageCache); + birthdayCache = null; + person.setBirthday(null); + Integer effect = dao.insertForce(person); + Assert.isTrue(effect == 1); + checkData(); + } + + @Test + @Order(4) + public void testInsertSelective() { + testDeleteById(); + setVars(); + Person person = new Person(); + person.setName(nameCache); + person.setAge(ageCache); + birthdayCache = null; + Integer effect = dao.insertSelective(person); + Assert.isTrue(effect == 1); + checkData(); + } + + @Test + @Order(5) + public void testInsertSelectiveForce() { + Person person = new Person(); + person.setName(nameCache); + ageCache = defaultAge; + birthdayCache = new Date(); + person.setAge(ageCache); + person.setBirthday(birthdayCache); + Integer effect = dao.insertSelectiveForce(person); + Assert.isTrue(effect == 1); + checkData(); + } + + @Test + @Order(1000) + public void testDeleteById() { + Integer effect = dao.deleteById(nameCache); + Assert.isTrue(effect == 1); + Person person = dao.selectById(nameCache); + Assert.isTrue(person == null); + } + +} diff --git a/src/main/java/org/nebula/contrib/ngbatis/proxy/NebulaDaoBasic.java b/src/main/java/org/nebula/contrib/ngbatis/proxy/NebulaDaoBasic.java index 8de7c941..e6e088ad 100644 --- a/src/main/java/org/nebula/contrib/ngbatis/proxy/NebulaDaoBasic.java +++ b/src/main/java/org/nebula/contrib/ngbatis/proxy/NebulaDaoBasic.java @@ -187,7 +187,7 @@ default Long countPage(Page page) { // region insert zoom /** - *

插入一条记录,全属性插入

+ *

插入一条记录,全属性插入(IF NOT EXISTS)

* * @param record 当前表对应的记录数据 * @return 是否删除成功,成功 1,失败 0 @@ -202,7 +202,22 @@ default Integer insert(T record) { } /** - *

插入非空字段。

+ *

插入一条记录,全属性插入

+ * + * @param record 当前表对应的记录数据 + * @return 是否删除成功,成功 1,失败 0 + */ + default Integer insertForce(T record) { + String cqlTpl = getCqlTpl(); + ResultSet rs = (ResultSet) proxy( + this.getClass(), ResultSet.class, cqlTpl, + new Class[]{Object.class}, record + ); + return rs.isSucceeded() ? 1 : 0; + } + + /** + *

插入非空字段。(IF NOT EXISTS)

* * @param record 单个顶点 * @return 是否删除成功,成功 1,失败 0 @@ -216,6 +231,21 @@ default Integer insertSelective(T record) { return resultSet.isSucceeded() ? 1 : 0; } + /** + *

插入非空字段。无论具有原有数据该id的节点是否已存在

+ * + * @param record 单个顶点 + * @return 是否删除成功,成功 1,失败 0 + */ + default Integer insertSelectiveForce(T record) { + String cqlTpl = getCqlTpl(); + ResultSet rs = (ResultSet) proxy( + this.getClass(), ResultSet.class, cqlTpl, + new Class[]{Object.class}, record + ); + return rs.isSucceeded() ? 1 : 0; + } + /** * 批量插入全字段 * diff --git a/src/main/resources/NebulaDaoBasic.xml b/src/main/resources/NebulaDaoBasic.xml index bdeedd5b..7e342313 100644 --- a/src/main/resources/NebulaDaoBasic.xml +++ b/src/main/resources/NebulaDaoBasic.xml @@ -178,6 +178,21 @@ ); + + + @var kv = ng.kv( ng_args[0] ); + @var id = ng.id( ng_args[0] ); + INSERT VERTEX + @for ( entry in @kv.multiTagColumns ) { + `${ entry.key }` ( + ${ ng.join( entry.value, ", ", "ng.schemaFmt" ) } + ) ${ entryLP.last ? '' : ','} + @} + VALUES ${ id } : ( + ${ ng.join( @kv.values ) } + ); + + @var kv = ng.kv( ng_args[0], '', true, true ); @var id = ng.id( ng_args[0] ); @@ -192,6 +207,22 @@ ); + + + @var kv = ng.kv( ng_args[0], '', true, true ); + @var id = ng.id( ng_args[0] ); + INSERT VERTEX + @for ( entry in @kv.multiTagColumns ) { + `${ entry.key }` ( + ${ ng.join( entry.value, ", ", "ng.schemaFmt" ) } + ) ${ entryLP.last ? '' : ','} + @} + VALUES ${ id } : ( + ${ ng.join( @kv.values ) } + ); + + + @for ( row in ng_args[0] ) { @var kv = ng.kv( row );