Пример #1
0
        public SqlString ToSqlString(ICriteria criteria, ICriteriaQuery criteriaQuery, IDictionary <string, IFilter> enabledFilters)
        {
            string entityName         = criteriaQuery.GetEntityName(criteria, propertyName);
            string actualPropertyName = criteriaQuery.GetPropertyName(propertyName);
            string sqlAlias           = criteriaQuery.GetSQLAlias(criteria, propertyName);

            ISessionFactoryImplementor factory             = criteriaQuery.Factory;
            IQueryableCollection       collectionPersister = GetQueryableCollection(entityName, actualPropertyName, factory);

            string[] collectionKeys = collectionPersister.KeyColumnNames;
            string[] ownerKeys      = ((ILoadable)factory.GetEntityPersister(entityName)).IdentifierColumnNames;

            StringBuilder innerSelect = new StringBuilder();

            innerSelect.Append("(select 1 from ")
            .Append(collectionPersister.TableName)
            .Append(" where ")
            .Append(
                new ConditionalFragment().SetTableAlias(sqlAlias).SetCondition(ownerKeys, collectionKeys).ToSqlStringFragment());
            if (collectionPersister.HasWhere)
            {
                innerSelect.Append(" and (")
                .Append(collectionPersister.GetSQLWhereString(collectionPersister.TableName))
                .Append(") ");
            }

            innerSelect.Append(")");

            return(new SqlString(new string[] { ExcludeEmpty ? "exists" : "not exists", innerSelect.ToString() }));
        }
		public override SqlString ToSqlString(ICriteria criteria, ICriteriaQuery criteriaQuery, IDictionary<string, IFilter> enabledFilters)
		{
			string entityName = criteriaQuery.GetEntityName(criteria, propertyName);
			string actualPropertyName = criteriaQuery.GetPropertyName(propertyName);
			string sqlAlias = criteriaQuery.GetSQLAlias(criteria, propertyName);

			ISessionFactoryImplementor factory = criteriaQuery.Factory;
			IQueryableCollection collectionPersister = GetQueryableCollection(entityName, actualPropertyName, factory);

			string[] collectionKeys = collectionPersister.KeyColumnNames;
			string[] ownerKeys = ((ILoadable)factory.GetEntityPersister(entityName)).IdentifierColumnNames;

			StringBuilder innerSelect = new StringBuilder();
			innerSelect.Append("(select 1 from ")
				.Append(collectionPersister.TableName)
				.Append(" where ")
				.Append(
				new ConditionalFragment().SetTableAlias(sqlAlias).SetCondition(ownerKeys, collectionKeys).ToSqlStringFragment());
			if (collectionPersister.HasWhere)
			{
				innerSelect.Append(" and (")
					.Append(collectionPersister.GetSQLWhereString(collectionPersister.TableName))
					.Append(") ");
			}

			innerSelect.Append(")");

			return new SqlString(new string[] {ExcludeEmpty ? "exists" : "not exists", innerSelect.ToString()});
		}
Пример #3
0
        //note: now that Criterion are adding typed values via ICriteriaQuery.AddUsedTypedValues this function is never called.
        public override TypedValue[] GetTypedValues(ICriteria criteria, ICriteriaQuery criteriaQuery)
        {
            IEntityPersister meta = criteriaQuery.Factory.GetEntityPersister(criteriaQuery.GetEntityName(criteria));

            string[] propertyNames = meta.PropertyNames;
            IType[]  propertyTypes = meta.PropertyTypes;
            object[] values        = GetPropertyValues(meta, criteria, criteriaQuery);

            var list = new List <TypedValue>();

            for (int i = 0; i < propertyNames.Length; i++)
            {
                object value = values[i];
                IType  type  = propertyTypes[i];
                string name  = propertyNames[i];

                bool isPropertyIncluded = (i != meta.VersionProperty && IsPropertyIncluded(value, name, type));

                if (isPropertyIncluded)
                {
                    if (propertyTypes[i].IsComponentType)
                    {
                        AddComponentTypedValues(name, value, (IAbstractComponentType)type, list, criteria, criteriaQuery);
                    }
                    else
                    {
                        AddPropertyTypedValue(value, type, list);
                    }
                }
            }

            return(list.ToArray());
        }
