001package gudusoft.gsqlparser.resolver2.scope; 002 003import gudusoft.gsqlparser.nodes.TOrderBy; 004import gudusoft.gsqlparser.resolver2.ScopeType; 005 006/** 007 * Scope for ORDER BY clause. 008 * 009 * <p>Visibility rules: 010 * - Can access SELECT list columns (by name or alias) 011 * - Can access SELECT list positional references (ORDER BY 1, 2, etc.) 012 * - In some databases, can also access columns from FROM clause directly 013 * - Can reference aggregate functions in SELECT list 014 * 015 * <p>Example: 016 * <pre> 017 * SELECT department, COUNT(*) as emp_count 018 * FROM employees 019 * GROUP BY department 020 * ORDER BY emp_count DESC, -- Can use SELECT list alias 021 * department ASC, -- Can use SELECT list column 022 * 1 -- Can use positional reference 023 * </pre> 024 * 025 * <p>The ORDER BY scope primarily delegates to SELECT scope, 026 * but may also access FROM scope depending on database vendor. 027 */ 028public class OrderByScope extends AbstractScope { 029 030 /** The ORDER BY AST node */ 031 private final TOrderBy orderByNode; 032 033 /** Parent SELECT scope (for SELECT list alias resolution) */ 034 private IScope selectScope; 035 036 /** Parent FROM scope (for direct column resolution, database-dependent) */ 037 private IScope fromScope; 038 039 public OrderByScope(IScope parent, TOrderBy orderByNode) { 040 super(parent, orderByNode, ScopeType.ORDER_BY); 041 this.orderByNode = orderByNode; 042 } 043 044 /** 045 * Set the SELECT scope for alias resolution. 046 * ORDER BY can reference SELECT list columns and aliases. 047 * 048 * @param selectScope the SELECT scope 049 */ 050 public void setSelectScope(IScope selectScope) { 051 this.selectScope = selectScope; 052 } 053 054 /** 055 * Set the FROM scope for column resolution. 056 * In some databases, ORDER BY can reference FROM clause columns directly. 057 * 058 * @param fromScope the FROM scope 059 */ 060 public void setFromScope(IScope fromScope) { 061 this.fromScope = fromScope; 062 } 063 064 /** 065 * Get the SELECT scope. 066 * 067 * @return the SELECT scope, or null if not set 068 */ 069 public IScope getSelectScope() { 070 return selectScope; 071 } 072 073 /** 074 * Get the FROM scope. 075 * 076 * @return the FROM scope, or null if not set 077 */ 078 public IScope getFromScope() { 079 return fromScope; 080 } 081 082 public TOrderBy getOrderByNode() { 083 return orderByNode; 084 } 085 086 @Override 087 public String toString() { 088 return "OrderByScope"; 089 } 090}