protected Query PrepareQuery(IDictionary <string, object> context) { if (context == null) { context = new Dictionary <string, object>(); } var relex = new NI.Data.SimpleStringTemplate(Relex).FormatTemplate(context); var q = new NI.Data.RelationalExpressions.RelExParser().Parse(relex); if (q.Condition != null) { DataHelper.SetQueryVariables(q.Condition, (varNode) => { if (context.ContainsKey(varNode.Name)) { varNode.Set(context[varNode.Name]); } else { varNode.Unset(); } }); } if (ExtendedProperties != null) { q.ExtendedProperties = new Dictionary <string, object>(); foreach (var extProp in ExtendedProperties) { q.ExtendedProperties[extProp] = context.ContainsKey(extProp) ? context[extProp] : null; } } return(q); }
/// <summary> /// Performs default SQL template parsing that can handle simple code snippets /// </summary> protected string FormatSelectSql(ViewContext viewContext) { var strTpl = new SimpleStringTemplate(SqlCommandTextTemplate, 2); strTpl.ReplaceMissedTokens = false; var cachedProps = new Dictionary <string, SimpleStringTemplate.TokenResult>(); return(strTpl.FormatTemplate((token) => { if (cachedProps.ContainsKey(token)) { return cachedProps[token]; } SimpleStringTemplate.TokenResult res = SimpleStringTemplate.TokenResult.NotDefined; var p = viewContext.GetType().GetProperty(token); if (p != null) { if (token == "SqlOrderBy" && viewContext.IsCountQuery) // order by is not applicable for count queries { res = SimpleStringTemplate.TokenResult.NotApplicable; } else { var val = p.GetValue(viewContext, null); res = new SimpleStringTemplate.TokenResult(val); } } else if (viewContext.Query.ExtendedProperties != null && viewContext.Query.ExtendedProperties.ContainsKey(token)) { res = new SimpleStringTemplate.TokenResult(viewContext.Query.ExtendedProperties[token]); } cachedProps[token] = res; return res; })); }