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

     1  package main
     2  
     3  import (
     4  	"database/sql"
     5  	"flag"
     6  	"github.com/team-ide/go-driver/db_dm"
     7  	"github.com/team-ide/go-driver/db_kingbase_v8r6"
     8  	"github.com/team-ide/go-driver/db_mysql"
     9  	"github.com/team-ide/go-driver/db_oracle"
    10  	"github.com/team-ide/go-driver/db_postgresql"
    11  	"github.com/team-ide/go-driver/db_shentong"
    12  	"github.com/team-ide/go-driver/db_sqlite3"
    13  	"strings"
    14  )
    15  
    16  var (
    17  	do = flag.String("do", "", "操作:export(导出)、import(导入)、sync(同步)")
    18  
    19  	sourceDialect  = flag.String("sourceDialect", "", "源 数据库 方言 mysql、sqlite3、dm、kingbase、oracle、shentong")
    20  	sourceHost     = flag.String("sourceHost", "", "源 数据库 host")
    21  	sourcePort     = flag.Int("sourcePort", 0, "源 数据库 port")
    22  	sourceUser     = flag.String("sourceUser", "", "源 数据库 user")
    23  	sourcePassword = flag.String("sourcePassword", "", "源 数据库 password")
    24  	sourceDatabase = flag.String("sourceDatabase", "", "源 数据库 连接库(库名、用户名、SID)")
    25  
    26  	fileType    = flag.String("fileType", "", "文件 类型:sql、excel、txt、csv(sql将导出单个文件,其它每个表导出一个文件)")
    27  	fileDialect = flag.String("fileDialect", "", "文件 数据库 方言")
    28  	skipOwner   = flag.String("skipOwner", "", "忽略库名,多个使用“,”隔开")
    29  	skipTable   = flag.String("skipTable", "", "忽略表名,多个使用“,”隔开")
    30  
    31  	exportDialect     = flag.String("exportDialect", "", "导出 数据库 方言")
    32  	exportDir         = flag.String("exportDir", "", "导出 文件存储目录")
    33  	exportOwner       = flag.String("exportOwner", "", "导出 库(库名、表拥有者),默认全部,多个使用“,”隔开")
    34  	exportTable       = flag.String("exportTable", "", "导出 表,默认全部,多个使用“,”隔开")
    35  	exportStruct      = flag.String("exportStruct", "", "导出 结构体,默认true,适用于导出类型为sql、excel")
    36  	exportData        = flag.String("exportData", "", "导出 数据,默认true")
    37  	exportAppendOwner = flag.String("exportAppendOwner", "", "sql文件类型的sql拼接 连接库(库名、用户名),拼接原库名或重命名后的库名")
    38  
    39  	importOwner                 = flag.String("importOwner", "", "导入 库(库名、表拥有者),并指定文件路径,多个使用“,”隔开")
    40  	importOwnerCreateIfNotExist = flag.String("importOwnerCreateIfNotExist", "", "导入 库如果不存在,则创建")
    41  	importOwnerCreatePassword   = flag.String("importOwnerCreatePassword", "", "导入 库创建的密码,只有库为所属者有效,默认为sourcePassword,如:oracle等数据库")
    42  
    43  	targetDialect             = flag.String("targetDialect", "", "目标 数据库 方言 mysql、sqlite3、dm、kingbase、oracle、shentong")
    44  	targetHost                = flag.String("targetHost", "", "目标 数据库 host")
    45  	targetPort                = flag.Int("targetPort", 0, "目标 数据库 port")
    46  	targetUser                = flag.String("targetUser", "", "目标 数据库 user")
    47  	targetPassword            = flag.String("targetPassword", "", "目标 数据库 password")
    48  	targetDatabase            = flag.String("targetDatabase", "", "目标 数据库 连接库(库名、用户名、SID)")
    49  	syncOwner                 = flag.String("syncOwner", "", "同步 库(库名、表拥有者),默认全部,多个使用“,”隔开")
    50  	syncStruct                = flag.String("syncStruct", "", "同步 结构体,默认true,适用于导出类型为sql、excel")
    51  	syncData                  = flag.String("syncData", "", "同步 数据,默认true")
    52  	syncOwnerCreateIfNotExist = flag.String("syncOwnerCreateIfNotExist", "", "同步 库如果不存在,则创建")
    53  	syncOwnerCreatePassword   = flag.String("syncOwnerCreatePassword", "", "同步 库创建的密码,只有库为所属者有效,默认为targetPassword,如:oracle等数据库")
    54  )
    55  
    56  func main() {
    57  	flag.Parse()
    58  	//flag.PrintDefaults()
    59  	if *do == "" {
    60  		println("请输入 操作(do)")
    61  		return
    62  	}
    63  
    64  	switch strings.ToLower(*do) {
    65  	case "export":
    66  		doExport()
    67  		break
    68  	case "import":
    69  		doImport()
    70  		break
    71  	case "sync":
    72  		doSync()
    73  		break
    74  	default:
    75  		panic("do [" + *do + "] not support")
    76  	}
    77  }
    78  
    79  func getDbInfo(dbType string, user string, password string, host string, port int, database string) (db *sql.DB, err error) {
    80  	switch strings.ToLower(dbType) {
    81  	case "mysql":
    82  		dsn := db_mysql.GetDSN(user, password, host, port, database)
    83  		db, err = db_mysql.Open(dsn)
    84  		break
    85  	case "sqlite", "sqlite3":
    86  		dsn := db_sqlite3.GetDSN(database)
    87  		db, err = db_sqlite3.Open(dsn)
    88  		break
    89  	case "dameng", "dm":
    90  		dsn := db_dm.GetDSN(user, password, host, port)
    91  		db, err = db_dm.Open(dsn)
    92  		break
    93  	case "kingbase", "kb":
    94  		dsn := db_kingbase_v8r6.GetDSN(user, password, host, port, database)
    95  		db, err = db_kingbase_v8r6.Open(dsn)
    96  		break
    97  	case "oracle":
    98  		dsn := db_oracle.GetDSN(user, password, host, port, database)
    99  		db, err = db_oracle.Open(dsn)
   100  		break
   101  	case "shentong", "st":
   102  		dsn := db_shentong.GetDSN(user, password, host, port, database)
   103  		db, err = db_shentong.Open(dsn)
   104  		break
   105  	case "postgresql", "ps":
   106  		dsn := db_postgresql.GetDSN(user, password, host, port, database)
   107  		db, err = db_postgresql.Open(dsn)
   108  		break
   109  	}
   110  	return
   111  }