示例#1
0
        /// <summary>
        /// Converts the SimpleExpression to a <see cref="SqlString"/>.
        /// </summary>
        /// <returns>A SqlString that contains a valid Sql fragment.</returns>
        public override SqlString ToSqlString(ICriteria criteria, ICriteriaQuery criteriaQuery, IDictionary <string, IFilter> enabledFilters)
        {
            SqlString[] columnNames =
                CriterionUtil.GetColumnNamesForSimpleExpression(
                    propertyName,
                    _projection,
                    criteriaQuery,
                    criteria,
                    enabledFilters,
                    this,
                    value);

            Parameter[] parameters = criteriaQuery.NewQueryParameter(GetParameterTypedValue(criteria, criteriaQuery)).ToArray();

            if (ignoreCase)
            {
                if (columnNames.Length != 1)
                {
                    throw new HibernateException(
                              "case insensitive expression may only be applied to single-column properties: " +
                              propertyName);
                }

                return(new SqlStringBuilder(6)
                       .Add(criteriaQuery.Factory.Dialect.LowercaseFunction)
                       .Add(StringHelper.OpenParen)
                       .Add(columnNames[0])
                       .Add(StringHelper.ClosedParen)
                       .Add(Op)
                       .Add(parameters.Single())
                       .ToSqlString());
            }
            else
            {
                SqlStringBuilder sqlBuilder = new SqlStringBuilder(4 * columnNames.Length);

                for (int i = 0; i < columnNames.Length; i++)
                {
                    if (i > 0)
                    {
                        sqlBuilder.Add(" and ");
                    }

                    sqlBuilder.Add(columnNames[i])
                    .Add(Op)
                    .Add(parameters[i]);
                }
                return(sqlBuilder.ToSqlString());
            }
        }
示例#2
0
        public override SqlString ToSqlString(ICriteria criteria, ICriteriaQuery criteriaQuery, IDictionary <string, IFilter> enabledFilters)
        {
            if (_projection == null)
            {
                AssertPropertyIsNotCollection(criteriaQuery, criteria);
            }

            if (_values.Length == 0)
            {
                // "something in ()" is always false
                return(new SqlString("1=0"));
            }

            //TODO: add default capacity
            SqlStringBuilder result = new SqlStringBuilder();

            SqlString[] columnNames =
                CriterionUtil.GetColumnNames(_propertyName, _projection, criteriaQuery, criteria, enabledFilters);

            // Generate SqlString of the form:
            // columnName1 in (values) and columnName2 in (values) and ...

            for (int columnIndex = 0; columnIndex < columnNames.Length; columnIndex++)
            {
                SqlString columnName = columnNames[columnIndex];

                if (columnIndex > 0)
                {
                    result.Add(" and ");
                }

                result
                .Add(columnName)
                .Add(" in (");

                for (int i = 0; i < _values.Length; i++)
                {
                    if (i > 0)
                    {
                        result.Add(StringHelper.CommaSpace);
                    }
                    result.AddParameter();
                }

                result.Add(")");
            }

            return(result.ToSqlString());
        }
示例#3
0
        public override SqlString ToSqlString(ICriteria criteria, ICriteriaQuery criteriaQuery,
                                              IDictionary <string, IFilter> enabledFilters)
        {
            //TODO: add a default capacity
            SqlStringBuilder sqlBuilder = new SqlStringBuilder();

            //IType propertyType = criteriaQuery.GetTypeUsingProjection( criteria, _propertyName );
            SqlString[] columnNames =
                CriterionUtil.GetColumnNames(_propertyName, _projection, criteriaQuery, criteria, enabledFilters);

            criteriaQuery.AddUsedTypedValues(GetTypedValues(criteria, criteriaQuery));
            if (columnNames.Length == 1)
            {
                sqlBuilder
                .Add(columnNames[0])
                .Add(" between ")
                .AddParameter()
                .Add(" and ")
                .AddParameter();
            }
            else
            {
                bool andNeeded = false;

                for (int i = 0; i < columnNames.Length; i++)
                {
                    if (andNeeded)
                    {
                        sqlBuilder.Add(" AND ");
                    }
                    andNeeded = true;

                    sqlBuilder.Add(columnNames[i])
                    .Add(" >= ")
                    .AddParameter();
                }

                for (int i = 0; i < columnNames.Length; i++)
                {
                    sqlBuilder.Add(" AND ")
                    .Add(columnNames[i])
                    .Add(" <= ")
                    .AddParameter();
                }
            }

            return(sqlBuilder.ToSqlString());
        }
