mindxdl---common---db_handler.go
// 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的更多相关文章
- Socket聊天程序——Common
写在前面: 上一篇记录了Socket聊天程序的客户端设计,为了记录的完整性,这里还是将Socket聊天的最后一个模块--Common模块记录一下.Common的设计如下: 功能说明: Common模块 ...
- angularjs 1 开发简单案例(包含common.js,service.js,controller.js,page)
common.js var app = angular.module('app', ['ngFileUpload']) .factory('SV_Common', function ($http) { ...
- Common Bugs in C Programming
There are some Common Bugs in C Programming. Most of the contents are directly from or modified from ...
- ANSI Common Lisp Practice - My Answers - Chatper - 3
Ok, Go ahead. 1 (a) (b) (c) (d) 2 注:union 在 Common Lisp 中的作用就是求两个集合的并集.但是这有一个前提,即给的两个列表已经满足集合的属性了.具体 ...
- [LeetCode] Lowest Common Ancestor of a Binary Tree 二叉树的最小共同父节点
Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. According ...
- [LeetCode] Lowest Common Ancestor of a Binary Search Tree 二叉搜索树的最小共同父节点
Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BS ...
- [LeetCode] Longest Common Prefix 最长共同前缀
Write a function to find the longest common prefix string amongst an array of strings. 这道题让我们求一系列字符串 ...
- 48. 二叉树两结点的最低共同父结点(3种变种情况)[Get lowest common ancestor of binary tree]
[题目] 输入二叉树中的两个结点,输出这两个结点在数中最低的共同父结点. 二叉树的结点定义如下: C++ Code 123456 struct BinaryTreeNode { int ...
- 动态规划求最长公共子序列(Longest Common Subsequence, LCS)
1. 问题描述 子串应该比较好理解,至于什么是子序列,这里给出一个例子:有两个母串 cnblogs belong 比如序列bo, bg, lg在母串cnblogs与belong中都出现过并且出现顺序与 ...
- 【leetcode】Longest Common Prefix
题目简述: Write a function to find the longest common prefix string amongst an array of strings. 解题思路: c ...
随机推荐
- Go 语言入门 3-动态数组(slice)的特性及实现原理
go 语言中的动态数组(slice),是基于数组实现的,可以相比数组而言更加的灵活.其他语言的 slice 通常仅是一个 API, 但是 go 语言的 slice 不仅仅是一种操作, 也是一种数据结构 ...
- git reset总结
git reset git 的重置操作 有三种模式:hard.mixed(默认).soft 1. hard 用法 hard会重置stage区和工作区,和移动代码库上HEAD 和branch的指针所指向 ...
- MasaFramework -- 锁与分布式锁
前言 什么是锁?什么是分布式锁?它们之间有什么样的关系? 什么是锁 加锁(lock)是2018年公布的计算机科学技术名词,是指将控制变量置位,控制共享资源不能被其他线程访问.通过加锁,可以确保在同一时 ...
- Java SE Object类
1.Object类 equals 方法 == 和 equals 的对比 == 是一个比较运算符 == 既可以判断基本类型,又可以判断引用类型 == 如果判断的是基本类型,判断的是值是否相等 == 如果 ...
- Prometheus Operator 对接 Thanos
文章转载自:https://jishuin.proginn.com/p/763bfbd56ae4 使用 Prometheus Operator 来进行监控,在 Prometheus 高可用的章节中也手 ...
- 简书是如何把用户wo逼疯的
趁验证码还有一分钟时间,吐槽一下简书. 准备开始在简书写文章,遇到一些问题. 一.markdown的问题 1.不支持html 2....... 二.绑定手机--这是一个bug 我原来是使用邮箱注册的, ...
- CF600E Lomsat gelral (线段树合并)
相当于是线段树合并的模板题,比(雨天的尾巴)还要板. 唯一注意的是线段树的更新,因为同一子树中可能有多种颜色占主导地位,要输出编号和,比如一颗子树中,1出现3次(最多),3出现3次,那么应该输出4. ...
- 一个好玩的deep learning Demo!
对于生活中的熟悉的动物,我们人脑经过一次扫描,便可以得到该动物的物种!那么机器是如何识别这个图片上的动物是属于哪一物种呢? 本次实验借生活中最常见的猫和狗来探究其原理! 环境准备: tensorflo ...
- Bug改不完,迭代总延期,咋办?
摘要:本文从流程上需要改进的地方进行讨论,分四个方面来分析产生这个问题的原因. 本文分享自华为云社区<Bug改不完,迭代总延期,咋办?>,作者: 华为云PaaS服务小智. 前言 随着互联网 ...
- JVM运行模式和逃逸分析
JVM三种运行模式: 解释模式(Interpreted Mode):只使用解释器(-Xint强制JVM使用解释模式),执行一行JVM字节码就编译一行为机器码.(可以马上看到效果,但是运行过程比较慢) ...