You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
367 lines
8.9 KiB
367 lines
8.9 KiB
package datacenter
|
|
|
|
import (
|
|
"appPlatform/overall"
|
|
"appPlatform/overall/publicmethod"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"strings"
|
|
|
|
"gorm.io/driver/mysql"
|
|
"gorm.io/driver/postgres"
|
|
"gorm.io/driver/sqlite"
|
|
"gorm.io/driver/sqlserver"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
/*
|
|
*
|
|
@ 作者: 秦东
|
|
@ 时间: 2024-12-24 11:28:46
|
|
@ 功能: 建立SqlServer
|
|
@ 参数
|
|
|
|
#
|
|
|
|
@ 返回值
|
|
|
|
#
|
|
|
|
@ 方法原型
|
|
|
|
#
|
|
*/
|
|
func (c *DatabaseConfig) ConnectToSqlServer() (*gorm.DB, error) {
|
|
// connStr := "provider=SQLOLEDB;data source=你的服务器名;initial catalog=你的数据库名;user id=你的用户名;password=你的密码;"
|
|
// db, err := sql.Open("adodb", connStr)
|
|
// connStr := fmt.Sprintf("server=%v;user id=<%v>;password=<%v>;port=<%v>;database=<%v>;", c.Host,
|
|
// c.User, c.Password, c.Port, c.Name)
|
|
// db, err := sql.Open("mssql", connStr)
|
|
dsn := fmt.Sprintf("sqlserver://gorm:LoremIpsum86@%v:%v?database=%v", c.Host, c.Port, c.Name)
|
|
db, err := gorm.Open(sqlserver.Open(dsn), &gorm.Config{})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return db, nil
|
|
}
|
|
|
|
/*
|
|
*
|
|
@ 作者: 秦东
|
|
@ 时间: 2024-12-24 09:59:12
|
|
@ 功能: 建立PostgreSQL
|
|
@ 参数
|
|
|
|
#
|
|
|
|
@ 返回值
|
|
|
|
#
|
|
|
|
@ 方法原型
|
|
|
|
#
|
|
*/
|
|
func (c *DatabaseConfig) ConnectToPostgreSQL() (*gorm.DB, error) {
|
|
connStr := fmt.Sprintf("user=%s password=%s dbname=%s host=%s port=%d sslmode=disable",
|
|
c.User, c.Password, c.Name, c.Host, c.Port)
|
|
// db, err := sql.Open("postgres", connStr)
|
|
db, err := gorm.Open(postgres.Open(connStr), &gorm.Config{})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return db, nil
|
|
}
|
|
|
|
/*
|
|
*
|
|
@ 作者: 秦东
|
|
@ 时间: 2024-12-24 09:59:12
|
|
@ 功能: 建立MySQL
|
|
@ 参数
|
|
|
|
#
|
|
|
|
@ 返回值
|
|
|
|
#
|
|
|
|
@ 方法原型
|
|
|
|
#
|
|
*/
|
|
func (c *DatabaseConfig) ConnectToMySQL() (*gorm.DB, error) {
|
|
connStr := fmt.Sprintf("%v:%v@tcp(%v:%v)/%v?charset=utf8mb4", c.User, c.Password, c.Host, c.Port, c.Name)
|
|
// db, err := sql.Open("mysql", connStr)
|
|
fmt.Printf("sqlOpen: %v\n", connStr)
|
|
|
|
sqlConfig := mysql.Config{
|
|
DSN: connStr, // DSN
|
|
DefaultStringSize: 255, // string 类型字段的默认长度
|
|
DisableDatetimePrecision: true, // 禁用 datetime 精度,MySQL 5.6 之前的数据库不支持
|
|
DontSupportRenameIndex: true, // 重命名索引时采用删除并新建的方式,MySQL 5.7 之前的数据库和 MariaDB 不支持重命名索引
|
|
DontSupportRenameColumn: true, // 用 `change` 重命名列,MySQL 8 之前的数据库和 MariaDB 不支持重命名列
|
|
SkipInitializeWithVersion: false, // 根据版本自动配置
|
|
}
|
|
|
|
db, err := gorm.Open(mysql.New(sqlConfig))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return db, nil
|
|
}
|
|
|
|
/*
|
|
*
|
|
@ 作者: 秦东
|
|
@ 时间: 2024-12-24 09:59:12
|
|
@ 功能: 建立SQLite
|
|
@ 参数
|
|
|
|
#
|
|
|
|
@ 返回值
|
|
|
|
#
|
|
|
|
@ 方法原型
|
|
|
|
#
|
|
*/
|
|
func (c *DatabaseConfig) ConnectToSQLite() (*gorm.DB, error) {
|
|
// db, err := sql.Open("sqlite3", c.Name)
|
|
db, err := gorm.Open(sqlite.Open("gorm.db"), &gorm.Config{})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return db, nil
|
|
}
|
|
|
|
/*
|
|
*
|
|
@ 作者: 秦东
|
|
@ 时间: 2024-12-24 16:25:17
|
|
@ 功能: Tidb
|
|
@ 参数
|
|
|
|
#
|
|
|
|
@ 返回值
|
|
|
|
#
|
|
|
|
@ 方法原型
|
|
|
|
#
|
|
*/
|
|
func (c *DatabaseConfig) ConnectToTiDb() (*gorm.DB, error) {
|
|
sql := fmt.Sprintf("%v:%v@tcp(%v:%v)/%v", c.User, c.Password, c.Host, c.Port, c.Name)
|
|
db, err := gorm.Open(mysql.Open(sql), &gorm.Config{})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return db, nil
|
|
}
|
|
|
|
/*
|
|
*
|
|
@ 作者: 秦东
|
|
@ 时间: 2024-12-24 10:04:00
|
|
@ 功能: 启动数据库
|
|
@ 参数
|
|
|
|
#
|
|
|
|
@ 返回值
|
|
|
|
#
|
|
|
|
@ 方法原型
|
|
|
|
#
|
|
*/
|
|
func (d *DataBastType) StartDataBast() (openDb *gorm.DB, err error) {
|
|
var configInfo DatabaseConfig
|
|
|
|
if d.Ip == overall.CONSTANT_CONFIG.Appsetup.DefaultIP {
|
|
d.Ip = "127.0.0.1"
|
|
}
|
|
|
|
configInfo.Name = d.DataBaseName
|
|
configInfo.Host = d.Ip
|
|
configInfo.Port = d.Port
|
|
configInfo.User = d.UserName
|
|
configInfo.Password = d.Pwd
|
|
switch strings.ToLower(d.Type) {
|
|
case "PostgreSQL", "postgresql", "postgre sql":
|
|
openDb, err = configInfo.ConnectToPostgreSQL()
|
|
case "MySQL", "mysql":
|
|
openDb, err = configInfo.ConnectToMySQL()
|
|
case "SQLite", "sqlite":
|
|
openDb, err = configInfo.ConnectToSQLite()
|
|
case "SqlServer", "sql server", "sqlserver":
|
|
openDb, err = configInfo.ConnectToSqlServer()
|
|
case "Tidb", "tidb", "TiDb", "TIDB":
|
|
openDb, err = configInfo.ConnectToMySQL()
|
|
default:
|
|
msg := fmt.Sprintf("对不起!当前不支持'%v'数据库。", d.DataBaseName)
|
|
err = errors.New(msg)
|
|
}
|
|
return
|
|
}
|
|
|
|
/*
|
|
*
|
|
@ 作者: 秦东
|
|
@ 时间: 2024-12-24 13:07:56
|
|
@ 功能: 获取数据库中说有表
|
|
@ 参数
|
|
|
|
#
|
|
|
|
@ 返回值
|
|
|
|
#
|
|
|
|
@ 方法原型
|
|
|
|
#获取数据库表comment
|
|
*/
|
|
func (d *DataBastType) ObtainDataBaseAllTableMysql() (tableKeyValue []DataBaseTableInfo, err error) {
|
|
// tableKeyValue := publicmethod.MapOut[string]()
|
|
sqlDb, err := d.StartDataBast()
|
|
if err != nil {
|
|
return
|
|
}
|
|
var valData []string
|
|
// // sql := fmt.Sprintf("SHOW %v", d.DataBaseName)
|
|
sqlDb.Raw("SHOW TABLES").Scan(&valData)
|
|
// if err != nil {
|
|
// return tableKeyValue, err
|
|
// }
|
|
// fmt.Println(rows)
|
|
fmt.Printf("%T----->%v\n", valData, valData)
|
|
// var
|
|
for _, v := range valData {
|
|
fmt.Printf("%T----->%v\n", v, v)
|
|
// sqlStr := fmt.Sprintf("SHOW `%v`", v)
|
|
|
|
// sql := "SELECT TABLE_NAME AS `Table`, TABLE_COMMENT AS `Table Comment` FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = ?"
|
|
|
|
// var list []Result
|
|
// err = sqlDb.Raw(sqlStr).Scan(&list).Error
|
|
// tableKeyValue[v] = list
|
|
var tableName string
|
|
sql := fmt.Sprintf("SELECT TABLE_COMMENT FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = \"%v\" AND TABLE_NAME = \"%v\";", d.DataBaseName, v)
|
|
err = sqlDb.Raw(sql).Scan(&tableName).Error
|
|
// tableKeyValue[v] = tableInfo
|
|
var tableInfo DataBaseTableInfo
|
|
tableInfo.TableName = tableName
|
|
tableInfo.TableKey = v
|
|
sqlStr := fmt.Sprintf("SHOW FULL COLUMNS FROM `%v`", v)
|
|
err = sqlDb.Raw(sqlStr).Scan(&tableInfo.Fields).Error
|
|
tableKeyValue = append(tableKeyValue, tableInfo)
|
|
}
|
|
// defer rows.Close()
|
|
// var tableName []string
|
|
// for rows.Next() {
|
|
// var table string
|
|
// err = rows.Scan(&table)
|
|
// if err == nil {
|
|
// tableName = append(tableName, table)
|
|
// }
|
|
// }
|
|
// if len(tableName) > 0 {
|
|
|
|
// rows, err = sqlDb.Query("SHOW TABLE_COMMENT FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA IS NOT NULL")
|
|
// for _,v := range tableName{
|
|
// sql := fmt.Sprintf("SHOW TABLE_COMMENT FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = %v AND TABLE_NAME = %v",d.DataBaseName,v)
|
|
// rows, err = sqlDb.Query("SHOW TABLE_COMMENT FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA IS NOT NULL")
|
|
// }
|
|
// if err != nil {
|
|
// return tableKeyValue, err
|
|
// }
|
|
// defer rows.Close()
|
|
|
|
// var tableComments = make(map[string]string)
|
|
// for rows.Next() {
|
|
// var table, comment string
|
|
// if err := rows.Scan(&table, &comment); err != nil {
|
|
// // log.Fatal(err)
|
|
// fmt.Printf("rows----err---->:%v\n", err)
|
|
// }
|
|
// tableComments[table] = comment
|
|
// }
|
|
// fmt.Printf("rows-------->:%v\n", tableComments)
|
|
// }
|
|
|
|
return tableKeyValue, err
|
|
}
|
|
|
|
/*
|
|
*
|
|
@ 作者: 秦东
|
|
@ 时间: 2024-12-30 16:49:14
|
|
@ 功能: 获取数据源
|
|
@ 参数
|
|
|
|
#
|
|
|
|
@ 返回值
|
|
|
|
#
|
|
|
|
@ 方法原型
|
|
|
|
#
|
|
*/
|
|
func GainDataStorce(dataId string) (openDb *gorm.DB, err error) {
|
|
sendUrl := fmt.Sprintf("http://120.224.6.6:29911/prod-api/database/app/datasource/page?pageNum=1&pageSize=%v&databaseName=&dataType=", 100000)
|
|
// fmt.Printf("1获取数据源===>%v\n", sendUrl)
|
|
getData := publicmethod.CurlGet(sendUrl)
|
|
// fmt.Printf("2获取数据源===>%v\n", getData)
|
|
var sendMap map[string]interface{}
|
|
json.Unmarshal(getData, &sendMap)
|
|
var sqlDb DataBastType
|
|
// fmt.Printf("3获取数据源===>%v\n", sendMap)
|
|
if code, ok := sendMap["code"]; ok {
|
|
// fmt.Printf("4获取数据源===>%v\n", code)
|
|
|
|
codeInt, _ := publicmethod.StringToInt64(code)
|
|
if codeInt == 200 {
|
|
// jsonval, _ := json.Marshal(sendMap)
|
|
// fmt.Printf("\n\n\n6获取数据源===>%v\n\n\n", string(jsonval))
|
|
if dataval, isOk := sendMap["data"]; isOk {
|
|
if dataMap, ok := dataval.(map[string]interface{}); ok {
|
|
if records, isOk := dataMap["records"]; isOk {
|
|
if recordAry, isOk := records.([]interface{}); isOk {
|
|
for _, v := range recordAry {
|
|
if vMAp, ok := v.(map[string]interface{}); ok {
|
|
// fmt.Printf("5获取数据源===>%T\n===>%v\n\n\n", vMAp, vMAp["id"])
|
|
idStr := publicmethod.TypeToInterface(vMAp["id"])
|
|
if idStr == dataId {
|
|
typeVal, _ := publicmethod.StringToInt64(vMAp["datasourceType"])
|
|
sqlDb.Type = publicmethod.DatabaseIntToString(typeVal)
|
|
sqlDb.Ip = publicmethod.TypeToInterface(vMAp["ipAddress"])
|
|
sqlDb.DataBaseName = publicmethod.TypeToInterface(vMAp["databaseName"])
|
|
sqlDb.Port, _ = publicmethod.StringToInt64(vMAp["port"])
|
|
sqlDb.UserName = publicmethod.TypeToInterface(vMAp["account"])
|
|
sqlDb.Pwd = publicmethod.TypeToInterface(vMAp["password"])
|
|
}
|
|
|
|
}
|
|
// fmt.Printf("5获取数据源===>%T\n===>%v\n\n\n", v, v)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|
|
}
|
|
}
|
|
// fmt.Printf("7获取数据源===>%T\n===>%v\n\n\n", sqlDb, sqlDb)
|
|
openDb, err = sqlDb.StartDataBast()
|
|
return
|
|
}
|
|
|