示例#4
0
        public override TypedValue[] GetTypedValues(ICriteria criteria, ICriteriaQuery criteriaQuery)
        {
            List <TypedValue> typedValues = new List <TypedValue>();

            if (projection != null)
            {
                typedValues.AddRange(projection.GetTypedValues(criteria, criteriaQuery));
                typedValues.AddRange(CriterionUtil.GetTypedValues(criteriaQuery, criteria, projection, null, value.ToString().ToLower()));
            }
            else
            {
                typedValues.Add(criteriaQuery.GetTypedValue(criteria, propertyName, value.ToString().ToLower()));
            }

            return(typedValues.ToArray());
        }
示例#5
0
        public override SqlString ToSqlString(ICriteria criteria, ICriteriaQuery criteriaQuery)
        {
            var columns      = CriterionUtil.GetColumnNamesAsSqlStringParts(projection, criteriaQuery, criteria);
            var value        = criteriaQuery.NewQueryParameter(typedValue).Single();
            var arguments    = new[] { columns[0], value };
            var dialect      = criteriaQuery.Factory.Dialect;
            var functionName = dialect.FullTextSearchFunction;

            var dialectFunction = criteriaQuery.Factory.SQLFunctionRegistry.FindSQLFunction(functionName);

            if (dialectFunction == null)
            {
                throw new HibernateException(string.Format("The current dialect '{0}' doesn't support the function: '{1}'",
                                                           criteriaQuery.Factory.Dialect, functionName));
            }
            return(dialectFunction.Render(arguments, criteriaQuery.Factory));
        }
示例#6
0
        public override SqlString ToSqlString(ICriteria criteria, ICriteriaQuery criteriaQuery, IDictionary <string, IFilter> enabledFilters)
        {
            SqlString[] columns = CriterionUtil.GetColumnNamesUsingProjection(projection, criteriaQuery, criteria, enabledFilters);
            if (columns.Length != 1)
            {
                throw new HibernateException("Like may only be used with single-column properties / projections.");
            }

            SqlStringBuilder lhs = new SqlStringBuilder(6);

            if (ignoreCase)
            {
                Dialect.Dialect dialect = criteriaQuery.Factory.Dialect;
                lhs.Add(dialect.LowercaseFunction)
                .Add(StringHelper.OpenParen)
                .Add(columns[0])
                .Add(StringHelper.ClosedParen);
            }
            else
            {
                lhs.Add(columns[0]);
            }

            criteriaQuery.AddUsedTypedValues(GetTypedValues(criteria, criteriaQuery));

            if (ignoreCase)
            {
                Dialect.Dialect dialect = criteriaQuery.Factory.Dialect;
                lhs.Add(" like ")
                .Add(dialect.LowercaseFunction)
                .Add(StringHelper.OpenParen)
                .AddParameter()
                .Add(StringHelper.ClosedParen);
            }
            else
            {
                lhs.Add(" like ").AddParameter();
            }

            if (escapeChar.HasValue)
            {
                lhs.Add(" escape '" + escapeChar + "'");
            }
            return(lhs.ToSqlString());
        }
