Skip to content

Instantly share code, notes, and snippets.

@oinume
Created November 12, 2014 15:00
Show Gist options
  • Save oinume/602623198f8be291f923 to your computer and use it in GitHub Desktop.
Save oinume/602623198f8be291f923 to your computer and use it in GitHub Desktop.
package main
import (
"errors"
"fmt"
"os"
_ "github.com/go-sql-driver/mysql"
"github.com/go-xorm/xorm"
_ "github.com/mattn/go-sqlite3"
)
type Status struct {
Name string
Color string
}
var (
Registed Status = Status{"Registed", "white"}
Approved Status = Status{"Approved", "green"}
Removed Status = Status{"Removed", "red"}
Statuses map[string]Status = map[string]Status{
Registed.Name: Registed,
Approved.Name: Approved,
Removed.Name: Removed,
}
)
func (s *Status) FromDB(bytes []byte) error {
if r, ok := Statuses[string(bytes)]; ok {
*s = r
return nil
} else {
return errors.New("no this data")
}
}
func (s *Status) ToDB() ([]byte, error) {
return []byte(s.Name), nil
}
type User struct {
Id int64
Name string
Status Status `xorm:"varchar(40)"`
}
func main() {
f := "conversion.db"
os.Remove(f)
//Orm, err := xorm.NewEngine("mysql", "root:@/xorm?charset=utf8")
Orm, err := xorm.NewEngine("sqlite3", f)
if err != nil {
fmt.Println(err)
return
}
Orm.ShowSQL = true
err = Orm.CreateTables(&User{})
if err != nil {
fmt.Println(err)
return
}
Orm.Exec("TRUNCATE TABLE user")
_, err = Orm.Insert(&User{1, "xlw", Registed})
if err != nil {
fmt.Println(err)
return
}
// users := make([]User, 0)
// err = Orm.Where("status = ?", "Registed").Find(&users)
// if err != nil {
// fmt.Println(err)
// return
// }
//
// fmt.Println(users)
//
user := User{}
exist, err := Orm.Id(1).Get(&user)
if err != nil {
fmt.Println(err)
return
}
if exist {
fmt.Println(user)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment