Imports Npgsql

Module PostgreSQLModule
    Private pgSqlCon As NpgsqlConnection = New NpgsqlConnection("Host=mes-db.postgres.database.azure.com;Username=jionmt@mes-db;Password=jmtdb6919!!pg;Database=mes-jion-data")
    Private sqlCmd As NpgsqlCommand = New NpgsqlCommand
    Private sqlData As NpgsqlDataReader
    Public pgDataQuery() As Object

    Public Function pgDBOpen() As Boolean
        Try

            If pgSqlCon.State <> ConnectionState.Open Then
                pgSqlCon.Open()
            End If

            Return False
        Catch ex As Exception
            MsgBox("PostgreSQL Command Error:" + vbCrLf + ex.Message)
            Return True
        End Try
    End Function

    Private Function pgDBClose() As Boolean
        Try

            If pgSqlCon.State = ConnectionState.Open Then
                pgSqlCon.Close()
            End If

            Return False
        Catch ex As Exception
            MsgBox("PostgreSQL Command Error:" + vbCrLf + ex.Message)
            Return True
        End Try
    End Function

    Public Function pgDBCommand(cmdStr As String) As Boolean
        Try
            pgDBOpen()
            sqlCmd.CommandText = cmdStr
            sqlCmd.Connection = pgSqlCon
            sqlCmd.ExecuteNonQuery()
            pgDBClose()
            Return True

        Catch ex As Exception
            MsgBox("MySQL Command Error:" + vbCrLf + ex.Message)
            pgDBClose()
            Return False
        End Try
    End Function

    Public Function pgDBQueryReader(cmdStr As String) As Integer
        Try
            Dim fieldCount As Integer
            Dim initRun As Boolean = True
            Dim DataTypeName As String
            Dim TmpData As Object
            Dim QueryCount As ULong

            If pgDBOpen() Then
                MsgBox("DB 연결을 진행할 수 없습니다.", vbCritical)
                Return 0
            End If

            sqlCmd.CommandText = cmdStr
            sqlCmd.Connection = pgSqlCon
            sqlData = sqlCmd.ExecuteReader

            fieldCount = sqlData.FieldCount

            While sqlData.Read
                If initRun Then
                    ReDim pgDataQuery(fieldCount - 1)
                    initRun = False
                Else
                    ReDim Preserve pgDataQuery(UBound(pgDataQuery) + fieldCount)
                End If

                For cnt = 0 To fieldCount - 1
                    DataTypeName = sqlData.GetDataTypeName(cnt)

                    If DataTypeName = "DATE" Then
                        TmpData = sqlData.GetDateTime(cnt)
                    Else
                        TmpData = sqlData.GetValue(cnt)
                    End If

                    If DBNull.Value.Equals(TmpData) Or TmpData.Equals(String.Empty) Then
                        pgDataQuery(QueryCount) = "0"
                    Else
                        pgDataQuery(QueryCount) = TmpData
                    End If

                    QueryCount += 1

                Next
            End While

            If pgDBClose() Then
                MsgBox("DB 연결 종료를 진행할 수 없습니다.", vbCritical)
                Return 0
            End If

            Return QueryCount

        Catch ex As Exception
            MsgBox(ex.Message)
            If pgSqlCon.State = ConnectionState.Open Then pgSqlCon.Close()
            Return 0
        End Try
    End Function
End Module
