func (this *Database) ExecSql(mailbox rpc.Mailbox, msg *rpc.Message) (errcode int32, reply *rpc.Message) { var sqlstr string var callback string var callbackparams share.DBParams if server.Check(server.ParseArgs(msg, &sqlstr, &callback, &callbackparams)) { return 0, nil } sqlconn := db.sql var r sql.Result var err error app := server.GetAppById(mailbox.App) if app == nil { log.LogError(server.ErrAppNotFound) return 0, nil } if r, err = sqlconn.Exec(sqlstr); err != nil { log.LogError("sql:", sqlstr) server.Check(app.Call(nil, callback, callbackparams, 0, err.Error())) return 0, nil } eff, _ := r.RowsAffected() server.Check(app.Call(nil, callback, callbackparams, eff, "")) return 0, nil }
func (this *Database) RecvLetter(mailbox rpc.Mailbox, msg *rpc.Message) (errcode int32, reply *rpc.Message) { var uid uint64 var serial_no uint64 var callback string var callbackparams share.DBParams if server.Check(server.ParseArgs(msg, &uid, &serial_no, &callback, &callbackparams)) { return 0, nil } letter, err := this.recvLetterBySerial(uid, serial_no) if err != nil { log.LogError(err) return share.ERR_REPLY_FAILED, nil } if callback == "_" { return 0, nil } callbackparams["result"] = false if letter != nil { callbackparams["result"] = true callbackparams["letter"] = letter } server.Check(server.MailTo(nil, &mailbox, callback, callbackparams)) return 0, nil }
func (this *Database) QuerySql(mailbox rpc.Mailbox, msg *rpc.Message) (errcode int32, reply *rpc.Message) { var sqlstr string var callback string var callbackparams share.DBParams if server.Check(server.ParseArgs(msg, &sqlstr, &callback, &callbackparams)) { return 0, nil } sqlconn := db.sql var r *sql.Rows var err error app := server.GetAppById(mailbox.App) if app == nil { log.LogError(server.ErrAppNotFound) return 0, nil } if r, err = sqlconn.Query(sqlstr); err != nil { log.LogError(err) return 0, nil } defer r.Close() var cols []string cols, err = r.Columns() if err != nil { server.Check(app.Call(nil, callback, callbackparams, []share.DBRow{share.DBRow{"error": []byte(err.Error())}})) return 0, nil } result := make([]interface{}, len(cols)) for k := range result { result[k] = new([]byte) } arrresult := make([]share.DBRow, 0, 100) for r.Next() { err = r.Scan(result...) if err != nil { log.LogError("sql:", sqlstr) server.Check(app.Call(nil, callback, callbackparams, []share.DBRow{share.DBRow{"error": []byte(err.Error())}})) return 0, nil } mapresult := make(share.DBRow, len(cols)) for k, v := range cols { mapresult[v] = *result[k].(*[]byte) } arrresult = append(arrresult, mapresult) if len(arrresult) == 100 { break } } server.Check(app.Call(nil, callback, callbackparams, arrresult)) return 0, nil }
func (this *Database) UpdateRow(mailbox rpc.Mailbox, msg *rpc.Message) (errcode int32, reply *rpc.Message) { var tbl string var values map[string]interface{} var condition string var callback string var callbackparams share.DBParams if server.Check(server.ParseArgs(msg, &tbl, &values, &condition, &callback, &callbackparams)) { return 0, nil } sqlconn := db.sql var r sql.Result var err error app := server.GetAppById(mailbox.App) if app == nil { log.LogError(server.ErrAppNotFound) return 0, nil } if condition != "" { condition = fmt.Sprintf("WHERE %s", condition) } args := make([]interface{}, 0, len(values)) var sql bytes.Buffer sql.WriteString("UPDATE `") sql.WriteString(tbl) sql.WriteString("` SET") split := " " for k, v := range values { sql.WriteString(split) split = ", " sql.WriteString("`") sql.WriteString(k) sql.WriteString("`") sql.WriteString("=?") args = append(args, v) } sqlstr := fmt.Sprintf("%s %s", sql.String(), condition) if r, err = sqlconn.Exec(sqlstr, args...); err != nil { log.LogError("sql:", sqlstr) if callback == "_" { log.LogError(err) return 0, nil } server.Check(app.Call(nil, callback, callbackparams, 0, err.Error())) return 0, nil } if callback == "_" { return 0, nil } eff, _ := r.RowsAffected() server.Check(app.Call(nil, callback, callbackparams, eff, "")) return 0, nil }
func (this *Database) QueryLetter(mailbox rpc.Mailbox, msg *rpc.Message) (errcode int32, reply *rpc.Message) { var uid uint64 var callback string var callbackparams share.DBParams if server.Check(server.ParseArgs(msg, &uid, &callback, &callbackparams)) { return 0, nil } count, _ := this.queryLetter(uid) callbackparams["count"] = count server.Check(server.MailTo(nil, &mailbox, callback, callbackparams)) return 0, nil }
func (this *Database) SendSystemLetter(mailbox rpc.Mailbox, msg *rpc.Message) (errcode int32, reply *rpc.Message) { var source_name string var recvacc, recvrole string var typ int32 var title, content, appendix string var callback string var callbackparams share.DBParams if server.Check(server.ParseArgs(msg, &source_name, &recvacc, &recvrole, &typ, &title, &content, &appendix, &callback, &callbackparams)) { return 0, nil } var roleid uint64 if recvacc == "" && recvrole == "" { roleid = 0 } else { var err error roleid, err = GetRoleUid(recvacc, recvrole) if err != nil { callbackparams["result"] = false callbackparams["err"] = "role not found" if callback != "_" { server.Check(server.MailTo(nil, &mailbox, callback, callbackparams)) } log.LogError(err) return 0, nil } } serial_no, err := this.systemLetter(source_name, roleid, recvacc, recvrole, typ, title, content, appendix) if err != nil { if callback != "_" { callbackparams["result"] = false callbackparams["err"] = err.Error() server.Check(server.MailTo(nil, &mailbox, callback, callbackparams)) } log.LogError(err) return 0, nil } if callback != "_" { callbackparams["result"] = true callbackparams["serial_no"] = serial_no server.Check(server.MailTo(nil, &mailbox, callback, callbackparams)) } return 0, nil }
func (this *Database) SaveObject(mailbox rpc.Mailbox, msg *rpc.Message) (errcode int32, reply *rpc.Message) { var object share.DbSave var callbackparams share.DBParams if server.Check(server.ParseArgs(msg, &object, &callbackparams)) { return share.ERR_ARGS_ERROR, nil } reply = server.CreateMessage(callbackparams) sqlconn := db.sql err := SaveItem(sqlconn, true, object.Data.DBId, object.Data) if err != nil { return share.ERR_REPLY_FAILED, reply } return share.ERR_REPLY_SUCCEED, reply }
func (this *Database) Log(mailbox rpc.Mailbox, msg *rpc.Message) (errcode int32, reply *rpc.Message) { var log_name string var log_source, log_type int32 var log_content, log_comment string if server.Check(server.ParseArgs(msg, &log_name, &log_source, &log_type, &log_content, &log_comment)) { return 0, nil } sqlconn := db.sql uid, err := sqlconn.GetUid("serial_no") if err != nil { log.LogError(err) return 0, nil } sql := fmt.Sprintf("INSERT INTO `log_data`(`serial_no`, `log_time`,`log_name`, `log_source`, `log_type`, `log_content`, `log_comment`) VALUES(?,?,?,?,?,?,?)") server.Check2(sqlconn.Exec(sql, uid, time.Now().Format(util.TIME_LAYOUT), log_name, log_source, log_type, log_content, log_comment)) return 0, nil }
func (this *Database) Count(mailbox rpc.Mailbox, msg *rpc.Message) (errcode int32, reply *rpc.Message) { var tbl string var condition string var callback string var callbackparams share.DBParams if server.Check(server.ParseArgs(msg, &tbl, &condition, &callback, &callbackparams)) { return 0, nil } sqlconn := db.sql var r *sql.Rows var err error app := server.GetAppById(mailbox.App) if app == nil { log.LogError(server.ErrAppNotFound) } if condition != "" { condition = fmt.Sprintf("WHERE %s", condition) } sqlstr := fmt.Sprintf("SELECT COUNT(*) FROM `%s` %s LIMIT 1", tbl, condition) if r, err = sqlconn.Query(sqlstr); err != nil { log.LogError("sql:", sqlstr) return 0, nil } defer r.Close() if !r.Next() { server.Check(app.Call(nil, callback, -1)) return 0, nil } var count int32 if err = r.Scan(&count); err != nil { log.LogError(err) server.Check(app.Call(nil, callback, -1)) return 0, nil } server.Check(app.Call(nil, callback, callbackparams, count)) return 0, nil }
func (this *Database) DeleteObject(mailbox rpc.Mailbox, msg *rpc.Message) (errcode int32, reply *rpc.Message) { var ent string var dbid uint64 var callbackparams share.DBParams var err error if server.Check(server.ParseArgs(msg, &ent, &dbid, &callbackparams)) { return share.ERR_ARGS_ERROR, nil } reply = server.CreateMessage(callbackparams) sqlconn := db.sql sqlstr := fmt.Sprintf("DELETE FROM `tbl_%s` WHERE `id`=?", strings.ToLower(ent)) if _, err = sqlconn.Exec(sqlstr, dbid); err != nil { log.LogError("sql:", sqlstr) return share.ERR_REPLY_FAILED, reply } return share.ERR_REPLY_SUCCEED, reply }
func (this *Database) DeleteRow(mailbox rpc.Mailbox, msg *rpc.Message) (errcode int32, reply *rpc.Message) { var tbl string var condition string var callback string var callbackparams share.DBParams if server.Check(server.ParseArgs(msg, &tbl, &condition, &callback, &callbackparams)) { return 0, nil } sqlconn := db.sql var r sql.Result var err error app := server.GetAppById(mailbox.App) if app == nil { log.LogError(server.ErrAppNotFound) return 0, nil } if condition != "" { condition = fmt.Sprintf(" WHERE %s", condition) } sqlstr := fmt.Sprintf("DELETE FROM `%s`%s", tbl, condition) if r, err = sqlconn.Exec(sqlstr); err != nil { log.LogError("sql:", sqlstr) if callback != "_" { server.Check(app.Call(nil, callback, callbackparams, 0, err.Error())) return 0, nil } } if callback == "_" { return 0, nil } eff, _ := r.RowsAffected() server.Check(app.Call(nil, callback, callbackparams, eff, "")) return 0, nil }
func (this *Database) LoadObject(mailbox rpc.Mailbox, msg *rpc.Message) (errcode int32, reply *rpc.Message) { var ent string var dbid uint64 var callbackparams share.DBParams var err error if server.Check(server.ParseArgs(msg, &ent, &dbid, &callbackparams)) { return share.ERR_ARGS_ERROR, nil } reply = server.CreateMessage(callbackparams) sqlconn := db.sql savedata := share.DbSave{} savedata.Data, err = LoadEntity(sqlconn, dbid, ent, 0) if err != nil { return share.ERR_REPLY_FAILED, reply } server.AppendArgs(reply, savedata) return share.ERR_REPLY_SUCCEED, reply }
func (this *Database) LookLetter(mailbox rpc.Mailbox, msg *rpc.Message) (errcode int32, reply *rpc.Message) { var uid uint64 var callback string var callbackparams share.DBParams if server.Check(server.ParseArgs(msg, &uid, &callback, &callbackparams)) { return 0, nil } letters, err := this.lookLetter(uid) if err != nil { log.LogError(err) return 0, nil } callbackparams["result"] = false if letters != nil { callbackparams["result"] = true callbackparams["letters"] = letters } server.Check(server.MailTo(nil, &mailbox, callback, callbackparams)) return 0, nil }
func (this *Database) QueryRows(mailbox rpc.Mailbox, msg *rpc.Message) (errcode int32, reply *rpc.Message) { var tbl string var condition string var orderby string var index, count int32 var callback string var callbackparams share.DBParams if server.Check(server.ParseArgs(msg, &tbl, &condition, &orderby, &index, &count, &callback, &callbackparams)) { return 0, nil } sqlconn := db.sql var r *sql.Rows var err error if count <= 0 { log.LogError("count must above zero") return 0, nil } app := server.GetAppById(mailbox.App) if app == nil { log.LogError(server.ErrAppNotFound) return 0, nil } if condition != "" { condition = fmt.Sprintf(" WHERE %s", condition) } if orderby != "" { orderby = fmt.Sprintf(" ORDER BY %s", orderby) } sqlstr := fmt.Sprintf("SELECT * FROM `%s`%s%s LIMIT %d, %d", tbl, condition, orderby, index, count) if r, err = sqlconn.Query(sqlstr); err != nil { log.LogError(err) return 0, nil } defer r.Close() var cols []string cols, err = r.Columns() if err != nil { server.Check(app.Call(nil, callback, callbackparams, []share.DBRow{share.DBRow{"error": []byte(err.Error())}}, index, count)) return 0, nil } result := make([]interface{}, len(cols)) for k := range result { result[k] = new([]byte) } arrresult := make([]share.DBRow, 0, count) for r.Next() { err = r.Scan(result...) if err != nil { log.LogError("sql:", sqlstr) server.Check(app.Call(nil, callback, callbackparams, []share.DBRow{share.DBRow{"error": []byte(err.Error())}}, index, count)) return 0, nil } mapresult := make(share.DBRow, len(cols)) for k, v := range cols { mapresult[v] = *result[k].(*[]byte) } arrresult = append(arrresult, mapresult) } server.Check(app.Call(nil, callback, callbackparams, arrresult, index, count)) return 0, nil }
func (this *Database) InsertRows(mailbox rpc.Mailbox, msg *rpc.Message) (errcode int32, reply *rpc.Message) { var tbl string var keys []string var values []interface{} var callback string var callbackparams share.DBParams if server.Check(server.ParseArgs(msg, &tbl, &keys, &values, &callback, &callbackparams)) { return 0, nil } sqlconn := db.sql var r sql.Result var err error app := server.GetAppById(mailbox.App) if app == nil { log.LogError(server.ErrAppNotFound) return 0, nil } var sql bytes.Buffer sql.WriteString("INSERT INTO `") sql.WriteString(tbl) sql.WriteString("`(") split := " " for _, v := range keys { sql.WriteString(split) split = ", " sql.WriteString("`") sql.WriteString(v) sql.WriteString("`") } sql.WriteString(") VALUES") split = " " if len(values)%len(keys) != 0 { log.LogError("length is not match") return 0, nil } rows := len(values) / len(keys) for j := 0; j < rows; j++ { sql.WriteString(split) sql.WriteString("(") split = " " for i := 0; i < len(keys); i++ { sql.WriteString(split) sql.WriteString("?") split = ", " } split = ", " sql.WriteString(")") } if r, err = sqlconn.Exec(sql.String(), values...); err != nil { log.LogError("sql:", sql.String()) if callback == "_" { log.LogError(err) return 0, nil } server.Check(app.Call(nil, callback, callbackparams, 0, err.Error())) return 0, nil } if callback == "_" { return 0, nil } eff, _ := r.RowsAffected() server.Check(app.Call(nil, callback, callbackparams, eff, "")) return 0, nil }