sync.go 2.0 KB

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