Exemple #1
0
        public string ToQueryText(IDataProvider dataProvider, ref int uniqueMarker)
        {
            if (dataProvider == null)
            {
                throw new ArgumentNullException("dataRepository", "Cannot create query part without a schema from the data repository.");
            }

            IColumn column = dataProvider.Schema.FindColumn(this.TableName, this.ColumnName);
            if (column == null)
                throw new QueryGenerationException("Could not find column schema information for the constraint: " + this.TableName + "." + this.ColumnName);

            StringBuilder queryText = new StringBuilder(128);
            this.Parameters.Clear();

            // start with the opening paren
            if (this.Negate)
            {
                queryText.Append("NOT (");
            }
            else
            {
                queryText.Append("(");
            }

            if (this.Comparison == ConstraintComparison.Between)
            {
                if (this.StartValue == null || this.EndValue == null)
                    throw new QueryGenerationException("Start or End value for a BETWEEN constraint is null");

                // need to create the parameters
                IDataParameter start = dataProvider.CreateParameter(uniqueMarker, column, this.StartValue);
                uniqueMarker++; //always increment after using
                this.Parameters.Add(start);
                IDataParameter end = dataProvider.CreateParameter(uniqueMarker, column, this.EndValue);
                uniqueMarker++; //always increment after using
                this.Parameters.Add(end);

                queryText.Append(dataProvider.QualifyColumnName(column));
                queryText.Append(SqlFragment.BETWEEN);
                queryText.Append(start.ParameterName);
                queryText.Append(SqlFragment.AND);
                queryText.Append(end.ParameterName);
            }
            else if (this.Comparison == ConstraintComparison.In || this.Comparison == ConstraintComparison.NotIn)
            {

            }
            else
            {
                queryText.Append(dataProvider.QualifyColumnName(column));
                queryText.Append(GetComparisonOperator(this.Comparison));
                if (this.Comparison == ConstraintComparison.Is || this.Comparison == ConstraintComparison.IsNot)
                {
                    if (this.ParameterValue == null || this.ParameterValue == DBNull.Value)
                    {
                        queryText.Append("NULL");
                    }
                }
                else
                {
                    IDataParameter value = dataProvider.CreateParameter(uniqueMarker, column, this.ParameterValue);
                    uniqueMarker++; //always increment after using
                    this.Parameters.Add(value);
                    queryText.Append(value.ParameterName);
                }
            }

            // add ending paren
            queryText.Append(")");
            return queryText.ToString();
        }