From 6df1432cc18c977b439ef8ad0bb1bfadae16f215 Mon Sep 17 00:00:00 2001 From: guzeng Date: Wed, 10 Mar 2021 15:39:21 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0sqlserver=E8=BF=9E=E6=8E=A5?= =?UTF-8?q?=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- chain.go | 35 +++++++++++++++++++++++++++++---- conn.go | 2 +- sqlserver.go | 55 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 87 insertions(+), 5 deletions(-) create mode 100644 sqlserver.go diff --git a/chain.go b/chain.go index 5b724c6..a9e4a76 100644 --- a/chain.go +++ b/chain.go @@ -26,8 +26,19 @@ type Query struct { page int page_size int 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 { this.dbname = dbname return this @@ -183,7 +194,11 @@ func (this *Query) QueryStmt() error { return errors.New("参数错误,条件值错误") } - stmt, err = DB.Prepare(sql) + if this.conn == nil { + this.conn = DB + } + + stmt, err = this.conn.Prepare(sql) if err != nil { return err @@ -226,7 +241,11 @@ func (this *Query) UpdateStmt() error { return errors.New("参数错误,条件值错误") } - stmt, err = DB.Prepare(sql) + if this.conn == nil { + this.conn = DB + } + + stmt, err = this.conn.Prepare(sql) if err != nil { return err @@ -264,7 +283,11 @@ func (this *Query) CreateStmt() error { return errors.New("参数错误,条件值错误") } - stmt, err = DB.Prepare(sql) + if this.conn == nil { + this.conn = DB + } + + stmt, err = this.conn.Prepare(sql) if err != nil { return err @@ -312,7 +335,11 @@ func (this *Query) DeleteStmt() error { return errors.New("参数错误,条件值错误") } - stmt, err = DB.Prepare(sql) + if this.conn == nil { + this.conn = DB + } + + stmt, err = this.conn.Prepare(sql) if err != nil { return err diff --git a/conn.go b/conn.go index a2b36b0..54882b2 100755 --- a/conn.go +++ b/conn.go @@ -22,10 +22,10 @@ func Connect(DBHOST, DBUSER, DBPWD, DBNAME, DBPORT string, conns ...int) error { if DBHOST != "" && DBUSER != "" && DBPWD != "" && DBPORT != "" { //&& DBNAME != "" for i := 0; i < 10; i++ { - time.Sleep(time.Second * 5) DB, dbConnErr = sql.Open("mysql", DBUSER+":"+DBPWD+"@tcp("+DBHOST+":"+DBPORT+")/"+DBNAME+"?charset=utf8") if dbConnErr != nil { log.Println("ERROR", "can not connect to Database, ", dbConnErr) + time.Sleep(time.Second * 5) } else { if len(conns) > 0 { DB.SetMaxOpenConns(conns[0]) //用于设置最大打开的连接数,默认值为0表示不限制 diff --git a/sqlserver.go b/sqlserver.go new file mode 100644 index 0000000..9c5be06 --- /dev/null +++ b/sqlserver.go @@ -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() +}