Пример #4
0
        private static SqlString[] GetColumnNamesUsingPropertyName(
            ICriteriaQuery criteriaQuery,
            ICriteria criteria,
            string propertyName,
            object value,
            ICriterion critertion)
        {
            string[] columnNames  = criteriaQuery.GetColumnsUsingProjection(criteria, propertyName);
            IType    propertyType = criteriaQuery.GetTypeUsingProjection(criteria, propertyName);

            if (value != null && !(value is System.Type) && !propertyType.ReturnedClass.IsInstanceOfType(value))
            {
                throw new QueryException(string.Format(
                                             "Type mismatch in {0}: {1} expected type {2}, actual type {3}",
                                             critertion.GetType(), propertyName, propertyType.ReturnedClass, value.GetType()));
            }

            if (propertyType.IsCollectionType)
            {
                throw new QueryException(string.Format(
                                             "cannot use collection property ({0}.{1}) directly in a criterion,"
                                             + " use ICriteria.CreateCriteria instead",
                                             criteriaQuery.GetEntityName(criteria), propertyName));
            }
            return(Array.ConvertAll <string, SqlString>(columnNames, delegate(string col)
            {
                return new SqlString(col);
            }));
        }
        public override SqlString ToSqlString(ICriteria criteria, ICriteriaQuery criteriaQuery)
        {
            var entityName         = criteriaQuery.GetEntityName(criteria, propertyName);
            var actualPropertyName = criteriaQuery.GetPropertyName(propertyName);
            var sqlAlias           = criteriaQuery.GetSQLAlias(criteria, propertyName);

            var factory             = criteriaQuery.Factory;
            var collectionPersister = GetQueryableCollection(entityName, actualPropertyName, factory);

            var collectionKeys = collectionPersister.KeyColumnNames;
            var ownerKeys      = ((ILoadable)factory.GetEntityPersister(entityName)).IdentifierColumnNames;

            var innerSelect = new StringBuilder();

            innerSelect.Append("(select 1 from ")
            .Append(collectionPersister.TableName)
            .Append(" where ")
            .Append(
                new ConditionalFragment().SetTableAlias(sqlAlias).SetCondition(ownerKeys, collectionKeys).ToSqlStringFragment());
            if (collectionPersister.HasWhere)
            {
                innerSelect.Append(" and (")
                .Append(collectionPersister.GetSQLWhereString(collectionPersister.TableName))
                .Append(") ");
            }

            innerSelect.Append(")");

            return(new SqlString(new object[] { ExcludeEmpty ? "exists" : "not exists", innerSelect.ToString() }));
        }
