u
This commit is contained in:
1 parent
7552670e4a
commit
01ea998605
13 files changed
+173
-45
No files matched your search
@@ -0,0 +1,38 @@
|
||||
package db
|
||||
|
||||
import (
|
||||
"base-framework/pkg/config"
|
||||
"base-framework/pkg/router"
|
||||
"base-framework/pkg/utils/response"
|
||||
"database/sql"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
// Client 封装数据库连接对象
|
||||
type Client struct {
|
||||
Conn *sql.DB
|
||||
}
|
||||
|
||||
// NewClient 根据 context 获取 orgId 并返回 Client 对象
|
||||
func NewClient(c *router.Context) *Client {
|
||||
val, ok := c.Get("orgId")
|
||||
fmt.Println(val)
|
||||
if !ok {
|
||||
response.Error(c).Code(response.CodeInvalidOrgCode).Send()
|
||||
return nil
|
||||
}
|
||||
|
||||
orgId, ok := val.(string)
|
||||
if !ok || orgId == "" {
|
||||
response.Error(c).Code(response.CodeInvalidOrgCode).Send()
|
||||
return nil
|
||||
}
|
||||
|
||||
conn, ok := config.GetDB(orgId)
|
||||
if !ok || conn == nil {
|
||||
response.Error(c).Code(response.CodeInvalidOrgCode).Send()
|
||||
return nil
|
||||
}
|
||||
|
||||
return &Client{Conn: conn}
|
||||
}
|
||||
Reference in new issue
Block a user