-- | It receives an RTuple and lookups the value at a specfic column name.
-- Then it compares this value with the specified search value. If it is eaqual to the search value
-- then it returns the specified Return Value. If not, the it returns the specified default Value (if the ignore indicator is not set).
-- If you pass an empty RTuple, then it returns Null.
-- Calls error if this map contains no mapping for the key.
decodeColValue ::
        ColumnName  -- ^ ColumnName key
    ->  RDataType   -- ^ Search value
    ->  RDataType   -- ^ Return value
    ->  RDataType   -- ^ Default value   
    ->  IgnoreDefault -- ^ Ignore default indicator     
    ->  RTuple      -- ^ input RTuple
    ->  RDataType
decodeColValue cname searchVal returnVal defaultVal ignoreInd tup = 
    if isRTupEmpty tup
        then Null
        else 
            case tup <!> cname of
                searchVal   -> returnVal
                v           -> if ignoreInd == Ignore then v else defaultVal 


-- | It receives an RTuple and a default value. It returns a new RTuple which is identical to the source one
-- but every Null value in the specified colummn has been replaced by a default value
decodeColValue ::
        ColumnName  -- ^ ColumnName key
    ->  RDataType   -- ^ Search value
    ->  RDataType   -- ^ Return value
    ->  RDataType   -- ^ Default value   
    ->  IgnoreDefault -- ^ Ignore default indicator     
    ->  RTuple      -- ^ input RTuple
    ->  RDataType
decodeColValue cname searchVal returnVal defaultVal ignoreInd tup = 
    if isRTupEmpty tup
        then Null
        else 
            case tup <!> cname of
                searchVal   -> returnVal
                v           -> if ignoreInd == Ignore then v else defaultVal 


-- | It receives an RTuple and a default value. It returns a new RTuple which is identical to the source one
-- but every Null value in the specified colummn has been replaced by a default value
decodeColValue cname searchVal returnVal defaultVal ignoreInd tup = 
    if isRTupEmpty tup
        then Null
        else 
            case tup <!> cname of
                searchVal   -> returnVal
                v           -> if ignoreInd == Ignore then v else defaultVal 


-- | It receives an RTuple and a default value. It returns a new RTuple which is identical to the source one
-- but every Null value in the specified colummn has been replaced by a default value