基于serf的数据库同步模块库
liuxiaolong
2019-08-14 d19fdcd3f11e4e092957880bc9f81b8d0429d0a2
dbself.go
@@ -135,10 +135,16 @@
   Pk int `json:"pk"`
}
type DumpSql struct {
   Sql string `json:"sql"`
}
func ExecuteQueryByGorm(tableNames []string) ([]string, error) {
   if tableNames !=nil {
      var arr []string
      var dumpSql []DumpSql
      for _,table :=range tableNames {
         dumpSql = make([]DumpSql, 0)
         var tDescArr []TableDesc
         tSql := fmt.Sprintf(`PRAGMA table_info("%s")`, table)
         err := localDb.Raw(tSql).Scan(&tDescArr).Error
@@ -153,16 +159,23 @@
         for _,col :=range tDescArr {
            columnNames = append(columnNames, fmt.Sprintf(`'||quote("%s")||'`, col.Name))
         }
         tSql = fmt.Sprintf(`SELECT 'INSERT INTO "%s" VALUES(%s)' FROM "%s";`,
         tSql = fmt.Sprintf(`SELECT 'INSERT INTO "%s" VALUES(%s)' as sql FROM "%s";`,
            table,
            strings.Join(columnNames, ","),
            table)
         var dumpSqls []string
         err = localDb.Raw(tSql).Scan(&dumpSqls).Error
         fmt.Println("tSql:",tSql)
         err = localDb.Raw(tSql).Scan(&dumpSql).Error
         if err !=nil {
            return nil,errors.New("dump err")
            continue
         }
         arr = append(arr, dumpSqls...)
         if len(dumpSql)>0 {
            for _,d :=range dumpSql {
               arr = append(arr, d.Sql)
            }
         }
      }
      return arr,nil
   }