代码拉取完成,页面将自动刷新
同步操作将从 K./go-adm 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
package adm
import (
"database/sql"
"errors"
"fmt"
)
type Connection struct {
name *string
isConn bool
conn *sql.DB
error error
}
var globalConns = make(map[string]*Connection)
func Conn(name string) *Connection {
conf := GetConf(name)
if conf == nil {
return &Connection{error: errors.New("Can't find config " + name + "!") }
}
conn, exists := globalConns[name]
if !exists {
globalConns[name] = &Connection{name: &conf.Name, isConn: false }
globalConns[name].connect(conf)
return globalConns[name]
}
return conn
}
func (this *Connection) connect(conf *XmlDb) {
if this.conn != nil { return }
db, err := sql.Open(conf.DSN, conf.GetConnDSN())
if err != nil {
this.error = err
return
}
db.Ping()
if err != nil {
this.error = err
return
}
// 先把db绑定,毕竟已经连接上了
if conf.MaxConn > 0 {
db.SetMaxOpenConns(conf.MaxConn)
}
if conf.MaxIdle > 0 {
db.SetMaxIdleConns(conf.MaxIdle)
}
if _, err := db.Exec("use " + conf.Db); err != nil {
this.error = err
return
}
this.conn = db
this.isConn = true
}
func (this *Connection) IsConn() bool {
return this.isConn
}
func (this *Connection) GetName() string {
return *this.name
}
func (this *Connection) GetDB() (*sql.DB, error) {
if !this.IsConn() {
if this.HasErr() {
return nil, this.GetErr()
} else {
return nil, errors.New(fmt.Sprintf("database %s does not connect", this.GetName()))
}
}
return this.conn, nil
}
func (this *Connection) GetErr() error {
return this.error
}
func (this *Connection) HasErr() bool {
return this.error != nil
}
func (this *Connection) Query(sql string, params ...interface{}) *ResultSet {
if !this.IsConn() {
if this.HasErr() {
return NewQueryResult(nil, this.error)
} else {
NewQueryResult(nil, errors.New(fmt.Sprintf("database %s does not connect", this.GetName())))
}
}
if len(sql) <= 0 {
NewQueryResult(nil, errors.New("empty sql!"))
}
rows, err := this.conn.Query(sql, params...)
return NewQueryResult(rows, err)
}
func (this *Connection) Find(query *QueryBuilder) *ResultSet {
err := query.Verify()
if err != nil {
return NewQueryResult(nil, err)
}
return this.Query(query.GetSQL(), query.GetParams()...)
}
func (this *Connection) Exec(t QueryType, sql string, params ...interface {}) *ExecResult {
if !this.IsConn() {
if this.HasErr() {
return NewExecResult(t, nil, this.error)
} else {
err := errors.New(fmt.Sprintf("database %s does not connect", this.GetName()))
return NewExecResult(t, nil, err)
}
}
if len(sql) <= 0 {
return NewExecResult(t, nil, errors.New("empty sql!"))
}
stmt, err := this.conn.Prepare(sql)
if err != nil {
if stmt != nil {
stmt.Close()
}
return NewExecResult(t, nil, err)
}
defer stmt.Close()
rs, err := stmt.Exec(params...)
return NewExecResult(t, rs, err)
}
func (this *Connection) execQuery(query *QueryBuilder) *ExecResult {
err := query.Verify()
t := query.GetQueryType()
if err != nil {
return NewExecResult(t, nil, err)
}
sql, params := query.GetExecSQLParams()
return this.Exec(t, sql, params...)
}
func (this *Connection) Insert(query *QueryBuilder) *ExecResult {
return this.execQuery(query)
}
func (this *Connection) Update(query *QueryBuilder) *ExecResult {
return this.execQuery(query)
}
func (this *Connection) Delete(query *QueryBuilder) *ExecResult {
return this.execQuery(query)
}
//func (this *Connection) QueryRow(query *Query) (*sql.Row) {
// if !this.IsConn() {
// return nil
// }
// return this.conn.QueryRow(query.GetSQL(), query.GetParams() ...)
//}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。