/// <summary> /// Converts this Criteria object to a string, using field names instead of property names and entity names instead of /// source names. The <see cref="AddParameterDelegate"/> allows a database query builder to create a parameter value /// when adding the value to the string for use with parametrized SQL. Also see <see cref="ISqlStatement"/>. /// /// The <see cref="ToString()"/> method uses this method with a simple delegate that converts DateTimes and Guids /// to sensible string representations and to /// </summary> /// See <see cref="PropNameConverterDelegate"/> /// <param name="formatter">A formatter for any specific database <see cref="SqlFormatter"/></param> /// <param name="addParameter">The delegate to use to convert the value in object form to a value in string form. /// See <see cref="AddParameterDelegate"/></param> /// <returns>The Criteria in string form.</returns> public string ToString(ISqlFormatter formatter, AddParameterDelegate addParameter, IDictionary<string, string> aliases) { if (IsComposite()) { string rightCriteriaAsString; if (LogicalOperator == LogicalOp.Not) { rightCriteriaAsString = new CriteriaDB(RightCriteria).ToString(formatter, addParameter, aliases); return string.Format("{0} ({1})", _logicalOps[(int)LogicalOperator], rightCriteriaAsString); } string leftCriteriaAsString = new CriteriaDB(LeftCriteria).ToString(formatter, addParameter, aliases); rightCriteriaAsString = new CriteriaDB(RightCriteria).ToString(formatter, addParameter, aliases); return string.Format("({0}) {1} ({2})", leftCriteriaAsString, _logicalOps[(int)LogicalOperator], rightCriteriaAsString); } string valueString; string comparisonOperator = ComparisonOperatorString(); if (_criteria.CanBeParametrised()) { valueString = addParameter(FieldValue); } else { if (FieldValue == null) { valueString = "NULL"; if (this.ComparisonOperator == ComparisonOp.Equals) comparisonOperator = "IS"; if (this.ComparisonOperator == ComparisonOp.NotEquals) comparisonOperator = "IS NOT"; } else { valueString = Convert.ToString(FieldValue); } } string sourceEntityName = ""; string separator = ""; if (Field.Source != null) { var fieldSourceName = Field.Source.ChildSourceLeaf.ToString(); if (!aliases.ContainsKey(fieldSourceName)) { var userMessage = string.Format("The source '{0}' for the property '{1}' " + "in the given criteria does not have an alias provided for it.", Field.Source, Field.PropertyName); var developerMessage = userMessage + " The criteria object may have not been prepared correctly before the aliases were set up."; throw new HabaneroDeveloperException(userMessage, developerMessage); } sourceEntityName = aliases[fieldSourceName]; separator = "."; } string fieldNameString = formatter.DelimitField(Field.FieldName); return string.Format("{0}{1}{2} {3} {4}", sourceEntityName, separator, fieldNameString, comparisonOperator, valueString); }
/// <summary> /// Converts this Criteria object to a string, using field names instead of property names and entity names (aliased) instead of /// source names. The <see cref="AddParameterDelegate"/> allows a database query builder to create a parameter value /// when adding the value to the string for use with parametrized SQL. Also see <see cref="ISqlStatement"/>. /// /// The <see cref="ToString()"/> method uses this method with a simple delegate that converts DateTimes and Guids /// to sensible string representations and to /// /// Provide a set of aliases to replace entity names with their aliases. /// </summary> /// See <see cref="PropNameConverterDelegate"/> /// <param name="formatter">A formatter for any specific database <see cref="SqlFormatter"/></param> /// <param name="addParameter">The delegate to use to convert the value in object form to a value in string form. /// See <see cref="AddParameterDelegate"/></param> /// <param name="aliases">The mapping of aliases to use</param> /// <returns>The Criteria in string form.</returns> public string ToString(ISqlFormatter formatter, AddParameterDelegate addParameter, IDictionary <string, string> aliases) { if (IsComposite()) { string rightCriteriaAsString; if (LogicalOperator == LogicalOp.Not) { rightCriteriaAsString = new CriteriaDB(RightCriteria).ToString(formatter, addParameter, aliases); return(string.Format("{0} ({1})", _logicalOps[(int)LogicalOperator], rightCriteriaAsString)); } string leftCriteriaAsString = new CriteriaDB(LeftCriteria).ToString(formatter, addParameter, aliases); rightCriteriaAsString = new CriteriaDB(RightCriteria).ToString(formatter, addParameter, aliases); return(string.Format("({0}) {1} ({2})", leftCriteriaAsString, _logicalOps[(int)LogicalOperator], rightCriteriaAsString)); } string valueString; string comparisonOperator = ComparisonOperatorString(); if (_criteria.CanBeParametrised()) { valueString = addParameter(FieldValue); } else { if (FieldValue == null) { valueString = "NULL"; if (this.ComparisonOperator == ComparisonOp.Equals) { comparisonOperator = "IS"; } if (this.ComparisonOperator == ComparisonOp.NotEquals) { comparisonOperator = "IS NOT"; } } else { valueString = Convert.ToString(FieldValue); } } string sourceEntityName = ""; string separator = ""; if (Field.Source != null) { var fieldSourceName = Field.Source.ChildSourceLeaf.ToString(); if (!aliases.ContainsKey(fieldSourceName)) { var userMessage = string.Format("The source '{0}' for the property '{1}' " + "in the given criteria does not have an alias provided for it.", Field.Source, Field.PropertyName); var developerMessage = userMessage + " The criteria object may have not been prepared correctly before the aliases were set up."; throw new HabaneroDeveloperException(userMessage, developerMessage); } sourceEntityName = aliases[fieldSourceName]; separator = "."; } string fieldNameString = formatter.DelimitField(Field.FieldName); return(string.Format("{0}{1}{2} {3} {4}", sourceEntityName, separator, fieldNameString, comparisonOperator, valueString)); }
public string ToString(ISqlFormatter formatter, AddParameterDelegate addParameter) { return ToString(formatter, addParameter, new Dictionary<string, string>()); }
/// <summary> /// Converts this Criteria to a string for use in a sql statement. /// </summary> /// <param name="formatter">The formatter to use</param> /// <param name="addParameter">A delegate defining how to add a parameter to the string.</param> /// <returns>A sql string snippet for this criteria</returns> public string ToString(ISqlFormatter formatter, AddParameterDelegate addParameter) { return(ToString(formatter, addParameter, new Dictionary <string, string>())); }