Imports System.Data.SQLite


Public Class clsSQLITE
    Private Shared db_location As String
    Public Shared konf_logerrorsonly As Boolean
    Public Shared konf_EmailAktiv As Boolean
    Public Shared konf_intervall As Integer
    Public Shared konf_WDLAUFWERK As String
    Public Shared konf_LASTTICK As Date

    Public Shared Function Init()
        Try
            db_location = "data source=" & My.Settings.SQliteConnection
            Dim SQLconnect As New SQLite.SQLiteConnection()
            SQLconnect.ConnectionString = db_location
            SQLconnect.Open()
            SQLconnect.Close()
            clsSQLITE.GetGrundkonfig()
            Return True
        Catch ex As Exception
            ClassLogger.Add(ex.Message, True, "clsSQLITE.Init")
            Return False
        End Try
    End Function
    Public Shared Function GetGrundkonfig()
        Try
            Dim dt As DataTable = Return_Datatable("SELECT * TBKONFIGURATION WHERE GUID = 1")
            If dt.Rows.Count > 0 Then
                For Each row As DataRow In dt.Rows
                    konf_logerrorsonly = row.Item("LOG_ERRORS_ONLY")
                    konf_intervall = row.Item("INTERVALL")
                    konf_WDLAUFWERK = row.Item("WD_LAUFWERK")
                    konf_EmailAktiv = row.Item("EMAIL_AKTIV")
                    konf_LASTTICK = row.Item("LAST_TICK")
                    If konf_EmailAktiv Then
                        clsEmail.Init()
                    End If
                Next

            End If
            Return True
        Catch ex As Exception
            ClassLogger.Add(ex.Message, True, "GetGrundkonfig")
            Return False
        End Try

    End Function
    Public Shared Function Return_Datatable(Select_anweisung As String)
        Try
            Dim SQLconnect As New SQLite.SQLiteConnection()
            Dim SQLcommand As SQLite.SQLiteCommand
            SQLconnect.ConnectionString = db_location
            SQLconnect.Open()
            SQLcommand = SQLconnect.CreateCommand
            SQLcommand.CommandText = Select_anweisung

            Dim adapter1 As SQLite.SQLiteDataAdapter = New SQLite.SQLiteDataAdapter(SQLcommand)
            Dim dt As DataTable = New DataTable()
            adapter1.Fill(dt)
            SQLconnect.Close()
            Return dt
        Catch ex As Exception
            ClassLogger.Add(ex.Message & vbNewLine & " SQL: " & Select_anweisung, True, "clsSQLITE.Return_Datatable")
            Return Nothing
        End Try
    End Function
    Public Shared Sub Update_Command(vTabelle As String, vspalte As String, vvalue As String)
        Dim update As String = "UPDATE " & vTabelle & " SET " & vspalte & " = '" & vvalue & "' WHERE GUID = 1"
        Try
            Dim SQLconnect As New SQLite.SQLiteConnection()
            Dim SQLcommand As SQLite.SQLiteCommand
            SQLconnect.ConnectionString = db_location
            SQLconnect.Open()
            SQLcommand = SQLconnect.CreateCommand
            'Update Last Created Record in Foo
            SQLcommand.CommandText = update
            SQLcommand.ExecuteNonQuery()
            SQLcommand.Dispose()
            SQLconnect.Close()
        Catch ex As Exception
            ClassLogger.Add(ex.Message & vbNewLine & " SQL: " & update, True, "clsSQLITE.Update_Command ")
        End Try
    End Sub

    Public Shared Function Execute_non_Query(ExecuteCMD As String)
        Try
            Dim SQLconnect As New SQLite.SQLiteConnection()
            Dim SQLcommand As SQLite.SQLiteCommand
            SQLconnect.ConnectionString = db_location
            SQLconnect.Open()
            SQLcommand = SQLconnect.CreateCommand
            'Update Last Created Record in Foo
            SQLcommand.CommandText = ExecuteCMD
            SQLcommand.ExecuteNonQuery()
            SQLcommand.Dispose()
            SQLconnect.Close()
            Return True
        Catch ex As Exception
            ClassLogger.Add(ex.Message & vbNewLine & " SQL: " & ExecuteCMD, True, "clsSQLITE.Execute_non_Query")
            Return False
        End Try
    End Function
    Public Shared Function Execute_Scalar(cmdscalar As String)
        Dim result As String
        Try
            Dim SQLconnect As New SQLite.SQLiteConnection()
            Dim SQLcommand As SQLite.SQLiteCommand
            SQLconnect.ConnectionString = db_location
            SQLconnect.Open()
            SQLcommand = SQLconnect.CreateCommand
            'Update Last Created Record in Foo
            SQLcommand.CommandText = cmdscalar
            result = clsHelper.Prevent_Null(SQLcommand.ExecuteScalar())
            SQLcommand.Dispose()
            SQLconnect.Close()
            Return result
        Catch ex As Exception
            ClassLogger.Add(ex.Message & vbNewLine & " SQL: " & cmdscalar, True, "clsSQLITE.Execute_Scalar")
            Return Nothing
        End Try
    End Function
End Class