Пример #6
0
        ///<summary>
        /// HQL 문을 생성한다.
        ///</summary>
        ///<exception cref="QueryException"></exception>
        public override SqlString ToSqlString(ICriteria criteria, ICriteriaQuery criteriaQuery,
                                              IDictionary <string, IFilter> enabledFilters)
        {
            string    alias;
            Type      targetType;
            ICriteria aliasedCriteria;

            if (_propertyNameOrAlias.IsWhiteSpace())
            {
                alias = criteriaQuery.GetSQLAlias(criteria);
                string entityName = criteriaQuery.GetEntityName(criteria);
                targetType = criteriaQuery.Factory.GetEntityPersister(entityName).GetMappedClass(EntityMode.Poco);
            }
            else if ((aliasedCriteria = criteria.GetCriteriaByAlias(_propertyNameOrAlias)) != null)
            {
                alias = criteriaQuery.GetSQLAlias(aliasedCriteria);
                string entityName = criteriaQuery.GetEntityName(aliasedCriteria);
                targetType = criteriaQuery.Factory.GetEntityPersister(entityName).GetMappedClass(EntityMode.Poco);
            }
            else
            {
                alias = criteriaQuery.GetSQLAlias(criteria, _propertyNameOrAlias);
                var type = criteriaQuery.GetTypeUsingProjection(criteria, _propertyNameOrAlias);

                if (!type.IsEntityType)
                {
                    throw new QueryException("Only entities can be used with an IsAExpression");
                }

                targetType = type.ReturnedClass;
            }

            if (!targetType.IsAssignableFrom(_entityClass))
            {
                return(new SqlString("1=0"));
            }

            var queryable = ObtainQueryable(criteriaQuery);
            var condition = queryable.WhereJoinFragment(alias, true, true);

            if (condition.IndexOfCaseInsensitive(" and ") == 0)
            {
                condition = condition.Substring(5);
            }

            return((condition.Length > 0) ? condition : new SqlString("1=1"));
        }
		/// <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 enabledFilters)
		{
			string[] columnNames = criteriaQuery.GetColumnsUsingProjection(criteria, _propertyName);
			IType propertyType = criteriaQuery.GetTypeUsingProjection(criteria, _propertyName);

			if (_value != null && !(_value is System.Type) && !propertyType.ReturnedClass.IsInstanceOfType(_value))
			{
				throw new QueryException(string.Format(
				                         	"Type mismatch in {0}: {1} expected type {2}, actual type {3}",
				                         	GetType(), _propertyName, propertyType.ReturnedClass, _value.GetType()));
			}

			if (propertyType.IsCollectionType)
			{
				throw new QueryException(string.Format(
				                         	"cannot use collection property ({0}.{1}) directly in a criterion,"
				                         	+ " use ICriteria.CreateCriteria instead",
				                         	criteriaQuery.GetEntityName(criteria), _propertyName));
			}

			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)
					.AddParameter()
					.ToSqlString();
			}
			else
			{
				//TODO: add default capacity
				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)
						.AddParameter();
				}
				return sqlBuilder.ToSqlString();
			}
		}
Пример #8
0
        private void SetFields(ICriteriaQuery criteriaQuery)
        {
            //Persister is required, so let's use it as "initialized marker"
            if (Persister != null)
            {
                return;
            }

            if (!(criteriaQuery is ISupportEntityProjectionCriteriaQuery entityProjectionQuery))
            {
                throw new ArgumentException(
                          $"Projecting to entities requires a '{criteriaQuery.GetType().FullName}' type to implement " +
                          $"{nameof(ISupportEntityProjectionCriteriaQuery)} interface.",
                          nameof(criteriaQuery));
            }

            var criteria = entityProjectionQuery.RootCriteria;

            if (!Lazy)
            {
                entityProjectionQuery.RegisterEntityProjection(this);
            }

            if (_entityAlias == null)
            {
                _entityAlias = criteria.Alias;
            }

            ICriteria subcriteria =
                criteria.GetCriteriaByAlias(_entityAlias)
                ?? throw new HibernateException($"Criteria\\QueryOver alias '{_entityAlias}' for entity projection is not found.");

            var entityName =
                criteriaQuery.GetEntityName(subcriteria)
                ?? throw new HibernateException($"Criteria\\QueryOver alias '{_entityAlias}' is not associated with an entity.");

            Persister =
                criteriaQuery.Factory.GetEntityPersister(entityName) as IQueryable
                ?? throw new HibernateException($"Projecting to entities requires a '{typeof(IQueryable).FullName}' persister, '{entityName}' does not have one.");

            if (_entityType == null)
            {
                _entityType = Persister.Type.ReturnedClass;
            }

            TableAlias = criteriaQuery.GetSQLAlias(
                subcriteria,
                Persister.IdentifierPropertyName ?? string.Empty);

            ColumnAliasSuffix = BasicLoader.GenerateSuffix(criteriaQuery.GetIndexForAlias());

            _identifierColumnAliases = Persister.GetIdentifierAliases(ColumnAliasSuffix);

            _types = new IType[] { TypeFactory.ManyToOne(Persister.EntityName, true) };
        }
