EDMI: WIP GraphQLJob

This commit is contained in:
Jonathan Jenne 2020-05-14 13:17:11 +02:00
parent de2bb5e88d
commit f3986bb8fa
2 changed files with 43 additions and 10 deletions

View File

@ -8,6 +8,7 @@ Public Class GraphQLConfig
Public Property Queries As New List(Of Query)
Public Class Query
Public Property Name As String
Public Property ConnectionString As String = ""
Public Property ClearBeforeFill As Boolean = False
Public Property QueryString As String = ""

View File

@ -4,6 +4,9 @@ Imports DigitalData.Modules.Jobs
Imports DigitalData.Modules.Config
Imports DigitalData.Modules.Logging
Imports Newtonsoft.Json.Linq
Imports System.Collections.Generic
Imports System.Linq
Imports System.Text.RegularExpressions
Public Class GraphQLJob
Inherits JobBase
@ -25,16 +28,17 @@ Public Class GraphQLJob
End With
' Login to get cookie
_Logger.Info("Logging in..")
_Logger.Debug("Logging in")
Dim oLoginResponse = oInterface.Login()
' save cookie for future requests
oInterface.SaveCookies(oLoginResponse.Cookies.Item(0))
_Logger.Info("Getting the data..")
_Logger.Debug("Getting the data")
For Each oQuery As GraphQLConfig.Query In oConfigManager.Config.Queries
Dim oCurrentQuery = oQuery
Dim oDatabase As New Database.MSSQLServer(_LogConfig, oQuery.ConnectionString)
' get the data
Dim oDataResponse = oInterface.GetData(oCurrentQuery.QueryString, oCurrentQuery.OperationName)
@ -47,11 +51,11 @@ Public Class GraphQLJob
End Using
End Using
oCurrentQuery = HandleResponse(oResult, oCurrentQuery)
HandleResponse(oResult, oCurrentQuery, oDatabase)
Next
' logout
_Logger.Info("Logging out..")
_Logger.Debug("Logging out")
Dim oLogoutResponse = oInterface.Logout()
Catch ex As Exception
_Logger.Error(ex)
@ -59,19 +63,47 @@ Public Class GraphQLJob
End Try
End Sub
Private Function HandleResponse(JsonString As String, QueryData As GraphQLConfig.Query) As GraphQLConfig.Query
Private Function HandleResponse(JsonString As String, QueryData As GraphQLConfig.Query, DB As Database.MSSQLServer)
Dim oObj As JObject = JObject.Parse(JsonString)
Dim oResultList = oObj.SelectToken(QueryData.MappingBasePath)
Dim oMappings = QueryData.MappingFields
_Logger.Info("Processing Queue [{0}] with [{1}] Items", QueryData.Name, oResultList.Count)
If QueryData.ClearBeforeFill Then
_Logger.Info("Clearing Table {0} before insert", QueryData.DestinationTable)
Try
DB.ExecuteNonQuery($"DELETE FROM {QueryData.DestinationTable}")
Catch ex As Exception
_Logger.Error(ex)
End Try
End If
For Each oResultItem In oResultList
Try
Dim oValues As New List(Of String)
Dim oKeys As New List(Of String)
For Each oMapping In oMappings
Dim oValue = oResultItem.SelectToken(oMapping.SourcePath).ToString()
' TODO: Build SQL
oValues.Add(oValue)
oKeys.Add(oMapping.DestinationColumn)
Next
'TODO: Insert into db
Dim oColumnValues = oValues.
Select(Function(Value) Regex.Replace(Value, "'", "''")).
Select(Function(Value) $"'{Value}'").
ToList()
Dim oValueString = String.Join(",", oColumnValues)
Dim oColumns = String.Join(",", oKeys.ToArray)
Dim oSQL As String = $"INSERT INTO {QueryData.DestinationTable} ({oColumns}) VALUES ({oValueString})"
DB.ExecuteNonQuery(oSQL)
Catch ex As Exception
_Logger.Error(ex)
End Try
Next
Return QueryData