//插入 func (this *Application) Insert(name string) *Result { db := mymysql.Conn() stmt, err := db.Prepare("insert into application(name, token)values(?,?)") if err != nil { return NewErrorResult(nil) } defer stmt.Close() token := strings.Replace(uuid.NewV4().String(), "-", "", -1) if _, err := stmt.Exec(name, token); err != nil { errRes := NewErrorResult(nil) if e, ok := err.(*mysql.MySQLError); ok { //重复主键 errRes.SetObject(e.Error()) if e.Number == 1062 { errRes.SetCode(mymysql.ErrDupRows) } else { errRes.SetCode(mymysql.ErrDatabase) } } else { errRes.SetCode(mymysql.ErrDatabase) } return errRes } this.Token = token return &Result{ Code: RESULT_CODE_SUCCESS, Object: this, Success: true, } }
func (this *Application) DeleteById(id int64) *Result { db := mymysql.Conn() st, err := db.Prepare("DELETE FROM roles WHERE id = ?") if err != nil { return NewErrorResult(err.Error()) } defer st.Close() result, err := st.Exec(id) if err != nil { return NewErrorResult(err.Error()) } num, _ := result.RowsAffected() if num > 0 { return &Result{ Code: RESULT_CODE_SUCCESS, Object: num, Success: true, } } else { return NewErrorResult(err.Error()) } }
//根据应用名称查询 func (this *Application) FindByName(name string) *Result { db := mymysql.Conn() stmt, err := db.Prepare("select id, name, token from application where name=?") if err != nil { return NewErrorResult(nil) } defer stmt.Close() row := stmt.QueryRow(name) var tmpId sql.NullInt64 var tmpName sql.NullString var tmpToken sql.NullString if err := row.Scan(&tmpId, &tmpName, &tmpToken); err != nil { errRes := NewErrorResult(err.Error()) if err == sql.ErrNoRows { errRes.SetCode(mymysql.ErrNotFound) } else { errRes.SetCode(mymysql.ErrDatabase) } return errRes } if tmpId.Valid && tmpId.Int64 != 0 { if tmpId.Valid { this.Id = tmpId.Int64 } if tmpName.Valid { this.Name = tmpName.String } if tmpToken.Valid { this.Token = tmpToken.String } return NewResult(this) } else { return NewResult(nil) } }
func (this *Application) UpdateByName(name string) *Result { db := mymysql.Conn() stmt, err := db.Prepare("update application set token=? where name=?") if err != nil { return NewErrorResult(nil) } defer stmt.Close() token := strings.Replace(uuid.NewV4().String(), "-", "", -1) result, err := stmt.Exec(token, name) if err != nil { return NewErrorResult(err.Error()) } num, err := result.RowsAffected() if num > 0 { return &Result{ Code: RESULT_CODE_SUCCESS, Object: token, Success: true, } } else { return NewErrorResult(err.Error()) } }
//根据查询条件获取数据 func (this *Application) GetAll(queryVal map[string]string, limit int64, offset int64) (apps []Application, err error) { sqlStr := "select id,name,token from application" if len(queryVal) > 0 { sqlStr += " where " first := true for k, v := range queryVal { if !first { sqlStr += " AND " } else { first = false } sqlStr += k sqlStr += " = " sqlStr += "'" sqlStr += v sqlStr += "'" } } sqlStr += " LIMIT " + fmt.Sprintf("%d", limit) if offset > 0 { sqlStr += " OFFSET " + fmt.Sprintf("%d", offset) } db := mymysql.Conn() st, err := db.Prepare(sqlStr) if err != nil { return nil, err } defer st.Close() rows, err := st.Query() if err != nil { return nil, err } defer rows.Close() apps = make([]Application, 0, limit) for rows.Next() { var tmpId sql.NullInt64 var tmpName sql.NullString var tmpToken sql.NullString if err := rows.Scan(&tmpId, &tmpName, &tmpToken); err != nil { return nil, err } r := Application{} if tmpId.Valid { r.Id = tmpId.Int64 } if tmpName.Valid { r.Name = tmpName.String } if tmpToken.Valid { r.Token = tmpToken.String } apps = append(apps, r) } if err := rows.Err(); err != nil { return nil, err } return apps, nil }