在高隆:如何处理未知变量或如何处理多个数据库?

前端之家收集整理的这篇文章主要介绍了在高隆:如何处理未知变量或如何处理多个数据库?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在使用具有多个数据库的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,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,反之亦然.

我仅在启动服务器时将数据库连接到数据库,并将客户端存储在客户端变量中.然而,问题是Mongo返回的是另一个客户端,而不是MysqL.

>在代码中我有HERE_LIES_MY_PROBLEM的地方应该是什么?
>还是要让Go的范例错误处理这些事情?

解决方法

如果要使用这些方法保留界面,则应该将您的界面更改为:

接口:

// 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,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,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
}

蒙戈:

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,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
}

主要:

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,addStringC)
    if err != nil {
        // do something
    }

    return // the API response
})

但是您也可以从Intfc中删除Connect()错误方法,只需使用Add和Get即可,但应更新如下所示的软件包:

MysqL

// Connect to MysqL,it could be any function name
func Connect() (*MysqL,error) {
    connection,"yourusername:yourpassword@/yourdatabase")
    if err != nil {
        return nil,error
    }
    return &MysqL{conn: connection}
}

蒙戈

// 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)
}

猜你在找的MsSQL相关文章