Пример #9
0
        public override SqlString ToSqlString(ICriteria criteria, ICriteriaQuery criteriaQuery, IDictionary <string, IFilter> enabledFilters)
        {
            SqlStringBuilder builder = new SqlStringBuilder();

            builder.Add(StringHelper.OpenParen);

            IEntityPersister meta = criteriaQuery.Factory.GetEntityPersister(criteriaQuery.GetEntityName(criteria));

            String[] propertyNames  = meta.PropertyNames;
            IType[]  propertyTypes  = meta.PropertyTypes;
            object[] propertyValues = GetPropertyValues(meta, criteria, criteriaQuery);
            for (int i = 0; i < propertyNames.Length; i++)
            {
                object propertyValue = propertyValues[i];
                String propertyName  = propertyNames[i];

                bool isPropertyIncluded = i != meta.VersionProperty &&
                                          IsPropertyIncluded(propertyValue, propertyName, propertyTypes[i]);
                if (isPropertyIncluded)
                {
                    if (propertyTypes[i].IsComponentType)
                    {
                        AppendComponentCondition(
                            propertyName,
                            propertyValue,
                            (IAbstractComponentType)propertyTypes[i],
                            criteria,
                            criteriaQuery,
                            enabledFilters,
                            builder
                            );
                    }
                    else
                    {
                        AppendPropertyCondition(
                            propertyName,
                            propertyValue,
                            criteria,
                            criteriaQuery,
                            enabledFilters,
                            builder
                            );
                    }
                }
            }
            if (builder.Count == 1)
            {
                builder.Add("1=1");                 // yuck!
            }

            builder.Add(StringHelper.ClosedParen);
            return(builder.ToSqlString());
        }
Пример #10
0
        private EntityMode GetEntityMode(ICriteria criteria, ICriteriaQuery criteriaQuery)
        {
            IEntityPersister meta   = criteriaQuery.Factory.GetEntityPersister(criteriaQuery.GetEntityName(criteria));
            EntityMode?      result = meta.GuessEntityMode(_entity);

            if (!result.HasValue)
            {
                return(EntityMode.Poco);                //this occurs for anon objects
                //throw new InvalidCastException(_entity.GetType().FullName);
            }
            return(result.Value);
        }
Пример #11
0
        public override SqlString ToSqlString(ICriteria criteria, ICriteriaQuery criteriaQuery,
                                              IDictionary <string, IFilter> enabledFilters)
        {
            string    alias      = null;
            Type      targetType = null;
            ICriteria aliasedCriteria;

            if (string.IsNullOrEmpty(_alias))
            {
                alias = criteriaQuery.GetSQLAlias(criteria);
                string entityName = criteriaQuery.GetEntityName(criteria);
                targetType = criteriaQuery.Factory.GetEntityPersister(entityName).GetMappedClass(EntityMode.Poco);
            }
            else if ((aliasedCriteria = criteria.GetCriteriaByAlias(_alias)) != null)
            {
                alias = criteriaQuery.GetSQLAlias(aliasedCriteria);
                string entityName = criteriaQuery.GetEntityName(aliasedCriteria);
                targetType = criteriaQuery.Factory.GetEntityPersister(entityName).GetMappedClass(EntityMode.Poco);
            }

            if (targetType == null || !targetType.IsAssignableFrom(_entityClass))
            {
                return(new SqlString("1=0"));
            }

            IQueryable queryable = ObtainQueryable(criteriaQuery);
            string     condition = queryable.FilterFragment(alias, enabledFilters);

            if (condition.IndexOf(" and ", StringComparison.InvariantCultureIgnoreCase) == 0)
            {
                condition = condition.Substring(5);
            }

            if (condition == string.Empty)
            {
                condition = "1=1";
            }

            return(new SqlString(condition));
        }
