001package gudusoft.gsqlparser.nodes; 002 003import gudusoft.gsqlparser.ESqlClause; 004import gudusoft.gsqlparser.TCustomSqlStatement; 005import gudusoft.gsqlparser.TSourceToken; 006 007/** 008 * List of order by item which is type of {@link TOrderByItem}. 009*/ 010public class TOrderBy extends TParseTreeNode { 011 012 public TOrderBy(){ 013 014 } 015 public TOrderBy(String text){ 016 this(); 017 setText(text); 018 } 019 020 public void setText(String nodeText){ 021 if (nodeText.trim().startsWith("order")){ 022 super.setText(" "+nodeText); 023 }else{ 024 super.setText(" order by "+nodeText); 025 } 026 } 027 028 public void setSiblings(boolean siblings) { 029 this.siblings = siblings; 030 } 031 032 public boolean isSiblings() { 033 034 return siblings; 035 } 036 037 private boolean siblings = false; 038 private TExpression resetWhenCondition; 039 040 public TExpression getResetWhenCondition() { 041 return resetWhenCondition; 042 } 043 044 public TOrderByItemList getItems() { 045 if (items == null){ 046 items = new TOrderByItemList(); 047 } 048 return items; 049 } 050 051 private TOrderByItemList items = null; 052 053 public void init(Object arg1) 054 { 055 items = (TOrderByItemList)arg1; 056 } 057 058 public void init(Object arg1,Object arg2) 059 { 060 init(arg1); 061 resetWhenCondition = (TExpression)arg2; 062 } 063 064 065 public void doParse(TCustomSqlStatement psql, ESqlClause plocation){ 066 items.doParse(psql,plocation); 067 } 068 069 public void accept(TParseTreeVisitor v){ 070 v.preVisit(this); 071// this.getItems().accept(v); 072 v.postVisit(this); 073 } 074 075 public void acceptChildren(TParseTreeVisitor v){ 076 v.preVisit(this); 077 this.getItems().acceptChildren(v); 078 v.postVisit(this); 079 } 080 081 /** 082 * Used to add order by item manually, at least one item in list before add this new item 083 * @param item 084 */ 085 public void addOrderByItem(String item){ 086 if (items.size() == 0) return; 087 TOrderByItem orderBy = new TOrderByItem(); 088 orderBy.setGsqlparser(this.getGsqlparser()); 089 orderBy.setString(","+item); 090 091 TSourceToken last_token = items.getEndToken(); 092 orderBy.addAllMyTokensToTokenList(last_token.container,last_token.posinlist + 1); 093 094 for(int i=0;i<last_token.getNodesEndWithThisToken().size();i++){ 095 TParseTreeNode node = last_token.getNodesEndWithThisToken().get(i); 096 if (!(node instanceof TOrderByItem)){ 097 // change all end token of parse tree node except the last order by item 098 node.setEndToken(orderBy.getEndToken()); 099 } 100 } 101 102// boolean isLastTokenOfStmt = (last_token.posinlist ==last_token.container.size() - 1); 103// if (isLastTokenOfStmt){ 104// last_token.getNodesStartFromThisToken().getElement(0).setEndToken(orderBy.getEndToken()); 105// } 106 107 items.addOrderByItem(orderBy); 108 109 } 110 111 /** 112 * @deprecated As of v2.0.9.0 use {@link #getItems()} instead 113 * 114 * @param index 115 */ 116 public void removeOrderByItem(int index){ 117 if (TParseTreeNode.doubleLinkedTokenListToString){ 118 119 }else{ 120 if (items.size() > 1) { 121 TSourceToken st ; 122 if (index != items.size() - 1){ 123 st = items.getOrderByItem(index).getEndToken().searchToken(",",1); 124 }else{ 125 st = items.getOrderByItem(index).getStartToken().searchToken(",",-1); 126 } 127 128 items.getOrderByItem(index).removeAllMyTokensFromTokenList(st); 129 130 }else if (items.size() == 1){ 131 this.setString(" "); 132 } 133 134 } 135 items.removeAndSyncTokens(index); 136 } 137 138 139 public void setResetWhenCondition(TExpression resetWhenCondition) { 140 this.resetWhenCondition = resetWhenCondition; 141 } 142 143 public void setItems(TOrderByItemList items) { 144 this.items = items; 145 } 146 147 private TFetchFirstClause fetchFirstClause; 148 private TOffsetClause offsetClause; 149 150 public void setFetchFirstClause(TFetchFirstClause fetchFirstClause) { 151 this.fetchFirstClause = fetchFirstClause; 152 } 153 154 public void setOffsetClause(TOffsetClause offsetClause) { 155 this.offsetClause = offsetClause; 156 } 157 158 public TFetchFirstClause getFetchFirstClause() { 159 return fetchFirstClause; 160 } 161 162 public TOffsetClause getOffsetClause() { 163 return offsetClause; 164 } 165}