Skip to content

Commit

Permalink
feat#324: [NebulaDaoBasic] add methods: insertForce(v) insertSelectiv…
Browse files Browse the repository at this point in the history
…eForce(v)
  • Loading branch information
CorvusYe committed Dec 18, 2024
1 parent 8cc98a7 commit 087e9ad
Show file tree
Hide file tree
Showing 3 changed files with 188 additions and 2 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);
}

}
34 changes: 32 additions & 2 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
31 changes: 31 additions & 0 deletions 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

0 comments on commit 087e9ad

Please sign in to comment.