Пример #12
0
        /// <summary>
        /// Render a SqlString for the expression.
        /// </summary>
        /// <param name="criteria"></param>
        /// <param name="criteriaQuery"></param>
        /// <param name="enabledFilters"></param>
        /// <returns>
        /// A SqlString that contains a valid Sql fragment.
        /// </returns>
        public override SqlString ToSqlString(ICriteria criteria, ICriteriaQuery criteriaQuery, IDictionary <string, IFilter> enabledFilters)
        {
            IEnumerable <Parameter> parameters = criteriaQuery.NewQueryParameter(GetParameterTypedValue(criteria, criteriaQuery));

            Parameter p = null;

            foreach (Parameter p_ in parameters)
            {
                p = p_;
            }

            //Parameter[] p = criteriaQuery.NewQueryParameter(GetTypedValues(criteria, criteriaQuery)[0]).ToArray();
            ISpatialDialect spatialDialect = (ISpatialDialect)criteriaQuery.Factory.Dialect;

            string[] columnsUsingProjection = criteriaQuery.GetColumnsUsingProjection(criteria, this.propertyName);
            IType    typeUsingProjection    = criteriaQuery.GetTypeUsingProjection(criteria, this.propertyName);

            if (typeUsingProjection.IsCollectionType)
            {
                throw new QueryException(string.Format("cannot use collection property ({0}.{1}) directly in a criterion, use ICriteria.CreateCriteria instead", criteriaQuery.GetEntityName(criteria), this.propertyName));
            }
            string[] keyColumns = criteriaQuery.GetIdentifierColumns(criteria);


            string entityType = criteriaQuery.GetEntityName(criteria, this.propertyName);
            AbstractEntityPersister entityPersister = (AbstractEntityPersister)criteriaQuery.Factory.GetEntityPersister(entityType);

            // Only one key column is assumed
            string keyColumn   = keyColumns[0];
            string alias       = criteriaQuery.GetSQLAlias(criteria, this.propertyName);
            string tableName   = entityPersister.TableName;
            int    aliasLength = alias.Length + 1;

            SqlStringBuilder builder = new SqlStringBuilder(10 * columnsUsingProjection.Length);

            for (int i = 0; i < columnsUsingProjection.Length; i++)
            {
                if (i > 0)
                {
                    builder.Add(" AND ");
                }
                string geometryColumn = columnsUsingProjection[i].Remove(0, aliasLength);
                builder.Add(spatialDialect.GetSpatialFilterString(alias, geometryColumn, keyColumn, tableName, p));
            }
            return(builder.ToSqlString());
        }
 /// <summary>
 /// Gets the column names.
 /// </summary>
 /// <param name="criteria">The criteria.</param>
 /// <param name="criteriaQuery">The criteria query.</param>
 /// <param name="propertyName">Name of the property.</param>
 /// <returns></returns>
 private string[] GetColumnNames(ICriteria criteria, ICriteriaQuery criteriaQuery, string propertyName)
 {
     string[] columns = criteriaQuery.GetColumnsUsingProjection(criteria, propertyName);
     IType type = criteriaQuery.GetTypeUsingProjection(criteria, propertyName);
     if (type.ReturnedClass != typeof(IGeometry))
     {
         throw new QueryException(string.Format("Type mismatch in {0}: {1} expected type {2}, actual type {3}", base.GetType(), propertyName, typeof(IGeometry), type.ReturnedClass));
     }
     if (type.IsCollectionType)
     {
         throw new QueryException(string.Format("cannot use collection property ({0}.{1}) directly in a criterion, use ICriteria.CreateCriteria instead", criteriaQuery.GetEntityName(criteria), propertyName));
     }
     return columns;
 }
