数据库初始化
This commit is contained in:
		
							
								
								
									
										102
									
								
								client.go
									
									
									
									
									
								
							
							
						
						
									
										102
									
								
								client.go
									
									
									
									
									
								
							| @ -50,7 +50,37 @@ type client struct { | ||||
| // Author : go_developer@163.com<白茶清欢> | ||||
| // | ||||
| // Date : 19:19 2022/6/5 | ||||
| func (c *client) AddWithConfigFile(cfgFile string) error { | ||||
| func (c *client) AddWithConfigFile(cfgFilePath string) error { | ||||
| 	var ( | ||||
| 		err      error | ||||
| 		cfg      *cfgFile | ||||
| 		dbClient *DBClient | ||||
| 	) | ||||
|  | ||||
| 	if cfg, err = c.getCfg(cfgFilePath); nil != err { | ||||
| 		return err | ||||
| 	} | ||||
| 	if nil == cfg { | ||||
| 		// 不支持的配置文件格式 | ||||
| 		return nil | ||||
| 	} | ||||
| 	dbClient = &DBClient{ | ||||
| 		dbFlag:         cfg.Flag, | ||||
| 		loggerInstance: nil, | ||||
| 		master:         nil, | ||||
| 		slave:          nil, | ||||
| 		extraFieldList: nil, | ||||
| 		cfg:            Mysql{}, | ||||
| 	} | ||||
| 	if dbClient.master, err = c.GetDatabaseClient(cfg.Config.Master, nil); nil != err { | ||||
| 		return err | ||||
| 	} | ||||
| 	if dbClient.slave, err = c.GetDatabaseClient(cfg.Config.Slave, nil); nil != err { | ||||
| 		return err | ||||
| 	} | ||||
| 	c.lock.Lock() | ||||
| 	c.clientTable[dbClient.dbFlag] = dbClient | ||||
| 	c.lock.Unlock() | ||||
| 	return nil | ||||
| } | ||||
|  | ||||
| @ -90,20 +120,22 @@ func (c *client) getMysqlCfgFileList(cfgDir string) ([]*cfgFile, error) { | ||||
| // | ||||
| // Date : 18:05 2022/6/11 | ||||
| func (c *client) getCfg(cfgPath string) (*cfgFile, error) { | ||||
| 	result := &cfgFile{ | ||||
| 		Path:   cfgPath, | ||||
| 		Type:   "", | ||||
| 		Config: Mysql{}, | ||||
| 	} | ||||
| 	fileArr := strings.Split(cfgPath, ".") | ||||
| 	if len(fileArr) < 2 { | ||||
| 		// 获取不到类型 | ||||
| 		return nil, errors.New("文件格式必须是JSON或者YAML") | ||||
| 	} | ||||
| 	fileType := strings.ToLower(fileArr[len(fileArr)-1]) | ||||
| 	fileFlagArr := strings.Split(fileArr[0], string(filepath.Separator)) | ||||
| 	result := &cfgFile{ | ||||
| 		Path:   cfgPath, | ||||
| 		Type:   "", | ||||
| 		Flag:   fileFlagArr[len(fileFlagArr)-1], | ||||
| 		Config: Database{}, | ||||
| 	} | ||||
| 	var ( | ||||
| 		err     error | ||||
| 		cfgInfo Mysql | ||||
| 		cfgInfo Database | ||||
| 	) | ||||
| 	switch fileType { | ||||
| 	case FileTypeYaml: | ||||
| @ -113,17 +145,22 @@ func (c *client) getCfg(cfgPath string) (*cfgFile, error) { | ||||
| 		if err = util.File.ReadYmlContent(cfgPath, &result.Config); nil != err { | ||||
| 			return nil, fmt.Errorf("%s 配置文件解析失败, 原因 : %s", cfgPath, err.Error()) | ||||
| 		} | ||||
| 		return result, nil | ||||
| 	case FileTypeJson: | ||||
| 		result.Type = FileTypeJson | ||||
| 		if err = util.File.ReadJSONContent(cfgPath, &cfgInfo); nil != err { | ||||
| 			return nil, fmt.Errorf("%s 配置文件解析失败, 原因 : %s", cfgPath, err.Error()) | ||||
| 		} | ||||
| 		return result, nil | ||||
| 	default: | ||||
| 		// 不是JSON , 也不是YML, 跳过 | ||||
| 		return nil, nil | ||||
| 	} | ||||
| 	if len(result.Config.Master.Timezone) == 0 { | ||||
| 		// 默认使用本地时区 | ||||
| 		result.Config.Master.Timezone = "Local" | ||||
| 	} else { | ||||
| 		result.Config.Slave.Timezone = result.Config.Master.Timezone | ||||
| 	} | ||||
| 	return result, nil | ||||
| } | ||||
|  | ||||
| // GetDBClient 获取db client | ||||
| @ -217,6 +254,52 @@ func (c *client) getGormClient() (*gorm.DB, error) { | ||||
| 	return nil, nil | ||||
| } | ||||
|  | ||||
| // GetDatabaseClient 获取数据库连接 | ||||
| // | ||||
| // Author : go_developer@163.com<白茶清欢> | ||||
| // | ||||
| // Date : 18:41 2022/6/11 | ||||
| func (c *client) GetDatabaseClient(conf *Mysql, logConf *LogConfig) (*gorm.DB, error) { | ||||
| 	var ( | ||||
| 		instance       *gorm.DB | ||||
| 		err            error | ||||
| 		loggerInstance *zap.Logger | ||||
| 	) | ||||
|  | ||||
| 	if instance, err = gorm.Open(mysql.Open(c.buildConnectionDSN(conf)), &gorm.Config{}); nil != err { | ||||
| 		return nil, err | ||||
| 	} | ||||
|  | ||||
| 	if nil != logConf { | ||||
| 		if loggerInstance, err = getLogInstance(logConf, nil); nil != err { | ||||
| 			return nil, err | ||||
| 		} | ||||
| 		instance.Logger = wrapper.NewGormLoggerWithInstance(nil, instance, loggerInstance, "", nil) | ||||
| 	} | ||||
|  | ||||
| 	return instance, nil | ||||
| } | ||||
|  | ||||
| // buildConnectionDSN 构建连接信息 | ||||
| // | ||||
| // Author : go_developer@163.com<白茶清欢> | ||||
| // | ||||
| // Date : 18:42 2022/6/11 | ||||
| func (c *client) buildConnectionDSN(conf *Mysql) string { | ||||
| 	return fmt.Sprintf( | ||||
| 		"%s:%s@tcp(%s:%d)/%s?charset=%s&parseTime=True&loc=%s", | ||||
| 		conf.Username, | ||||
| 		conf.Password, | ||||
| 		conf.Host, | ||||
| 		conf.Port, | ||||
| 		conf.Database, | ||||
| 		conf.Charset, | ||||
| 		conf.Timezone, | ||||
| 	) | ||||
| } | ||||
|  | ||||
| // ========================== 以下是老版本支持 | ||||
|  | ||||
| // NewDBClient ... | ||||
| // | ||||
| // Author : go_developer@163.com<白茶清欢> | ||||
| @ -251,6 +334,7 @@ type DBClient struct { | ||||
| 	master         *gorm.DB    // 主库 | ||||
| 	slave          *gorm.DB    // 从库 | ||||
| 	extraFieldList []string    // 提取的字段 | ||||
| 	cfg            Mysql       // 数据库配置 | ||||
| } | ||||
|  | ||||
| // SetFlag 设置数据库标识 | ||||
|  | ||||
| @ -48,9 +48,10 @@ type LogConfig struct { | ||||
| // | ||||
| // Date : 14:47 2022/6/9 | ||||
| type cfgFile struct { | ||||
| 	Path   string `json:"path"`   // 配置文件路径 | ||||
| 	Type   string `json:"type"`   // 配置文件类型 | ||||
| 	Config Mysql  `json:"config"` // 解析之后的配置文件 | ||||
| 	Flag   string   `json:"flag"`   // 数据库标识 | ||||
| 	Path   string   `json:"path"`   // 配置文件路径 | ||||
| 	Type   string   `json:"type"`   // 配置文件类型 | ||||
| 	Config Database `json:"config"` // 解析之后的配置文件 | ||||
| } | ||||
|  | ||||
| const ( | ||||
| @ -86,6 +87,7 @@ type Mysql struct { | ||||
| 	Charset     string      `json:"charset" yaml:"charset"`             // 数据库编码 | ||||
| 	Connection  *Connection `json:"connection" yaml:"connection"`       // 连接配置 | ||||
| 	LogFileName string      `json:"log_file_name" yaml:"log_file_name"` // 日志文件名 | ||||
| 	Timezone    string      `json:"timezone" yaml:"timezone"`           // 时区 | ||||
| } | ||||
|  | ||||
| // Connection 连接数配置 | ||||
|  | ||||
		Reference in New Issue
	
	Block a user