github.com/team-ide/go-dialect@v1.9.20/export.go (about)

     1  package main
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  	"github.com/team-ide/go-dialect/dialect"
     7  	"github.com/team-ide/go-dialect/worker"
     8  	"strings"
     9  )
    10  
    11  func doExport() {
    12  	if *fileType == "" {
    13  		println("请输入 文件 类型")
    14  		return
    15  	}
    16  	if *exportDir == "" {
    17  		println("请输入 导出 生成文件目录")
    18  		return
    19  	}
    20  	//if *exportOwner == "" {
    21  	//	println("请输入 导出 库或表所属者")
    22  	//	return
    23  	//}
    24  	db, err := getDbInfo(*sourceDialect, *sourceUser, *sourcePassword, *sourceHost, *sourcePort, *sourceDatabase)
    25  	if err != nil {
    26  		panic(err)
    27  	}
    28  	dia, err := dialect.NewDialect(*sourceDialect)
    29  	if err != nil {
    30  		panic(err)
    31  	}
    32  	if db == nil || dia == nil {
    33  		panic("sourceDialect [" + *sourceDialect + "] not support")
    34  	}
    35  
    36  	exportDia, err := dialect.NewDialect(*exportDialect)
    37  	if err != nil {
    38  		panic(err)
    39  	}
    40  	dataSourceType := worker.GetDataSource(*fileType)
    41  	if dataSourceType == nil {
    42  		panic("fileType [" + *fileType + "] not support")
    43  	}
    44  
    45  	var owners = getExportOwners(*exportOwner)
    46  	bs, _ := json.Marshal(owners)
    47  	fmt.Println("owners:", string(bs))
    48  
    49  	skipOwnerStr := *skipOwner
    50  	skipOwnerStr = strings.TrimSpace(skipOwnerStr)
    51  
    52  	task := worker.NewTaskExport(db, dia, exportDia, &worker.TaskExportParam{
    53  		SkipOwnerNames:  strings.Split(skipOwnerStr, ","),
    54  		Owners:          owners,
    55  		ExportStruct:    *exportStruct == "" || *exportStruct == "1" || *exportStruct == "true",
    56  		ExportData:      *exportData == "" || *exportData == "1" || *exportData == "true",
    57  		AppendOwnerName: *exportAppendOwner == "1" || *exportAppendOwner == "true",
    58  		Dir:             *exportDir,
    59  		ExportBatchSql:  true,
    60  		FormatIndexName: func(ownerName string, tableName string, index *dialect.IndexModel) string {
    61  			return tableName + "_" + index.IndexName
    62  		},
    63  		DataSourceType: dataSourceType,
    64  		BatchNumber:    1000,
    65  		ErrorContinue:  true,
    66  		OnProgress: func(progress *worker.TaskProgress) {
    67  			progress.OnError = func(err error) {
    68  				dataBytes, _ := json.Marshal(progress)
    69  				println("progress:" + string(dataBytes))
    70  				println("progress error:" + err.Error())
    71  			}
    72  			//println(string(bs))
    73  		},
    74  	})
    75  	err = task.Start()
    76  	if err != nil {
    77  		panic(err)
    78  	}
    79  	println("导出成功")
    80  }
    81  
    82  func getExportOwners(ownerInfoStr string) (owners []*worker.TaskExportOwner) {
    83  	ownerInfoStr = strings.TrimSpace(ownerInfoStr)
    84  	ownerStrList := strings.Split(ownerInfoStr, ",")
    85  	for _, ownerStr := range ownerStrList {
    86  		ownerStr = strings.TrimSpace(ownerStr)
    87  		if ownerStr == "" {
    88  			continue
    89  		}
    90  		ss := strings.Split(ownerStr, "=")
    91  		if len(ss) > 1 {
    92  			owners = append(owners, &worker.TaskExportOwner{
    93  				SourceName: strings.TrimSpace(ss[0]),
    94  				TargetName: strings.TrimSpace(ss[1]),
    95  			})
    96  		} else if len(ss) > 0 {
    97  			owners = append(owners, &worker.TaskExportOwner{
    98  				SourceName: strings.TrimSpace(ss[0]),
    99  			})
   100  		}
   101  	}
   102  	return
   103  }