Skip to content

Commit

Permalink
Merge pull request #335 from CorvusYe/master
Browse files Browse the repository at this point in the history
fix#329: correct the return value type and clear the interface generic.
  • Loading branch information
CorvusYe authored Dec 18, 2024
2 parents 4bc2d09 + 087e9ad commit 8dd9f1f
Show file tree
Hide file tree
Showing 5 changed files with 203 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -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
* <br>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);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -555,10 +556,16 @@ public void listStartNodes() {

@Test
public void listEndNodes() {
List<Person> personList = repository.listEndNodes("易小海", Like.class);
List<Person> personList = repository.listEndNodes("叶小南", Like.class);
System.out.println(JSON.toJSONString(personList));
}

@Test
public void listEndNodesPlayer() {
List<Player> playerList = repository.listEndNodes("叶小南", Like.class, Player.class);
System.out.println(JSON.toJSONString(playerList));
}

@Test
public void startNode() {
Person whoIsStartForTest = repository.startNode(Like.class, "易小海");
Expand Down
44 changes: 36 additions & 8 deletions src/main/java/org/nebula/contrib/ngbatis/proxy/NebulaDaoBasic.java
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ default Long countPage(Page<T> page) {
// region insert zoom

/**
* <p>插入一条记录,全属性插入</p>
* <p>插入一条记录,全属性插入(IF NOT EXISTS)</p>
*
* @param record 当前表对应的记录数据
* @return 是否删除成功,成功 1,失败 0
Expand All @@ -202,7 +202,22 @@ default Integer insert(T record) {
}

/**
* <p>插入非空字段。</p>
* <p>插入一条记录,全属性插入</p>
*
* @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;
}

/**
* <p>插入非空字段。(IF NOT EXISTS)</p>
*
* @param record 单个顶点
* @return 是否删除成功,成功 1,失败 0
Expand All @@ -216,6 +231,21 @@ default Integer insertSelective(T record) {
return resultSet.isSucceeded() ? 1 : 0;
}

/**
* <p>插入非空字段。无论具有原有数据该id的节点是否已存在</p>
*
* @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;
}

/**
* 批量插入全字段
*
Expand Down Expand Up @@ -451,13 +481,12 @@ default List<T> listStartNodes(Class<?> edgeType, I endId) {
* @param endId 结束节点的 id
* @return 开始节点列表
*/
default List<?> listStartNodes(Class<?> startType, Class<?> edgeType, I endId) {
default <E> List<E> listStartNodes(Class<E> startType, Class<?> edgeType, I endId) {
String cqlTpl = getCqlTpl();
String startVertexName = vertexName(startType);
String edgeName = edgeName(edgeType);
Class<? extends NebulaDaoBasic> daoType = this.getClass();
Class<?> returnType = entityType(daoType);
return (List<?>) proxy(daoType, returnType, cqlTpl,
return (List<E>) proxy(daoType, startType, cqlTpl,
new Class[]{Class.class, Class.class, Serializable.class}, startVertexName, edgeName,
endId);
}
Expand All @@ -482,13 +511,12 @@ default List<T> listEndNodes(I startId, Class<?> edgeType) {
* @param endType 结束节点的类型
* @return 结束节点列表
*/
default List<?> listEndNodes(I startId, Class<?> edgeType, Class<?> endType) {
default <E> List<E> listEndNodes(I startId, Class<?> edgeType, Class<E> endType) {
String cqlTpl = getCqlTpl();
String endVertexName = vertexName(endType);
String edgeName = edgeName(edgeType);
Class<? extends NebulaDaoBasic> daoType = this.getClass();
Class<?> returnType = entityType(daoType);
return (List<?>) proxy(daoType, returnType, cqlTpl,
return (List<E>) proxy(daoType, endType, cqlTpl,
new Class[]{Serializable.class, Class.class, Class.class}, startId, edgeName,
endVertexName);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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);
Expand Down
33 changes: 32 additions & 1 deletion src/main/resources/NebulaDaoBasic.xml
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,21 @@
);
</insert>


<insert id="insertForce">
@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 ) }
);
</insert>

<insert id="insertSelective">
@var kv = ng.kv( ng_args[0], '', true, true );
@var id = ng.id( ng_args[0] );
Expand All @@ -192,6 +207,22 @@
);
</insert>


<insert id="insertSelectiveForce">
@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 ) }
);
</insert>


<insert id="insertBatch">
@for ( row in ng_args[0] ) {
@var kv = ng.kv( row );
Expand Down Expand Up @@ -398,7 +429,7 @@
</select>

<select id="listEndNodes">
MATCH (n: `${ p2 }`)-[r: `${ p1 }`]->(n2)
MATCH (n)-[r: `${ p1 }`]->(n2: `${ p2 }`)
WHERE id(n) == $p0
RETURN n2
</select>
Expand Down

0 comments on commit 8dd9f1f

Please sign in to comment.