// 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的更多相关文章

  1. Socket聊天程序——Common

    写在前面: 上一篇记录了Socket聊天程序的客户端设计,为了记录的完整性,这里还是将Socket聊天的最后一个模块--Common模块记录一下.Common的设计如下: 功能说明: Common模块 ...

  2. angularjs 1 开发简单案例(包含common.js,service.js,controller.js,page)

    common.js var app = angular.module('app', ['ngFileUpload']) .factory('SV_Common', function ($http) { ...

  3. Common Bugs in C Programming

    There are some Common Bugs in C Programming. Most of the contents are directly from or modified from ...

  4. ANSI Common Lisp Practice - My Answers - Chatper - 3

    Ok, Go ahead. 1 (a) (b) (c) (d) 2 注:union 在 Common Lisp 中的作用就是求两个集合的并集.但是这有一个前提,即给的两个列表已经满足集合的属性了.具体 ...

  5. [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 ...

  6. [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 ...

  7. [LeetCode] Longest Common Prefix 最长共同前缀

    Write a function to find the longest common prefix string amongst an array of strings. 这道题让我们求一系列字符串 ...

  8. 48. 二叉树两结点的最低共同父结点(3种变种情况)[Get lowest common ancestor of binary tree]

    [题目] 输入二叉树中的两个结点,输出这两个结点在数中最低的共同父结点. 二叉树的结点定义如下:  C++ Code  123456   struct BinaryTreeNode {     int ...

  9. 动态规划求最长公共子序列(Longest Common Subsequence, LCS)

    1. 问题描述 子串应该比较好理解,至于什么是子序列,这里给出一个例子:有两个母串 cnblogs belong 比如序列bo, bg, lg在母串cnblogs与belong中都出现过并且出现顺序与 ...

  10. 【leetcode】Longest Common Prefix

    题目简述: Write a function to find the longest common prefix string amongst an array of strings. 解题思路: c ...

随机推荐

  1. 第五十六篇:webpack的loader(四) -打包js中的高级语法

    好家伙, 1.打包处理js文件中的高级语法 webpack只能打包处理一部分高级的JavaScript 语法.对于那些webpack无法处理的高级js 语法,需要借 助于 babel-loader 进 ...

  2. SpringMVC 05: SpringMVC中携带数据的页面跳转

    SpringMVC默认的参数对象 SpringMVC默认的参数对象是指,不用再另行创建,相当于SpringMVC内置对象,可以直接声明并使用 默认的参数对象有:HttpServletRequest,H ...

  3. flutter系列之:flutter中常用的ListView layout详解

    目录 简介 ListView详解 ListView中的特有属性 ListView的构造函数 ListView的使用 总结 简介 ListView是包含多个child组件的widget,在ListVie ...

  4. 从零打造“乞丐版” React(一)——从命令式编程到声明式编程

    这个系列的目的是通过使用 JS 实现"乞丐版"的 React,让读者了解 React 的基本工作原理,体会 React 带来的构建应用的优势 1 HTML 构建静态页面 使用 HT ...

  5. Django ORM 实现数据的单表 增删改查

    一.配置环境 1 Django 连接数据库(MySQL) DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', 'NAME' ...

  6. Elasticsearch 数据建模指南

    文章转载自:https://mp.weixin.qq.com/s/vSh6w3eL_oQvU1mxnxsArA 0.题记 我在做 Elasticsearch 相关咨询和培训过程中,发现大家普遍更关注实 ...

  7. 8. Ceph 基础篇 - 运维常用操作

    文章转载自:https://mp.weixin.qq.com/s?__biz=MzI1MDgwNzQ1MQ==&mid=2247485300&idx=1&sn=aacff9f7 ...

  8. vivo 公司 Kubernetes 集群 Ingress 网关实践

    文章转载自:https://mp.weixin.qq.com/s/qPqrJ3un1peeWgG9xO2m-Q 背景 vivo 人工智能计算平台小组从 2018 年底开始建设 AI 计算平台至今,已经 ...

  9. GitLab基础知识

    GitLab基本介绍 GitLab是利用Ruby on Rails一个开源的版本管理系统,实现一个自托管的Git项目仓库,可通过Web界面进行访问公开的或者私人项目. 与Github类似,GitLab ...

  10. Lock 锁底层实现

    ★ 1.讲讲 Lock 锁 是一个接口,有三个实现类,分别是常用的 可重入锁,读锁.写锁.常用的是可重入锁. 加锁使用lock() 方法,解锁使用 unlock() 方法.Lock的底层是 AQS+C ...