Paymentsense / Dapper.SimpleLoad

Dapper.SimpleLoad
MIT License
13 stars 4 forks source link

Generating incorrect SQL for single mapping auto-query with custom WHERE clause #14

Open bartread opened 8 years ago

bartread commented 8 years ago

Here's the generated SQL:

SELECT  [ta0_psr].[ProSupportRuleKey], 
    [ta0_psr].[Type], 
    [ta0_psr].[RuleFlag], 
    [ta0_psr].[Context], 
    [ta0_psr].[Requirement], 
    [ta0_psr].[Classification], 
    [ta0_psr].[DocumentCategory], 
    [ta0_psr].[FieldCategory], 
    [ta0_psr].[RuleMccCode], 
    [ta0_psr].[Title], 
    [ta0_psr].[Details], 
    [ta0_psr].[AdditionalInformation], 
    [ta0_psr].[Reason], 
    [ta0_psr].[IsNoteRequired], 
    [ta0_psr].[IsForFirstData], 
    [ta0_psr].[IsForGlobalPayments], 
    [ta0_psr].[IsForValitor]
                FROM [app].[PRO_SUPPORT_RULES_LUT] AS ta0_psr
WHERE r.[isForFirstData] = @isForFirstData
    AND r.[isForGlobalPayments] = @isForGlobalPayments
    AND r.[isForValitor] = @isForValitor
;

Here's the code that results in the above FrankenSQL:

    [Table("[app].[PRO_SUPPORT_RULES_LUT]"), ReferenceData]
    public class ProSupportRule
    {
        [PrimaryKey]
        public int? ProSupportRuleKey { get; set; }
        public RuleType Type { get; set; }
        public RuleStatementFlag RuleFlag { get; set; }
        public RuleContext Context { get; set; }
        public RuleRequirement Requirement { get; set; }
        public RuleClassification Classification { get; set; }

        public DocumentCategoryEnum DocumentCategory { get; set; }
        public HighlightFieldEnum? FieldCategory { get; set; }

        public int? RuleMccCode { get; set; }
        public string Title { get; set; }
        public string Details { get; set; }
        public string AdditionalInformation { get; set; }
        public string Reason { get; set; }
        public bool IsNoteRequired { get; set; }

        public bool IsForFirstData { get; set; }
        public bool IsForGlobalPayments { get; set; }
        public bool IsForValitor { get; set; }

        private string _ruleFireReason;

        [SimpleSaveIgnore, SimpleLoadIgnore]
        public bool HasFired { get; set; }

        [SimpleSaveIgnore, SimpleLoadIgnore]
        public string RuleFireReason
        {
            get
            {
                if (string.IsNullOrEmpty(_ruleFireReason))
                {
                    _ruleFireReason = RuleFlag.GetDescriptions();
                }

                return _ruleFireReason;
            }
        }
    }

...
...

        public IEnumerable<ProSupportRule> GetAllRules(ApplicationDetailsDto application)
        {
            var whereClause = "";

            switch (application.Acquirer)
            {
                case AppAcquirerEnum.FDMS:
                    whereClause = "r.[IsForFirstData] = @isForFirstData";
                    break;
                case AppAcquirerEnum.Global:
                    whereClause = "r.[IsForGlobalPayments] = @isForGlobalPayments";
                    break;
                case AppAcquirerEnum.Valitor:
                    whereClause = "r.[IsForValitor] = @isForValitor";
                    break;
            }

            var allRules = Execute(conn => conn.AutoQuery<ProSupportRule>(
                new [] { "r" },
                whereClause,
                new 
                {
                    isForFirstData = application.Acquirer == AppAcquirerEnum.FDMS,
                    isForGlobalPayments = application.Acquirer == AppAcquirerEnum.Global,
                    isForValitor = application.Acquirer == AppAcquirerEnum.Valitor
                }));

            return allRules;
        }
'''
bartread commented 8 years ago

There's actually no need for this to use the custom WHERE clause query, so I'll workaround for now by switching to a standard AutoQuery. The fact that we have other custom WHERE clause queries elsewhere that work is odd though - it seems the fact that this only involves a single table may be the problem. Can't say for sure at the moment, obviously, but hopefully a test case will reveal all.

bartread commented 8 years ago

Also, weirdly, in this case it appears to have generated a WHERE clause, using the correct alias, rather than use the custom WHERE clause supplied. WTH?

bartread commented 8 years ago

And, weirdly, I can't reproduce this in a test. It works just fine, generating the expected SQL:

SELECT  [r].[ProSupportRuleKey], 
    [r].[RuleMccCode], 
    [r].[Title], 
    [r].[Details], 
    [r].[AdditionalInformation], 
    [r].[Reason], 
    [r].[IsNoteRequired], 
    [r].[IsForFirstData], 
    [r].[IsForGlobalPayments], 
    [r].[IsForValitor]
FROM [app].[PRO_SUPPORT_RULES_LUT] AS r
WHERE r.[IsForFirstData] = @isForFirstData;