diff --git a/Modules.Jobs/EDMI/GraphQL/GraphQLConfig.vb b/Modules.Jobs/EDMI/GraphQL/GraphQLConfig.vb index 722855ed..383e58ff 100644 --- a/Modules.Jobs/EDMI/GraphQL/GraphQLConfig.vb +++ b/Modules.Jobs/EDMI/GraphQL/GraphQLConfig.vb @@ -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 = "" diff --git a/Modules.Jobs/EDMI/GraphQL/GraphQLJob.vb b/Modules.Jobs/EDMI/GraphQL/GraphQLJob.vb index 39b036b7..f32b9b8b 100644 --- a/Modules.Jobs/EDMI/GraphQL/GraphQLJob.vb +++ b/Modules.Jobs/EDMI/GraphQL/GraphQLJob.vb @@ -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 - For Each oMapping In oMappings - Dim oValue = oResultItem.SelectToken(oMapping.SourcePath).ToString() + Try + Dim oValues As New List(Of String) + Dim oKeys As New List(Of String) - ' TODO: Build SQL - Next + For Each oMapping In oMappings + Dim oValue = oResultItem.SelectToken(oMapping.SourcePath).ToString() - 'TODO: Insert into db + oValues.Add(oValue) + oKeys.Add(oMapping.DestinationColumn) + Next + + 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