Пример #14
0
		private EntityMode GetEntityMode(ICriteria criteria, ICriteriaQuery criteriaQuery)
		{
			IEntityPersister meta = criteriaQuery.Factory.GetEntityPersister(criteriaQuery.GetEntityName(criteria));
			EntityMode? result = meta.GuessEntityMode(_entity);
			if (!result.HasValue)
			{
				return EntityMode.Poco; //this occurs for anon objects
				//throw new InvalidCastException(_entity.GetType().FullName);
			}
			return result.Value;
		}
Пример #15
0
		//note: now that Criterion are adding typed values via ICriteriaQuery.AddUsedTypedValues this function is never called.
		public override TypedValue[] GetTypedValues(ICriteria criteria, ICriteriaQuery criteriaQuery)
		{
			IEntityPersister meta = criteriaQuery.Factory.GetEntityPersister(criteriaQuery.GetEntityName(criteria));
			string[] propertyNames = meta.PropertyNames;
			IType[] propertyTypes = meta.PropertyTypes;
			object[] values = GetPropertyValues(meta, criteria, criteriaQuery);

			ArrayList list = new ArrayList();
			for (int i = 0; i < propertyNames.Length; i++)
			{
				object value = values[i];
				IType type = propertyTypes[i];
				string name = propertyNames[i];

				bool isPropertyIncluded = (i != meta.VersionProperty && IsPropertyIncluded(value, name, type));

				if (isPropertyIncluded)
				{
					if (propertyTypes[i].IsComponentType)
					{
						AddComponentTypedValues(name, value, (IAbstractComponentType) type, list, criteria, criteriaQuery);
					}
					else
					{
						AddPropertyTypedValue(value, type, list);
					}
				}
			}

			return (TypedValue[]) list.ToArray(typeof(TypedValue));
		}
Пример #16
0
		public override SqlString ToSqlString(ICriteria criteria, ICriteriaQuery criteriaQuery, IDictionary<string, IFilter> enabledFilters)
		{
			SqlStringBuilder builder = new SqlStringBuilder();
			builder.Add(StringHelper.OpenParen);

			IEntityPersister meta = criteriaQuery.Factory.GetEntityPersister(criteriaQuery.GetEntityName(criteria));
			String[] propertyNames = meta.PropertyNames;
			IType[] propertyTypes = meta.PropertyTypes;
			object[] propertyValues = GetPropertyValues(meta, criteria, criteriaQuery);
			for (int i = 0; i < propertyNames.Length; i++)
			{
				object propertyValue = propertyValues[i];
				String propertyName = propertyNames[i];

				bool isPropertyIncluded = i != meta.VersionProperty &&
				                          IsPropertyIncluded(propertyValue, propertyName, propertyTypes[i]);
				if (isPropertyIncluded)
				{
					if (propertyTypes[i].IsComponentType)
					{
						AppendComponentCondition(
							propertyName,
							propertyValue,
							(IAbstractComponentType) propertyTypes[i],
							criteria,
							criteriaQuery,
							enabledFilters,
							builder
							);
					}
					else
					{
						AppendPropertyCondition(
							propertyName,
							propertyValue,
							criteria,
							criteriaQuery,
							enabledFilters,
							builder
							);
					}
				}
			}
			if (builder.Count == 1)
			{
				builder.Add("1=1"); // yuck!
			}

			builder.Add(StringHelper.ClosedParen);
			return builder.ToSqlString();
		}
 /// <summary>
 /// Render a SqlString for the expression.
 /// </summary>
 /// <param name="criteria"></param>
 /// <param name="criteriaQuery"></param>
 /// <param name="enabledFilters"></param>
 /// <returns>
 /// A SqlString that contains a valid Sql fragment.
 /// </returns>
 public override SqlString ToSqlString(ICriteria criteria, ICriteriaQuery criteriaQuery, IDictionary<string, IFilter> enabledFilters)
 {
     //criteriaQuery.AddUsedTypedValues(GetTypedValues(criteria, criteriaQuery));
     ISpatialDialect spatialDialect = (ISpatialDialect)criteriaQuery.Factory.Dialect;
     string[] columnsUsingProjection = criteriaQuery.GetColumnsUsingProjection(criteria, this.propertyName);
     IType typeUsingProjection = criteriaQuery.GetTypeUsingProjection(criteria, this.propertyName);
     if (typeUsingProjection.ReturnedClass != typeof(IGeometry))
     {
         throw new QueryException(string.Format("Type mismatch in {0}: {1} expected type {2}, actual type {3}", GetType(), this.propertyName, typeof(IGeometry), typeUsingProjection.ReturnedClass));
     }
     if (typeUsingProjection.IsCollectionType)
     {
         throw new QueryException(string.Format("cannot use collection property ({0}.{1}) directly in a criterion, use ICriteria.CreateCriteria instead", criteriaQuery.GetEntityName(criteria), this.propertyName));
     }
     SqlStringBuilder builder = new SqlStringBuilder(2 * columnsUsingProjection.Length);
     for (int i = 0; i < columnsUsingProjection.Length; i++)
     {
         if (i > 0)
         {
             builder.Add(" AND ");
         }
         builder.Add(spatialDialect.GetSpatialValidationString(columnsUsingProjection[i], this.validation, true));
     }
     return builder.ToSqlString();
 }
