/// <summary> /// /// </summary> /// <param name="factory"></param> /// <param name="persistentClass"></param> /// <param name="alias"></param> /// <returns></returns> public override SqlString ToSqlString(ISessionFactoryImplementor factory, System.Type persistentClass, string alias, IDictionary aliasClasses) { //TODO: add default capacity SqlStringBuilder sqlBuilder = new SqlStringBuilder(); IType propertyType = AbstractCriterion.GetType(factory, persistentClass, _propertyName, aliasClasses); string[] columnNames = AbstractCriterion.GetColumns(factory, persistentClass, _propertyName, alias, aliasClasses); Parameter[] parameters = Parameter.GenerateParameters(factory, columnNames, propertyType); if (columnNames.Length != 1) { throw new HibernateException("insensitive like may only be used with single-column properties"); } if (factory.Dialect is PostgreSQLDialect) { sqlBuilder.Add(columnNames[0]); sqlBuilder.Add(" ilike "); } else { sqlBuilder.Add(factory.Dialect.LowercaseFunction) .Add("(") .Add(columnNames[0]) .Add(")") .Add(" like "); } sqlBuilder.Add(parameters[0]); return(sqlBuilder.ToSqlString()); }
/// <summary> /// Converts the SimpleExpression to a <see cref="SqlString"/>. /// </summary> /// <param name="factory">The ISessionFactory that contains the mapping for the Type.</param> /// <param name="persistentClass">The Class the Expression is being built for.</param> /// <param name="alias">The alias to use for the table.</param> /// <returns>A SqlString that contains a valid Sql fragment.</returns> public override SqlString ToSqlString(ISessionFactoryImplementor factory, System.Type persistentClass, string alias, IDictionary aliasClasses) { string[] columnNames = AbstractCriterion.GetColumns(factory, persistentClass, _propertyName, alias, aliasClasses); IType propertyType = AbstractCriterion.GetType(factory, persistentClass, _propertyName, aliasClasses); if (propertyType.IsPersistentCollectionType) { throw new QueryException(string.Format( "cannot use collection property ({0}.{1}) directly in a criterion," + " use ICriteria.CreateCriteria instead", persistentClass.FullName, _propertyName)); } Parameter[] parameters = Parameter.GenerateParameters(factory, columnNames, propertyType); 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(factory.Dialect.LowercaseFunction) .Add(StringHelper.OpenParen) .Add(columnNames[0]) .Add(StringHelper.ClosedParen) .Add(Op) .Add(parameters[0]) .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) .Add(parameters[i]); } return(sqlBuilder.ToSqlString()); } }
public override SqlString ToSqlString(ISessionFactoryImplementor factory, System.Type persistentClass, string alias, IDictionary aliasClasses) { if (_values.Length == 0) { // "something in ()" is always false return(new SqlString("1=0")); } //TODO: add default capacity SqlStringBuilder result = new SqlStringBuilder(); IType propertyType = AbstractCriterion.GetType(factory, persistentClass, _propertyName, aliasClasses); SqlType[] columnSqlTypes = propertyType.SqlTypes(factory); string[] columnNames = AbstractCriterion.GetColumns(factory, persistentClass, _propertyName, alias, aliasClasses); // 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]; SqlType columnSqlType = columnSqlTypes[columnIndex]; if (columnIndex > 0) { result.Add(" and "); } result .Add(columnName) .Add(" in ("); Parameter[] valueParameters = GenerateValueParameters(columnName, columnSqlType, _values.Length); for (int i = 0; i < valueParameters.Length; i++) { if (i > 0) { result.Add(StringHelper.CommaSpace); } result.Add(valueParameters[i]); } result.Add(")"); } return(result.ToSqlString()); }
public override SqlString ToSqlString(ISessionFactoryImplementor factory, System.Type persistentClass, string alias, IDictionary aliasClasses) { //TODO: add a default capacity SqlStringBuilder sqlBuilder = new SqlStringBuilder(); IType propertyType = AbstractCriterion.GetType(factory, persistentClass, _propertyName, aliasClasses); string[] columnNames = AbstractCriterion.GetColumns(factory, persistentClass, _propertyName, alias, aliasClasses); Parameter[] loParameters = Parameter.GenerateParameters( factory, StringHelper.Suffix(columnNames, "_lo"), propertyType); Parameter[] hiParameters = Parameter.GenerateParameters( factory, StringHelper.Suffix(columnNames, "_hi"), propertyType); bool andNeeded = false; for (int i = 0; i < columnNames.Length; i++) { if (andNeeded) { sqlBuilder.Add(" AND "); } andNeeded = true; sqlBuilder.Add(columnNames[i]) .Add(" between ") .Add(loParameters[i]) .Add(" and ") .Add(hiParameters[i]); } return(sqlBuilder.ToSqlString()); }