github.com/insionng/yougam@v0.0.0-20170714101924-2bc18d833463/libraries/go-xorm/xorm/examples/sync.go (about)

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  
     6  	_ "github.com/go-sql-driver/mysql"
     7  	"github.com/insionng/yougam/libraries/xorm"
     8  	_ "github.com/lib/pq"
     9  	_ "github.com/mattn/go-sqlite3"
    10  )
    11  
    12  type SyncUser2 struct {
    13  	Id      int64
    14  	Name    string `xorm:"unique"`
    15  	Age     int    `xorm:"index"`
    16  	Title   string
    17  	Address string
    18  	Genre   string
    19  	Area    string
    20  	Date    int
    21  }
    22  
    23  type SyncLoginInfo2 struct {
    24  	Id       int64
    25  	IP       string `xorm:"index"`
    26  	UserId   int64
    27  	AddedCol int
    28  	// timestamp should be updated by database, so only allow get from db
    29  	TimeStamp string
    30  	// assume
    31  	Nonuse int    `xorm:"unique"`
    32  	Newa   string `xorm:"index"`
    33  }
    34  
    35  func sync(engine *xorm.Engine) error {
    36  	return engine.Sync(&SyncLoginInfo2{}, &SyncUser2{})
    37  }
    38  
    39  func sqliteEngine() (*xorm.Engine, error) {
    40  	f := "sync.db"
    41  	//os.Remove(f)
    42  
    43  	return xorm.NewEngine("sqlite3", f)
    44  }
    45  
    46  func mysqlEngine() (*xorm.Engine, error) {
    47  	return xorm.NewEngine("mysql", "root:@/test?charset=utf8")
    48  }
    49  
    50  func postgresEngine() (*xorm.Engine, error) {
    51  	return xorm.NewEngine("postgres", "dbname=xorm_test sslmode=disable")
    52  }
    53  
    54  type engineFunc func() (*xorm.Engine, error)
    55  
    56  func main() {
    57  	//engines := []engineFunc{sqliteEngine, mysqlEngine, postgresEngine}
    58  	//engines := []engineFunc{sqliteEngine}
    59  	//engines := []engineFunc{mysqlEngine}
    60  	engines := []engineFunc{postgresEngine}
    61  	for _, enginefunc := range engines {
    62  		Orm, err := enginefunc()
    63  		fmt.Println("--------", Orm.DriverName(), "----------")
    64  		if err != nil {
    65  			fmt.Println(err)
    66  			return
    67  		}
    68  		Orm.ShowSQL(true)
    69  		err = sync(Orm)
    70  		if err != nil {
    71  			fmt.Println(err)
    72  		}
    73  
    74  		_, err = Orm.Where("id > 0").Delete(&SyncUser2{})
    75  		if err != nil {
    76  			fmt.Println(err)
    77  		}
    78  
    79  		user := &SyncUser2{
    80  			Name:    "testsdf",
    81  			Age:     15,
    82  			Title:   "newsfds",
    83  			Address: "fasfdsafdsaf",
    84  			Genre:   "fsafd",
    85  			Area:    "fafdsafd",
    86  			Date:    1000,
    87  		}
    88  		_, err = Orm.Insert(user)
    89  		if err != nil {
    90  			fmt.Println(err)
    91  			return
    92  		}
    93  
    94  		isexist, err := Orm.IsTableExist("sync_user2")
    95  		if err != nil {
    96  			fmt.Println(err)
    97  			return
    98  		}
    99  		if !isexist {
   100  			fmt.Println("sync_user2 is not exist")
   101  			return
   102  		}
   103  	}
   104  }