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

     1  package main
     2  
     3  import (
     4  	"database/sql"
     5  	"encoding/json"
     6  	"github.com/team-ide/go-dialect/dialect"
     7  	"github.com/team-ide/go-dialect/worker"
     8  	"strings"
     9  )
    10  
    11  func doImport() {
    12  	if *fileType == "" {
    13  		println("请输入 文件 类型")
    14  		return
    15  	}
    16  	if *importOwner == "" {
    17  		println("请输入 库或表所属者名称")
    18  		return
    19  	}
    20  	db, err := getDbInfo(*sourceDialect, *sourceUser, *sourcePassword, *sourceHost, *sourcePort, *sourceDatabase)
    21  	if err != nil {
    22  		panic(err)
    23  	}
    24  	dia, err := dialect.NewDialect(*sourceDialect)
    25  	if err != nil {
    26  		panic(err)
    27  	}
    28  	if db == nil || dia == nil {
    29  		panic("sourceDialect [" + *sourceDialect + "] not support")
    30  	}
    31  
    32  	dataSourceType := worker.GetDataSource(*fileType)
    33  	if dataSourceType == nil {
    34  		panic("fileType [" + *fileType + "] not support")
    35  	}
    36  
    37  	password := *importOwnerCreatePassword
    38  	if password == "" {
    39  		password = *sourcePassword
    40  	}
    41  	var owners = getImportOwners(*importOwner)
    42  	for _, owner := range owners {
    43  		owner.Password = password
    44  	}
    45  
    46  	task := worker.NewTaskImport(db, dia,
    47  		func(owner *worker.TaskImportOwner) (workDb *sql.DB, err error) {
    48  			ownerName := owner.Name
    49  			ownerUsername := owner.Username
    50  			ownerPassword := owner.Password
    51  
    52  			if ownerPassword == "" {
    53  				ownerPassword = password
    54  			}
    55  
    56  			if *sourceDialect == "sqlite" || *sourceDialect == "sqlite3" {
    57  				workDb = db
    58  				return
    59  			}
    60  			if *sourceDialect == "mysql" {
    61  				if ownerUsername == "" {
    62  					ownerUsername = *sourceUser
    63  				}
    64  				workDb, err = getDbInfo(*sourceDialect, ownerUsername, ownerPassword, *sourceHost, *sourcePort, ownerName)
    65  				return
    66  			}
    67  			if ownerUsername == "" {
    68  				ownerUsername = ownerName
    69  			}
    70  			workDb, err = getDbInfo(*sourceDialect, ownerName, password, *sourceHost, *sourcePort, *sourceDatabase)
    71  			return
    72  		},
    73  		&worker.TaskImportParam{
    74  			Owners:                owners,
    75  			OwnerCreateIfNotExist: *importOwnerCreateIfNotExist == "1" || *importOwnerCreateIfNotExist == "true",
    76  			FormatIndexName: func(ownerName string, tableName string, index *dialect.IndexModel) string {
    77  				return tableName + "_" + index.IndexName
    78  			},
    79  			DataSourceType: dataSourceType,
    80  			ErrorContinue:  true,
    81  			OnProgress: func(progress *worker.TaskProgress) {
    82  				progress.OnError = func(err error) {
    83  					dataBytes, _ := json.Marshal(progress)
    84  					println("progress:" + string(dataBytes))
    85  					println("progress error:" + err.Error())
    86  				}
    87  				//println(string(bs))
    88  			},
    89  		})
    90  	err = task.Start()
    91  	if err != nil {
    92  		panic(err)
    93  	}
    94  	println("导入成功")
    95  }
    96  
    97  func getImportOwners(ownerInfoStr string) (owners []*worker.TaskImportOwner) {
    98  	ownerStrList := strings.Split(ownerInfoStr, ",")
    99  	for _, ownerStr := range ownerStrList {
   100  		ss := strings.Split(ownerStr, "=")
   101  		if len(ss) > 1 {
   102  			owners = append(owners, &worker.TaskImportOwner{
   103  				Name: strings.TrimSpace(ss[0]),
   104  				Path: strings.TrimSpace(ss[1]),
   105  			})
   106  		} else if len(ss) > 0 {
   107  			owners = append(owners, &worker.TaskImportOwner{
   108  				Name: strings.TrimSpace(ss[0]),
   109  			})
   110  		}
   111  	}
   112  	return
   113  }