Imports System.Text

Namespace Database.Base
    Public Class DatabaseData
        Enum TypeEnum
            [Bit]
            [Char]
            [Date]
            [DateTime]
            [Double]
            [Enum]
            [Float]
            [Int]
            [IntUnsigned]
            [Json]
            [Text]
            [Time]
            Varchar
            [Year]
        End Enum

        ''' <summary>
        ''' 列名
        ''' </summary>
        ''' <returns></returns>
        Public Property ColumnName() As String

        ''' <summary>
        ''' 当前值
        ''' </summary>
        ''' <returns></returns>
        Public Property Value() As String


        ''' <summary>
        ''' 默认值
        ''' </summary>
        ''' <returns></returns>
        Public Property DefaultValue() As String

        ''' <summary>
        ''' 数据类型
        ''' </summary>
        ''' <returns></returns>
        Public Property DataType() As TypeEnum

        ''' <summary>
        ''' 数据类型长度
        ''' </summary>
        ''' <returns></returns>
        Public Property DataTypeLength() As Integer

        ''' <summary>
        ''' 是否允许为空
        ''' </summary>
        ''' <returns></returns>
        Public Property IsNull() As Boolean = True

        ''' <summary>
        ''' 是否自动增长
        ''' </summary>
        ''' <returns></returns>
        Public Property IsAutoIncrement() As Boolean

        ''' <summary>
        ''' 是否为主键
        ''' </summary>
        ''' <returns></returns>
        Public Property IsPrimaryKey() As Boolean

        Public Function ToAddColString() As String
            Dim sb As New StringBuilder
            sb.Append($"`{ColumnName}`")

            Select Case DataType
                Case TypeEnum.Char, TypeEnum.Varchar
                    sb.Append($" {DataType}({DataTypeLength}) ")
                Case TypeEnum.Int
                    sb.Append($" {DataType}")
                    If IsAutoIncrement Then sb.Append($" AUTO_INCREMENT")
                Case Else
                    sb.Append($" {DataType}")
            End Select

            sb.Append(IIf(IsNull, " Default Null", " Not Null"))

            If IsPrimaryKey Then
                sb.Append($" PRIMARY KEY")
            End If

            Return sb.ToString()
        End Function
    End Class
End Namespace