package weed3rdb.features;

import org.junit.Test;
import org.noear.weed.*;
import weed3rdb.DbUtil;

public class TranTest {
    @Test
    public void test0() throws Throwable {
        DbContext db1 = DbUtil.db;

        clear(db1);

        Trans.tran(()->{
            db1.sql("insert into test (v1) values (1024);").insert();
            db1.sql("insert into test (v1) values (1024);").insert();

            db1.sql("update test set v1=12 where id=?",1).update();
        });

        assert  db1.table("test").count()==2;
    }

    @Test
    public void test01() throws Throwable {
        DbContext db1 = DbUtil.db;

        clear(db1);

        try {
            Trans.tran(() -> {
                db1.sql("insert into test (v1) values (1024);").insert();
                db1.sql("insert into test (v1) values (1024);").insert();

                throw new RuntimeException("不让你加");
            });
        }catch (Exception ex){

        }

        assert  db1.table("test").count()==0;
    }

    @Test
    public void test1() throws Throwable {
        DbContext db1 = DbUtil.db;
        DbContext db2 = DbUtil.db;

        clear(db1);

        Trans.tran(()->{
            db1.sql("insert into test (v1) values (1024);").insert();
            db2.sql("insert into test (v1) values (1024);").insert();
        });

        assert  db1.table("test").count()==2;
    }


    @Test
    public void test11() throws Throwable {
        DbContext db1 = DbUtil.db;
        DbContext db2 = DbUtil.db;

        clear(db1);

        try {
            Trans.tran(()->{
                db1.sql("insert into test (v1) values (1024);").insert();
                db2.sql("insert into test (v1) values (1024);").insert();

                throw new RuntimeException("不让你加");
            });

        } catch (Exception ex) {

        }

        db1.sql("insert into test (v1) values (1024);").insert();

        long count = db1.table("test").count();
        System.out.print(count);
        assert count == 1;
    }


    public void demo2() throws Throwable {
        DbContext db1 = DbUtil.db;
        DbContext db2 = DbUtil.db;

        new DbTranQueue().execute((tq) -> {
            VarHolder<Long> tmp = new VarHolder<>();

            db1.tran(tq, t -> {
                tmp.value = db1.sql("").insert();
            });

            db2.tran(tq, t -> {
                db2.sql("").update();
            });
        });
    }

    public void demo2_1() throws Throwable {
        DbContext db1 = DbUtil.db;
        DbContext db2 = DbUtil.db;

        new DbTran().execute((tq) -> {
            VarHolder<Long> tmp = new VarHolder<>();

            tmp.value = db1.sql("").insert();
            db2.sql("").update();

        });
    }

    private void clear(DbContext db) throws Exception {
        db.exe("TRUNCATE TABLE test");
    }
}

@@ -501,33 +501,39 @@
 1.table(%22test%22).
-c
+selectC
 ount()==2;%0A    %7D
@@ -2141,37 +2141,20 @@
     
-new DbTranQueue().execute((tq
+Trans.tran((
 ) -%3E
@@ -2214,44 +2214,8 @@
 );%0A%0A
-            db1.tran(tq, t -%3E %7B%0A    
     
@@ -2255,32 +2255,16 @@
 nsert();
-%0A            %7D);
 %0A%0A      
@@ -2277,75 +2277,23 @@
 db2.
-tran(tq, t -%3E %7B%0A                db2.sql(%22%22).update();%0A            %7D
+sql(%22%22).update(
 );%0A 
@@ -2438,37 +2438,23 @@
     
-new DbTran().execute((tq) 
+Trans.tran(()
 -%3E
- 
 %7B%0A  

Update TranTest.java