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 PgUtil
import (
"dsSupport/Const/ErrorConst"
"dsSupport/Utils/LogUtil"
"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 err error
func init ( ) {
//postgresql
psqlInfo := fmt . Sprintf ( "host=%s port=%d user=%s password=%s dbname=%s sslmode=disable" , "10.10.14.107" , 5432 , "dsideal" , "DsideaL147258369" , "base_db_dev" )
//格式
Engine , err = xorm . NewEngine ( "postgres" , psqlInfo )
if err != nil {
LogUtil . Error ( ErrorConst . SqlQueryError , err . Error ( ) )
}
//设置数据库连接池
Engine . SetMaxIdleConns ( 200 ) //设置连接池中的保持连接的最大连接数。
Engine . SetMaxOpenConns ( 200 ) //设置打开数据库的最大连接数,包含正在使用的连接和连接池的连接。
Engine . SetConnMaxLifetime ( time . Minute * 5 )
//设置数据时区
var location * time . Location
location , _ = time . LoadLocation ( "Asia/Shanghai" )
Engine . TZLocation = location
//与 struct的映射方式, 这里采用蛇型方法, 默认是蛇形
Engine . SetTableMapper ( core . SnakeMapper { } )
//显示+记录SQL日志
f , _ := os . Create ( "./Logs/pg_sql.log" )
logger := log . NewSimpleLogger ( f )
logger . SetLevel ( log . LOG_DEBUG )
Engine . SetLogger ( log . NewLoggerAdapter ( logger ) )
Engine . ShowSQL ( true ) // 则会在控制台打印出生成的SQL语句
}