1Func addgroup (w http. Responsewriter, req*http. Request) {2 3 outputreq (req)4 5Responsejson:=Map[string]interface{}{jsonkeymessage: "", Jsonkeyresult:nil}6 7 Initresponse (W)8Err:=req. Parseform ()9 ifErr!=Nil {Ten Writeresponse (Err. Error (), Responsejson, W, http. Statusinternalservererror) One return A } - -Nickname:=ToString (req. Form["nickname"]) the Delete(req. Form, "nickname") -Icon:=ToString (req. Form["icon"]) - Delete(req. Form, "icon") - +Requestjson:=Initrequestjson (req. Form) -Custominfo, err:=JSON. Marshal (Requestjson) + ifErr!=Nil { A Writeresponse (Err. Error (), Responsejson, W, http. Statusinternalservererror) at return - } - -DB, Err:=Opensql () -Defer db.Close() - ifErr!=Nil { in Writeresponse (Err. Error (), Responsejson, W, http. Statusinternalservererror) - return to } + -TX, ERR:=Db.Begin() the ifErr!=Nil { * Writeresponse (Err. Error (), Responsejson, W, http. Statusinternalservererror) $ returnPanax Notoginseng } - thestmt, err:=Tx.Prepare(` + Insert intoobjects (Nickname,icon,custom,isuser) A Values($1,$2,$3, false) returning ID the `) + ifErr!=Nil { - Writeresponse (Err. Error (), Responsejson, W, http. Statusinternalservererror) $ return $ } - -Rows, err:=stmt. Query (nickname, Icon, Custominfo) the ifErr!=Nil { -Errstring:=Err. Error ()+" "+ErrorString (TX.Rollback())Wuyi writeresponse (errstring, Responsejson, W, http. Statusinternalservererror) the return - } Wu - if!rows.Next() { AboutErrstring:="Add Groupfailed! "+ErrorString (TX.Rollback()) $ writeresponse (errstring, Responsejson, W, http. Statusinternalservererror) - return - } - A varGroupID Int64 +Err=Rows. Scan (&GroupID) the ///Fmt. Println ("GroupID:",+GroupID) - ifErr!=Nil { $Errstring:=Err. Error ()+" "+ErrorString (TX.Rollback()) the writeresponse (errstring, Responsejson, W, http. Statusinternalservererror) the return the } the -Err=Rows.Close() in ifErr!=Nil { theErrstring:=Err. Error ()+" "+ErrorString (TX.Rollback()) the writeresponse (errstring, Responsejson, W, http. Statusinternalservererror) About return the } theErr=stmt.Close() the ifErr!=Nil { +Errstring:=Err. Error ()+" "+ErrorString (TX.Rollback()) - writeresponse (errstring, Responsejson, W, http. Statusinternalservererror) the returnBayi } thestmt, err=Tx.Prepare("Insert intoGroups (Id,createtime)Values($1, now ()) ") theDefer stmt.Close() - ifErr!=Nil { -Errstring:=Err. Error ()+" "+ErrorString (TX.Rollback()) the writeresponse (errstring, Responsejson, W, http. Statusinternalservererror) the return the } the -result, err:=stmt.Exec(GroupID) the //ROWS1, err:=Tx.Exec("Insert intoGroups (Id,createtime)Values('"+ FMT. Sprint (GroupID) + "', now ()) ") the ifErr!=Nil { theErrstring:=Err. Error ()+" "+ErrorString (TX.Rollback())94 writeresponse (errstring, Responsejson, W, http. Statusinternalservererror) the return the } the 98Affect, err:=result. Rowsaffected () About ifErr!=Nil { -Errstring:=Err. Error ()+" "+ErrorString (TX.Rollback())101 writeresponse (errstring, Responsejson, W, http. Statusinternalservererror)102 return103 }104 ifAffect!= 1 { theErrstring:="Add Groupfailed! "+ErrorString (TX.Rollback())106 writeresponse (errstring, Responsejson, W, http. Statusinternalservererror)107 return108 }109 the_, Err=Tx.Exec("Commit")111 ifErr!=Nil { the Writeresponse (Err. Error (), Responsejson, W, http. Statusinternalservererror)113 return the } theResponsejson[Jsonkeyresult] =FMT. Sprint (GroupID) the Writeresponse ("Success", Responsejson, W, http. Statusok)117}
PostgreSQL Transaction
Go+postgresql Server