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 ...
随机推荐
- JAVA中自定义扩展Swagger的能力,自动生成参数取值含义说明,提升开发效率
大家好,又见面了. 在JAVA做前后端分离的项目开发的时候,服务端需要提供接口文档供周边人员做接口的对接指导.越来越多的项目都在尝试使用一些基于代码自动生成接口文档的工具来替代由开发人员手动编写接口文 ...
- immutable 与 stable 函数的差异
Stable 函数不能修改数据库,单个Query中所有行给定同样的参数确保返回相同的结果.这种稳定级别允许优化器将多次函数调用转换为一次.在索引扫描的条件中使用这种函数是可行的,因为索引扫描只计算一次 ...
- 【Android 逆向】switch 的smail特征
JAVA 源码 ... String str1 = packedSwitch(1); ... private String packedSwitch(int i) { String str = nul ...
- 安装docker及使用docker安装其他软件(手动挂载数据卷)
中秋明月,豪门有,贫家也有,极慰人心 Linux安装docker 可以参考官方的安装文档 centos安装docker: https://docs.docker.com/engine/install/ ...
- 理解virt、res、shr之间的关系(linux系统篇)
前言 想必在linux上写过程序的同学都有分析进程占用多少内存的经历,或者被问到这样的问题--你的程序在运行时占用了多少内存(物理内存)? 通常我们可以通过top命令查看进程占用了多少内存.这里我们可 ...
- innodb_flush_log_at_trx_commit 和 sync_binlog 参数详 解
"innodb_flush_log_at_trx_commit"和"sync_binlog"两个参数是控制 MySQL 磁盘写入策略以及数据安全性的关键参数.当 ...
- docker bridge 到 k8s pod 跨节点网络通信机制演进
- NetworkPolicy网络策略以及举例说明
网络策略(NetworkPolicy)是一种关于pod间及pod与其他网络端点间所允许的通信规则的规范.NetworkPolicy 资源使用标签选择pod,并定义选定pod所允许的通信规则. 前提 网 ...
- 2.Prometheus邮件报警配置
1.安装配置 Alertmanager wget https://github.com/prometheus/alertmanager/releases/download/v0.20.0/alertm ...
- js对象结构赋值const {XXX } =this
样例1: const { xxx } = this.state; 上面的写法是es6的写法,其实就相当于: const xxx = this.state.xxx 样例2: const {comment ...