Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
杨树贤
/
search_server
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
b2e372a1
authored
Jul 06, 2020
by
huangchengyi
Browse files
Options
_('Browse Files')
Download
Plain Diff
Merge branch 'dev' of
http://119.23.72.7/mushishixian/search_server
into dev
# Conflicts: # test/test4.go
parents
d113977f
3ca364bd
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
24 additions
and
32 deletions
go.mod
pkg/mysql/mysql.go
go.mod
View file @
b2e372a1
...
@@ -7,6 +7,8 @@ require (
...
@@ -7,6 +7,8 @@ require (
github.com/gin-contrib/cors v1.3.1
github.com/gin-contrib/cors v1.3.1
github.com/gin-gonic/gin v1.6.3
github.com/gin-gonic/gin v1.6.3
github.com/go-ini/ini v1.57.0
github.com/go-ini/ini v1.57.0
github.com/go-sql-driver/mysql v1.5.0
github.com/go-xorm/xorm v0.7.9
github.com/gogo/protobuf v1.3.1 // indirect
github.com/gogo/protobuf v1.3.1 // indirect
github.com/golang/protobuf v1.4.2
github.com/golang/protobuf v1.4.2
github.com/gomodule/redigo v2.0.1-0.20180401191855-9352ab68be13+incompatible
github.com/gomodule/redigo v2.0.1-0.20180401191855-9352ab68be13+incompatible
...
@@ -33,8 +35,8 @@ require (
...
@@ -33,8 +35,8 @@ require (
github.com/stretchr/testify v1.5.1 // indirect
github.com/stretchr/testify v1.5.1 // indirect
github.com/syyongx/php2go v0.9.4
github.com/syyongx/php2go v0.9.4
github.com/tidwall/gjson v1.6.0
github.com/tidwall/gjson v1.6.0
github.com/urfave/cli v1.22.4 // indirect
github.com/uniplaces/carbon v0.1.6
github.com/uniplaces/carbon v0.1.6
github.com/urfave/cli v1.22.4 // indirect
go.etcd.io/bbolt v1.3.4
go.etcd.io/bbolt v1.3.4
go.uber.org/zap v1.14.1 // indirect
go.uber.org/zap v1.14.1 // indirect
golang.org/x/net v0.0.0-20200520182314-0ba52f642ac2 // indirect
golang.org/x/net v0.0.0-20200520182314-0ba52f642ac2 // indirect
...
...
pkg/mysql/mysql.go
View file @
b2e372a1
package
mysql
package
mysql
import
(
import
(
"fmt"
"github.com/go-xorm/xorm"
"github.com/jinzhu/gorm"
_
"github.com/go-sql-driver/mysql"
_
"github.com/jinzhu/gorm/dialects/mysql"
"search_server/pkg/config"
"search_server/pkg/config"
"search_server/pkg/e"
"search_server/pkg/e"
)
)
//mysql相关
var
DatabaseConMap
map
[
string
]
*
xorm
.
Engine
var
mysqlDb
,
bomDb
*
gorm
.
DB
var
DatabaseConMap
=
map
[
string
]
*
gorm
.
DB
{
"mysql"
:
mysqlDb
,
"bom"
:
bomDb
,
}
func
Setup
()
error
{
func
_setup
()
error
{
DatabaseConMap
=
make
(
map
[
string
]
*
xorm
.
Engine
,
0
)
DatabaseList
:=
config
.
BuildDatabaseList
()
DatabaseList
:=
config
.
BuildDatabaseList
()
var
err
error
var
err
error
//循环生成数据库链接
//循环生成数据库链接
...
@@ -26,34 +23,27 @@ func Setup() error {
...
@@ -26,34 +23,27 @@ func Setup() error {
host
:=
db
.
Host
host
:=
db
.
Host
database
:=
db
.
Database
database
:=
db
.
Database
if
db
.
Prefix
!=
""
{
dataSourceName
:=
userName
+
":"
+
password
+
"@tcp("
+
host
+
")/"
+
database
+
"?charset=utf8"
gorm
.
DefaultTableNameHandler
=
func
(
DB
*
gorm
.
DB
,
defaultTableName
string
)
string
{
return
db
.
Prefix
+
defaultTableName
}
}
DatabaseConMap
[
conName
],
err
=
gorm
.
Open
(
"mysql"
,
fmt
.
Sprintf
(
"%s:%s@tcp(%s:3306)/%s?charset=utf8&parseTime=True&loc=Local"
,
DatabaseConMap
[
conName
],
err
=
xorm
.
NewEngine
(
"mysql"
,
dataSourceName
)
userName
,
password
,
host
,
database
))
if
err
!=
nil
{
if
err
!=
nil
{
return
e
.
NewFatalError
(
err
.
Error
())
//这里返回致命异常
return
e
.
NewFatalError
(
err
.
Error
())
//这里返回致命异常
}
}
DatabaseConMap
[
conName
]
.
SingularTable
(
true
)
if
db
.
MaxIdleCons
==
0
{
//日志打印SQL
db
.
MaxIdleCons
=
10
DatabaseConMap
[
conName
]
.
ShowSQL
(
true
)
}
if
db
.
MaxOpenCons
==
0
{
//设置连接池的空闲数大小
db
.
MaxOpenCons
=
100
DatabaseConMap
[
conName
]
.
SetMaxIdleConns
(
db
.
MaxIdleCons
)
}
//设置最大打开连接数
DatabaseConMap
[
conName
]
.
DB
()
.
SetMaxIdleConns
(
db
.
MaxIdleCons
)
DatabaseConMap
[
conName
]
.
SetMaxOpenConns
(
db
.
MaxOpenCons
)
DatabaseConMap
[
conName
]
.
DB
()
.
SetMaxOpenConns
(
db
.
MaxOpenCons
)
if
config
.
Get
(
"gorm.mode"
)
.
String
()
==
"debug"
{
DatabaseConMap
[
conName
]
.
LogMode
(
true
)
}
}
}
return
nil
return
nil
}
}
func
Connection
(
conName
string
)
*
gorm
.
DB
{
func
Conn
(
conName
string
)
*
xorm
.
Engine
{
return
DatabaseConMap
[
conName
]
return
DatabaseConMap
[
conName
]
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment