forked from shawnfeng/sutil
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
281 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
// Copyright 2014 The dbrouter Author. All rights reserved. | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package db | ||
|
||
import ( | ||
"fmt" | ||
"github.com/shawnfeng/sutil/slog" | ||
"sync" | ||
"time" | ||
) | ||
|
||
const ( | ||
DB_TYPE_MONGO = "mongo" | ||
DB_TYPE_MYSQL = "mysql" | ||
DB_TYPE_POSTGRES = "postgres" | ||
) | ||
|
||
type ConfigInfo struct { | ||
DBAddr string | ||
UserName string | ||
PassWord string | ||
TimeOut time.Duration | ||
} | ||
|
||
type DBConfiger interface { | ||
GetConfig(dbType, dbName string) *ConfigInfo | ||
} | ||
|
||
type DefaultDBConfig struct { | ||
userName string | ||
passWord string | ||
} | ||
|
||
func NewDefaultDBConfig(servName string) DBConfiger { | ||
passWord := "MegQqwb@RPZWI55N" | ||
return &DefaultDBConfig{ | ||
userName: servName, | ||
passWord: passWord, | ||
} | ||
} | ||
|
||
func (m *DefaultDBConfig) GetConfig(dbType, dbName string) *ConfigInfo { | ||
return &ConfigInfo{ | ||
DBAddr: "common.kingshard.pri.ibbanyu.com:4000", | ||
UserName: m.userName, | ||
PassWord: m.passWord, | ||
TimeOut: 3 * time.Second, | ||
} | ||
} | ||
|
||
type DBInstance interface { | ||
getType() string | ||
} | ||
|
||
func NewInstance(dbtype, dbname, addr, userName, passWord string, timeout time.Duration) (DBInstance, error) { | ||
|
||
switch dbtype { | ||
case DB_TYPE_POSTGRES: | ||
return NewdbSql(dbtype, dbname, addr, userName, passWord, timeout) | ||
|
||
case DB_TYPE_MYSQL: | ||
return NewdbSql(dbtype, dbname, addr, userName, passWord, timeout) | ||
|
||
default: | ||
return nil, fmt.Errorf("dbtype %s error", dbtype) | ||
} | ||
} | ||
|
||
type DBInstanceManager struct { | ||
instances sync.Map | ||
config DBConfiger | ||
} | ||
|
||
func NewDBInstanceManager(servName string) *DBInstanceManager { | ||
config := NewDefaultDBConfig(servName) | ||
return &DBInstanceManager{ | ||
config: config, | ||
} | ||
} | ||
|
||
func (m *DBInstanceManager) buildKey(dbtype, dbname string) string { | ||
return fmt.Sprintf("%s.%s", dbtype, dbname) | ||
} | ||
|
||
func (m *DBInstanceManager) add(dbtype, dbname string, ins DBInstance) { | ||
m.instances.Store(m.buildKey(dbtype, dbname), ins) | ||
} | ||
|
||
func (m *DBInstanceManager) get(dbtype, dbname string) DBInstance { | ||
fun := "DBInstanceManager.get-->" | ||
|
||
var err error | ||
var dbIn DBInstance | ||
name := m.buildKey(dbtype, dbname) | ||
in, ok := m.instances.Load(name) | ||
if ok == false { | ||
|
||
configInfo := m.config.GetConfig(dbtype, dbname) | ||
dbIn, err = NewInstance(dbtype, dbname, configInfo.DBAddr, configInfo.UserName, configInfo.PassWord, configInfo.TimeOut) | ||
if err != nil { | ||
slog.Errorf("%s NewInstance err, dbname: %s, err: %s", fun, dbname, err.Error()) | ||
} | ||
return dbIn | ||
} | ||
|
||
dbIn, ok = in.(DBInstance) | ||
if ok == false { | ||
slog.Errorf("%s ins.(DBInstance) err, name: %s", fun, name) | ||
return nil | ||
} | ||
|
||
return dbIn | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
// Copyright 2013 The dbrouter Author. All rights reserved. | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package db | ||
|
||
import ( | ||
"database/sql" | ||
"fmt" | ||
_ "github.com/go-sql-driver/mysql" | ||
"github.com/jmoiron/sqlx" | ||
_ "github.com/lib/pq" | ||
"github.com/shawnfeng/sutil/slog" | ||
"github.com/shawnfeng/sutil/stime" | ||
"time" | ||
) | ||
|
||
type DB struct { | ||
*sqlx.DB | ||
} | ||
|
||
func (db *DB) NamedExecWrapper(tables []interface{}, query string, arg interface{}) (sql.Result, error) { | ||
query = fmt.Sprintf(query, tables...) | ||
return db.DB.NamedExec(query, arg) | ||
} | ||
|
||
func (db *DB) NamedQueryWrapper(tables []interface{}, query string, arg interface{}) (*sqlx.Rows, error) { | ||
query = fmt.Sprintf(query, tables...) | ||
return db.DB.NamedQuery(query, arg) | ||
} | ||
|
||
func (db *DB) SelectWrapper(tables []interface{}, dest interface{}, query string, args ...interface{}) error { | ||
query = fmt.Sprintf(query, tables...) | ||
return db.DB.Select(dest, query, args...) | ||
} | ||
|
||
func (db *DB) ExecWrapper(tables []interface{}, query string, args ...interface{}) (sql.Result, error) { | ||
query = fmt.Sprintf(query, tables...) | ||
return db.DB.Exec(query, args...) | ||
} | ||
|
||
func (db *DB) QueryRowxWrapper(tables []interface{}, query string, args ...interface{}) *sqlx.Row { | ||
query = fmt.Sprintf(query, tables...) | ||
return db.DB.QueryRowx(query, args...) | ||
} | ||
|
||
func (db *DB) QueryxWrapper(tables []interface{}, query string, args ...interface{}) (*sqlx.Rows, error) { | ||
query = fmt.Sprintf(query, tables...) | ||
return db.DB.Queryx(query, args...) | ||
} | ||
|
||
func (db *DB) GetWrapper(tables []interface{}, dest interface{}, query string, args ...interface{}) error { | ||
query = fmt.Sprintf(query, tables...) | ||
return db.DB.Get(dest, query, args...) | ||
} | ||
|
||
func NewDB(sqlxdb *sqlx.DB) *DB { | ||
db := &DB{ | ||
sqlxdb, | ||
} | ||
return db | ||
} | ||
|
||
type dbSql struct { | ||
dbType string | ||
dbName string | ||
dbAddr string | ||
timeOut time.Duration | ||
userName string | ||
passWord string | ||
db *DB | ||
} | ||
|
||
func (m *dbSql) getType() string { | ||
return m.dbType | ||
} | ||
|
||
func NewdbSql(dbtype, dbname, addr, userName, passWord string, timeout time.Duration) (*dbSql, error) { | ||
|
||
if timeout == 0 { | ||
timeout = 3 * time.Second | ||
} | ||
|
||
info := &dbSql{ | ||
dbType: dbtype, | ||
dbName: dbname, | ||
dbAddr: addr, | ||
timeOut: timeout, | ||
userName: userName, | ||
passWord: passWord, | ||
} | ||
|
||
var err error | ||
info.db, err = dial(info) | ||
info.db.SetMaxIdleConns(32) | ||
return info, err | ||
} | ||
|
||
func dial(info *dbSql) (db *DB, err error) { | ||
fun := "dial-->" | ||
|
||
var dataSourceName string | ||
if info.dbType == DB_TYPE_MYSQL { | ||
dataSourceName = fmt.Sprintf("%s:%s@tcp(%s)/%s", info.userName, info.passWord, info.dbAddr, info.dbName) | ||
|
||
} else if info.dbType == DB_TYPE_POSTGRES { | ||
dataSourceName = fmt.Sprintf("postgres://%s:%s@%s/%s?sslmode=disable", | ||
info.userName, info.passWord, info.dbAddr, info.dbName) | ||
} | ||
|
||
slog.Infof("%s dbtype:%s datasourcename:%s", fun, info.dbType, dataSourceName) | ||
sqlxdb, err := sqlx.Connect(info.dbType, dataSourceName) | ||
return NewDB(sqlxdb), err | ||
} | ||
|
||
func (m *dbSql) getDB() *DB { | ||
return m.db | ||
} | ||
|
||
func (m *DBInstanceManager) SqlExec(dbName string, query func(*DB, []interface{}) error, tables ...string) error { | ||
fun := "SqlExec-->" | ||
|
||
st := stime.NewTimeStat() | ||
|
||
if len(tables) <= 0 { | ||
return fmt.Errorf("tables is empty") | ||
} | ||
|
||
ins := m.get(DB_TYPE_MYSQL, dbName) | ||
if ins == nil { | ||
return fmt.Errorf("db instance not find: dbname:%s", dbName) | ||
} | ||
|
||
dbsql, ok := ins.(*dbSql) | ||
if !ok { | ||
return fmt.Errorf("db instance type error: dbname:%s, dbtype:%s", dbName, ins.getType()) | ||
} | ||
|
||
db := dbsql.getDB() | ||
|
||
defer func() { | ||
dur := st.Duration() | ||
slog.Infof("%s type:%s dbname:%s query:%d", fun, ins.getType(), dbName, dur) | ||
}() | ||
|
||
var tmptables []interface{} | ||
for _, item := range tables { | ||
tmptables = append(tmptables, item) | ||
} | ||
|
||
return query(db, tmptables) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters