#region Using Directives using System; using System.Collections; using System.Collections.Generic; using System.ComponentModel; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using Northwind.Entities; using Northwind.Data; using Northwind.Web.UI; #endregion namespace Northwind.Web.Data { /// /// Binds SQL filter expressions to a parameter object. /// [CLSCompliant(true)] [ParseChildren(true), PersistChildren(false)] public class SqlParameter : Parameter { #region Constructors /// /// Initializes a new instance of the SqlParameter class. /// public SqlParameter() : base() { } #endregion Constructors #region Properties /// /// The Filters member variable. /// private IList filters; /// /// Gets or sets the Filters property. /// [PersistenceMode(PersistenceMode.InnerProperty)] public IList Filters { get { if ( filters == null ) { filters = new ArrayList(); } return filters; } } /// /// The CallbackControlID member variable. /// private String callbackControlID; /// /// Gets or sets the CallbackControlID property. /// public String CallbackControlID { get { return callbackControlID; } set { callbackControlID = value; } } /// /// The UseParameterizedFilters member variable. /// private bool useParameterizedFilters = true; /// /// Gets or sets the UseParameterizedFilters property. /// public bool UseParameterizedFilters { get { return useParameterizedFilters; } set { useParameterizedFilters = value; } } #endregion Properties #region Evaluate /// /// Updates and returns the value of the SqlParameter object. /// /// The current System.Web.HttpContext of the request. /// The System.Web.UI.Control that the parameter is bound to. /// A System.Object that represents the updated and current value of the parameter. protected override object Evaluate(HttpContext context, Control control) { Object returnValue = null; bool isCallback = false; if ( !UseParameterizedFilters ) { returnValue = String.Empty; } if ( !String.IsNullOrEmpty(CallbackControlID) ) { IList controls = FormUtil.GetControls(control.Page, CallbackControlID); if ( controls != null && controls.Count > 0 ) { try { isCallback = (bool) EntityUtil.GetPropertyValue(controls[0], "IsCallback"); } catch ( Exception ) { /* ignore */ } } } if ( filters != null && filters.Count > 0 ) { if ( UseParameterizedFilters ) { returnValue = ( filters[0] as ISqlFilter ).GetSqlFilterParameters(control, filters, isCallback); } else { returnValue = ( filters[0] as ISqlFilter ).GetSqlFilterString(control, filters, isCallback); } } return returnValue; } #endregion Evaluate } #region SqlFilter /// /// Provides SQL filter expressions for the class. /// /// An enumeration of entity column names. [CLSCompliant(true)] public abstract class SqlFilter : ISqlFilter { #region Properties /// /// The Column member variable. /// private EntityColumn column; /// /// Gets or sets the Column property. /// public EntityColumn Column { get { return column; } set { column = value; } } /// /// The ControlID member variable. /// private String controlID; /// /// The QueryStringField member variable. /// private String queryStringField; /// /// The SessionField member variable. /// private String sessionField; /// /// Gets or sets the ControlID property. /// public String ControlID { get { return controlID; } set { controlID = value; } } /// /// The QueryStringField member variable. /// public String QueryStringField { get { return queryStringField; } set { queryStringField = value; } } /// /// The SessionField member variable. /// public String SessionField { get { return sessionField; } set { sessionField = value; } } /// /// The PropertyName member variable. /// private String propertyName; /// /// Gets or sets the PropertyName property. /// public String PropertyName { get { return propertyName; } set { propertyName = value; } } /// /// The ComparisionType member variable. /// private SqlComparisonType comparisonType; /// /// Gets or sets the ComparisionType property. /// public SqlComparisonType ComparisionType { get { return comparisonType; } set { comparisonType = value; } } #endregion Properties #region Methods /// /// Gets the filter value. /// /// The that the parameter is bound to. /// Indicates whether this is a callback request. protected virtual String GetFilterValue(Control control, bool isCallback) { Object value = null; if (!string.IsNullOrEmpty(QueryStringField)) { value = HttpContext.Current.Request.QueryString[QueryStringField]; } else if (!string.IsNullOrEmpty(SessionField)) { value = HttpContext.Current.Session[SessionField]; } else { Control input = FormUtil.FindControl(control, ControlID); if (input == null) { // if the ControlID was not found using the supplied control, we will // search the entire conrol tree for the current page IList controls = FormUtil.GetControls(control.Page, ControlID); if (controls != null && controls.Count > 0) { input = controls[0]; } } if (input != null) { if (isCallback) { value = HttpContext.Current.Request[input.UniqueID]; } else { value = FormUtil.GetValue(input, PropertyName); } } } String format = "{0}"; if ( ComparisionType == SqlComparisonType.Contains ) { format = "%{0}%"; } else if ( ComparisionType == SqlComparisonType.StartsWith ) { format = "{0}%"; } else if ( ComparisionType == SqlComparisonType.EndsWith ) { format = "%{0}"; } return String.Format(format, value); } /// /// Applies the filters to the specified object. /// /// The object. /// The that the parameter is bound to. /// A collection of objects. /// Indicates whether this is a callback request. protected virtual void ApplyFilters(SqlFilterBuilder sql, Control control, IList filters, bool isCallback) { String value; foreach ( SqlFilter filter in filters ) { value = filter.GetFilterValue(control, isCallback); if ( filter.ApplyFilter != null ) { SqlFilterEventArgs args = new SqlFilterEventArgs(sql, filter.Column, value); filter.ApplyFilter(this, args); } else { sql.Append(filter.Column, value); } } } /// /// Creates a new instance of a class /// that can be used to generate a SQL filter expression for this filter. /// /// protected virtual SqlFilterBuilder GetFilterBuilder(bool useParameterizedFilters) { return useParameterizedFilters ? new ParameterizedSqlFilterBuilder() : new SqlFilterBuilder(); } #endregion Methods #region Events /// /// Occurs when the filter expression is to be created. /// public event SqlFilterEventHandler ApplyFilter; #endregion Events #region ISqlFilter Members /// /// Gets the SQL filter expression that is represented by the specified filters. /// /// The that the parameter is bound to. /// A collection of objects. /// Indicates whether this is a callback request. /// A SQL filter expression. public String GetSqlFilterString(Control control, IList filters, bool isCallback) { SqlFilterBuilder sql = GetFilterBuilder(false); ApplyFilters(sql, control, filters, isCallback); return sql.ToString(); } /// /// Gets the collection of SQL filter parameters that is represented by the specified filters. /// /// The that the parameter is bound to. /// A collection of objects. /// Indicates whether this is a callback request. /// A collection SQL filter parameters. public SqlFilterParameterCollection GetSqlFilterParameters(Control control, IList filters, bool isCallback) { SqlFilterBuilder sql = GetFilterBuilder(true); ApplyFilters(sql, control, filters, isCallback); return (sql as ParameterizedSqlFilterBuilder).GetParameters(); } #endregion ISqlFilter Members } #endregion SqlFilter #region SqlFilterEventHandler /// /// Represents the method that will handle the event. /// /// An enumeration of entity column names. /// The data source control that the filter is bound to. /// The event data. public delegate void SqlFilterEventHandler(object sender, SqlFilterEventArgs e); /// /// Provides data for the event. /// /// An enumeration of entity column names. public class SqlFilterEventArgs : EventArgs { /// /// Initializes a new instance of the SqlFilterEventArgs class. /// /// An object. /// The current column. /// The column filter value. public SqlFilterEventArgs(SqlFilterBuilder builder, EntityColumn column, String filter) { this.filterBuilder = builder; this.column = column; this.filter = filter; } #region Properties /// /// The FilterBuilder member variable. /// private SqlFilterBuilder filterBuilder; /// /// Gets or sets the FilterBuilder property. /// public SqlFilterBuilder FilterBuilder { get { return filterBuilder; } } /// /// The Column member variable. /// private EntityColumn column; /// /// Gets or sets the Column property. /// public EntityColumn Column { get { return column; } } /// /// The Filter member variable. /// private String filter; /// /// Gets or sets the Filter property. /// public String Filter { get { return filter; } } #endregion Properties } #endregion SqlFilterEventHandler #region ISqlFilter /// /// Provides the ability to construct a valid SQL filter expression. /// [CLSCompliant(true)] public interface ISqlFilter { /// /// Gets the SQL filter expression that is represented by the specified filters. /// /// The that the parameter is bound to. /// A collection of objects. /// Indicates whether this is a callback request. /// A SQL filter expression. String GetSqlFilterString(Control control, IList filters, bool isCallback); /// /// Gets the collection of SQL filter parameters that is represented by the specified filters. /// /// The that the parameter is bound to. /// A collection of objects. /// Indicates whether this is a callback request. /// A collection SQL filter parameters. SqlFilterParameterCollection GetSqlFilterParameters(Control control, IList filters, bool isCallback); } #endregion ISqlFilter }