func ChargeBalance(uid int64, money float64) (string, error) { //检查用户是否存在 err := IsUserExist(uid) if err != nil { return "", err } var txerr error txerr = nil t := time.Now() tx, err := Db.Begin() recordTxErr(&txerr, &err) _, err = dbutil.DbUpdate(Db, "UPDATE tb_user SET balance=balance+? WHERE uid=?", money, uid) recordTxErr(&txerr, &err) unixTime := t.Unix() //插入order,生成订单,得oid orderNo := fmt.Sprintf("%02d%2d%02d%4d%2d%2d\n", t.Month(), t.Minute(), t.Day(), t.Year(), t.Second(), t.Hour()) orderNo = orderNo + strconv.FormatInt(unixTime, 10) + strconv.FormatInt(uid, 10) + strconv.FormatInt(0, 10) orderComment := "charge" oid, err := dbutil.DbInsert2(tx, "INSERT INTO tb_order (uid,price,orderTime,orderNo,comment) VALUES (?,?,?,?,?)", uid, money, unixTime, orderNo, orderComment) recordTxErr(&txerr, &err) //创建交易记录 _, err = dbutil.DbInsert2(tx, "INSERT INTO tb_deal (uid,oid,dealTime,dealPrice) VALUES (?,?,?,?)", uid, oid, unixTime, money) recordTxErr(&txerr, &err) if txerr != nil { tx.Rollback() return "", errors.New("ChargeBalance error") } else { tx.Commit() return "success", nil } }
func DeleteUserByMobile(mobile string) (string, error) { i, err := dbutil.DbUpdate(Db, "delete from tb_user where mobile = ?", mobile) PanicErr(err) if err == nil { if i == 0 { return "no such user", errors.New("no such user") } else { return "success", nil } } return "config error", errors.New("config error") }
func DeleteUser(userid int64) (string, error) { i, err := dbutil.DbUpdate(Db, "delete from tb_user where uid = ?", userid) PanicErr(err) if err == nil { if i == 0 { return "no such user", errors.New("no such user") } else { return "success", nil } } return "config error", errors.New("config error") }
func AuthDriver(uid int64) (string, error) { flag, err := dbutil.DbUpdate(Db, "UPDATE tb_user SET isDriver=? WHERE uid=?", true, uid) PanicErr(err) if err != nil { return "database error", err } else { if flag == 0 { return "no such user", nil } else { return "success", nil } } }
func FillInfo(userName string, hobby string, head string, gender int64, uid int64) (string, error) { flag, err := dbutil.DbUpdate(Db, "UPDATE tb_user SET userName=?,hobby=?,head=?,gender=? WHERE uid=?", userName, hobby, head, gender, uid) PanicErr(err) if err != nil { return "database error", err } else { if flag == 0 { return "no such user", nil } else { return "success", nil } } }
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() } }
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 }
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) }