internal static SqlString[] GetColumnNamesUsingProjection(IProjection projection, ICriteriaQuery criteriaQuery, ICriteria criteria, IDictionary <string, IFilter> enabledFilters) { SqlString sqlString = projection.ToSqlString(criteria, criteriaQuery.GetIndexForAlias(), criteriaQuery, enabledFilters); return(new SqlString[] { SqlStringHelper.RemoveAsAliasesFromSql(sqlString) }); }
/// <summary> /// Generate a sequence of <c>LEFT OUTER JOIN</c> clauses for the given associations. /// </summary> protected JoinFragment MergeOuterJoins(IList <OuterJoinableAssociation> associations) { JoinFragment outerjoin = Dialect.CreateOuterJoinFragment(); var sortedAssociations = GetSortedAssociations(associations); OuterJoinableAssociation last = null; foreach (OuterJoinableAssociation oj in sortedAssociations) { if (last != null && last.IsManyToManyWith(oj)) { oj.AddManyToManyJoin(outerjoin, (IQueryableCollection)last.Joinable); } else { // NH Different behavior : NH1179 and NH1293 // Apply filters for entity joins and Many-To-One associations SqlString filter = null; var enabledFiltersForJoin = oj.ForceFilter ? enabledFilters : enabledFiltersForManyToOne; if (oj.ForceFilter || enabledFiltersForJoin.Count > 0) { string manyToOneFilterFragment = oj.Joinable.FilterFragment(oj.RHSAlias, enabledFiltersForJoin); bool joinClauseDoesNotContainsFilterAlready = oj.On?.IndexOfCaseInsensitive(manyToOneFilterFragment) == -1; if (joinClauseDoesNotContainsFilterAlready) { filter = new SqlString(manyToOneFilterFragment); } } if (TableGroupJoinHelper.ProcessAsTableGroupJoin(new[] { oj }, new[] { oj.On, filter }, true, outerjoin, alias => true, factory)) { continue; } oj.AddJoins(outerjoin); // Ensure that the join condition is added to the join, not the where clause. // Adding the condition to the where clause causes left joins to become inner joins. if (SqlStringHelper.IsNotEmpty(filter)) { outerjoin.AddFromFragmentString(filter); } } last = oj; } return(outerjoin); }
internal static SqlString[] GetColumnNamesUsingProjection(IProjection projection, ICriteriaQuery criteriaQuery, ICriteria criteria) { if (projection is IPropertyProjection propertyProjection) { return(GetColumnNamesUsingPropertyName(criteriaQuery, criteria, propertyProjection.PropertyName)); } SqlString sqlString = projection.ToSqlString(criteria, criteriaQuery.GetIndexForAlias(), criteriaQuery); return(new SqlString[] { SqlStringHelper.RemoveAsAliasesFromSql(sqlString) }); }
public override SqlString ToSqlString(ICriteria criteria, int position, ICriteriaQuery criteriaQuery, IDictionary <string, IFilter> enabledFilters) { ISessionFactoryImplementor factory = criteriaQuery.Factory; SqlType[] sqlTypeCodes = type.SqlTypes(factory); if (sqlTypeCodes.Length != 1) { throw new QueryException("invalid Hibernate type for CastProjection"); } string sqlType = factory.Dialect.GetCastTypeName(sqlTypeCodes[0]); int loc = position * GetHashCode(); SqlString val = projection.ToSqlString(criteria, loc, criteriaQuery, enabledFilters); val = SqlStringHelper.RemoveAsAliasesFromSql(val); return(new SqlString("cast( ", val, " as ", sqlType, ") as ", GetColumnAliases(position)[0])); }
public List <TestModel> GetModel(TestSearchModel searchModel) { string sql = @" SELECT RoleNo , RoleName , RoleDescription , Status FROM dbo.WmsRole (NOLOCK) WHERE 1 = 1 {0} "; List <SqlParameter> parameterList = new List <SqlParameter>(); StringBuilder sbWhere = new StringBuilder(); SqlStringHelper.CreateSqlWhereAndPara(searchModel.RoleNo, SqlDbType.VarChar, "RoleNo", "RoleNo", ref sbWhere, ref parameterList); SqlStringHelper.CreateSqlWhereAndPara(searchModel.RoleName, SqlDbType.VarChar, "RoleName", "RoleName", ref sbWhere, ref parameterList); sql = string.Format(sql, sbWhere); return(ExecList <TestModel>(sql, false, parameterList)); }
public OuterJoinableAssociation(IAssociationType joinableType, String lhsAlias, String[] lhsColumns, String rhsAlias, JoinType joinType, SqlString withClause, ISessionFactoryImplementor factory, IDictionary <string, IFilter> enabledFilters) { this.joinableType = joinableType; this.lhsAlias = lhsAlias; this.lhsColumns = lhsColumns; this.rhsAlias = rhsAlias; this.joinType = joinType; joinable = joinableType.GetAssociatedJoinable(factory); rhsColumns = JoinHelper.GetRHSColumnNames(joinableType, factory); on = new SqlString(joinableType.GetOnCondition(rhsAlias, factory, enabledFilters)); if (SqlStringHelper.IsNotEmpty(withClause)) { on = on.Append(" and ( ").Append(withClause).Append(" )"); } this.enabledFilters = enabledFilters; // needed later for many-to-many/filter application }
private bool NeedsTableGroupJoin(List <Join> joins, SqlString[] withClauseFragments, bool includeSubclasses) { // If the rewrite is disabled or we don't have a with clause, we don't need a table group join if (/*!collectionJoinSubquery ||*/ withClauseFragments.All(x => SqlStringHelper.IsEmpty(x))) { return(false); } // If we only have one join, a table group join is only necessary if subclass columns are used in the with clause if (joins.Count == 1) { return(joins[0].Joinable is AbstractEntityPersister persister && persister.HasSubclassJoins(includeSubclasses)); //NH Specific: No alias processing //return isSubclassAliasDereferenced( joins[ 0], withClauseFragment ); } //NH Specific: No alias processing (see hibernate JoinSequence.NeedsTableGroupJoin) return(true); }
private SqlString GetWithClause(IDictionary <string, IFilter> enabledFilters, ref SqlString withClauseFragment, Join join, IJoinable last) { string on = join.AssociationType.GetOnCondition(join.Alias, factory, enabledFilters); var withConditions = new List <object>(); if (!string.IsNullOrEmpty(on)) { withConditions.Add(on); } if (last != null && IsManyToManyRoot(last) && ((IQueryableCollection)last).ElementType == join.AssociationType) { // the current join represents the join between a many-to-many association table // and its "target" table. Here we need to apply any additional filters // defined specifically on the many-to-many string manyToManyFilter = ((IQueryableCollection)last) .GetManyToManyFilterFragment(join.Alias, enabledFilters); if (!string.IsNullOrEmpty(manyToManyFilter)) { withConditions.Add(manyToManyFilter); } } else if (string.IsNullOrEmpty(on)) { // NH Different behavior : NH1179 and NH1293 // Apply filters for entity joins and Many-To-One association var enabledFiltersForJoin = ForceFilter ? enabledFilters : FilterHelper.GetEnabledForManyToOne(enabledFilters); if (ForceFilter || enabledFiltersForJoin.Count > 0) { withConditions.Add(join.Joinable.FilterFragment(join.Alias, enabledFiltersForJoin)); } } if (withClauseFragment != null && !IsManyToManyRoot(join.Joinable)) { withConditions.Add(withClauseFragment); withClauseFragment = null; } return(SqlStringHelper.JoinParts(" and ", withConditions)); }
/// <summary> /// Render the SQL fragment /// </summary> public virtual SqlString ToSqlString(ICriteria criteria, ICriteriaQuery criteriaQuery) { if (projection != null) { SqlString sb = SqlString.Empty; SqlString produced = this.projection.ToSqlString(criteria, 0, criteriaQuery); SqlString truncated = SqlStringHelper.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())); }
public override SqlString ToSqlString(ICriteria criteria, int loc, ICriteriaQuery criteriaQuery) { if (projection == null) { return(new SqlString(aggregate, "(", criteriaQuery.GetColumn(criteria, propertyName), ") as y", loc.ToString(), "_")); } return(new SqlString( aggregate, "(", SqlStringHelper.RemoveAsAliasesFromSql( projection.ToSqlString( criteria, loc, criteriaQuery)), ") as y", loc.ToString(), "_")); }
private void AddJoinNodes(IRestrictableStatement query, JoinSequence join, FromElement fromElement, bool supportRootAlias) { JoinFragment joinFragment = join.ToJoinFragment( _walker.EnabledFilters, fromElement.UseFromFragment || fromElement.IsDereferencedBySuperclassOrSubclassProperty, fromElement.WithClauseFragment, fromElement.WithClauseJoinAlias, supportRootAlias ? join.RootAlias : string.Empty ); SqlString frag = joinFragment.ToFromFragmentString; SqlString whereFrag = joinFragment.ToWhereFragmentString; // If the from element represents a JOIN_FRAGMENT and it is // a theta-style join, convert its type from JOIN_FRAGMENT // to FROM_FRAGMENT if (fromElement.Type == HqlSqlWalker.JOIN_FRAGMENT && (join.IsThetaStyle || SqlStringHelper.IsNotEmpty(whereFrag))) { fromElement.Type = HqlSqlWalker.FROM_FRAGMENT; fromElement.JoinSequence.SetUseThetaStyle(true); // this is used during SqlGenerator processing } // If there is a FROM fragment and the FROM element is an explicit, then add the from part. if (fromElement.UseFromFragment /*&& StringHelper.isNotEmpty( frag )*/) { SqlString fromFragment = ProcessFromFragment(frag, join).Trim(); if (log.IsDebugEnabled()) { log.Debug("Using FROM fragment [{0}]", fromFragment); } ProcessDynamicFilterParameters(fromFragment, fromElement, _walker); } _syntheticAndFactory.AddWhereFragment( joinFragment, whereFrag, query, fromElement, _walker ); }
/// <summary> /// 根据角色判断方法是否有权限 /// </summary> /// <param name="className">类名</param> /// <param name="methodName">方法名</param> /// <param name="rolesId">角色Id</param> /// <returns>是否有权限</returns> public bool CheckClassMethod(string className, string methodName, string rolesId) { bool ret = false; string where = string.Format("tmp.RolesId = {0}", rolesId); IList <ZhuJi.UUMS.Domain.PermissionByMethods> list = base.GetObjects(where, string.Empty); foreach (ZhuJi.UUMS.Domain.PermissionByMethods pbm in list) { string whereMethods = SqlStringHelper.ClearInput(string.Format("tmp.ExplainInfo.Id = {0} And tmp.ClassName Like '{1}' And tmp.MethodName Like '{2}'", pbm.ExplainInfo.Id, className, methodName)); ZhuJi.UUMS.NHibernateDAL.Methods methodsService = new ZhuJi.UUMS.NHibernateDAL.Methods(); IList <ZhuJi.UUMS.Domain.Methods> listMethods = methodsService.GetObjects(whereMethods, string.Empty); if (listMethods.Count > 0) { ret = pbm.Allowed; break; } } return(ret); }
public override SqlString ToSqlString(ICriteria criteria, int position, ICriteriaQuery criteriaQuery, IDictionary <string, IFilter> enabledFilters) { var buf = new SqlStringBuilder().Add("count("); if (distinct) { buf.Add("distinct "); } if (projection != null) { buf.Add(SqlStringHelper.RemoveAsAliasesFromSql(projection.ToSqlString(criteria, position, criteriaQuery, enabledFilters))); } else { buf.Add(criteriaQuery.GetColumn(criteria, propertyName)); } buf.Add(") as y").Add(position.ToString()).Add("_"); return(buf.ToSqlString()); }
public override SqlString ToSqlString(ICriteria criteria, int loc, ICriteriaQuery criteriaQuery, IDictionary <string, IFilter> enabledFilters) { if (projection != null) { return (new SqlString(new object[] { aggregate, "(", SqlStringHelper.RemoveAsAliasesFromSql(projection.ToSqlString(criteria, loc, criteriaQuery, enabledFilters)), ") as y", loc.ToString(), "_" })); } else { return (new SqlString(new object[] { aggregate, "(", criteriaQuery.GetColumn(criteria, propertyName), ") as y", loc.ToString(), "_" })); } }
public override SqlString ToSqlString(ICriteria criteria, ICriteriaQuery criteriaQuery, IDictionary <string, IFilter> enabledFilters) { SqlString[] columns = CriterionUtil.GetColumnNames(null, _projection, criteriaQuery, criteria, enabledFilters); if (columns.Length != 1) { throw new HibernateException("Like may only be used with single-column properties / projections."); } var value = SqlStringHelper.RemoveAsAliasesFromSql(_value.ToSqlString(criteria, 0, criteriaQuery, enabledFilters)); var dialect = criteriaQuery.Factory.Dialect; var builder = new SqlStringBuilder(8) .Add(dialect.LowercaseFunction) .Add(StringHelper.OpenParen) .Add(columns[0]) .Add(StringHelper.ClosedParen) .Add(" like ") .Add(dialect.LowercaseFunction) .Add(StringHelper.OpenParen) .Add(value) .Add(StringHelper.ClosedParen); return(builder.ToSqlString()); }
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.Add(SqlStringHelper.RemoveAsAliasesFromSql(projection.ToSqlString(criteria, loc, criteriaQuery))); } 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()); }
private static HashSet <string> GetDependsOn(OuterJoinableAssociation association) { if (SqlStringHelper.IsEmpty(association.On)) { return(null); } var dependencies = new HashSet <string>(StringComparer.OrdinalIgnoreCase); foreach (Match match in aliasRegex.Matches(association.On.ToString())) { string alias = match.Value; if (string.Equals(alias, association.RHSAlias, StringComparison.OrdinalIgnoreCase)) { continue; } dependencies.Add(alias); } return(dependencies); }
private void Out(IASTNode n) { var parameterNode = n as ParameterNode; if (parameterNode != null) { var list = parameterNode.HqlParameterSpecification.GetIdsForBackTrack(sessionFactory).Select( backTrack => { var parameter = Parameter.Placeholder; parameter.BackTrack = backTrack; return(parameter); }).ToList(); Out(SqlStringHelper.ParametersList(list)); } else if (n is SqlNode) { Out(((SqlNode)n).RenderText(sessionFactory)); } else { Out(n.Text); } if (parameterNode != null) { collectedParameters.Add(parameterNode.HqlParameterSpecification); } else if (n is IParameterContainer) { var parameterContainer = (IParameterContainer)n; if (parameterContainer.HasEmbeddedParameters) { IParameterSpecification[] specifications = parameterContainer.GetEmbeddedParameters(); collectedParameters.AddRange(specifications); } } }
public override SqlString ToSqlString(ICriteria criteria, int loc, ICriteriaQuery criteriaQuery, IDictionary <string, IFilter> enabledFilters) { ISessionFactoryImplementor factory = criteriaQuery.Factory; SqlType[] sqlTypeCodes = NHibernateUtil.Double.SqlTypes(factory); string sqlType = factory.Dialect.GetCastTypeName(sqlTypeCodes[0]); string parameter; if (projection != null) { parameter = SqlStringHelper.RemoveAsAliasesFromSql(projection.ToSqlString(criteria, loc, criteriaQuery, enabledFilters)).ToString(); } else { parameter = criteriaQuery.GetColumn(criteria, propertyName); } string expression = string.Format("{0}(cast({1} as {2})) as {3}", aggregate, parameter, sqlType, GetColumnAliases(loc)[0]); return(new SqlString(expression)); }
public override SqlString ToSqlString(ICriteria criteria, int position, ICriteriaQuery criteriaQuery, IDictionary <string, IFilter> enabledFilters) { SqlString condition = criterion.ToSqlString(criteria, criteriaQuery, enabledFilters); SqlString ifTrue = whenTrue.ToSqlString(criteria, position + GetHashCode() + 1, criteriaQuery, enabledFilters); ifTrue = SqlStringHelper.RemoveAsAliasesFromSql(ifTrue); SqlString ifFalse = whenFalse.ToSqlString(criteria, position + GetHashCode() + 2, criteriaQuery, enabledFilters); ifFalse = SqlStringHelper.RemoveAsAliasesFromSql(ifFalse); return(new SqlStringBuilder() .Add("(") .Add("case when ") .Add(condition) .Add(" then ") .Add(ifTrue) .Add(" else ") .Add(ifFalse) .Add(" end") .Add(")") .Add(" as ") .Add(GetColumnAliases(position)[0]) .ToSqlString()); }
private static SqlString GetProjectionArgument(ICriteriaQuery criteriaQuery, ICriteria criteria, IProjection projection, int loc) { SqlString sql = projection.ToSqlString(criteria, loc, criteriaQuery); return(SqlStringHelper.RemoveAsAliasesFromSql(sql)); }
public virtual SqlString ToGroupSqlString(ICriteria criteria, ICriteriaQuery criteriaQuery, IDictionary <string, IFilter> enabledFilters) { return(SqlStringHelper.RemoveAsAliasesFromSql(this.projection.ToSqlString(criteria, 0, criteriaQuery, enabledFilters))); }
public virtual SqlString ToGroupSqlString(ICriteria criteria, ICriteriaQuery criteriaQuery, IDictionary <string, IFilter> enabledFilters) { //This is kind of a hack. The hack is based on the fact that ToGroupSqlString always called after ToSqlString. return(SqlStringHelper.RemoveAsAliasesFromSql(renderedProjection)); }
private static SqlString[] GetProjectionColumns(IProjection projection, ICriteriaQuery criteriaQuery, ICriteria criteria) { var sqlString = projection.ToSqlString(criteria, criteriaQuery.GetIndexForAlias(), criteriaQuery); return(new[] { SqlStringHelper.RemoveAsAliasesFromSql(sqlString) }); }
private SqlString GetColumnNamesString(int scalarColumnIndex) { return(SqlStringHelper.Join(new SqlString(", "), Walker.SelectClause.ColumnNames[scalarColumnIndex])); }
public static SqlString GetColumnName(this IProjection projection, ICriteria criteria, ICriteriaQuery criteriaQuery, IDictionary <string, IFilter> enabledFilters) { return(SqlStringHelper.RemoveAsAliasesFromSql( projection.ToSqlString(criteria, criteriaQuery.GetIndexForAlias(), criteriaQuery, enabledFilters) )); }