/// <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(); }
private static SqlString[] GetColumnNamesUsingPropertyName( ICriteriaQuery criteriaQuery, ICriteria criteria, string propertyName, object value, ICriterion critertion) { var columnNames = criteriaQuery.GetColumnsUsingProjection(criteria, propertyName); var propertyType = criteriaQuery.GetTypeUsingProjection(criteria, propertyName); var type = propertyType.ReturnedClass; var elementTypes = type.GetGenericArguments(); var elementType = (elementTypes.Length > 0) ? elementTypes[0] : null; if (value != null) { if (!type.IsInstanceOfType(value) && elementType == null) { throw new QueryException(string.Format( "Type mismatch in {0}: {1} expected type {2}, actual type {3}", critertion.GetType(), propertyName, type, value.GetType())); } else if (!type.IsInstanceOfType(value) && !elementType.IsInstanceOfType(value)) { throw new QueryException(string.Format( "Type mismatch in {0}: {1} expected types {2} or {3}, actual type {4}", critertion.GetType(), propertyName, type, elementType, value.GetType())); } } return(Array.ConvertAll(columnNames, (col) => new SqlString(col))); }
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); }); }
/// <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()); }
public override TypedValue[] GetTypedValues(ICriteria criteria, ICriteriaQuery criteriaQuery) { ArrayList list = new ArrayList(); IType type = criteriaQuery.GetTypeUsingProjection(criteria, _propertyName); if (type.IsComponentType) { IAbstractComponentType actype = (IAbstractComponentType) type; IType[] types = actype.Subtypes; for (int i = 0; i < types.Length; i++) { for (int j = 0; j < _values.Length; j++) { object subval = _values[j] == null ? null : actype.GetPropertyValues(_values[j])[i]; list.Add(new TypedValue(types[i], subval)); } } } else { for (int j = 0; j < _values.Length; j++) { list.Add(new TypedValue(type, _values[j])); } } return (TypedValue[]) list.ToArray(typeof(TypedValue)); }
public IType GetTypeUsingProjection(ICriteria subcriteria, string propertyName) { //first look for a reference to a projection alias IProjection projection = rootCriteria.Projection; IType[] projectionTypes = projection == null ? null : projection.GetTypes(propertyName, subcriteria, this); if (projectionTypes == null) { //it does not refer to an alias of a projection, //look for a property if (TryGetType(subcriteria, propertyName, out var type)) { return(type); } if (outerQueryTranslator != null) { return(outerQueryTranslator.GetTypeUsingProjection(subcriteria, propertyName)); } throw new QueryException("Could not find property " + propertyName); } else { if (projectionTypes.Length != 1) { //should never happen, i think throw new QueryException("not a single-length projection: " + propertyName); } return(projectionTypes[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); })); }
private SqlType[] SqlTypes(ICriteria criteria, ICriteriaQuery criteriaQuery) { var type = projection == null ? criteriaQuery.GetTypeUsingProjection(criteria, propertyName) : projection.GetTypes(criteria, criteriaQuery)[0]; return(type.SqlTypes(criteriaQuery.Factory)); }
/// <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(); } }
private void AssertPropertyIsNotCollection(ICriteriaQuery criteriaQuery, ICriteria criteria) { IType type = criteriaQuery.GetTypeUsingProjection(criteria, _propertyName); if (type.IsCollectionType) { throw new QueryException("Cannot use collections with InExpression"); } }
public override SqlString ToSqlString(ICriteria criteria, ICriteriaQuery criteriaQuery) { //we only need this for SQL Server, and or large amount of values if ((criteriaQuery.Factory.Dialect is MsSql2005Dialect) == false || values.Length < maximumNumberOfParametersToNotUseXml) { return(expr.ToSqlString(criteria, criteriaQuery)); } //TODO: NON FUNZIONA DOPO IL REFACTOR NHIBERNATE 5 IType type = criteriaQuery.GetTypeUsingProjection(criteria, propertyName); if (type.IsCollectionType) { throw new QueryException("Cannot use collections with InExpression"); } if (values.Length == 0) { // "something in ()" is always false return(new SqlString("1=0")); } SqlStringBuilder result = new SqlStringBuilder(); string[] columnNames = criteriaQuery.GetColumnsUsingProjection(criteria, propertyName); // Generate SqlString of the form: // columnName1 in (xml query) and columnName2 in (xml query) and ... IEnumerable <Parameter> parameters = criteriaQuery.NewQueryParameter(this.GetTypedValues(criteria, criteriaQuery).First <TypedValue>()); for (int columnIndex = 0; columnIndex < columnNames.Length; columnIndex++) { string columnName = columnNames[columnIndex]; if (columnIndex > 0) { result.Add(" and "); } SqlType sqlType = type.SqlTypes(criteriaQuery.Factory)[columnIndex]; result .Add(columnName) .Add(" in (") .Add("SELECT ParamValues.Val.value('.','") .Add(criteriaQuery.Factory.Dialect.GetTypeName(sqlType)) .Add("') FROM ") .Add(parameters.ElementAt(columnIndex)) .Add(".nodes('/items/val') as ParamValues(Val)") .Add(")"); } return(result.ToSqlString()); }
public override TypedValue[] GetTypedValues(ICriteria criteria, ICriteriaQuery criteriaQuery) { //we only need this for SQL Server, and or large amount of values if ((criteriaQuery.Factory.Dialect is MsSql2005Dialect) == false || values.Length < maximumNumberOfParametersToNotUseXml) { return(expr.GetTypedValues(criteria, criteriaQuery)); } IEntityPersister persister = null; IType type = criteriaQuery.GetTypeUsingProjection(criteria, propertyName); if (type.IsEntityType) { persister = criteriaQuery.Factory.GetEntityPersister(type.ReturnedClass.FullName); } StringWriter sw = new StringWriter(); XmlWriterSettings xmlWriterSettings = new XmlWriterSettings(); xmlWriterSettings.Encoding = System.Text.Encoding.UTF8; XmlWriter writer = XmlWriter.Create(sw, xmlWriterSettings); writer.WriteStartElement("items"); foreach (object value in values) { if (value == null) { continue; } object valToWrite; if (persister != null) { valToWrite = persister.GetIdentifier(value); } else { valToWrite = value; } writer.WriteElementString("val", valToWrite.ToString()); } writer.WriteEndElement(); writer.WriteEndDocument(); writer.Flush(); string xmlString = sw.GetStringBuilder().ToString(); return(new TypedValue[] { new TypedValue(new CustomType(typeof(XmlType), new Dictionary <string, string>()), xmlString), }); }
/// <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); }
/// <summary> /// Determine the type of the elements in the IN clause. /// </summary> private IType GetElementType(ICriteria criteria, ICriteriaQuery criteriaQuery) { if (_projection == null) { return(criteriaQuery.GetTypeUsingProjection(criteria, _propertyName)); } IType[] types = _projection.GetTypes(criteria, criteriaQuery); if (types.Length != 1) { throw new QueryException("Cannot use projections that return more than a single column with InExpression"); } return(types[0]); }
public override SqlString ToSqlString(ICriteria criteria, ICriteriaQuery criteriaQuery, IDictionary enabledFilters) { IType type = criteriaQuery.GetTypeUsingProjection(criteria, _propertyName); if (type.IsCollectionType) { throw new QueryException("Cannot use collections with InExpression"); } if (_values.Length == 0) { // "something in ()" is always false return new SqlString("1=0"); } //TODO: add default capacity SqlStringBuilder result = new SqlStringBuilder(); string[] columnNames = criteriaQuery.GetColumnsUsingProjection(criteria, _propertyName); // Generate SqlString of the form: // columnName1 in (values) and columnName2 in (values) and ... for (int columnIndex = 0; columnIndex < columnNames.Length; columnIndex++) { string 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(); }
public override TypedValue[] GetTypedValues(ICriteria criteria, ICriteriaQuery criteriaQuery) { List <TypedValue> list = new List <TypedValue>(); IType type; if (_projection == null) { type = criteriaQuery.GetTypeUsingProjection(criteria, _propertyName); } else { IType[] types = _projection.GetTypes(criteria, criteriaQuery); if (types.Length != 1) { throw new QueryException("Cannot use projections that return more than a single column with InExpression"); } type = types[0]; list.AddRange(_projection.GetTypedValues(criteria, criteriaQuery)); } if (type.IsComponentType) { IAbstractComponentType actype = (IAbstractComponentType)type; IType[] types = actype.Subtypes; for (int i = 0; i < types.Length; i++) { for (int j = 0; j < _values.Length; j++) { object subval = _values[j] == null ? null : actype.GetPropertyValues(_values[j], EntityMode.Poco)[i]; list.Add(new TypedValue(types[i], subval, EntityMode.Poco)); } } } else { for (int j = 0; j < _values.Length; j++) { list.Add(new TypedValue(type, _values[j], EntityMode.Poco)); } } return(list.ToArray()); }
/// <summary> /// Return sql string /// </summary> public override SqlString ToSqlString(ICriteria criteria, ICriteriaQuery criteriaQuery, IDictionary <string, IFilter> enabledFilters) { // we only need this for SQL Server, and or large amount of values if ((criteriaQuery.Factory.Dialect is MsSql2005Dialect) == false || _values.Length < _maxParametersToNotUseXml) { return(_expr.ToSqlString(criteria, criteriaQuery, enabledFilters)); } var type = criteriaQuery.GetTypeUsingProjection(criteria, _propertyName); if (type.IsCollectionType) { throw new QueryException("Cannot use collections with InExpression"); } if (_values.Length == 0) { return(new SqlString("1=0")); // " somthing in ()" is always false } var result = new SqlStringBuilder(); var columnNames = criteriaQuery.GetColumnsUsingProjection(criteria, _propertyName); // Generate SqlString of the form: // columnName1 in (xml query) and columnName2 in (xml query) and ... for (int columnIndex = 0; columnIndex < columnNames.Length; columnIndex++) { string columnName = columnNames[columnIndex]; if (columnIndex > 0) { result.Add(" and "); } var sqlType = type.SqlTypes(criteriaQuery.Factory)[columnIndex]; result .Add(columnName) .Add(" in (") .Add("SELECT ParamValues.Val.value('.','") .Add(criteriaQuery.Factory.Dialect.GetTypeName(sqlType)) .Add("') FROM ") .AddParameter() .Add(".nodes('/items/val') as ParamValues(Val)") .Add(")"); } return(result.ToSqlString()); }
///<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")); }
public override TypedValue[] GetTypedValues(ICriteria criteria, ICriteriaQuery criteriaQuery) { ArrayList list = new ArrayList(); IType type; if (projection == null) { type = criteriaQuery.GetTypeUsingProjection(criteria, _propertyName); } else { IType[] types = projection.GetTypes(criteria, criteriaQuery); if(types.Length!=1) { throw new QueryException("Cannot use projections that return more than a single column with InExpression"); } type = types[0]; list.AddRange(projection.GetTypedValues(criteria, criteriaQuery)); } if (type.IsComponentType) { IAbstractComponentType actype = (IAbstractComponentType) type; IType[] types = actype.Subtypes; for (int i = 0; i < types.Length; i++) { for (int j = 0; j < _values.Length; j++) { object subval = _values[j] == null ? null : actype.GetPropertyValues(_values[j], EntityMode.Poco)[i]; list.Add(new TypedValue(types[i], subval, EntityMode.Poco)); } } } else { for (int j = 0; j < _values.Length; j++) { list.Add(new TypedValue(type, _values[j], EntityMode.Poco)); } } return (TypedValue[]) list.ToArray(typeof (TypedValue)); }
/// <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> /// Render the SQL fragment /// </summary> public virtual SqlString ToSqlString(ICriteria criteria, ICriteriaQuery criteriaQuery) { if (projection != null) { SqlString sb = new SqlString(); SqlString produced = this.projection.ToSqlString(criteria, 0, criteriaQuery, new Dictionary <string, IFilter>()); SqlString truncated = NHibernate.Util.StringHelper.RemoveAsAliasesFromSql(produced); sb = sb.Append(truncated); sb = sb.Append(ascending ? " asc" : " desc"); return(sb); } string[] columns = criteriaQuery.GetColumnAliasesUsingProjection(criteria, propertyName); Type.IType type = criteriaQuery.GetTypeUsingProjection(criteria, propertyName); StringBuilder fragment = new StringBuilder(); ISessionFactoryImplementor factory = criteriaQuery.Factory; for (int i = 0; i < columns.Length; i++) { bool lower = ignoreCase && IsStringType(type.SqlTypes(factory)[i]); if (lower) { fragment.Append(factory.Dialect.LowercaseFunction) .Append("("); } fragment.Append(columns[i]); if (lower) { fragment.Append(")"); } fragment.Append(ascending ? " asc" : " desc"); if (i < columns.Length - 1) { fragment.Append(", "); } } return(new SqlString(fragment.ToString())); }
/// <summary> /// Render the SQL fragment /// </summary> public virtual SqlString ToSqlString(ICriteria criteria, ICriteriaQuery criteriaQuery) { if (projection != null) { SqlString sb = new SqlString(); SqlString produced = this.projection.ToSqlString(criteria, 0, criteriaQuery, new Dictionary<string, IFilter>()); SqlString truncated = NHibernate.Util.StringHelper.RemoveAsAliasesFromSql(produced); sb = sb.Append(truncated); sb = sb.Append(ascending ? " asc" : " desc"); return sb; } string[] columns = criteriaQuery.GetColumnAliasesUsingProjection(criteria, propertyName); Type.IType type = criteriaQuery.GetTypeUsingProjection(criteria, propertyName); StringBuilder fragment = new StringBuilder(); ISessionFactoryImplementor factory = criteriaQuery.Factory; for (int i = 0; i < columns.Length; i++) { bool lower = ignoreCase && IsStringType(type.SqlTypes(factory)[i]); if (lower) { fragment.Append(factory.Dialect.LowercaseFunction) .Append("("); } fragment.Append(columns[i]); if (lower) { fragment.Append(")"); } fragment.Append(ascending ? " asc" : " desc"); if (i < columns.Length - 1) { fragment.Append(", "); } } return new SqlString(fragment.ToString()); }
/// <summary> /// Determine the type of the elements in the IN clause. /// </summary> private IType GetElementType(ICriteria criteria, ICriteriaQuery criteriaQuery) { if (_projection == null) return criteriaQuery.GetTypeUsingProjection(criteria, _propertyName); IType[] types = _projection.GetTypes(criteria, criteriaQuery); if (types.Length != 1) throw new QueryException("Cannot use projections that return more than a single column with InExpression"); return types[0]; }
public override IType[] GetTypes(ICriteria criteria, ICriteriaQuery criteriaQuery) { IType projection = criteriaQuery.GetTypeUsingProjection(criteria, propertyName); return(new IType[] { projection }); }
/// <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; }
/// <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(); }
public override TypedValue[] GetTypedValues(ICriteria criteria, ICriteriaQuery criteriaQuery) { //we only need this for SQL Server, and or large amount of values if ((criteriaQuery.Factory.Dialect is MsSql2005Dialect) == false || values.Length < maximumNumberOfParametersToNotUseXml) { return expr.GetTypedValues(criteria, criteriaQuery); } IEntityPersister persister = null; IType type = criteriaQuery.GetTypeUsingProjection(criteria, propertyName); if (type.IsEntityType) { persister = criteriaQuery.Factory.GetEntityPersister(type.ReturnedClass.FullName); } StringWriter sw = new StringWriter(); XmlWriter writer = XmlWriter.Create(sw); writer.WriteStartElement("items"); foreach (object value in values) { if (value == null) continue; object valToWrite; if (persister != null) valToWrite = persister.GetIdentifier(value, EntityMode.Poco); else valToWrite = value; writer.WriteElementString("val", valToWrite.ToString()); } writer.WriteEndElement(); writer.WriteEndDocument(); writer.Flush(); string xmlString = sw.GetStringBuilder().ToString(); return new TypedValue[] { new TypedValue(new CustomType(typeof(XmlType), new Hashtable()), xmlString, EntityMode.Poco), }; }
public override IType[] GetTypes(ICriteria criteria, ICriteriaQuery criteriaQuery) { IType projection = criteriaQuery.GetTypeUsingProjection(criteria, propertyName); return new IType[] {projection}; }
public override SqlString ToSqlString(ICriteria criteria, ICriteriaQuery criteriaQuery, IDictionary<string,IFilter> enabledFilters) { //we only need this for SQL Server, and or large amount of values if ((criteriaQuery.Factory.Dialect is MsSql2005Dialect) == false || values.Length < maximumNumberOfParametersToNotUseXml) { return expr.ToSqlString(criteria, criteriaQuery, enabledFilters); } IType type = criteriaQuery.GetTypeUsingProjection(criteria, propertyName); if (type.IsCollectionType) { throw new QueryException("Cannot use collections with InExpression"); } if (values.Length == 0) { // "something in ()" is always false return new SqlString("1=0"); } SqlStringBuilder result = new SqlStringBuilder(); string[] columnNames = criteriaQuery.GetColumnsUsingProjection(criteria, propertyName); // Generate SqlString of the form: // columnName1 in (xml query) and columnName2 in (xml query) and ... criteriaQuery.AddUsedTypedValues(this.GetTypedValues(criteria, criteriaQuery)); for (int columnIndex = 0; columnIndex < columnNames.Length; columnIndex++) { string columnName = columnNames[columnIndex]; if (columnIndex > 0) { result.Add(" and "); } SqlType sqlType = type.SqlTypes(criteriaQuery.Factory)[columnIndex]; result .Add(columnName) .Add(" in (") .Add("SELECT ParamValues.Val.value('.','") .Add(criteriaQuery.Factory.Dialect.GetTypeName(sqlType)) .Add("') FROM ") .AddParameter() .Add(".nodes('/items/val') as ParamValues(Val)") .Add(")"); } return result.ToSqlString(); }