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.

107 lines
2.2 KiB

This file contains ambiguous Unicode characters!

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 SqlKit
import (
"dsSzxy/Utils/ConfigUtil"
"dsSzxy/Utils/DbUtil"
"fmt"
"strings"
)
//操作数据库的变量
var db = DbUtil.Engine
/**
功能对于简单表提供分页的通用SQL组装方法返回通用分页的SQL语句和获取查询总数的SQL语句
作者:黄海
时间2020-02-21
*/
func Count(baseSql string) (int32, error) {
//转小写
baseSql = strings.ToLower(baseSql)
//截取去掉最后面的 limit ?
baseSql = strings.Split(baseSql, " limit ")[0]
countSql := "select count(1) as count from (" + baseSql + ") as t100"
var count int32
_, err := db.SQL(countSql).Get(&count)
if err != nil {
fmt.Println(err.Error())
return 0, err
}
return count, nil
}
/**
功能判断一个sql是不是多表关联查询
作者:黄海
时间2020-06-10
*/
func isMultiFieldSql(sql string) bool {
sql = strings.ToLower(sql)
if strings.Index(sql, " join ") > 0 {
return true
} else {
return false
}
}
/**
功能是不是带有distinct的sql
作者:黄海
时间2020-06-12
*/
func isDistinctSql(sql string) bool {
sql = strings.ToLower(sql)
if strings.Index(sql, " distinct ") > 0 {
return true
} else {
return false
}
}
/**
功能对于查询SQL返回查询结果并返回
作者:黄海
时间2020-03-25
*/
func pageSql(sql string) ([]map[string]interface{}, int32, error) {
//总数
count, err := Count(sql)
if err != nil {
fmt.Println(err.Error())
return nil, 0, err
}
//数据
list, err := db.SQL(sql).Query().List()
if err != nil {
fmt.Println(err.Error())
return nil, 0, err
}
return list, count, nil
}
/**
功能:获取指定的表有哪些列
作者:黄海
时间2020-6-12
*/
func GetTableColumns(tableName string) ([]map[string]interface{}, error) {
sql := `select COLUMN_NAME from information_schema.COLUMNS where table_name = ? and table_schema = '` + ConfigUtil.MysqlDbName + `'`
return db.SQL(sql, tableName).Query().List()
}
/**
功能:获取指定表的成批提交的大小
作者:黄海
时间2020-06-12
*/
func GetTableBatchSize(tableName string) int {
var maxFileds = 65535
//计算每个批次的大小
list, _ := GetTableColumns(tableName)
batchSize := maxFileds / len(list)
return batchSize
}