Skip to content

Commit

Permalink
ADD:db util
Browse files Browse the repository at this point in the history
  • Loading branch information
kylin2017 committed Jan 21, 2019
1 parent bd75df4 commit 3b766ba
Show file tree
Hide file tree
Showing 4 changed files with 281 additions and 0 deletions.
115 changes: 115 additions & 0 deletions db/instance.go
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
}
152 changes: 152 additions & 0 deletions db/sql.go
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)
}
4 changes: 4 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,18 @@ module github.com/shawnfeng/sutil

require (
github.com/BurntSushi/toml v0.3.1 // indirect
github.com/bitly/go-simplejson v0.5.0
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/fzzy/radix v0.4.9-0.20141113025130-a3a55de9c594
github.com/go-redis/redis v6.15.1+incompatible
github.com/go-sql-driver/mysql v1.4.1
github.com/golang/protobuf v0.0.0-20140729232320-25535e35a86c
github.com/google/uuid v1.1.0
github.com/jmoiron/sqlx v1.2.0
github.com/julienschmidt/httprouter v1.0.1-0.20150106073633-b55664b9e920
github.com/kaneshin/go-pkg v0.0.0-20150919125626-a8e1479186cf
github.com/kr/pretty v0.1.0 // indirect
github.com/lib/pq v1.0.0
github.com/pkg/errors v0.8.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/sdming/gosnow v0.0.0-20130403030620-3a05c415e886
Expand Down
10 changes: 10 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/bitly/go-simplejson v0.5.0 h1:6IH+V8/tVMab511d5bn4M7EwGXZf9Hj6i2xSwkNEM+Y=
github.com/bitly/go-simplejson v0.5.0/go.mod h1:cXHtHw4XUPsvGaxgjIAn8PhEWG9NfngEKAMDJEczWVA=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/fzzy/radix v0.4.9-0.20141113025130-a3a55de9c594 h1:oNI7duAqnx59p+HQvLXlVcACWG50vb0QhH1JVdhnqCk=
github.com/fzzy/radix v0.4.9-0.20141113025130-a3a55de9c594/go.mod h1:KhtJfdbo4PD2LEOYO7QCVSIH0pOcZEZ/SpNsXgwQtkk=
github.com/go-redis/redis v6.15.1+incompatible h1:BZ9s4/vHrIqwOb0OPtTQ5uABxETJ3NRuUNoSUurnkew=
github.com/go-redis/redis v6.15.1+incompatible/go.mod h1:NAIEuMOZ/fxfXJIrKDQDz8wamY7mA7PouImQ2Jvg6kA=
github.com/go-sql-driver/mysql v1.4.0/go.mod h1:zAC/RDZ24gD3HViQzih4MyKcchzm+sOG5ZlKdlhCg5w=
github.com/go-sql-driver/mysql v1.4.1 h1:g24URVg0OFbNUTx9qqY1IRZ9D9z3iPyi5zKhQZpNwpA=
github.com/go-sql-driver/mysql v1.4.1/go.mod h1:zAC/RDZ24gD3HViQzih4MyKcchzm+sOG5ZlKdlhCg5w=
github.com/golang/protobuf v0.0.0-20140729232320-25535e35a86c h1:htMd+c4wJLC4hd4/rFtIjFeMtYzrcV+wer42v7D5Rj0=
github.com/golang/protobuf v0.0.0-20140729232320-25535e35a86c/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/google/uuid v1.1.0 h1:Jf4mxPC/ziBnoPIdpQdPJ9OeiomAUHLvxmPRSPH9m4s=
github.com/google/uuid v1.1.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/jmoiron/sqlx v1.2.0 h1:41Ip0zITnmWNR/vHV+S4m+VoUivnWY5E4OJfLZjCJMA=
github.com/jmoiron/sqlx v1.2.0/go.mod h1:1FEQNm3xlJgrMD+FBdI9+xvCksHtbpVBBw5dYhBSsks=
github.com/julienschmidt/httprouter v1.0.1-0.20150106073633-b55664b9e920 h1:daoMBd4Qxjst0piA1oPTvN42YR+reI7oZdXWGXh8ad0=
github.com/julienschmidt/httprouter v1.0.1-0.20150106073633-b55664b9e920/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w=
github.com/kaneshin/go-pkg v0.0.0-20150919125626-a8e1479186cf h1:kBpksZbhb1xNFDTPbxh5H5G0o0MJZKrH004ELvq/zWI=
Expand All @@ -19,6 +26,9 @@ github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORN
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/lib/pq v1.0.0 h1:X5PMW56eZitiTeO7tKzZxFCSpbFZJtkMMooicw2us9A=
github.com/lib/pq v1.0.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
github.com/mattn/go-sqlite3 v1.9.0/go.mod h1:FPy6KqzDD04eiIsT53CuJW3U88zkxoIYsOqkbpncsNc=
github.com/pkg/errors v0.8.0 h1:WdK/asTD0HN+q6hsWO3/vpuAkAr+tw6aNJNDFFf0+qw=
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
Expand Down

0 comments on commit 3b766ba

Please sign in to comment.