Exemplo n.º 1
0
func T2(db *sql.DB, t *testing.T) {
	dbutil.DbQuery(db, "select * from ttable where tid>?", 1)
	iid, _ := dbutil.DbInsert(db, "insert into ttable(tname,titem,tval,status,time) values('name','item','val','N',now())")
	dbutil.DbUpdate(db, "delete from ttable where tid=?", iid)

	if tx, err := db.Begin(); err == nil {
		dbutil.DbInsert2(tx, "selectt * from ttable where tid>?", 1)
		tx.Rollback()
	}
	if tx, err := db.Begin(); err == nil {
		dbutil.DbInsert2(tx, "insert into ttable(tname,titem,tval,status,time) values('name','item','val','N',now())")
		tx.Commit()
	}
}
Exemplo n.º 2
0
Arquivo: util.go Projeto: dgdn/goutil
func Insert(o DbModel) (int64, error) {
	dfs := parseToDbfield(o)
	s := bytes.Buffer{}
	s.WriteString("insert into ")
	s.WriteString(o.GetTableName())
	s.WriteString("(")
	s.WriteString(selectColumn(dfs))
	s.WriteString(")values(")
	s.WriteString(placeholders(dfs))
	s.WriteString(")")
	log.D("sql:%v", s.String())
	log.D("args:%v", args(dfs))
	return dbutil.DbInsert(DbConn(), s.String(), args(dfs))
}
Exemplo n.º 3
0
func Register(userName string, mobile string, hobby string, head string, gender int64) (int64, error) {
	user, err := findUserByMobile(mobile)
	if err == nil {
		return user.Uid, errors.New("user is existed")
	}
	id, err := dbutil.DbInsert(Db, "INSERT INTO tb_user (userName,mobile,hobby,head,gender) VALUES (?,?,?,?,?)", userName, mobile, hobby, head, gender)
	PanicErr(err)

	if err != nil {
		return user.Uid, err
	} else {
		user, err := FindUserById(id)
		return user.Uid, err
	}
}
Exemplo n.º 4
0
func ListData() error {
	var ts []TSt
	err := dbutil.DbQueryS(DbCon(), &ts, "select * from ttable where tid>?", 1)
	if err != nil {
		log.D("Err:%v", err.Error())
		return err
	}
	if len(ts) < 1 {
		log.D("Err:%v", "NOT DATA")
		return errors.New("NOT DATA")
	}
	iid, err := dbutil.DbInsert(DbCon(), "insert into ttable(tname,titem,tval,status,time) values('name','item','val','N',now())")
	if err != nil {
		log.D("Err:%v", err.Error())
		return err
	}
	_, err = dbutil.DbUpdate(DbCon(), "delete from ttable where tid=?", iid)
	if err != nil {
		log.D("Err:%v", err.Error())
		return err
	}
	return nil
}
Exemplo n.º 5
0
func T(db *sql.DB, t *testing.T) {
	fmt.Println("----->")
	err := dbutil.DbExecF(db, "ttable.sql")
	if err != nil {
		t.Error(err.Error())
	}
	fmt.Println("abbbb--->")
	res, err := dbutil.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))
	//
	var mres []TSt
	err = dbutil.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)
	//
	ivs, err := dbutil.DbQueryInt(db, "select * from ttable where tid")
	if err != nil {
		t.Error(err.Error())
		return
	}
	if len(ivs) < 1 {
		t.Error("not data")
		return
	}
	//
	svs, err := dbutil.DbQueryString(db, "select tname from ttable")
	if err != nil {
		t.Error(err.Error())
		return
	}
	if len(svs) < 1 {
		t.Error("not data")
		return
	}
	//
	iid, err := dbutil.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)
	//
	tx, _ := db.Begin()
	iid2, err := dbutil.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()
	//
	erow, err := dbutil.DbUpdate(db, "delete from ttable where tid=?", iid)
	if err != nil {
		t.Error(err.Error())
		return
	}
	fmt.Println(erow, "-----")
	//
	tx, _ = db.Begin()
	erow, err = dbutil.DbUpdate2(tx, "delete from ttable where tid=?", iid2)
	if err != nil {
		t.Error(err.Error())
		return
	}
	fmt.Println(erow, "-----")
	tx.Commit()
	//
	_, err = dbutil.DbQuery(db, "selectt * from ttable where tid>?", 1)
	if err == nil {
		t.Error("not error")
		return
	}
	_, err = dbutil.DbQueryInt(db, "selectt * from ttable where tid>?", 1)
	if err == nil {
		t.Error("not error")
		return
	}
	_, err = dbutil.DbQueryString(db, "selectt * from ttable where tid>?", 1)
	if err == nil {
		t.Error("not error")
		return
	}
	_, err = dbutil.DbInsert(db, "selectt * from ttable where tid>?", 1)
	if err == nil {
		t.Error("not error")
		return
	}
	tx, _ = db.Begin()
	_, err = dbutil.DbInsert2(tx, "selectt * from ttable where tid>?", 1)
	if err == nil {
		t.Error("not error")
		return
	}
	tx.Rollback()
	//
	_, err = dbutil.DbUpdate(db, "selectt * from ttable where tid>?", 1)
	if err == nil {
		t.Error("not error")
		return
	}
	//
	tx, _ = db.Begin()
	_, err = dbutil.DbUpdate2(tx, "selectt * from ttable where tid>?", 1)
	if err == nil {
		t.Error("not error")
		return
	}
	tx.Rollback()
	//
	_, err = dbutil.DbQuery(db, "select * from ttable where tid>?", 1, 2)
	if err == nil {
		t.Error("not error")
		return
	}
	_, err = dbutil.DbQueryInt(db, "select * from ttable where tid>?", 1, 2)
	if err == nil {
		t.Error("not error")
		return
	}
	_, err = dbutil.DbQueryString(db, "select * from ttable where tid>?", 1, 2)
	if err == nil {
		t.Error("not error")
		return
	}
	_, err = dbutil.DbInsert(db, "select * from ttable where tid>?", 1, 2)
	if err == nil {
		t.Error("not error")
		return
	}
	//
	tx, _ = db.Begin()
	_, err = dbutil.DbInsert2(tx, "select * from ttable where tid>?", 1, 2)
	if err == nil {
		t.Error("not error")
		return
	}
	tx.Rollback()
	//
	_, err = dbutil.DbUpdate(db, "select * from ttable where tid>?", 1, 2)
	if err == nil {
		t.Error("not error")
		return
	}
	//
	tx, _ = db.Begin()
	_, err = dbutil.DbUpdate2(tx, "select * from ttable where tid>?", 1, 2)
	if err == nil {
		t.Error("not error")
		return
	}
	tx.Rollback()
	//
	err = dbutil.DbQueryS(nil, nil, "select * from ttable where tid>?", 1)
	if err == nil {
		t.Error("not error")
		return
	}
	dbutil.DbQueryInt(nil, "select * from ttable where tid>?", 1, 2)
	dbutil.DbQueryString(nil, "select * from ttable where tid>?", 1, 2)
	dbutil.DbInsert(nil, "select * from ttable where tid>?", 1, 2)
	dbutil.DbUpdate(nil, "select * from ttable where tid>?", 1, 2)
	dbutil.DbInsert2(nil, "select * from ttable where tid>?", 1, 2)
	dbutil.DbUpdate2(nil, "select * from ttable where tid>?", 1, 2)
}