Browse Source

修改库连接操作

master v0.5.1
guzeng 3 years ago
parent
commit
c8293a8936
3 changed files with 47 additions and 16 deletions
  1. +13
    -7
      chain.go
  2. +27
    -7
      conn.go
  3. +7
    -2
      sqlserver.go

+ 13
- 7
chain.go View File

@ -29,25 +29,31 @@ type Query struct {
stmt *sql.Stmt stmt *sql.Stmt
conn *sql.DB conn *sql.DB
debug bool debug bool
dbtype string
} }
func NewQuery(t ...string) *Query { func NewQuery(t ...string) *Query {
var conn_type *sql.DB = DB var conn_type *sql.DB = DB
var db_type string = "mysql"
if len(t) > 0 { if len(t) > 0 {
switch t[0] { switch t[0] {
case "mysql": case "mysql":
conn_type = DB conn_type = DB
db_type = "mysql"
case "mssql": //sql server case "mssql": //sql server
conn_type = MSDB_CONN conn_type = MSDB_CONN
db_type = "mssql"
} }
} }
return &Query{ return &Query{
conn: conn_type,
conn: conn_type,
dbtype: db_type,
} }
} }
@ -149,7 +155,7 @@ func (this *Query) QueryStmt() error {
return errors.New("参数错误,没有数据表") return errors.New("参数错误,没有数据表")
} }
table := getTableName(this.dbname, this.table)
table := getTableName(this.dbname, this.table, this.dbtype)
// var err error // var err error
@ -174,9 +180,9 @@ func (this *Query) QueryStmt() error {
continue continue
} }
if len(joinitem) == 3 { if len(joinitem) == 3 {
sql = helper.StringJoin(sql, " ", joinitem[2], " join ", getTableName(this.dbname, joinitem[0]), " on ", joinitem[1])
sql = helper.StringJoin(sql, " ", joinitem[2], " join ", getTableName(this.dbname, joinitem[0], this.dbtype), " on ", joinitem[1])
} else { //默认左连接 } else { //默认左连接
sql = helper.StringJoin(sql, " left join ", getTableName(this.dbname, joinitem[0]), " on ", joinitem[1])
sql = helper.StringJoin(sql, " left join ", getTableName(this.dbname, joinitem[0], this.dbtype), " on ", joinitem[1])
} }
} }
} }
@ -250,7 +256,7 @@ func (this *Query) UpdateStmt() error {
return errors.New("参数错误,缺少条件") return errors.New("参数错误,缺少条件")
} }
dbName := getTableName(this.dbname, this.table)
dbName := getTableName(this.dbname, this.table, this.dbtype)
var sql string var sql string
@ -294,7 +300,7 @@ func (this *Query) CreateStmt() error {
return errors.New("参数错误,没有数据表") return errors.New("参数错误,没有数据表")
} }
dbName := getTableName(this.dbname, this.table)
dbName := getTableName(this.dbname, this.table, this.dbtype)
var sql string var sql string
@ -339,7 +345,7 @@ func (this *Query) DeleteStmt() error {
return errors.New("参数错误,缺少条件") return errors.New("参数错误,缺少条件")
} }
dbName := getTableName(this.dbname, this.table)
dbName := getTableName(this.dbname, this.table, this.dbtype)
var sql string var sql string


+ 27
- 7
conn.go View File

@ -58,16 +58,36 @@ func CloseConn() error {
/** /**
* 检测表名 * 检测表名
*/ */
func getTableName(dbName, table string) string {
func getTableName(dbName, table string, dbtype ...string) string {
if strings.Contains(table, ".") {
return table
var db_type string = "mysql"
if len(dbtype) > 0 {
if dbtype[0] != "" {
db_type = dbtype[0]
}
} }
if dbName != "" {
return helper.StringJoin(dbName, ".", table)
} else {
return table
var ret string
switch db_type {
case "mysql":
if strings.Contains(table, ".") {
ret = table
}
if dbName != "" {
ret = helper.StringJoin(dbName, ".", table)
} else {
ret = table
}
case "mssql":
ret = helper.StringJoin(dbName, ".", table)
} }
return ret
} }
func GetDbTableName(dbName, table string) string { func GetDbTableName(dbName, table string) string {


+ 7
- 2
sqlserver.go View File

@ -4,6 +4,7 @@ import (
"database/sql" "database/sql"
"errors" "errors"
"fmt" "fmt"
"strconv"
"log" "log"
@ -26,7 +27,9 @@ func MSConnect(DBHOST, DBUSER, DBPWD, DBNAME, DBPORT string, conns ...int) error
//连接字符串 //连接字符串
connString := fmt.Sprintf("server=%s;port%d;database=%s;user id=%s;password=%s", DBHOST, DBPORT, DBNAME, DBUSER, DBPWD)
db_port, _ := strconv.Atoi(DBPORT)
connString := fmt.Sprintf("server=%s;port=%d;database=%s;user id=%s;password=%s", DBHOST, db_port, DBNAME, DBUSER, DBPWD)
log.Println(connString) log.Println(connString)
@ -39,7 +42,9 @@ func MSConnect(DBHOST, DBUSER, DBPWD, DBNAME, DBPORT string, conns ...int) error
time.Sleep(time.Second * 5) time.Sleep(time.Second * 5)
} else { } else {
log.Println("msdb connected")
err = MSDB_CONN.Ping()
log.Println("msdb connected", err)
break break
} }


Loading…
Cancel
Save