mindxdl---common---db_handler.go

2023-02-13,,

// Copyright (c) 2021. Huawei Technologies Co., Ltd. All rights reserved.

// Package common this file for db handler
package common

import (
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/mysql" // mysql driver
"huawei.com/npu-exporter/hwlog"
"sync"
)

const (
// DBType db type
DBType = "mysql"
// DBSecretName the secret name which contains db connection path
DBSecretName = "mysql-secret"
// DBSecretNameSpace the namespace for secret which contains db connection path
DBSecretNameSpace = "mindx-dl"
// GetDBSecretRetryCount retry count for getting secret
GetDBSecretRetryCount = 10
// GetDBSecretTimeInterval interval for getting secret, unit: second
GetDBSecretTimeInterval = 5
// DBSecretConnectionPathKey db connection path key in secret
DBSecretConnectionPathKey = "connection-path"
// DBTableOption db table option
DBTableOption = "gorm:table_options"
// InnoDBEngine innodb engine
InnoDBEngine = "ENGINE=InnoDB"
)

var dbOnce sync.Once

// DbCommonHandler db handler
type DbCommonHandler struct {
db *gorm.DB
dbType string
dbPath string
}

// NewDBInstance create db instance
func NewDBInstance(dbType, dbPath string) *DbCommonHandler {
handler := &DbCommonHandler{
dbType: dbType,
dbPath: dbPath,
}

return handler
}

func (handler *DbCommonHandler) initDBInstance() {
dbase, err := gorm.Open(handler.dbType, handler.dbPath)
if err != nil {
hwlog.RunLog.Fatal(err)
}
hwlog.RunLog.Info("connect to db success")
handler.db = dbase
}

// GetDB return db instance
func (handler *DbCommonHandler) GetDB() *gorm.DB {
dbOnce.Do(handler.initDBInstance)
return handler.db
}

// CloseDB close db connection
func (handler *DbCommonHandler) CloseDB() {
if handler.db == nil {
return
}

if err := handler.db.Close(); err != nil {
hwlog.RunLog.Error(err)
}
}

// SetScopes set scopes
func (handler *DbCommonHandler) SetScopes(scopes ...func(db *gorm.DB) *gorm.DB) *gorm.DB {
return handler.GetDB().Scopes(scopes...)
}

// SetPager return pager
func (handler *DbCommonHandler) SetPager(page, pageSize uint64) func(db *gorm.DB) *gorm.DB {
return func(db *gorm.DB) *gorm.DB {
offset := (page - 1) * pageSize
return db.Offset(offset).Limit(pageSize)
}
}

// SetQueryFields set query field
func (handler *DbCommonHandler) SetQueryFields(fieldsNameList interface{}) func(db *gorm.DB) *gorm.DB {
return func(db *gorm.DB) *gorm.DB {
return db.Select(fieldsNameList)
}
}

// SetModel set query table
func (handler *DbCommonHandler) SetModel(model interface{}) func(db *gorm.DB) *gorm.DB {
return func(db *gorm.DB) *gorm.DB {
return db.Model(model)
}
}

// SetJoins set joins
func (handler *DbCommonHandler) SetJoins(query string, values ...interface{}) func(db *gorm.DB) *gorm.DB {
return func(db *gorm.DB) *gorm.DB {
return db.Joins(query, values...)
}
}

// QueryAll query all data
func (handler *DbCommonHandler) QueryAll(db *gorm.DB, outPut, condition interface{}, param ...interface{}) *gorm.DB {
return db.Where(condition, param...).Find(outPut)
}

// First return the fist match data
func (handler *DbCommonHandler) First(db *gorm.DB, outPut, condition interface{}, param ...interface{}) *gorm.DB {
return db.Where(condition, param...).First(outPut)
}

mindxdl---common---db_handler.go的相关教程结束。

《mindxdl---common---db_handler.go.doc》

下载本文的Word格式文档,以方便收藏与打印。