返事遅れてすみません。h2
問題は、タグを追加しなかったため、質問がリストに表示されなかったことです。
drop table nested_category;
drop alias cat_add;
create table nested_category(id identity, lft int, rgt int, name varchar);
create alias cat_add as $$
void catAdd(Connection conn, String name, String after) throws SQLException {
Statement stat = conn.createStatement();
stat.execute("SET @myRight 0");
PreparedStatement prep = conn.prepareStatement(
"SELECT @myRight := rgt FROM nested_category WHERE name = ?");
prep.setString(1, after);
prep.execute();
stat.execute("UPDATE nested_category SET rgt = rgt + 2 WHERE rgt > @myRight");
stat.execute("UPDATE nested_category SET lft = lft + 2 WHERE lft > @myRight");
prep = conn.prepareStatement(
"INSERT INTO nested_category(name, lft, rgt) VALUES(?, @myRight + 1, @myRight + 2)");
prep.setString(1, name);
prep.execute();
}
$$;
call cat_add('television', null);
call cat_add('game consoles', 'television');
select * from nested_category;