示例#7
0
        public override SqlString ToSqlString(ICriteria criteria, ICriteriaQuery criteriaQuery)
        {
            var columns = CriterionUtil.GetColumnNamesAsSqlStringParts(projection, criteriaQuery, criteria);

            if (columns.Length != 1)
            {
                throw new HibernateException("Like may only be used with single-column properties / projections.");
            }

            SqlStringBuilder lhs = new SqlStringBuilder(6);

            if (ignoreCase)
            {
                Dialect.Dialect dialect = criteriaQuery.Factory.Dialect;
                lhs.Add(dialect.LowercaseFunction)
                .Add(StringHelper.OpenParen)
                .AddObject(columns[0])
                .Add(StringHelper.ClosedParen);
            }
            else
            {
                lhs.AddObject(columns[0]);
            }

            if (ignoreCase)
            {
                Dialect.Dialect dialect = criteriaQuery.Factory.Dialect;
                lhs.Add(" like ")
                .Add(dialect.LowercaseFunction)
                .Add(StringHelper.OpenParen)
                .Add(criteriaQuery.NewQueryParameter(typedValue).Single())
                .Add(StringHelper.ClosedParen);
            }
            else
            {
                lhs.Add(" like ").Add(criteriaQuery.NewQueryParameter(typedValue).Single());
            }

            if (escapeChar.HasValue)
            {
                lhs.Add(" escape '" + escapeChar + "'");
            }

            return(lhs.ToSqlString());
        }
        public override SqlString ToSqlString(ICriteria criteria, int position, ICriteriaQuery criteriaQuery)
        {
            var buf = new SqlStringBuilder().Add("count(");

            if (distinct)
            {
                buf.Add("distinct ");
            }
            if (projection != null)
            {
                buf.AddObject(CriterionUtil.GetColumnNameAsSqlStringPart(projection, criteriaQuery, criteria));
            }
            else
            {
                buf.Add(criteriaQuery.GetColumn(criteria, propertyName));
            }

            buf.Add(") as y").Add(position.ToString()).Add("_");
            return(buf.ToSqlString());
        }
示例#9
0
        public override SqlString ToSqlString(ICriteria criteria, int loc, ICriteriaQuery criteriaQuery)
        {
            ISessionFactoryImplementor factory = criteriaQuery.Factory;

            SqlType[] sqlTypeCodes = NHibernateUtil.Double.SqlTypes(factory);
            string    sqlType      = factory.Dialect.GetCastTypeName(sqlTypeCodes[0]);

            var sql = new SqlStringBuilder().Add(aggregate).Add("(");

            sql.Add("cast(");
            if (projection != null)
            {
                sql.AddObject(CriterionUtil.GetColumnNameAsSqlStringPart(projection, criteriaQuery, criteria));
            }
            else
            {
                sql.Add(criteriaQuery.GetColumn(criteria, propertyName));
            }
            sql.Add(" as ").Add(sqlType).Add(")");
            sql.Add(") as ").Add(GetColumnAliases(loc, criteria, criteriaQuery)[0]);
            return(sql.ToSqlString());
        }
示例#10
0
        public override SqlString ToSqlString(ICriteria criteria, int position, ICriteriaQuery criteriaQuery)
        {
            var sqlBuilder = new SqlStringBuilder(5 + (_cases.Length * 4));

            sqlBuilder.Add("(case");

            foreach (var projectionCase in _cases)
            {
                sqlBuilder.Add(" when ");
                sqlBuilder.Add(projectionCase.Criterion.ToSqlString(criteria, criteriaQuery));
                sqlBuilder.Add(" then ");
                sqlBuilder.AddObject(CriterionUtil.GetColumnNameAsSqlStringPart(projectionCase.Projection, criteriaQuery, criteria));
            }

            sqlBuilder.Add(" else ");
            sqlBuilder.AddObject(CriterionUtil.GetColumnNameAsSqlStringPart(_elseProjection, criteriaQuery, criteria));

            sqlBuilder.Add(" end) as ");
            sqlBuilder.Add(GetColumnAlias(position));

            return(sqlBuilder.ToSqlString());
        }