Пример #18
0
        /// <summary>
        /// Gets the column names.
        /// </summary>
        /// <param name="criteria">The criteria.</param>
        /// <param name="criteriaQuery">The criteria query.</param>
        /// <param name="propertyName">Name of the property.</param>
        /// <returns></returns>
        private string[] GetColumnNames(ICriteria criteria, ICriteriaQuery criteriaQuery, string propertyName)
        {
            string[] columns = criteriaQuery.GetColumnsUsingProjection(criteria, propertyName);
            IType    type    = criteriaQuery.GetTypeUsingProjection(criteria, propertyName);

            if (!typeof(IGeometry).IsAssignableFrom(type.ReturnedClass))
            {
                throw new QueryException(string.Format("Type mismatch in {0}: {1} expected type {2}, actual type {3}", base.GetType(), propertyName, typeof(IGeometry), type.ReturnedClass));
            }
            if (type.IsCollectionType)
            {
                throw new QueryException(string.Format("cannot use collection property ({0}.{1}) directly in a criterion, use ICriteria.CreateCriteria instead", criteriaQuery.GetEntityName(criteria), propertyName));
            }
            return(columns);
        }
Пример #19
0
		/// <summary>
		/// Render a SqlString for the expression.
		/// </summary>
		/// <param name="criteria"></param>
		/// <param name="criteriaQuery"></param>
		/// <param name="enabledFilters"></param>
		/// <returns>
		/// A SqlString that contains a valid Sql fragment.
		/// </returns>
		public override SqlString ToSqlString(ICriteria criteria, ICriteriaQuery criteriaQuery, IDictionary<string, IFilter> enabledFilters)
		{
			//criteriaQuery.AddUsedTypedValues(GetTypedValues(criteria, criteriaQuery));
			ISpatialDialect spatialDialect = (ISpatialDialect)criteriaQuery.Factory.Dialect;
			string[] columnsUsingProjection = criteriaQuery.GetColumnsUsingProjection(criteria, this.propertyName);
			IType typeUsingProjection = criteriaQuery.GetTypeUsingProjection(criteria, this.propertyName);
			if (typeUsingProjection.IsCollectionType)
			{
				throw new QueryException(string.Format("cannot use collection property ({0}.{1}) directly in a criterion, use ICriteria.CreateCriteria instead", criteriaQuery.GetEntityName(criteria), this.propertyName));
			}
			string[] keyColumns = criteriaQuery.GetIdentifierColumns(criteria);


			string entityType = criteriaQuery.GetEntityName(criteria, this.propertyName);
			AbstractEntityPersister entityPersister = (AbstractEntityPersister)criteriaQuery.Factory.GetEntityPersister(entityType);

			// Only one key column is assumed
			string keyColumn = keyColumns[0];
			string alias = criteriaQuery.GetSQLAlias(criteria, this.propertyName);
			string tableName = entityPersister.TableName;
			int aliasLength = alias.Length + 1;

			SqlStringBuilder builder = new SqlStringBuilder(10 * columnsUsingProjection.Length);
			for (int i = 0; i < columnsUsingProjection.Length; i++)
			{
				if (i > 0)
				{
					builder.Add(" AND ");
				}
				string geometryColumn = columnsUsingProjection[i].Remove(0, aliasLength);
				builder.Add(spatialDialect.GetSpatialFilterString(alias, geometryColumn, keyColumn, tableName));
			}
			return builder.ToSqlString();
		}
