1
|
package service
|
1
|
package service
|
2
|
|
2
|
|
3
|
import (
|
3
|
import (
|
|
|
4
|
+ "bytes"
|
4
|
"fmt"
|
5
|
"fmt"
|
5
|
"github.com/linmadan/egglib-go/core/application"
|
6
|
"github.com/linmadan/egglib-go/core/application"
|
6
|
"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/application/factory"
|
7
|
"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/application/factory"
|
|
@@ -10,6 +11,11 @@ import ( |
|
@@ -10,6 +11,11 @@ import ( |
10
|
"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/domain"
|
11
|
"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/domain"
|
11
|
"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/infrastructure/api/bytelib"
|
12
|
"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/infrastructure/api/bytelib"
|
12
|
"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/infrastructure/domainService"
|
13
|
"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/infrastructure/domainService"
|
|
|
14
|
+ "gorm.io/driver/mysql"
|
|
|
15
|
+ "gorm.io/driver/postgres"
|
|
|
16
|
+ "gorm.io/gorm"
|
|
|
17
|
+ "log"
|
|
|
18
|
+ "os"
|
13
|
"strings"
|
19
|
"strings"
|
14
|
)
|
20
|
)
|
15
|
|
21
|
|
|
@@ -217,3 +223,84 @@ func (tableService *TableService) GenerateBusinessTable(ctx *domain.Context, cmd |
|
@@ -217,3 +223,84 @@ func (tableService *TableService) GenerateBusinessTable(ctx *domain.Context, cmd |
217
|
}
|
223
|
}
|
218
|
return nil, nil
|
224
|
return nil, nil
|
219
|
}
|
225
|
}
|
|
|
226
|
+
|
|
|
227
|
+func (tableService *TableService) GenerateBusinessTablesView(ctx *domain.Context, cmd *command.GenerateBusinessTableViewRequest) (interface{}, error) {
|
|
|
228
|
+ var (
|
|
|
229
|
+ db *gorm.DB
|
|
|
230
|
+ err error
|
|
|
231
|
+ )
|
|
|
232
|
+ if cmd.DBType == "" {
|
|
|
233
|
+ cmd.SchemaName = "postgresql"
|
|
|
234
|
+ }
|
|
|
235
|
+ if cmd.SchemaName == "" {
|
|
|
236
|
+ cmd.SchemaName = "public"
|
|
|
237
|
+ }
|
|
|
238
|
+
|
|
|
239
|
+ buf := bytes.NewBuffer(nil)
|
|
|
240
|
+ if cmd.DBType == "postgresql" {
|
|
|
241
|
+ db, err = gorm.Open(postgres.Open(fmt.Sprintf("user=%v password=%v host=%v port=%v dbname=%v sslmode=disable TimeZone=Asia/Shanghai",
|
|
|
242
|
+ cmd.User, cmd.Password, cmd.Host, cmd.Port, cmd.DBName)), &gorm.Config{PrepareStmt: false})
|
|
|
243
|
+ if err != nil {
|
|
|
244
|
+ return nil, factory.FastError(err)
|
|
|
245
|
+ }
|
|
|
246
|
+ var tables []string
|
|
|
247
|
+ if tx := db.Raw(fmt.Sprintf(`select tablename from pg_tables where schemaname='%v';`, cmd.SchemaName)).Scan(&tables); tx.Error != nil {
|
|
|
248
|
+ return nil, factory.FastError(tx.Error)
|
|
|
249
|
+ }
|
|
|
250
|
+ type Field struct {
|
|
|
251
|
+ Field string `json:"field"`
|
|
|
252
|
+ Type string `json:"type"`
|
|
|
253
|
+ }
|
|
|
254
|
+ var fields = make([]Field, 0)
|
|
|
255
|
+ for _, t := range tables {
|
|
|
256
|
+ 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
|
|
|
257
|
+FROM pg_class c,pg_attribute a,pg_type t
|
|
|
258
|
+WHERE c.relname = '%v' and a.attnum > 0 and a.attrelid = c.oid and a.atttypid = t.oid
|
|
|
259
|
+ORDER BY a.attnum;`, t)).Scan(&fields); tx.Error != nil {
|
|
|
260
|
+ return nil, factory.FastError(tx.Error)
|
|
|
261
|
+ }
|
|
|
262
|
+ var commonFields []string
|
|
|
263
|
+ var jsonbFields []string
|
|
|
264
|
+ var pkFields []string
|
|
|
265
|
+ for index, f := range fields {
|
|
|
266
|
+ if index == 0 && strings.HasSuffix(f.Field, "id") {
|
|
|
267
|
+ //commonFields = append(commonFields, fmt.Sprintf("%v as id", f.Field))
|
|
|
268
|
+ pkFields = append(pkFields, f.Field)
|
|
|
269
|
+ }
|
|
|
270
|
+ if f.Type == "jsonb" || f.Type == "json" {
|
|
|
271
|
+ jsonbFields = append(jsonbFields, f.Field)
|
|
|
272
|
+ } else {
|
|
|
273
|
+ commonFields = append(commonFields, f.Field)
|
|
|
274
|
+ }
|
|
|
275
|
+ }
|
|
|
276
|
+ buf.WriteString(fmt.Sprintf("-- == 表【%v】建视图语句 ==\n", t))
|
|
|
277
|
+ buf.WriteString(fmt.Sprintf("drop view view_%v;\n", t))
|
|
|
278
|
+ buf.WriteString(fmt.Sprintf("create view view_%v as select %v from %v;\n", t, strings.Join(commonFields, ","), t))
|
|
|
279
|
+ buf.WriteString(fmt.Sprintf("-- pk %v\n", strings.Join(pkFields, ",")))
|
|
|
280
|
+ buf.WriteString(fmt.Sprintf("-- jsonb %v\n", strings.Join(jsonbFields, ",")))
|
|
|
281
|
+ buf.WriteString("\n\n")
|
|
|
282
|
+ }
|
|
|
283
|
+ generalWrite(cmd.DBName+"_tables_view.sql", buf)
|
|
|
284
|
+ } else if cmd.DBType == "mysql" {
|
|
|
285
|
+ db, err = gorm.Open(mysql.Open(fmt.Sprintf("%v:%v@tcp(%v:%v)/%v?charset=utf8&parseTime=True&loc=Local",
|
|
|
286
|
+ cmd.User, cmd.Password, cmd.Host, cmd.Port, cmd.DBName)), &gorm.Config{PrepareStmt: false})
|
|
|
287
|
+ }
|
|
|
288
|
+
|
|
|
289
|
+ return nil, nil
|
|
|
290
|
+}
|
|
|
291
|
+
|
|
|
292
|
+func generalWrite(filename string, buf *bytes.Buffer) {
|
|
|
293
|
+ f, err := os.OpenFile(filename, os.O_RDONLY|os.O_CREATE|os.O_APPEND, 0666)
|
|
|
294
|
+ if err != nil {
|
|
|
295
|
+ log.Println("open file error :", err)
|
|
|
296
|
+ return
|
|
|
297
|
+ }
|
|
|
298
|
+ // 关闭文件
|
|
|
299
|
+ defer f.Close()
|
|
|
300
|
+ // 字符串写入
|
|
|
301
|
+ _, err = f.WriteString(buf.String())
|
|
|
302
|
+ if err != nil {
|
|
|
303
|
+ log.Println(err)
|
|
|
304
|
+ return
|
|
|
305
|
+ }
|
|
|
306
|
+} |