mirror of
https://github.com/casdoor/casdoor.git
synced 2025-07-03 20:50:19 +08:00
Support more DBs in syncer.
This commit is contained in:
@ -20,8 +20,9 @@ import (
|
||||
|
||||
"github.com/astaxie/beego"
|
||||
"github.com/casbin/casdoor/conf"
|
||||
//_ "github.com/denisenkom/go-mssqldb" // db = mssql
|
||||
_ "github.com/go-sql-driver/mysql" // db = mysql
|
||||
//_ "github.com/lib/pq" // db = postgres
|
||||
//_ "github.com/lib/pq" // db = postgres
|
||||
"xorm.io/xorm"
|
||||
)
|
||||
|
||||
@ -85,13 +86,18 @@ func (a *Adapter) createDatabase() error {
|
||||
}
|
||||
|
||||
func (a *Adapter) open() {
|
||||
if a.driverName != "postgres" {
|
||||
if a.driverName != "postgres" && a.driverName != "mssql" {
|
||||
if err := a.createDatabase(); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
engine, err := xorm.NewEngine(a.driverName, a.dataSourceName+a.dbName)
|
||||
dataSourceName := a.dataSourceName + a.dbName
|
||||
if a.driverName != "mysql" {
|
||||
dataSourceName = a.dataSourceName
|
||||
}
|
||||
|
||||
engine, err := xorm.NewEngine(a.driverName, dataSourceName)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
@ -41,6 +41,7 @@ type Syncer struct {
|
||||
Port int `json:"port"`
|
||||
User string `xorm:"varchar(100)" json:"user"`
|
||||
Password string `xorm:"varchar(100)" json:"password"`
|
||||
DatabaseType string `xorm:"varchar(100)" json:"databaseType"`
|
||||
Database string `xorm:"varchar(100)" json:"database"`
|
||||
Table string `xorm:"varchar(100)" json:"table"`
|
||||
TablePrimaryKey string `xorm:"varchar(100)" json:"tablePrimaryKey"`
|
||||
|
@ -19,7 +19,6 @@ import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/astaxie/beego"
|
||||
"github.com/casbin/casdoor/util"
|
||||
"xorm.io/core"
|
||||
)
|
||||
@ -28,6 +27,10 @@ type OriginalUser = User
|
||||
|
||||
func (syncer *Syncer) getOriginalUsers() []*OriginalUser {
|
||||
sql := fmt.Sprintf("select * from %s", syncer.Table)
|
||||
if syncer.DatabaseType == "mssql" {
|
||||
sql = fmt.Sprintf("select * from [%s]", syncer.Table)
|
||||
}
|
||||
|
||||
results, err := syncer.Adapter.Engine.QueryString(sql)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
@ -142,11 +145,18 @@ func (syncer *Syncer) calculateHash(user *OriginalUser) string {
|
||||
|
||||
func (syncer *Syncer) initAdapter() {
|
||||
if syncer.Adapter == nil {
|
||||
dataSourceName := fmt.Sprintf("%s:%s@tcp(%s:%d)/", syncer.User, syncer.Password, syncer.Host, syncer.Port)
|
||||
var dataSourceName string
|
||||
if syncer.DatabaseType == "mssql" {
|
||||
dataSourceName = fmt.Sprintf("sqlserver://%s:%s@%s:%d?database=%s", syncer.User, syncer.Password, syncer.Host, syncer.Port, syncer.Database)
|
||||
} else {
|
||||
dataSourceName = fmt.Sprintf("%s:%s@tcp(%s:%d)/", syncer.User, syncer.Password, syncer.Host, syncer.Port)
|
||||
}
|
||||
|
||||
if !isCloudIntranet {
|
||||
dataSourceName = strings.ReplaceAll(dataSourceName, "dbi.", "db.")
|
||||
}
|
||||
syncer.Adapter = NewAdapter(beego.AppConfig.String("driverName"), dataSourceName, syncer.Database)
|
||||
|
||||
syncer.Adapter = NewAdapter(syncer.DatabaseType, dataSourceName, syncer.Database)
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user