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 if (items != null) { 067 items.doParse(psql, plocation); 068 } 069 } 070 071 public void accept(TParseTreeVisitor v){ 072 v.preVisit(this); 073// this.getItems().accept(v); 074 v.postVisit(this); 075 } 076 077 public void acceptChildren(TParseTreeVisitor v){ 078 v.preVisit(this); 079 this.getItems().acceptChildren(v); 080 v.postVisit(this); 081 } 082 083 /** 084 * Used to add order by item manually, at least one item in list before add this new item 085 * @param item 086 */ 087 public void addOrderByItem(String item){ 088 if (items.size() == 0) return; 089 TOrderByItem orderBy = new TOrderByItem(); 090 orderBy.setGsqlparser(this.getGsqlparser()); 091 orderBy.setString(","+item); 092 093 TSourceToken last_token = items.getEndToken(); 094 orderBy.addAllMyTokensToTokenList(last_token.container,last_token.posinlist + 1); 095 096 for(int i=0;i<last_token.getNodesEndWithThisToken().size();i++){ 097 TParseTreeNode node = last_token.getNodesEndWithThisToken().get(i); 098 if (!(node instanceof TOrderByItem)){ 099 // change all end token of parse tree node except the last order by item 100 node.setEndToken(orderBy.getEndToken()); 101 } 102 } 103 104// boolean isLastTokenOfStmt = (last_token.posinlist ==last_token.container.size() - 1); 105// if (isLastTokenOfStmt){ 106// last_token.getNodesStartFromThisToken().getElement(0).setEndToken(orderBy.getEndToken()); 107// } 108 109 items.addOrderByItem(orderBy); 110 111 } 112 113 /** 114 * @deprecated As of v2.0.9.0 use {@link #getItems()} instead 115 * 116 * @param index 117 */ 118 public void removeOrderByItem(int index){ 119 if (TParseTreeNode.doubleLinkedTokenListToString){ 120 121 }else{ 122 if (items.size() > 1) { 123 TSourceToken st ; 124 if (index != items.size() - 1){ 125 st = items.getOrderByItem(index).getEndToken().searchToken(",",1); 126 }else{ 127 st = items.getOrderByItem(index).getStartToken().searchToken(",",-1); 128 } 129 130 items.getOrderByItem(index).removeAllMyTokensFromTokenList(st); 131 132 }else if (items.size() == 1){ 133 this.setString(" "); 134 } 135 136 } 137 items.removeAndSyncTokens(index); 138 } 139 140 141 public void setResetWhenCondition(TExpression resetWhenCondition) { 142 this.resetWhenCondition = resetWhenCondition; 143 } 144 145 public void setItems(TOrderByItemList items) { 146 this.items = items; 147 } 148 149 private TFetchFirstClause fetchFirstClause; 150 private TOffsetClause offsetClause; 151 152 public void setFetchFirstClause(TFetchFirstClause fetchFirstClause) { 153 this.fetchFirstClause = fetchFirstClause; 154 } 155 156 public void setOffsetClause(TOffsetClause offsetClause) { 157 this.offsetClause = offsetClause; 158 } 159 160 public TFetchFirstClause getFetchFirstClause() { 161 return fetchFirstClause; 162 } 163 164 public TOffsetClause getOffsetClause() { 165 return offsetClause; 166 } 167}