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.

74 lines
2.1 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()
for i2 := range list2 {
dataType := list2[i2]["data_type"].(string)
fieldLength := list2[i2]["field_length"].(int64)
isNull := list2[i2]["is_null"].(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"`
}
if isNull == 0 {
c += " NOT NULL,"
} else {
c += ","
}
createTableSql += `"` + fieldName + `" ` + c + "\r\n"
}
createTableSql=createTableSql[0:len(createTableSql)-3]
createTableSql += `);` + "\r\n"
_, err := pgDb.SQL(createTableSql).Execute()
if err != nil {
fmt.Println(createTableSql)
panic(err.Error())
}
//主键有哪些
var pks = ""
for i2 := range list2 {
fieldName := list2[i2]["field_name"].(string)
isPk := list2[i2]["is_pk"].(int64)
if isPk == 1 {
pks += fieldName + ","
}
}
//去掉最后一个逗号
pks = pks[0 : len(pks)-1]
//添加主键描述
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())
}
fmt.Println("恭喜,数据仓库中相应表格创建完毕!")
}
}