2023-02-28 16:48:06 +08:00
|
|
|
// Copyright 2023 The Casdoor Authors. All Rights Reserved.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package sync
|
|
|
|
|
|
|
|
import (
|
2023-03-06 11:39:41 +08:00
|
|
|
"fmt"
|
2023-02-28 16:48:06 +08:00
|
|
|
"log"
|
2023-03-06 11:39:41 +08:00
|
|
|
"strconv"
|
|
|
|
|
2023-02-28 16:48:06 +08:00
|
|
|
"github.com/Masterminds/squirrel"
|
|
|
|
"github.com/xorm-io/xorm"
|
|
|
|
)
|
|
|
|
|
2023-03-12 05:10:23 +08:00
|
|
|
func getUpdateSql(schemaName string, tableName string, columnNames []string, newColumnVal []interface{}, pkColumnNames []string, pkColumnValue []interface{}) (string, []interface{}, error) {
|
2023-02-28 16:48:06 +08:00
|
|
|
updateSql := squirrel.Update(schemaName + "." + tableName)
|
|
|
|
for i, columnName := range columnNames {
|
|
|
|
updateSql = updateSql.Set(columnName, newColumnVal[i])
|
|
|
|
}
|
|
|
|
|
2023-03-06 11:39:41 +08:00
|
|
|
for i, pkColumnName := range pkColumnNames {
|
|
|
|
updateSql = updateSql.Where(squirrel.Eq{pkColumnName: pkColumnValue[i]})
|
2023-02-28 16:48:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
sql, args, err := updateSql.ToSql()
|
|
|
|
if err != nil {
|
|
|
|
return "", nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return sql, args, nil
|
|
|
|
}
|
|
|
|
|
2023-03-12 05:10:23 +08:00
|
|
|
func getInsertSql(schemaName string, tableName string, columnNames []string, columnValue []interface{}) (string, []interface{}, error) {
|
2023-02-28 16:48:06 +08:00
|
|
|
insertSql := squirrel.Insert(schemaName + "." + tableName).Columns(columnNames...).Values(columnValue...)
|
|
|
|
|
|
|
|
return insertSql.ToSql()
|
|
|
|
}
|
|
|
|
|
2023-03-12 05:10:23 +08:00
|
|
|
func getDeleteSql(schemaName string, tableName string, pkColumnNames []string, pkColumnValue []interface{}) (string, []interface{}, error) {
|
2023-02-28 16:48:06 +08:00
|
|
|
deleteSql := squirrel.Delete(schemaName + "." + tableName)
|
|
|
|
|
2023-03-06 11:39:41 +08:00
|
|
|
for i, columnName := range pkColumnNames {
|
|
|
|
deleteSql = deleteSql.Where(squirrel.Eq{columnName: pkColumnValue[i]})
|
2023-02-28 16:48:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return deleteSql.ToSql()
|
|
|
|
}
|
|
|
|
|
2023-03-12 05:10:23 +08:00
|
|
|
func createEngine(dataSourceName string) (*xorm.Engine, error) {
|
2023-02-28 16:48:06 +08:00
|
|
|
engine, err := xorm.NewEngine("mysql", dataSourceName)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// ping mysql
|
|
|
|
err = engine.Ping()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Println("mysql connection success……")
|
|
|
|
return engine, nil
|
|
|
|
}
|
2023-03-06 11:39:41 +08:00
|
|
|
|
2023-03-12 05:10:23 +08:00
|
|
|
func getServerId(engin *xorm.Engine) (uint32, error) {
|
2023-03-06 11:39:41 +08:00
|
|
|
res, err := engin.QueryInterface("SELECT @@server_id")
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
serverId, _ := strconv.ParseUint(fmt.Sprintf("%s", res[0]["@@server_id"]), 10, 32)
|
|
|
|
return uint32(serverId), nil
|
|
|
|
}
|
|
|
|
|
2023-03-12 05:10:23 +08:00
|
|
|
func getServerUuid(engin *xorm.Engine) (string, error) {
|
2023-03-06 11:39:41 +08:00
|
|
|
res, err := engin.QueryString("show variables like 'server_uuid'")
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2023-03-12 05:10:23 +08:00
|
|
|
serverUuid := fmt.Sprintf("%s", res[0]["Value"])
|
|
|
|
return serverUuid, err
|
2023-03-06 11:39:41 +08:00
|
|
|
}
|
|
|
|
|
2023-03-12 05:10:23 +08:00
|
|
|
func getPkColumnNames(columnNames []string, PKColumns []int) []string {
|
2023-03-06 11:39:41 +08:00
|
|
|
pkColumnNames := make([]string, len(PKColumns))
|
|
|
|
for i, index := range PKColumns {
|
|
|
|
pkColumnNames[i] = columnNames[index]
|
|
|
|
}
|
|
|
|
return pkColumnNames
|
|
|
|
}
|
|
|
|
|
2023-03-12 05:10:23 +08:00
|
|
|
func getPkColumnValues(columnValues []interface{}, PKColumns []int) []interface{} {
|
2023-03-06 11:39:41 +08:00
|
|
|
pkColumnNames := make([]interface{}, len(PKColumns))
|
|
|
|
for i, index := range PKColumns {
|
|
|
|
pkColumnNames[i] = columnValues[index]
|
|
|
|
}
|
|
|
|
return pkColumnNames
|
|
|
|
}
|