Support more DBs in syncer.

This commit is contained in:
Yang Luo 2021-12-24 22:53:35 +08:00
parent cc8c9b32ef
commit e79e3c36d0
12 changed files with 100 additions and 10 deletions

View File

@ -20,8 +20,9 @@ import (
"github.com/astaxie/beego" "github.com/astaxie/beego"
"github.com/casbin/casdoor/conf" "github.com/casbin/casdoor/conf"
//_ "github.com/denisenkom/go-mssqldb" // db = mssql
_ "github.com/go-sql-driver/mysql" // db = mysql _ "github.com/go-sql-driver/mysql" // db = mysql
//_ "github.com/lib/pq" // db = postgres //_ "github.com/lib/pq" // db = postgres
"xorm.io/xorm" "xorm.io/xorm"
) )
@ -85,13 +86,18 @@ func (a *Adapter) createDatabase() error {
} }
func (a *Adapter) open() { func (a *Adapter) open() {
if a.driverName != "postgres" { if a.driverName != "postgres" && a.driverName != "mssql" {
if err := a.createDatabase(); err != nil { if err := a.createDatabase(); err != nil {
panic(err) 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 { if err != nil {
panic(err) panic(err)
} }

View File

@ -41,6 +41,7 @@ type Syncer struct {
Port int `json:"port"` Port int `json:"port"`
User string `xorm:"varchar(100)" json:"user"` User string `xorm:"varchar(100)" json:"user"`
Password string `xorm:"varchar(100)" json:"password"` Password string `xorm:"varchar(100)" json:"password"`
DatabaseType string `xorm:"varchar(100)" json:"databaseType"`
Database string `xorm:"varchar(100)" json:"database"` Database string `xorm:"varchar(100)" json:"database"`
Table string `xorm:"varchar(100)" json:"table"` Table string `xorm:"varchar(100)" json:"table"`
TablePrimaryKey string `xorm:"varchar(100)" json:"tablePrimaryKey"` TablePrimaryKey string `xorm:"varchar(100)" json:"tablePrimaryKey"`

View File

@ -19,7 +19,6 @@ import (
"strings" "strings"
"time" "time"
"github.com/astaxie/beego"
"github.com/casbin/casdoor/util" "github.com/casbin/casdoor/util"
"xorm.io/core" "xorm.io/core"
) )
@ -28,6 +27,10 @@ type OriginalUser = User
func (syncer *Syncer) getOriginalUsers() []*OriginalUser { func (syncer *Syncer) getOriginalUsers() []*OriginalUser {
sql := fmt.Sprintf("select * from %s", syncer.Table) 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) results, err := syncer.Adapter.Engine.QueryString(sql)
if err != nil { if err != nil {
panic(err) panic(err)
@ -142,11 +145,18 @@ func (syncer *Syncer) calculateHash(user *OriginalUser) string {
func (syncer *Syncer) initAdapter() { func (syncer *Syncer) initAdapter() {
if syncer.Adapter == nil { 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 { if !isCloudIntranet {
dataSourceName = strings.ReplaceAll(dataSourceName, "dbi.", "db.") dataSourceName = strings.ReplaceAll(dataSourceName, "dbi.", "db.")
} }
syncer.Adapter = NewAdapter(beego.AppConfig.String("driverName"), dataSourceName, syncer.Database)
syncer.Adapter = NewAdapter(syncer.DatabaseType, dataSourceName, syncer.Database)
} }
} }

View File

@ -158,6 +158,24 @@ class SyncerEditPage extends React.Component {
}} /> }} />
</Col> </Col>
</Row> </Row>
<Row style={{marginTop: '20px'}} >
<Col style={{marginTop: '5px'}} span={(Setting.isMobile()) ? 22 : 2}>
{Setting.getLabel(i18next.t("syncer:Database type"), i18next.t("syncer:Database type - Tooltip"))} :
</Col>
<Col span={22} >
<Select virtual={false} style={{width: '100%'}} value={this.state.syncer.databaseType} onChange={(value => {this.updateSyncerField('databaseType', value);})}>
{
[
{id: 'mysql', name: 'MySQL'},
{id: 'postgres', name: 'PostgreSQL'},
{id: 'mssql', name: 'SQL Server'},
{id: 'oracle', name: 'Oracle'},
{id: 'sqlite3', name: 'Sqlite 3'},
].map((databaseType, index) => <Option key={index} value={databaseType.id}>{databaseType.name}</Option>)
}
</Select>
</Col>
</Row>
<Row style={{marginTop: '20px'}} > <Row style={{marginTop: '20px'}} >
<Col style={{marginTop: '5px'}} span={(Setting.isMobile()) ? 22 : 2}> <Col style={{marginTop: '5px'}} span={(Setting.isMobile()) ? 22 : 2}>
{Setting.getLabel(i18next.t("syncer:Database"), i18next.t("syncer:Database - Tooltip"))} : {Setting.getLabel(i18next.t("syncer:Database"), i18next.t("syncer:Database - Tooltip"))} :

View File

@ -58,6 +58,7 @@ class SyncerListPage extends React.Component {
port: 3306, port: 3306,
user: "root", user: "root",
password: "123456", password: "123456",
databaseType: "mysql",
database: "dbName", database: "dbName",
table: "tableName", table: "tableName",
tableColumns: [], tableColumns: [],
@ -176,6 +177,13 @@ class SyncerListPage extends React.Component {
width: '120px', width: '120px',
sorter: (a, b) => a.password.localeCompare(b.password), sorter: (a, b) => a.password.localeCompare(b.password),
}, },
{
title: i18next.t("syncer:Database type"),
dataIndex: 'databaseType',
key: 'databaseType',
width: '120px',
sorter: (a, b) => a.databaseType.localeCompare(b.databaseType),
},
{ {
title: i18next.t("syncer:Database"), title: i18next.t("syncer:Database"),
dataIndex: 'database', dataIndex: 'database',

View File

@ -40,6 +40,7 @@
"Please input your phone verification code!": "Please input your phone verification code!", "Please input your phone verification code!": "Please input your phone verification code!",
"Please input your verification code!": "Please input your verification code!", "Please input your verification code!": "Please input your verification code!",
"Send Code": "Send Code", "Send Code": "Send Code",
"Sending Code": "Sending Code",
"Submit and complete": "Submit and complete" "Submit and complete": "Submit and complete"
}, },
"forget": { "forget": {
@ -211,8 +212,12 @@
"Category - Tooltip": "Unique string-style identifier", "Category - Tooltip": "Unique string-style identifier",
"Client ID": "Client ID", "Client ID": "Client ID",
"Client ID - Tooltip": "Unique string-style identifier", "Client ID - Tooltip": "Unique string-style identifier",
"Client ID 2": "Client ID 2",
"Client ID 2 - Tooltip": "Client ID 2 - Tooltip",
"Client secret": "Client secret", "Client secret": "Client secret",
"Client secret - Tooltip": "Unique string-style identifier", "Client secret - Tooltip": "Unique string-style identifier",
"Client secret 2": "Client secret 2",
"Client secret 2 - Tooltip": "Client secret 2 - Tooltip",
"Copy": "Copy", "Copy": "Copy",
"Domain": "Domain", "Domain": "Domain",
"Domain - Tooltip": "Storage endpoint custom domain", "Domain - Tooltip": "Storage endpoint custom domain",
@ -327,6 +332,8 @@
"Column type": "Column type", "Column type": "Column type",
"Database": "Database", "Database": "Database",
"Database - Tooltip": "Database - Tooltip", "Database - Tooltip": "Database - Tooltip",
"Database type": "Database type",
"Database type - Tooltip": "Database type - Tooltip",
"Edit Syncer": "Edit Syncer", "Edit Syncer": "Edit Syncer",
"Is enabled": "Is enabled", "Is enabled": "Is enabled",
"Is enabled - Tooltip": "Is enabled - Tooltip", "Is enabled - Tooltip": "Is enabled - Tooltip",

View File

@ -40,8 +40,8 @@
"Please input your phone verification code!": "Please input your phone verification code!", "Please input your phone verification code!": "Please input your phone verification code!",
"Please input your verification code!": "Please input your verification code!", "Please input your verification code!": "Please input your verification code!",
"Send Code": "Send Code", "Send Code": "Send Code",
"Submit and complete": "Submit and complete", "Sending Code": "Sending Code",
"Sending Code": "Sending" "Submit and complete": "Submit and complete"
}, },
"forget": { "forget": {
"Account": "Account", "Account": "Account",
@ -212,8 +212,12 @@
"Category - Tooltip": "Category - Tooltip", "Category - Tooltip": "Category - Tooltip",
"Client ID": "Client ID", "Client ID": "Client ID",
"Client ID - Tooltip": "Client ID - Tooltip", "Client ID - Tooltip": "Client ID - Tooltip",
"Client ID 2": "Client ID 2",
"Client ID 2 - Tooltip": "Client ID 2 - Tooltip",
"Client secret": "Client secret", "Client secret": "Client secret",
"Client secret - Tooltip": "Client secret - Tooltip", "Client secret - Tooltip": "Client secret - Tooltip",
"Client secret 2": "Client secret 2",
"Client secret 2 - Tooltip": "Client secret 2 - Tooltip",
"Copy": "Copy", "Copy": "Copy",
"Domain": "Domain", "Domain": "Domain",
"Domain - Tooltip": "Domain - Tooltip", "Domain - Tooltip": "Domain - Tooltip",
@ -328,6 +332,8 @@
"Column type": "Column type", "Column type": "Column type",
"Database": "Database", "Database": "Database",
"Database - Tooltip": "Database - Tooltip", "Database - Tooltip": "Database - Tooltip",
"Database type": "Database type",
"Database type - Tooltip": "Database type - Tooltip",
"Edit Syncer": "Edit Syncer", "Edit Syncer": "Edit Syncer",
"Is enabled": "Is enabled", "Is enabled": "Is enabled",
"Is enabled - Tooltip": "Is enabled - Tooltip", "Is enabled - Tooltip": "Is enabled - Tooltip",

View File

@ -40,6 +40,7 @@
"Please input your phone verification code!": "Please input your phone verification code!", "Please input your phone verification code!": "Please input your phone verification code!",
"Please input your verification code!": "Please input your verification code!", "Please input your verification code!": "Please input your verification code!",
"Send Code": "Send Code", "Send Code": "Send Code",
"Sending Code": "Sending Code",
"Submit and complete": "Submit and complete" "Submit and complete": "Submit and complete"
}, },
"forget": { "forget": {
@ -211,8 +212,12 @@
"Category - Tooltip": "Unique string-style identifier", "Category - Tooltip": "Unique string-style identifier",
"Client ID": "Client ID", "Client ID": "Client ID",
"Client ID - Tooltip": "Unique string-style identifier", "Client ID - Tooltip": "Unique string-style identifier",
"Client ID 2": "Client ID 2",
"Client ID 2 - Tooltip": "Client ID 2 - Tooltip",
"Client secret": "Client secret", "Client secret": "Client secret",
"Client secret - Tooltip": "Unique string-style identifier", "Client secret - Tooltip": "Unique string-style identifier",
"Client secret 2": "Client secret 2",
"Client secret 2 - Tooltip": "Client secret 2 - Tooltip",
"Copy": "Copy", "Copy": "Copy",
"Domain": "Domain", "Domain": "Domain",
"Domain - Tooltip": "Storage endpoint custom domain", "Domain - Tooltip": "Storage endpoint custom domain",
@ -327,6 +332,8 @@
"Column type": "Column type", "Column type": "Column type",
"Database": "Database", "Database": "Database",
"Database - Tooltip": "Database - Tooltip", "Database - Tooltip": "Database - Tooltip",
"Database type": "Database type",
"Database type - Tooltip": "Database type - Tooltip",
"Edit Syncer": "Edit Syncer", "Edit Syncer": "Edit Syncer",
"Is enabled": "Is enabled", "Is enabled": "Is enabled",
"Is enabled - Tooltip": "Is enabled - Tooltip", "Is enabled - Tooltip": "Is enabled - Tooltip",

View File

@ -40,6 +40,7 @@
"Please input your phone verification code!": "Please input your phone verification code!", "Please input your phone verification code!": "Please input your phone verification code!",
"Please input your verification code!": "Please input your verification code!", "Please input your verification code!": "Please input your verification code!",
"Send Code": "Send Code", "Send Code": "Send Code",
"Sending Code": "Sending Code",
"Submit and complete": "Submit and complete" "Submit and complete": "Submit and complete"
}, },
"forget": { "forget": {
@ -211,8 +212,12 @@
"Category - Tooltip": "Unique string-style identifier", "Category - Tooltip": "Unique string-style identifier",
"Client ID": "Client ID", "Client ID": "Client ID",
"Client ID - Tooltip": "Unique string-style identifier", "Client ID - Tooltip": "Unique string-style identifier",
"Client ID 2": "Client ID 2",
"Client ID 2 - Tooltip": "Client ID 2 - Tooltip",
"Client secret": "Client secret", "Client secret": "Client secret",
"Client secret - Tooltip": "Unique string-style identifier", "Client secret - Tooltip": "Unique string-style identifier",
"Client secret 2": "Client secret 2",
"Client secret 2 - Tooltip": "Client secret 2 - Tooltip",
"Copy": "Copy", "Copy": "Copy",
"Domain": "Domain", "Domain": "Domain",
"Domain - Tooltip": "Storage endpoint custom domain", "Domain - Tooltip": "Storage endpoint custom domain",
@ -327,6 +332,8 @@
"Column type": "Column type", "Column type": "Column type",
"Database": "Database", "Database": "Database",
"Database - Tooltip": "Database - Tooltip", "Database - Tooltip": "Database - Tooltip",
"Database type": "Database type",
"Database type - Tooltip": "Database type - Tooltip",
"Edit Syncer": "Edit Syncer", "Edit Syncer": "Edit Syncer",
"Is enabled": "Is enabled", "Is enabled": "Is enabled",
"Is enabled - Tooltip": "Is enabled - Tooltip", "Is enabled - Tooltip": "Is enabled - Tooltip",

View File

@ -40,6 +40,7 @@
"Please input your phone verification code!": "Please input your phone verification code!", "Please input your phone verification code!": "Please input your phone verification code!",
"Please input your verification code!": "Please input your verification code!", "Please input your verification code!": "Please input your verification code!",
"Send Code": "Send Code", "Send Code": "Send Code",
"Sending Code": "Sending Code",
"Submit and complete": "Submit and complete" "Submit and complete": "Submit and complete"
}, },
"forget": { "forget": {
@ -211,8 +212,12 @@
"Category - Tooltip": "Unique string-style identifier", "Category - Tooltip": "Unique string-style identifier",
"Client ID": "Client ID", "Client ID": "Client ID",
"Client ID - Tooltip": "Unique string-style identifier", "Client ID - Tooltip": "Unique string-style identifier",
"Client ID 2": "Client ID 2",
"Client ID 2 - Tooltip": "Client ID 2 - Tooltip",
"Client secret": "Client secret", "Client secret": "Client secret",
"Client secret - Tooltip": "Unique string-style identifier", "Client secret - Tooltip": "Unique string-style identifier",
"Client secret 2": "Client secret 2",
"Client secret 2 - Tooltip": "Client secret 2 - Tooltip",
"Copy": "Copy", "Copy": "Copy",
"Domain": "Domain", "Domain": "Domain",
"Domain - Tooltip": "Storage endpoint custom domain", "Domain - Tooltip": "Storage endpoint custom domain",
@ -327,6 +332,8 @@
"Column type": "Column type", "Column type": "Column type",
"Database": "Database", "Database": "Database",
"Database - Tooltip": "Database - Tooltip", "Database - Tooltip": "Database - Tooltip",
"Database type": "Database type",
"Database type - Tooltip": "Database type - Tooltip",
"Edit Syncer": "Edit Syncer", "Edit Syncer": "Edit Syncer",
"Is enabled": "Is enabled", "Is enabled": "Is enabled",
"Is enabled - Tooltip": "Is enabled - Tooltip", "Is enabled - Tooltip": "Is enabled - Tooltip",

View File

@ -40,6 +40,7 @@
"Please input your phone verification code!": "Please input your phone verification code!", "Please input your phone verification code!": "Please input your phone verification code!",
"Please input your verification code!": "Please input your verification code!", "Please input your verification code!": "Please input your verification code!",
"Send Code": "Send Code", "Send Code": "Send Code",
"Sending Code": "Sending Code",
"Submit and complete": "Submit and complete" "Submit and complete": "Submit and complete"
}, },
"forget": { "forget": {
@ -211,8 +212,12 @@
"Category - Tooltip": "Unique string-style identifier", "Category - Tooltip": "Unique string-style identifier",
"Client ID": "Client ID", "Client ID": "Client ID",
"Client ID - Tooltip": "Unique string-style identifier", "Client ID - Tooltip": "Unique string-style identifier",
"Client ID 2": "Client ID 2",
"Client ID 2 - Tooltip": "Client ID 2 - Tooltip",
"Client secret": "Client secret", "Client secret": "Client secret",
"Client secret - Tooltip": "Unique string-style identifier", "Client secret - Tooltip": "Unique string-style identifier",
"Client secret 2": "Client secret 2",
"Client secret 2 - Tooltip": "Client secret 2 - Tooltip",
"Copy": "Copy", "Copy": "Copy",
"Domain": "Domain", "Domain": "Domain",
"Domain - Tooltip": "Storage endpoint custom domain", "Domain - Tooltip": "Storage endpoint custom domain",
@ -327,6 +332,8 @@
"Column type": "Column type", "Column type": "Column type",
"Database": "Database", "Database": "Database",
"Database - Tooltip": "Database - Tooltip", "Database - Tooltip": "Database - Tooltip",
"Database type": "Database type",
"Database type - Tooltip": "Database type - Tooltip",
"Edit Syncer": "Edit Syncer", "Edit Syncer": "Edit Syncer",
"Is enabled": "Is enabled", "Is enabled": "Is enabled",
"Is enabled - Tooltip": "Is enabled - Tooltip", "Is enabled - Tooltip": "Is enabled - Tooltip",

View File

@ -40,8 +40,8 @@
"Please input your phone verification code!": "请输入您的手机验证码!", "Please input your phone verification code!": "请输入您的手机验证码!",
"Please input your verification code!": "请输入您的验证码!", "Please input your verification code!": "请输入您的验证码!",
"Send Code": "发送验证码", "Send Code": "发送验证码",
"Submit and complete": "完成提交", "Sending Code": "发送中",
"Sending Code": "发送中" "Submit and complete": "完成提交"
}, },
"forget": { "forget": {
"Account": "账号", "Account": "账号",
@ -212,8 +212,12 @@
"Category - Tooltip": "分类", "Category - Tooltip": "分类",
"Client ID": "Client ID", "Client ID": "Client ID",
"Client ID - Tooltip": "Client ID", "Client ID - Tooltip": "Client ID",
"Client ID 2": "Client ID 2",
"Client ID 2 - Tooltip": "Client ID 2 - Tooltip",
"Client secret": "Client secret", "Client secret": "Client secret",
"Client secret - Tooltip": "Client secret", "Client secret - Tooltip": "Client secret",
"Client secret 2": "Client secret 2",
"Client secret 2 - Tooltip": "Client secret 2 - Tooltip",
"Copy": "Copy", "Copy": "Copy",
"Domain": "域名", "Domain": "域名",
"Domain - Tooltip": "存储节点自定义域名", "Domain - Tooltip": "存储节点自定义域名",
@ -328,6 +332,8 @@
"Column type": "列类型", "Column type": "列类型",
"Database": "数据库", "Database": "数据库",
"Database - Tooltip": "数据库名称", "Database - Tooltip": "数据库名称",
"Database type": "数据库类型",
"Database type - Tooltip": "数据库类型",
"Edit Syncer": "修改同步器", "Edit Syncer": "修改同步器",
"Is enabled": "已启用", "Is enabled": "已启用",
"Is enabled - Tooltip": "是否启用", "Is enabled - Tooltip": "是否启用",