You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.
package Model
import (
"dsSso/Const/ErrorConst"
"dsSso/Utils/CommonUtil"
"dsSso/Utils/DbUtil"
"dsSso/Utils/LogUtil"
"dsSso/Utils/RedisUtil"
"time"
)
var db = DbUtil . Engine
/**
功能: 用于将interface转为struct的结构体
作者:黄海
时间: 2020-02-05
*/
type Selector struct {
TableName string
PkField string
RedisPrefix string
}
/**
功能:注册一个函数,用于提取计算指定数据表对应的相关信息
作者:黄海
时间: 2020-03-27
*/
func ( m * Selector ) Get ( tableName string ) ( bool , Selector ) {
m . TableName = tableName
m . RedisPrefix = CommonUtil . GetSnakeCaseStr ( tableName ) + ":"
var success = true
success , m . PkField = GetTablePk ( tableName )
return success , * m
}
/**
功能:获取指定表的主键
作者:黄海
时间: 2020-03-26
*/
func GetTablePk ( tableName string ) ( bool , string ) {
//优先到缓存中查找
key := "pk_" + tableName
pk , err := RedisUtil . GET ( key )
if err != nil {
sql := "select column_name from information_schema.`key_column_usage` where table_name=? and constraint_name='primary'"
list , err := db . SQL ( sql , tableName ) . Query ( ) . List ( )
if err != nil || len ( list ) == 0 {
LogUtil . Error ( ErrorConst . SqlQueryError , err . Error ( ) )
return false , ""
}
pk = list [ 0 ] [ "column_name" ] . ( string )
//设置缓存
RedisUtil . SET ( key , pk , 24 * 7 * time . Hour )
}
return true , pk
}