Imports System.Data.Common
Imports UTS_Core.Database

Namespace UTSModule
    ''' <summary>
    ''' UTS使用的数据库连接参数
    ''' </summary>
    Public Class UtsDb
        Private Shared _remoteConn As New DbConnectionStringBuilder

        Public Shared Sub InitConnectParams(license As License.License)
            RemoteDbType = DbExecutor.DbTypeEnum.Mysql
            _remoteConn.Add("Server", license.MysqlServer) '从注册表中获取
            _remoteConn.Add("Port", license.MysqlPort)
            _remoteConn.Add("User id", license.MysqlUserID)
            _remoteConn.Add("Password", license.MysqlPassword)
            _remoteConn.Add("SslMode", "none")
            RemoteConnString = _remoteConn.ToString()
            RemotePrivateDb = license.MysqlDatabase
            RemotePublicDb = license.PublicDb

            LocalDbType = DbExecutor.DbTypeEnum.Sqlite
            Dim localConn As New DbConnectionStringBuilder
            localConn.Add("Data Source", $"{UtsRegistry.RootPath}\LocalDB\{license.SqliteName}")
            localConn.Add("Password", license.SqlitePassword)
            LocalConnString = localConn.ToString()
        End Sub

        ''' <summary>
        ''' 远程数据库地址修改
        ''' </summary>
        Public Shared Sub DbHostChanged(dbHost As String)
            If _remoteConn.ContainsKey("Server") Then
                _remoteConn("Server") = dbHost
                RemoteConnString = _remoteConn.ToString()
            End If

        End Sub


        ''' <summary>
        ''' 远程数据库类型
        ''' </summary>
        Public Shared RemoteDbType As DbExecutor.DbTypeEnum

        ''' <summary>
        ''' 远程数据库连接字符串
        ''' </summary>
        Public Shared RemoteConnString As String

        ''' <summary>
        ''' 远程私有库名
        ''' </summary>
        Public Shared RemotePrivateDb As String

        ''' <summary>
        ''' 远程公共库名
        ''' </summary>
        Public Shared RemotePublicDb As String



        ''' <summary>
        ''' 本地数据库类型
        ''' </summary>
        Public Shared LocalDbType As DbExecutor.DbTypeEnum

        ''' <summary>
        ''' 本地数据库连接字符串
        ''' </summary>
        Public Shared LocalConnString As String

    End Class
End Namespace