Exemple #1
0
func (l *Line) Sub(ctx *Ctx, left bool) (interface{}, error) {
	if len(l.Args) < 1 {
		return nil, Err("Usage:SUB path k=v... ,but:%v", l.L)
	}
	var ignore_err bool = false
	nctx := NewCtx(ctx)
	for _, arg := range l.Args {
		if arg == "-CTX" { //setting the same context to sub
			nctx = ctx
			continue
		}
		if arg == "-cookie" { //setting cookie to sub
			nctx.H = ctx.H
			continue
		}
		if arg == "-ig" {
			ignore_err = true
			continue
		}
		kvs := strings.SplitN(arg, "=", 2)
		if len(kvs) < 2 {
			continue
		}
		nctx.Kvs[ctx.Compile(kvs[0])] = ctx.Compile(kvs[1])
	}
	if ctx.Mark != nil {
		if nctx == ctx {
			om := ctx.Mark
			defer func() {
				ctx.Mark = om
			}()
		}
		nctx.Mark = ctx.Mark.Sub(l, ctx)
	}
	c := Compiler{}
	tpath := ctx.Compile(l.Args[0])
	if reg_http.MatchString(tpath) {
		spath := fmt.Sprintf("/tmp/%v.ig", util.Timestamp(time.Now()))
		defer os.RemoveAll(spath)
		err := util.DLoad(spath, tpath)
		if err != nil {
			return nil, err
		}
		tpath = spath //set the tmp script to subscript path
	} else if !filepath.IsAbs(tpath) {
		tpath = filepath.Join(l.C.Cwd, tpath)
	}
	err := c.Load(tpath)
	if err != nil {
		return nil, err
	}
	err = c.CompileAndExec(nctx, l.OnExeced)
	if ignore_err {
		return true, nil
	} else {
		return true, err
	}
}
Exemple #2
0
func (s *SrvSessionBuilder) Loop() {
	for s.looping {
		ary := []string{}
		now := util.Timestamp(time.Now())
		s.ks_lck.RLock()
		for k, v := range s.ks {
			delay := now - v.begin
			if delay > s.Timeout {
				s.evh.OnTimeout(v)
				ary = append(ary, k)
			}
		}
		s.ks_lck.RUnlock()
		if len(ary) > 0 {
			s.log("looping session time out,removing (%v)", ary)
		}
		s.ks_lck.Lock()
		for _, v := range ary {
			delete(s.ks, v)
		}
		s.ks_lck.Unlock()
		time.Sleep(s.CDelay * time.Millisecond)
	}
}
Exemple #3
0
func TestDbUtil(t *testing.T) {
	db, _ := sql.Open("mysql", test.TDbCon)
	defer db.Close()
	err := DbExecF(db, "ttable.sql")
	if err != nil {
		t.Error(err.Error())
	}
	res, err := DbQuery(db, "select * from ttable where tid>?", 1)
	if err != nil {
		t.Error(err.Error())
		return
	}
	if len(res) < 1 {
		t.Error("not data")
		return
	}
	if len(res[0]) < 1 {
		t.Error("data is empty")
		return
	}
	bys, err := json.Marshal(res)
	fmt.Println(string(bys))
	fmt.Println("T-->00")
	//
	var mres []TSt
	err = DbQueryS(db, &mres, "select * from ttable where tid>?", 1)
	if err != nil {
		t.Error(err.Error())
		return
	}
	if len(mres) < 1 {
		t.Error("not data")
		return
	}
	fmt.Println("...", mres[0].T, util.Timestamp(mres[0].Time), util.Timestamp(time.Now()))
	fmt.Println(mres, mres[0].Add1)
	//
	tx, _ := db.Begin()
	err = DbQueryS2(tx, &mres, "select * from ttable where tid>?", 1)
	if err != nil {
		t.Error(err.Error())
		return
	}
	tx.Commit()
	fmt.Println("T-->01")
	//
	ivs, err := DbQueryInt(db, "select * from ttable where tid")
	if err != nil {
		t.Error(err.Error())
		return
	}
	if len(ivs) < 1 {
		t.Error("not data")
		return
	}
	_, err = DbQueryI(db, "select count(*) from ttable")
	if err != nil {
		t.Error(err.Error())
		return
	}
	_, err = DbQueryI(db, "select tid from ttable where tid<1")
	if err == nil {
		t.Error("not error")
		return
	}
	_, err = DbQueryI(db, "selects tid from ttable where tid<1")
	if err == nil {
		t.Error("not error")
		return
	}
	fmt.Println("T-->02")
	//
	svs, err := DbQueryString(db, "select tname from ttable")
	if err != nil {
		t.Error(err.Error())
		return
	}
	if len(svs) < 1 {
		t.Error("not data")
		return
	}
	fmt.Println("T-->03")
	//
	tx, _ = db.Begin()
	svs, err = DbQueryString2(tx, "select tname from ttable")
	if err != nil {
		t.Error(err.Error())
		return
	}
	if len(svs) < 1 {
		t.Error("not data")
		return
	}
	tx.Commit()
	//
	iid, err := DbInsert(db, "insert into ttable(tname,titem,tval,status,time) values('name','item','val','N',now())")
	if err != nil {
		t.Error(err.Error())
		return
	}
	fmt.Println(iid)
	fmt.Println("T-->04")
	//
	tx, _ = db.Begin()
	iid2, err := DbInsert2(tx, "insert into ttable(tname,titem,tval,status,time) values('name','item','val','N',now())")
	if err != nil {
		t.Error(err.Error())
		return
	}
	fmt.Println(iid2)
	tx.Commit()
	fmt.Println("T-->05")
	//
	erow, err := DbUpdate(db, "delete from ttable where tid=?", iid)
	if err != nil {
		t.Error(err.Error())
		return
	}
	fmt.Println(erow)
	fmt.Println("T-->06")
	//
	tx, _ = db.Begin()
	erow, err = DbUpdate2(tx, "delete from ttable where tid=?", iid2)
	if err != nil {
		t.Error(err.Error())
		return
	}
	fmt.Println(erow)
	tx.Commit()
	//
	_, err = DbQuery(db, "selectt * from ttable where tid>?", 1)
	if err == nil {
		t.Error("not error")
		return
	}
	_, err = DbQueryInt(db, "selectt * from ttable where tid>?", 1)
	if err == nil {
		t.Error("not error")
		return
	}
	_, err = DbQueryString(db, "selectt * from ttable where tid>?", 1)
	if err == nil {
		t.Error("not error")
		return
	}
	_, err = DbInsert(db, "selectt * from ttable where tid>?", 1)
	if err == nil {
		t.Error("not error")
		return
	}
	tx, _ = db.Begin()
	_, err = DbInsert2(tx, "selectt * from ttable where tid>?", 1)
	if err == nil {
		t.Error("not error")
		return
	}
	tx.Rollback()
	//
	_, err = DbUpdate(db, "selectt * from ttable where tid>?", 1)
	if err == nil {
		t.Error("not error")
		return
	}
	//
	tx, _ = db.Begin()
	_, err = DbUpdate2(tx, "selectt * from ttable where tid>?", 1)
	if err == nil {
		t.Error("not error")
		return
	}
	tx.Rollback()
	//
	_, err = DbQuery(db, "select * from ttable where tid>?", 1, 2)
	if err == nil {
		t.Error("not error")
		return
	}
	_, err = DbQueryInt(db, "select * from ttable where tid>?", 1, 2)
	if err == nil {
		t.Error("not error")
		return
	}
	_, err = DbQueryString(db, "select * from ttable where tid>?", 1, 2)
	if err == nil {
		t.Error("not error")
		return
	}
	_, err = DbInsert(db, "select * from ttable where tid>?", 1, 2)
	if err == nil {
		t.Error("not error")
		return
	}
	//
	tx, _ = db.Begin()
	_, err = DbInsert2(tx, "select * from ttable where tid>?", 1, 2)
	if err == nil {
		t.Error("not error")
		return
	}
	tx.Rollback()
	//
	_, err = DbUpdate(db, "select * from ttable where tid>?", 1, 2)
	if err == nil {
		t.Error("not error")
		return
	}
	//
	tx, _ = db.Begin()
	_, err = DbUpdate2(tx, "select * from ttable where tid>?", 1, 2)
	if err == nil {
		t.Error("not error")
		return
	}
	tx.Rollback()
	//
	err = DbQueryS(nil, nil, "select * from ttable where tid>?", 1)
	if err == nil {
		t.Error("not error")
		return
	}
	DbQueryInt(nil, "select * from ttable where tid>?", 1, 2)
	DbQueryString(nil, "select * from ttable where tid>?", 1, 2)
	DbInsert(nil, "select * from ttable where tid>?", 1, 2)
	DbUpdate(nil, "select * from ttable where tid>?", 1, 2)
	DbInsert2(nil, "select * from ttable where tid>?", 1, 2)
	DbUpdate2(nil, "select * from ttable where tid>?", 1, 2)
	DbQuery2(nil, "select * from ttable where tid>?", 1, 2)
	DbQueryInt2(nil, "select * from ttable where tid>?", 1, 2)
	DbQueryString2(nil, "select * from ttable where tid>?", 1, 2)
	DbQueryS2(nil, nil, "query", 11)
	//
	fmt.Println("------->")
}
Exemple #4
0
func (s *SrvSession) Flush() error {
	s.begin = util.Timestamp(time.Now())
	return nil
}