首页 > 代码库 > go+postgresql服务器
go+postgresql服务器
1 func AddGroup(w http.ResponseWriter, req *http.Request) { 2 3 outputReq(req) 4 5 responseJson := map[string]interface{}{jsonKeyMessage: "", jsonKeyResult: nil} 6 7 initResponse(w) 8 err := req.ParseForm() 9 if err != nil { 10 writeResponse(err.Error(), responseJson, w, http.StatusInternalServerError) 11 return 12 } 13 14 nickname := toString(req.Form["nickname"]) 15 delete(req.Form, "nickname") 16 icon := toString(req.Form["icon"]) 17 delete(req.Form, "icon") 18 19 requestJson := initRequestJson(req.Form) 20 customInfo, err := json.Marshal(requestJson) 21 if err != nil { 22 writeResponse(err.Error(), responseJson, w, http.StatusInternalServerError) 23 return 24 } 25 26 db, err := openSql() 27 defer db.Close() 28 if err != nil { 29 writeResponse(err.Error(), responseJson, w, http.StatusInternalServerError) 30 return 31 } 32 33 tx, err := db.Begin() 34 if err != nil { 35 writeResponse(err.Error(), responseJson, w, http.StatusInternalServerError) 36 return 37 } 38 39 stmt, err := tx.Prepare(` 40 insert into objects(nickname,icon,custom,isUser) 41 values ($1,$2,$3,false) returning id 42 `) 43 if err != nil { 44 writeResponse(err.Error(), responseJson, w, http.StatusInternalServerError) 45 return 46 } 47 48 rows, err := stmt.Query(nickname, icon, customInfo) 49 if err != nil { 50 errString := err.Error() + " " + errorString(tx.Rollback()) 51 writeResponse(errString, responseJson, w, http.StatusInternalServerError) 52 return 53 } 54 55 if !rows.Next() { 56 errString := "add group failed! " + errorString(tx.Rollback()) 57 writeResponse(errString, responseJson, w, http.StatusInternalServerError) 58 return 59 } 60 61 var groupID int64 62 err = rows.Scan(&groupID) 63 ///fmt.Println("groupID:", +groupID) 64 if err != nil { 65 errString := err.Error() + " " + errorString(tx.Rollback()) 66 writeResponse(errString, responseJson, w, http.StatusInternalServerError) 67 return 68 } 69 70 err = rows.Close() 71 if err != nil { 72 errString := err.Error() + " " + errorString(tx.Rollback()) 73 writeResponse(errString, responseJson, w, http.StatusInternalServerError) 74 return 75 } 76 err = stmt.Close() 77 if err != nil { 78 errString := err.Error() + " " + errorString(tx.Rollback()) 79 writeResponse(errString, responseJson, w, http.StatusInternalServerError) 80 return 81 } 82 stmt, err = tx.Prepare("insert into groups(id,createTime) values($1,now())") 83 defer stmt.Close() 84 if err != nil { 85 errString := err.Error() + " " + errorString(tx.Rollback()) 86 writeResponse(errString, responseJson, w, http.StatusInternalServerError) 87 return 88 } 89 90 result, err := stmt.Exec(groupID) 91 //rows1, err := tx.Exec("insert into groups(id,createTime) values(‘" + fmt.Sprint(groupID) + "‘,now())") 92 if err != nil { 93 errString := err.Error() + " " + errorString(tx.Rollback()) 94 writeResponse(errString, responseJson, w, http.StatusInternalServerError) 95 return 96 } 97 98 affect, err := result.RowsAffected() 99 if err != nil { 100 errString := err.Error() + " " + errorString(tx.Rollback()) 101 writeResponse(errString, responseJson, w, http.StatusInternalServerError) 102 return 103 } 104 if affect != 1 { 105 errString := "add group failed! " + errorString(tx.Rollback()) 106 writeResponse(errString, responseJson, w, http.StatusInternalServerError) 107 return 108 } 109 110 _, err = tx.Exec("commit") 111 if err != nil { 112 writeResponse(err.Error(), responseJson, w, http.StatusInternalServerError) 113 return 114 } 115 responseJson[jsonKeyResult] = fmt.Sprint(groupID) 116 writeResponse("success", responseJson, w, http.StatusOK) 117 }
go+postgresql服务器
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。