示例#11
0
 private static object[] GetProjectionArguments(ICriteriaQuery criteriaQuery, ICriteria criteria, IProjection projection)
 {
     return(CriterionUtil.GetColumnNamesAsSqlStringParts(projection, criteriaQuery, criteria));
 }
        public TypedValue GetParameterTypedValue(ICriteria criteria, ICriteriaQuery criteriaQuery)
        {
            var matchValue = value.ToString().ToLower();

            return(CriterionUtil.GetTypedValue(criteriaQuery, criteria, projection, propertyName, matchValue));
        }
示例#13
0
        public override TypedValue[] GetTypedValues(ICriteria criteria, ICriteriaQuery criteriaQuery)
        {
            object icvalue = ignoreCase ? value.ToString().ToLower() : value;

            return(CriterionUtil.GetTypedValues(criteriaQuery, criteria, _projection, propertyName, icvalue));
        }
示例#14
0
        public TypedValue GetParameterTypedValue(ICriteria criteria, ICriteriaQuery criteriaQuery)
        {
            object icvalue = ignoreCase ? value.ToString().ToLower() : value;

            return(CriterionUtil.GetTypedValue(criteriaQuery, criteria, _projection, propertyName, icvalue));
        }
示例#15
0
        /// <summary>
        /// Converts the SimpleExpression to a <see cref="SqlString"/>.
        /// </summary>
        /// <returns>A SqlString that contains a valid Sql fragment.</returns>
        public override SqlString ToSqlString(ICriteria criteria, ICriteriaQuery criteriaQuery, IDictionary <string, IFilter> enabledFilters)
        {
            SqlString[] columnNames =
                CriterionUtil.GetColumnNamesForSimpleExpression(
                    propertyName,
                    _projection,
                    criteriaQuery,
                    criteria,
                    enabledFilters,
                    this,
                    value);

            TypedValue typedValue = GetParameterTypedValue(criteria, criteriaQuery);

            Parameter[] parameters = criteriaQuery.NewQueryParameter(typedValue).ToArray();

            if (ignoreCase)
            {
                if (columnNames.Length != 1)
                {
                    throw new HibernateException(
                              "case insensitive expression may only be applied to single-column properties: " +
                              propertyName);
                }

                return(new SqlString(
                           criteriaQuery.Factory.Dialect.LowercaseFunction,
                           StringHelper.OpenParen,
                           columnNames[0],
                           StringHelper.ClosedParen,
                           Op,
                           parameters.Single()));
            }
            else
            {
                SqlStringBuilder sqlBuilder = new SqlStringBuilder(4 * columnNames.Length);
                var columnNullness          = typedValue.Type.ToColumnNullness(typedValue.Value, criteriaQuery.Factory);

                if (columnNullness.Length != columnNames.Length)
                {
                    throw new AssertionFailure("Column nullness length doesn't match number of columns.");
                }

                for (int i = 0; i < columnNames.Length; i++)
                {
                    if (i > 0)
                    {
                        sqlBuilder.Add(" and ");
                    }

                    if (columnNullness[i])
                    {
                        sqlBuilder.Add(columnNames[i])
                        .Add(Op)
                        .Add(parameters[i]);
                    }
                    else
                    {
                        sqlBuilder.Add(columnNames[i])
                        .Add(" is null ");
                    }
                }
                return(sqlBuilder.ToSqlString());
            }
        }
示例#16
0
 public override TypedValue[] GetTypedValues(ICriteria criteria, ICriteriaQuery criteriaQuery)
 {
     return(CriterionUtil.GetTypedValues(criteriaQuery, criteria, _projection, _propertyName, _lo, _hi));
 }
示例#17
0
 public override TypedValue[] GetTypedValues(ICriteria criteria, ICriteriaQuery criteriaQuery)
 {
     return(CriterionUtil.GetTypedValues(criteriaQuery, criteria, projection, propertyName, value.ToString().ToLower()));
 }