Created
November 12, 2014 15:00
-
-
Save oinume/602623198f8be291f923 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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