我正在开发一个带有多个数据库的Go RESTful API应用程序。启动服务器时,用户提供要使用的数据库。
在应用程序中,我有三个函数,其中一个函数处理连接:selectedDb.Get()、selectedDb.Add()、selectedDb.Connect()。
如果有人选择Mysql,它会为Mysql处理事情,如果有人选择MongoDB,它会为Mongo处理事情等等。
我就是这样努力做到这一点的:
DbInterface.go
package dbinit
type Object struct {
Uuid string
Object string
Deleted bool
}
// The interface that all connectors should have
type Intfc interface {
Connect() HERE_LIES_MY_PROBLEM
Add(string, string, string) string
Get(string) (Object, bool)
}MySQL.go
package mysqlConnector
import (
...
)
type Mysql struct{}
// Connect to mysql
func (f Mysql) Connect() HERE_LIES_MY_PROBLEM {
client, err = sql.Open("mysql", "yourusername:yourpassword@/yourdatabase")
if err != nil {
panic(err.Error())
}
return client
}
// Add item to DB
func (f Mysql) Add(owner string, refType string, object string) string {
// do stuff related to this DB
return // a string
}
func (f Mysql) Get(Uuid string) (dbinit.Object, bool) {
// do stuff related to this DB
return // an object and a bool
}Mongo.go
package mongoConnector
import (
...
)
type Mongo struct{}
// Connect to mongo
func (f Mongo) Connect() HERE_LIES_MY_PROBLEM {
info := &mgo.DialInfo{
Addrs: []string{hosts},
Timeout: 60 * time.Second,
Database: database,
Username: username,
Password: password,
}
client, err := mgo.DialWithInfo(info)
if err != nil {
panic(err)
}
return client
}
// Add item to DB
func (f Mongo) Add(owner string, refType string, object string) string {
// do stuff related to this DB
return // a string
}
func (f Mongo) Get(Uuid string) (dbinit.Object, bool) {
// do stuff related to this DB
return // an object and a bool
}main.go
...
var selectedDb dbinit.Intfc
commandLineInput := "mysql" // just for the example
if commandLineInput == "mysql" {
selectedDb = mysqlConnector.Mysql{}
} else if commandLineInput == "mongo" {
selectedDb = mongoConnector.Mongo{}
}
client := selectedDb.Connect()
// this runs everytime the API is called
api.HandlerFoobar = foobar.handlerFunction(func(params foobar.Params) middleware.Responder {
// Here I want to add something to the selected dbinit
selectedDb.Get(client, addStringA, addStringB, addStringC)
return // the API response
})
...问题陈述
当我返回Mysql的客户端时,它不适用于Mongo,而visa则相反。
我只想在启动服务器并将client存储在客户端变量中时才连接到数据库。但是,问题是Mongo返回的客户端比Mysql还多,依此类推。
HERE_LIES_MY_PROBLEM的地方应该是什么?发布于 2017-05-08 06:55:17
如果希望保留这些方法的接口,则应将接口更改为:
接口:
// The interface that all connectors should have
type Intfc interface {
// Connect to the database, if an error occur at the moment
// of connection, return the error
Connect() error
// Add returns a string, it returns an error if something occurs
Add(string, string, string) (string, error)
Get(string) (Object, bool)
}MySQL:
type Mysql struct{
conn *sql.DB // contains the connection to the DB
}
// Connect to mysql
func (f *Mysql) Connect() error {
conn, err := sql.Open("mysql", "yourusername:yourpassword@/yourdatabase")
if err != nil {
return error
}
f.conn = conn
return nil
}
// Add item to DB
func (f *Mysql) Add(owner string, refType string, object string) (string, error) {
// do something
return // a string and error
}
func (f *Mysql) Get(Uuid string) (dbinit.Object, bool) {
// do something
return // an object and a bool
}Mongo:
type Mongo struct{
session *mgo.Session
}
// Connect to mongo
func (f *Mongo) Connect() error {
info := &mgo.DialInfo{
// some data
}
session, err := mgo.DialWithInfo(info)
if err != nil {
return error
}
f.session = session
return nil
}
// Add item to DB
func (f *Mongo) Add(owner string, refType string, object string) (string, error) {
// do something
return // a string and error (it could be nil at success)
}
func (f *Mongo) Get(Uuid string) (dbinit.Object, bool) {
// do something
return // an object and a bool
}Main:
var selectedDb dbinit.Intfc
commandLineInput := "mysql"
if commandLineInput == "mysql" {
selectedDb = &mysqlConnector.Mysql{}
} else if commandLineInput == "mongo" {
selectedDb = &mongoConnector.Mongo{}
}
err := selectedDb.Connect()
if err != nil {
panic(err)
}
// this runs everytime the API is called
api.HandlerFoobar = foobar.handlerFunction(func(params foobar.Params) middleware.Responder {
data, err := selectedDb.Add(addStringA, addStringB, addStringC)
if err != nil {
// do something
}
return // the API response
})但是您也可以从Connect() error中删除Intfc方法,只需使用Add和Get,但是您应该更新包如下:
Mysql
// Connect to mysql, it could be any function name
func Connect() (*Mysql, error) {
connection, err := sql.Open("mysql", "yourusername:yourpassword@/yourdatabase")
if err != nil {
return nil, error
}
return &Mysql{conn: connection}
}Mongo
// Connect to mongo, it could be any function name
func Connect() (*Mongo, error) {
info := &mgo.DialInfo{
// some data
}
s, err := mgo.DialWithInfo(info)
if err != nil {
return nil, error
}
return &Mongo{session: s}
}主
var selectedDb dbinit.Intfc
var err error
commandLineInput := "mysql"
if commandLineInput == "mysql" {
selectedDb, err = mysqlConnector.Connect()
} else if commandLineInput == "mongo" {
selectedDb, err = mongoConnector.Connect()
}
if err != nil {
panic(err)
}发布于 2017-05-03 11:40:36
详细说明我的意见,而不是
type Intfc interface {
Connect() HERE_LIES_MY_PROBLEM
Add(string, string, string) string
Get(string) (Object, bool)
}您可以使用
type Intfc interface {
Connect() DBClient
}和
type DBClient interface {
Add(string, string, string) string
Get(string) (Object, bool)
}
type MySQLClient sql.DB
type MongoClient mgo.Session
func (f Mysql) Connect() DBCLient {
client, err = sql.Open("mysql", "yourusername:yourpassword@/yourdatabase")
if err != nil {
panic(err.Error())
}
return MySQLClient(client)
}
func (f Mongo) Connect() DBClient {
info := &mgo.DialInfo{
Addrs: []string{hosts},
Timeout: 60 * time.Second,
Database: database,
Username: username,
Password: password,
}
client, err := mgo.DialWithInfo(info)
if err != nil {
panic(err)
}
return MongoClient(client)
}
func (s *MySQLClient) Add(...) {
// ...
}
func (s *MongoClient) Add(...) {
// ...
}发布于 2017-05-03 11:42:15
我认为,接口Intfc (或更好的名称DbIntfc)应该只有方法Get和Add。
它应该存在另一个函数--但不是DbIntfc的一部分,它返回DbIntfc --连接到MySql或MongoDb。让我们看看:
type MySqlDbIntfc struct{
db *Sql.DB
}
// Connect to mysql
func NewMySqlDbIntfc() (DbIntfc,error) {
// Please do not prefer panic in such abstract methods
client, err := sql.Open("mysql", "yourusername:yourpassword@/yourdatabase")
if err != nil {
return nil, err
}
return &MySqlDbIntfc{client}, nil
}
func (mySqlDb *MySqlDbIntfc) Get(Uuid string) (dbinit.Object, error) {
var obj dbinit.Object
err := mySqlDb.db.QueryRow("SELECT uuid, object, deleted FROM myTable WHERE uuid=?", Uuid).Scan(&obj.Uuid, &obj.Object, &obj.Deleted)
if err != nil {
return dbinit.Object{}, err
}
return obj, nil
}实现NewMgoDbIntfc应该很简单,包括NewMgoDbIntfc.Add/Get方法。
决定是否使用NewMySqlDbIntfc或NewMgoDbIntfc也应该很容易。
https://stackoverflow.com/questions/43755892
复制相似问题