这是嵌套集模型的“添加节点”SQL查询
LOCK TABLE mytestdb.tbltree WRITE;
SELECT @myRight := rgt FROM mytestdb.tbltree
WHERE name = 'apples';
UPDATE mytestdb.tbltree SET rgt = rgt + 2 WHERE rgt > @myRight;
UPDATE mytestdb.tbltree SET lft = lft + 2 WHERE lft > @myRight;
INSERT INTO mytestdb.tbltree(name, lft, rgt)
VALUES('beans', @myRight + 1, @myRight + 2);
UNLOCK TABLES;
以及映射到 jOOQ
Record record = create.select(Tbltree.RGT)
.from(Tbltree.TBLTREE)
.where(Tbltree.NAME.equal("apples"))
.fetchOne();
int myright = record.getValue(Tbltree.RGT);
create.update(Tbltree.TBLTREE).set(Tbltree.RGT, Tbltree.RGT.add(2))
.where(Tbltree.RGT.greaterThan(myright)).execute();
create.update(Tbltree.TBLTREE).set(Tbltree.LFT, Tbltree.LFT.add(2))
.where(Tbltree.LFT.greaterThan(myright)).execute();
TbltreeRecord record2 = (TbltreeRecord) create
.insertInto(Tbltree.TBLTREE, Tbltree.NAME, Tbltree.LFT, Tbltree.RGT)
.values("cherries",myright+1,myright+2)
.returning(Tbltree.ID)
.fetchOne();
我应该如何锁定桌子?我是不是该?
谢谢