|
|
package service
|
|
|
|
|
|
import (
|
|
|
"bytes"
|
|
|
"fmt"
|
|
|
"github.com/linmadan/egglib-go/core/application"
|
|
|
"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/application/factory"
|
...
|
...
|
@@ -10,6 +11,11 @@ import ( |
|
|
"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/domain"
|
|
|
"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/infrastructure/api/bytelib"
|
|
|
"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/infrastructure/domainService"
|
|
|
"gorm.io/driver/mysql"
|
|
|
"gorm.io/driver/postgres"
|
|
|
"gorm.io/gorm"
|
|
|
"log"
|
|
|
"os"
|
|
|
"strings"
|
|
|
)
|
|
|
|
...
|
...
|
@@ -217,3 +223,84 @@ func (tableService *TableService) GenerateBusinessTable(ctx *domain.Context, cmd |
|
|
}
|
|
|
return nil, nil
|
|
|
}
|
|
|
|
|
|
func (tableService *TableService) GenerateBusinessTablesView(ctx *domain.Context, cmd *command.GenerateBusinessTableViewRequest) (interface{}, error) {
|
|
|
var (
|
|
|
db *gorm.DB
|
|
|
err error
|
|
|
)
|
|
|
if cmd.DBType == "" {
|
|
|
cmd.SchemaName = "postgresql"
|
|
|
}
|
|
|
if cmd.SchemaName == "" {
|
|
|
cmd.SchemaName = "public"
|
|
|
}
|
|
|
|
|
|
buf := bytes.NewBuffer(nil)
|
|
|
if cmd.DBType == "postgresql" {
|
|
|
db, err = gorm.Open(postgres.Open(fmt.Sprintf("user=%v password=%v host=%v port=%v dbname=%v sslmode=disable TimeZone=Asia/Shanghai",
|
|
|
cmd.User, cmd.Password, cmd.Host, cmd.Port, cmd.DBName)), &gorm.Config{PrepareStmt: false})
|
|
|
if err != nil {
|
|
|
return nil, factory.FastError(err)
|
|
|
}
|
|
|
var tables []string
|
|
|
if tx := db.Raw(fmt.Sprintf(`select tablename from pg_tables where schemaname='%v';`, cmd.SchemaName)).Scan(&tables); tx.Error != nil {
|
|
|
return nil, factory.FastError(tx.Error)
|
|
|
}
|
|
|
type Field struct {
|
|
|
Field string `json:"field"`
|
|
|
Type string `json:"type"`
|
|
|
}
|
|
|
var fields = make([]Field, 0)
|
|
|
for _, t := range tables {
|
|
|
if tx := db.Raw(fmt.Sprintf(`SELECT a.attnum,a.attname AS field,t.typname AS type,a.attlen AS length,a.atttypmod AS lengthvar,a.attnotnull AS notnull
|
|
|
FROM pg_class c,pg_attribute a,pg_type t
|
|
|
WHERE c.relname = '%v' and a.attnum > 0 and a.attrelid = c.oid and a.atttypid = t.oid
|
|
|
ORDER BY a.attnum;`, t)).Scan(&fields); tx.Error != nil {
|
|
|
return nil, factory.FastError(tx.Error)
|
|
|
}
|
|
|
var commonFields []string
|
|
|
var jsonbFields []string
|
|
|
var pkFields []string
|
|
|
for index, f := range fields {
|
|
|
if index == 0 && strings.HasSuffix(f.Field, "id") {
|
|
|
//commonFields = append(commonFields, fmt.Sprintf("%v as id", f.Field))
|
|
|
pkFields = append(pkFields, f.Field)
|
|
|
}
|
|
|
if f.Type == "jsonb" || f.Type == "json" {
|
|
|
jsonbFields = append(jsonbFields, f.Field)
|
|
|
} else {
|
|
|
commonFields = append(commonFields, f.Field)
|
|
|
}
|
|
|
}
|
|
|
buf.WriteString(fmt.Sprintf("-- == 表【%v】建视图语句 ==\n", t))
|
|
|
buf.WriteString(fmt.Sprintf("drop view view_%v;\n", t))
|
|
|
buf.WriteString(fmt.Sprintf("create view view_%v as select %v from %v;\n", t, strings.Join(commonFields, ","), t))
|
|
|
buf.WriteString(fmt.Sprintf("-- pk %v\n", strings.Join(pkFields, ",")))
|
|
|
buf.WriteString(fmt.Sprintf("-- jsonb %v\n", strings.Join(jsonbFields, ",")))
|
|
|
buf.WriteString("\n\n")
|
|
|
}
|
|
|
generalWrite(cmd.DBName+"_tables_view.sql", buf)
|
|
|
} else if cmd.DBType == "mysql" {
|
|
|
db, err = gorm.Open(mysql.Open(fmt.Sprintf("%v:%v@tcp(%v:%v)/%v?charset=utf8&parseTime=True&loc=Local",
|
|
|
cmd.User, cmd.Password, cmd.Host, cmd.Port, cmd.DBName)), &gorm.Config{PrepareStmt: false})
|
|
|
}
|
|
|
|
|
|
return nil, nil
|
|
|
}
|
|
|
|
|
|
func generalWrite(filename string, buf *bytes.Buffer) {
|
|
|
f, err := os.OpenFile(filename, os.O_RDONLY|os.O_CREATE|os.O_APPEND, 0666)
|
|
|
if err != nil {
|
|
|
log.Println("open file error :", err)
|
|
|
return
|
|
|
}
|
|
|
// 关闭文件
|
|
|
defer f.Close()
|
|
|
// 字符串写入
|
|
|
_, err = f.WriteString(buf.String())
|
|
|
if err != nil {
|
|
|
log.Println(err)
|
|
|
return
|
|
|
}
|
|
|
} |
...
|
...
|
|