package md

import (
	"regexp"

	"xorm.io/xorm"
)

type DbInfo struct {
	User string
	Psw  string
	Name string
	Host string
}

func SplitDbInfo(eg *xorm.Engine) *DbInfo {
	if eg == nil {
		return &DbInfo{
			User: "nil",
			Psw:  "nil",
			Host: "nil",
			Name: "nil",
		}
	}
	pattern := `(\w+):(.*)@tcp\(([\w\.\-\:\_]+)\)\/(\w+)`
	reg := regexp.MustCompile(pattern).FindStringSubmatch(eg.DataSourceName())

	if len(reg) < 5 {
		return &DbInfo{
			User: "unknown",
			Psw:  "unknown",
			Host: "unknown",
			Name: "unknown",
		}
	}
	return &DbInfo{
		User: reg[1],
		Psw:  reg[2],
		Host: reg[3],
		Name: reg[4],
	}
}
