module GraphQLHelper where

import qualified Control.Exception as E
import qualified Components.Parsers.QueryParser as QP
import qualified Components.ObjectHandlers.ServerObjectValidator as SOV
import qualified Components.QueryComposers.SQLQueryComposer as QC
import qualified Components.ObjectHandlers.ServerObjectTrimmer as SOT
import Model.ServerObjectTypes
import Model.ServerExceptions


checkStringToObjects :: [(String,[String])] -> String -> [RootObject]
checkStringToObjects svrobjs str = if (QP.validateQuery str)==True then (QP.parseStringToObjects str svrobjs []) else E.throw SyntaxException
checkObjectsToSql :: [(String,[(String,String)])] -> [(String,[String])] -> [(String,[String])] -> [(String,String,[String])] -> [RootObject] -> ([RootObject],[[String]])
checkObjectsToSql sss sos sodn sor ros = if (SOV.checkObjectsAttributes reducedRobjs sss sos)==True then (reducedRobjs,QC.makeSqlQueries reducedRobjs sodn sor) else (E.throw InvalidObjectException)
                                         where reducedRobjs = SOT.mergeDuplicatedRootObjects ros
checkStringToObjectsWithVariables :: [(String,[String])] -> [(String,String,String)] -> String -> [RootObject]
checkStringToObjectsWithVariables svrobjs vars str = if (QP.validateQuery str)==True then (QP.parseStringToObjects str svrobjs vars) else E.throw SyntaxException
checkObjectsToSqlWithVariables :: [(String,[(String,String)])] -> [(String,[String])] -> [(String,[String])] -> [(String,String,[String])] -> [(String,String,String)] -> [RootObject] -> ([RootObject],[[String]])
checkObjectsToSqlWithVariables sss sos sodn sor vars ros = if (SOV.checkObjectsAttributes valueObjects sss sos)==True then (valueObjects,QC.makeSqlQueries valueObjects sodn sor) else (E.throw InvalidObjectSubFieldException)
                                    where
                                        reducedObjects = SOT.mergeDuplicatedRootObjects ros
                                        valueObjects = SOV.replaceObjectsVariables sss reducedObjects vars