func (this *User) InsertUser() (int, error) { o := orm.NewOrm() id, err := o.Insert(this) if err != nil { this.Id = int(id) } return int(id), err }
func GetUserByUsernmae(username string) (*User, int) { o := orm.NewOrm() var user User num, err := o.QueryTable("user").Filter("username", username).All(&user) if err != nil { panic(err) } return &user, int(num) }
func ReadUserByName(name string) *User { o := orm.NewOrm() user := User{Username: name} err := o.Read(&user, "Username") if err == orm.ErrNoRows { return nil } return &user }
func InsertIpByuser(ip []string, user *User) error { ips := make([]Ip, len(ip)) for index, val := range ip { ips[index] = Ip{User: user, Ip: val} } o := orm.NewOrm() _, err := o.InsertMulti(1, ips) return err }
func ReadUserById(id int) *User { o := orm.NewOrm() user := User{Id: id} err := o.Read(&user) _, err = o.LoadRelated(&user, "Ips") if err == orm.ErrNoRows { return nil } return &user }
func DeleteIpByuser(ip []string) error { o := orm.NewOrm() var err error for _, val := range ip { _, err = o.QueryTable("ip").Filter("ip", val).Delete() if err != nil { return err } } return nil }
// execute the sql already add in the sql func (m *Migration) Exec(name, status string) error { o := orm.NewOrm() for _, s := range m.sqls { beego.Info("exec sql:", s) r := o.Raw(s) _, err := r.Exec() if err != nil { return err } } return m.addOrUpdateRecord(name, status) }
func isRollBack(name string) bool { o := orm.NewOrm() var maps []orm.Params num, err := o.Raw("select * from migrations where `name` = ? order by id_migration desc", name).Values(&maps) if err != nil { beego.Info("get name has error", err) return false } if num <= 0 { return false } if maps[0]["status"] == "rollback" { return true } return false }
func (m *Migration) addOrUpdateRecord(name, status string) error { o := orm.NewOrm() if status == "down" { status = "rollback" p, err := o.Raw("update migrations set `status` = ?, `rollback_statements` = ?, `created_at` = ? where name = ?").Prepare() if err != nil { return nil } _, err = p.Exec(status, strings.Join(m.sqls, "; "), time.Now().Format(M_DB_DATE_FORMAT), name) return err } else { status = "update" p, err := o.Raw("insert into migrations(`name`, `created_at`, `statements`, `status`) values(?,?,?,?)").Prepare() if err != nil { return err } _, err = p.Exec(name, time.Now().Format(M_DB_DATE_FORMAT), strings.Join(m.sqls, "; "), status) return err } }
func (this *User) Update() error { o := orm.NewOrm() _, err := o.Update(this) return err }