Esempio n. 1
0
        public override void Append(FilterConstraintAppendContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException("context");
            }

            // get...
            SqlStatement statement = context.Statement;

            if (statement == null)
            {
                throw new ArgumentNullException("statement");
            }
            StringBuilder sql = context.Sql;

            if (sql == null)
            {
                throw new ArgumentNullException("sql");
            }
            if (Creator == null)
            {
                throw new ArgumentNullException("Creator");
            }

            // do the column name...
            if (this.Field.IsExtendedProperty)
            {
                // mbr - 25-09-2007 - moved to provider.
                if (Database.ExtensibilityProvider == null)
                {
                    throw new InvalidOperationException("Database.ExtensibilityProvider is null.");
                }
                Database.ExtensibilityProvider.AppendColumnNameForEntityFieldFilterConstraint(context, this, statement,
                                                                                              sql, this.Field);
            }
            else
            {
                AppendBitwiseOperators(sql, statement);

                // mbr - 2011-08-30 - join support - are we needing to do qualified names?  support wasn't included here before.  the second
                // clause seems strange, but is legacy...
                if (context.Creator.UseFullyQualifiedNames)
                {
                    sql.Append(statement.Dialect.FormatColumnName(this.Field, true));
                }
                else
                {
                    sql.Append(statement.Dialect.FormatNativeName(this.Field.NativeName));
                }
            }

            // get the value to use...
            object value = this.Value;

            if (value is Array)
            {
                // only support arrays on between...
                // mbr - 21-04-2006 - DateTime[] for example is not an object[].  Use Array instead.
                //object[] values = (object[])value;
                Array values = (Array)value;
                if (this.FilterOperator == SqlOperator.Between || this.FilterOperator == SqlOperator.NotBetween)
                {
                    // check...
                    if (values.Length != 2)
                    {
                        throw new InvalidOperationException(string.Format(Cultures.Exceptions, "Array length for 'between' operators must be 2, not '{0}'.", values.Length));
                    }

                    // check...
                    if (values.GetValue(0) == null)
                    {
                        throw new InvalidOperationException("First 'between' value cannot be CLR null.");
                    }
                    if (values.GetValue(0) is DBNull)
                    {
                        throw new InvalidOperationException("First 'between' value cannot be DB null");
                    }
                    if (values.GetValue(1) == null)
                    {
                        throw new InvalidOperationException("Second 'between' value cannot be null.");
                    }
                    if (values.GetValue(1) is DBNull)
                    {
                        throw new InvalidOperationException("Second 'between' value cannot be DB null.");
                    }
                }
                else
                {
                    throw new InvalidOperationException(string.Format(Cultures.Exceptions, "Array values are only supported with 'between' operators."));
                }
            }
            else if (value is DBNull)
            {
                // check...
                if (SqlFilter.IsDBNullValidForOperator(this.FilterOperator) == false)
                {
                    throw new InvalidOperationException(string.Format(Cultures.Exceptions, "Operator '{0}' cannot be used with DB null.", value));
                }
            }

            // Appends the filter to the sql builder
            var added = AppendSqlOperator(FilterOperator, Field, sql, value, statement, context);

            foreach (var param in added)
            {
                param.OriginalConstraint = this;
            }

            // mbr - 25-09-2007 - remove this weird code about the extended properties...
//			if(this.Field.IsExtendedProperty || this.FilterOperator == SqlOperator.BitwiseAnd)
            if ((this.Field.IsExtendedProperty && Database.ExtensibilityProvider is FlatTableExtensibilityProvider) ||
                this.FilterOperator == SqlOperator.BitwiseAnd)
            {
                sql.Append(")");
            }
        }