Пример #20
0
		private static SqlString[] GetColumnNamesUsingPropertyName(
			ICriteriaQuery criteriaQuery, 
			ICriteria criteria, 
			string propertyName, 
			object value, 
			ICriterion critertion)
		{
			string[] columnNames = criteriaQuery.GetColumnsUsingProjection(criteria, propertyName);
			IType propertyType = criteriaQuery.GetTypeUsingProjection(criteria, propertyName);

			if (value != null && !(value is System.Type) && !propertyType.ReturnedClass.IsInstanceOfType(value))
			{
				throw new QueryException(string.Format(
											"Type mismatch in {0}: {1} expected type {2}, actual type {3}",
											critertion.GetType(), propertyName, propertyType.ReturnedClass, value.GetType()));
			}

			if (propertyType.IsCollectionType)
			{
				throw new QueryException(string.Format(
											"cannot use collection property ({0}.{1}) directly in a criterion,"
											+ " use ICriteria.CreateCriteria instead",
											criteriaQuery.GetEntityName(criteria), propertyName));
			}
			return Array.ConvertAll<string, SqlString>(columnNames, delegate(string col)
			{
				return new SqlString(col);
			});
		}
Пример #21
0
        /// <summary>
        /// Render a SqlString for the expression.
        /// </summary>
        /// <param name="criteria"></param>
        /// <param name="criteriaQuery"></param>
        /// <param name="enabledFilters"></param>
        /// <returns>
        /// A SqlString that contains a valid Sql fragment.
        /// </returns>
        public override SqlString ToSqlString(ICriteria criteria, ICriteriaQuery criteriaQuery, IDictionary <string, IFilter> enabledFilters)
        {
            criteriaQuery.AddUsedTypedValues(GetTypedValues(criteria, criteriaQuery));
            ISpatialDialect spatialDialect = (ISpatialDialect)criteriaQuery.Factory.Dialect;

            string[] columnsUsingProjection = criteriaQuery.GetColumnsUsingProjection(criteria, this.propertyName);
            IType    typeUsingProjection    = criteriaQuery.GetTypeUsingProjection(criteria, this.propertyName);

            if (typeUsingProjection.ReturnedClass != typeof(IGeometry))
            {
                throw new QueryException(string.Format("Type mismatch in {0}: {1} expected type {2}, actual type {3}", GetType(), this.propertyName, typeof(IGeometry), typeUsingProjection.ReturnedClass));
            }
            if (typeUsingProjection.IsCollectionType)
            {
                throw new QueryException(string.Format("cannot use collection property ({0}.{1}) directly in a criterion, use ICriteria.CreateCriteria instead", criteriaQuery.GetEntityName(criteria), this.propertyName));
            }
            SqlStringBuilder builder = new SqlStringBuilder(2 * columnsUsingProjection.Length);

            for (int i = 0; i < columnsUsingProjection.Length; i++)
            {
                if (i > 0)
                {
                    builder.Add(" AND ");
                }
                builder.Add(spatialDialect.GetSpatialValidationString(columnsUsingProjection[i], this.validation, true));
            }
            return(builder.ToSqlString());
        }