|
|
package main
|
|
|
|
|
|
import (
|
|
|
"bufio"
|
|
|
"dsTools/Utils/ConfigUtil"
|
|
|
"dsTools/Utils/DbUtil"
|
|
|
"dsTools/Utils/FileUtil"
|
|
|
"fmt"
|
|
|
"io"
|
|
|
"os"
|
|
|
"strings"
|
|
|
)
|
|
|
|
|
|
func main() {
|
|
|
//1、定义参加同步的表有哪些
|
|
|
var syncTables []string
|
|
|
//读取
|
|
|
f, err := os.Open("./Config/syncDataTables.txt")
|
|
|
if err != nil {
|
|
|
fmt.Println("没有找到数据同步的定义表文件syncDataTables.txt!")
|
|
|
return
|
|
|
}
|
|
|
buf := bufio.NewReader(f)
|
|
|
var shouldBreak = false
|
|
|
for {
|
|
|
line, err := buf.ReadString('\n')
|
|
|
if err != nil || io.EOF == err {
|
|
|
shouldBreak = true
|
|
|
}
|
|
|
line = strings.TrimSpace(line)
|
|
|
if strings.Index(line, "#") < 0 && len(line) > 0 {
|
|
|
syncTables = append(syncTables, line)
|
|
|
}
|
|
|
if shouldBreak {
|
|
|
break
|
|
|
}
|
|
|
}
|
|
|
//遍历每一个表
|
|
|
for i := 0; i < len(syncTables); i++ {
|
|
|
tableName:=syncTables[i]
|
|
|
//到mysql中查询
|
|
|
sql := "desc " + tableName
|
|
|
list, err := DbUtil.Engine.QueryInterface(sql)
|
|
|
if err != nil {
|
|
|
fmt.Println(err)
|
|
|
}
|
|
|
var pinSql = "select "
|
|
|
for j := 0; j < len(list); j++ {
|
|
|
c := list[j]
|
|
|
field := string(c["Field"].([]byte))
|
|
|
Type := string(c["Type"].([]byte))
|
|
|
Key := string(c["Key"].([]byte))
|
|
|
if Key == "PRI" {
|
|
|
var pk = "concat('" + ConfigUtil.InstallAreaCode + "','_'," + field + ")"
|
|
|
pinSql += pk + " as id,"
|
|
|
pinSql += field + " as pk,"
|
|
|
} else if Type == "datetime" || Type == "timestamp" { //带时间的日期格式
|
|
|
pinSql += "date_format(" + field + ", '%Y-%m-%d %H:%i:%s') as " + field + ","
|
|
|
} else if Type == "date" { //不带时间的日期格式
|
|
|
pinSql += "date_format(" + field + ", '%Y-%m-%d') as " + field + ","
|
|
|
} else { //普通格式
|
|
|
pinSql += field + ","
|
|
|
}
|
|
|
}
|
|
|
pinSql += "'" + tableName + "' as table_name "
|
|
|
pinSql += " from " +tableName + " where last_updated_time>:sql_last_value"
|
|
|
//输出
|
|
|
p:="SyncSql/"
|
|
|
FileUtil.WriteToFile(p+tableName+".sql", pinSql)
|
|
|
fmt.Println("正在生成"+tableName+"...")
|
|
|
}
|
|
|
fmt.Println("恭喜,所有配置文件成功生成!")
|
|
|
}
|