1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283 |
- package main
- import (
- _ "github.com/mattn/go-sqlite3"
- "github.com/go-xorm/xorm"
- "os"
- "fmt"
- "github.com/go-xorm/core"
- "github.com/davecgh/go-spew/spew"
- )
- type User struct {
- Id int64
- Name string `xorm:"varchar(25) not null unique 'usr_name'"`
- }
- type Article struct {
- Id int64
- Title string `xorm:"varchar(25) not null unique 'art_title'"`
- }
- func configureOrm(orm *xorm.Engine) {
- // Show SQL statement on standard output
- orm.ShowSQL = true
- // Show debug infomation on standard output
- orm.ShowDebug = true
- // Show error infomation on standard output;
- orm.ShowErr = true
- // Show warnning information on standard output;
- orm.ShowWarn = true
- // TODO Show which info ?
- orm.ShowInfo = true
- f, err := os.Create("sql.log")
- if err != nil {
- println(err.Error())
- return
- }
- orm.Logger = xorm.NewSimpleLogger(f)
- orm.SetMaxIdleConns(1)
- orm.SetMaxOpenConns(10)
- // Set mapper: GonicMapper, SameMapper, SnakeMapper (default).
- orm.SetMapper(core.SnakeMapper{})
- }
- func addData(orm *xorm.Engine) {
- u1 := new(User)
- u1.Name = "FGM";
- x, err := orm.Insert(u1)
- if err != nil {
- fmt.Println(err)
- os.Exit(3)
- }
- spew.Dump(x, u1)
- }
- func check(e error) {
- if e != nil {
- panic(e)
- }
- }
- func main() {
- orm, err := xorm.NewEngine("sqlite3", "./test.db")
- if err != nil {
- panic(err)
- os.Exit(1)
- }
- fmt.Println("ORM loaded")
- configureOrm(orm)
- err = orm.Sync(new(User), new(Article))
- if err != nil {
- panic(err)
- os.Exit(2)
- }
- fmt.Println("ORM synced")
- addData(orm)
- orm.Close()
- }
|