Browse Source

增加sqlserver连接方法

master
guzeng 3 years ago
parent
commit
6df1432cc1
3 changed files with 87 additions and 5 deletions
  1. +31
    -4
      chain.go
  2. +1
    -1
      conn.go
  3. +55
    -0
      sqlserver.go

+ 31
- 4
chain.go View File

@ -26,8 +26,19 @@ type Query struct {
page int page int
page_size int page_size int
stmt *sql.Stmt stmt *sql.Stmt
conn *sql.DB
} }
func NewQuery() *Query {
return &Query{
conn: DB,
}
}
func (this *Query) Conn(conn *sql.DB) *Query {
this.conn = conn
return this
}
func (this *Query) Db(dbname string) *Query { func (this *Query) Db(dbname string) *Query {
this.dbname = dbname this.dbname = dbname
return this return this
@ -183,7 +194,11 @@ func (this *Query) QueryStmt() error {
return errors.New("参数错误,条件值错误") return errors.New("参数错误,条件值错误")
} }
stmt, err = DB.Prepare(sql)
if this.conn == nil {
this.conn = DB
}
stmt, err = this.conn.Prepare(sql)
if err != nil { if err != nil {
return err return err
@ -226,7 +241,11 @@ func (this *Query) UpdateStmt() error {
return errors.New("参数错误,条件值错误") return errors.New("参数错误,条件值错误")
} }
stmt, err = DB.Prepare(sql)
if this.conn == nil {
this.conn = DB
}
stmt, err = this.conn.Prepare(sql)
if err != nil { if err != nil {
return err return err
@ -264,7 +283,11 @@ func (this *Query) CreateStmt() error {
return errors.New("参数错误,条件值错误") return errors.New("参数错误,条件值错误")
} }
stmt, err = DB.Prepare(sql)
if this.conn == nil {
this.conn = DB
}
stmt, err = this.conn.Prepare(sql)
if err != nil { if err != nil {
return err return err
@ -312,7 +335,11 @@ func (this *Query) DeleteStmt() error {
return errors.New("参数错误,条件值错误") return errors.New("参数错误,条件值错误")
} }
stmt, err = DB.Prepare(sql)
if this.conn == nil {
this.conn = DB
}
stmt, err = this.conn.Prepare(sql)
if err != nil { if err != nil {
return err return err


+ 1
- 1
conn.go View File

@ -22,10 +22,10 @@ func Connect(DBHOST, DBUSER, DBPWD, DBNAME, DBPORT string, conns ...int) error {
if DBHOST != "" && DBUSER != "" && DBPWD != "" && DBPORT != "" { //&& DBNAME != "" if DBHOST != "" && DBUSER != "" && DBPWD != "" && DBPORT != "" { //&& DBNAME != ""
for i := 0; i < 10; i++ { for i := 0; i < 10; i++ {
time.Sleep(time.Second * 5)
DB, dbConnErr = sql.Open("mysql", DBUSER+":"+DBPWD+"@tcp("+DBHOST+":"+DBPORT+")/"+DBNAME+"?charset=utf8") DB, dbConnErr = sql.Open("mysql", DBUSER+":"+DBPWD+"@tcp("+DBHOST+":"+DBPORT+")/"+DBNAME+"?charset=utf8")
if dbConnErr != nil { if dbConnErr != nil {
log.Println("ERROR", "can not connect to Database, ", dbConnErr) log.Println("ERROR", "can not connect to Database, ", dbConnErr)
time.Sleep(time.Second * 5)
} else { } else {
if len(conns) > 0 { if len(conns) > 0 {
DB.SetMaxOpenConns(conns[0]) //用于设置最大打开的连接数,默认值为0表示不限制 DB.SetMaxOpenConns(conns[0]) //用于设置最大打开的连接数,默认值为0表示不限制


+ 55
- 0
sqlserver.go View File

@ -0,0 +1,55 @@
package dbquery
import (
"database/sql"
"log"
"time"
_ "github.com/denisenkom/go-mssqldb"
)
var MSDB_CONN *sql.DB
func MSConnect(DBHOST, DBUSER, DBPWD, DBNAME, DBPORT string, conns ...int) error {
log.Println("msdb connectting...")
var dbConnErr error
if DBHOST != "" && DBUSER != "" && DBPWD != "" && DBPORT != "" { //&& DBNAME != ""
for i := 0; i < 10; i++ {
time.Sleep(time.Second * 5)
//连接字符串
connString := fmt.Sprintf("server=%s;port%d;database=%s;user id=%s;password=%s", DBHOST, DBPORT, DBNAME, DBUSER, DBPWD)
log.Println(connString)
//建立连接
MSDB_CONN, dbConnErr = sql.Open("mssql", connString)
if dbConnErr != nil {
log.Println("ERROR", "can not connect to Database, ", dbConnErr)
} else {
log.Println("msdb connected")
break
}
}
} else {
return errors.New("msdb connection params errors")
}
return dbConnErr
}
func CloseMSConn() error {
return MSDB_CONN.Close()
}

Loading…
Cancel
Save