You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

73 lines
2.2 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

package main
import (
"dsSupport/Utils/CommonUtil"
"dsSupport/Utils/DbUtil"
"dsSupport/Utils/PgUtil"
"fmt"
)
var db = DbUtil.Engine
var pgDb = PgUtil.Engine
func main() {
//1、读取每一张需要创建的表
sql := "select * from t_dw_table"
list, _ := db.SQL(sql).Query().List()
for i := range list {
tableId := list[i]["table_id"].(int64)
tableName := list[i]["table_name"].(string)
createTableSql := `DROP TABLE IF EXISTS "public"."` + tableName + `";` + "\r\n"
pgDb.SQL(createTableSql).Execute()
createTableSql = `CREATE TABLE "public"."` + tableName + `" (` + "\r\n"
//2、根据表名获取相应的创建表的信息
sql = `select * from t_dw_table_field where table_id=?`
list2, _ := db.SQL(sql, tableId).Query().List()
//手动增加两个字段uuid+enable_flag
_map := make(map[string]interface{}, 0)
_map["data_type"] = "char"
_map["field_length"] = CommonUtil.ConvertIntToInt64(36)
_map["field_name"] = "uuid"
list2 = append(list2, _map)
_map2 := make(map[string]interface{}, 0)
_map2["data_type"] = "int"
_map2["field_length"] = CommonUtil.ConvertIntToInt64(4)
_map2["field_name"] = "enable_flag"
list2 = append(list2, _map2)
for i2 := range list2 {
dataType := list2[i2]["data_type"].(string)
fieldLength := list2[i2]["field_length"].(int64)
fieldName := list2[i2]["field_name"].(string)
var c = ""
if dataType == "int" {
c = "int" + CommonUtil.ConvertInt64ToString(fieldLength)
} else {
c = dataType + "(" + CommonUtil.ConvertInt64ToString(fieldLength) + ") " + ` COLLATE "pg_catalog"."default"`
}
c += ","
createTableSql += `"` + fieldName + `" ` + c + "\r\n"
}
createTableSql = createTableSql[0 : len(createTableSql)-3]
createTableSql += `);` + "\r\n"
_, err := pgDb.SQL(createTableSql).Execute()
fmt.Println(createTableSql)
if err != nil {
panic(err.Error())
}
//主键
var pks = "uuid"
//添加主键描述
createTableSql = `ALTER TABLE "public"."` + tableName + `" ADD CONSTRAINT "` + tableName + `_dw_pkey" PRIMARY KEY ("` + pks + `");` + "\r\n"
_, err = pgDb.SQL(createTableSql).Execute()
if err != nil {
panic(err.Error())
}
//bzlbm
fmt.Println("恭喜,数据仓库中相应表格创建完毕!")
}
}