go+postgresql伺服器

來源:互聯網
上載者:User

標籤:init   groups   suse   cts   objects   string   分享   query   fail   

  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         return103     }104     if affect != 1 {105         errString := "add group failed! " + errorString(tx.Rollback())106         writeResponse(errString, responseJson, w, http.StatusInternalServerError)107         return108     }109 110     _, err = tx.Exec("commit")111     if err != nil {112         writeResponse(err.Error(), responseJson, w, http.StatusInternalServerError)113         return114     }115     responseJson[jsonKeyResult] = fmt.Sprint(groupID)116     writeResponse("success", responseJson, w, http.StatusOK)117 }
postgresql事務

 

go+postgresql伺服器

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.