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.

124 lines
2.5 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 GPUtil
import (
"dsDataex/Utils/CommonUtil"
"dsDataex/Utils/ConfigUtil"
"fmt"
_ "github.com/lib/pq"
"github.com/xormplus/core"
"github.com/xormplus/xorm"
"github.com/xormplus/xorm/log"
"os"
"time"
)
var Engine *xorm.Engine
var ServerVersion string
/**
* @Author zhangjun
* @Description
* @Date 2021-02-06 10:00
* @Param
* @return
**/
func init() {
host := ConfigUtil.GreenPlumIp
port := ConfigUtil.GreenPlumPort
user := ConfigUtil.GreenPlumUser
dbname := ConfigUtil.GreenPlumDataBase
password := ConfigUtil.GreenPlumPwd
//mysql
master := fmt.Sprintf("host=%s port=%d user=%s password=%s dbname=%s sslmode=disable", host, port, user, password, dbname)
var err error
Engine, err = xorm.NewEngine("postgres", master)
if err != nil {
fmt.Println(err)
}
ServerVersion = Engine.DriverName()
//设置数据库连接池
Engine.SetMaxOpenConns(100)
Engine.SetMaxIdleConns(10)
Engine.SetConnMaxLifetime(time.Second * 30)
Engine.Ping()
go func() {
for {
time.Sleep(time.Duration(600) * time.Second)
Engine.Ping()
fmt.Println("\n[xorm] [info] " + CommonUtil.GetCurrentTime() + " PING DATABASE greenplum")
}
}()
//设置数据时区
var location *time.Location
location, err = time.LoadLocation("Asia/Shanghai")
Engine.TZLocation = location
//与 struct的映射方式这里采用蛇型方法默认是蛇形
Engine.SetTableMapper(core.SnakeMapper{})
//显示+记录SQL日志
f, _ := os.Create("./Logs/sql.log")
Engine.SetLogger(log.NewSimpleLogger(f))
Engine.ShowSQL(true) // 则会在控制台打印出生成的SQL语句
Engine.Logger().SetLevel(log.LOG_DEBUG) //则会在控制台打印info及以上的信息
}
/**
* @Author zhangjun
* @Description
* @Date 2021-02-06 10:00
* @Param
* @return
**/
func SqlQuery(sql string, param []string) (bool, []map[string]interface{}) {
result, err := Engine.SQL(sql, param).QueryInterface()
if err != nil {
return false, result
} else {
return true, result
}
}
/**
* @Author zhangjun
* @Description
* @Date 2021-02-06 11:11
* @Param
* @return
**/
func SqlQueryJson(sql string, param []string) (bool, string) {
result, err := Engine.SQL(sql, param).Query().Json()
if err != nil {
return false, err.Error()
} else {
return true, result
}
}
/**
* @Author zhangjun
* @Description
* @Date 2021-02-06 11:16
* @Param
* @return
**/
func SqlQueryXml(sql string, param []string) (bool, string) {
result, err := Engine.SQL(sql, param).Query().Xml()
if err != nil {
return false, err.Error()
} else {
return true, result
}
}