1 22 23 package org.xquark.extractor.sql; 24 25 import java.sql.ResultSet ; 26 import java.sql.SQLException ; 27 import java.sql.Statement ; 28 import java.util.ArrayList ; 29 import java.util.Iterator ; 30 import java.util.List ; 31 32 33 public class SqlExpression implements Constants 34 { 35 36 private static final String RCSRevision = "$Revision: 1.10 $"; 37 private static final String RCSName = "$Name: $"; 38 39 40 private List _precedents = null;; 41 42 private StatementInfo _sql = null; 44 private List _requestList = null; private List _resetStatementList = null; 46 47 public SqlExpression() {} 48 49 public String getName() 50 { 51 54 return null; 55 } 56 57 62 public List getPrecedents() 63 { 64 return _precedents; 65 } 66 67 72 public void setPrecedents(List aPrecedents) 73 { 74 _precedents = aPrecedents; 75 } 76 77 public void appendPrecedent(SqlExpression precedent) 78 { 79 if (null == _precedents) 80 _precedents = new ArrayList (); 81 82 _precedents.add(precedent); 83 } 84 85 public ResultSet Execute(Statement statement) throws SQLException 86 { 87 if ( null != _precedents ) 88 { 89 Iterator iter = _precedents.iterator(); 90 while (iter.hasNext()) { 91 ((SqlExpression)iter.next()).Execute( statement ); 92 } 93 } 94 return statement.executeQuery(toSql().sStmt); 95 } 96 97 public void cleanAll(Statement statement) throws SQLException { 98 if ( null != _precedents ) 99 { 100 Iterator iter = _precedents.iterator(); 101 while (iter.hasNext()) { 102 ((SqlExpression)iter.next()).cleanAll(statement); 103 } 104 } 105 clean(statement); 106 } 107 108 protected void clean(Statement statement) throws SQLException {} 109 110 111 public List getResetStatementList() { 112 if (_resetStatementList == null) 113 generateResetStatementList(); 114 return _resetStatementList; 115 } 116 117 public void generateResetStatementList() { 118 _resetStatementList = new ArrayList (); 119 generateResetStatementList(_resetStatementList); 120 } 121 122 protected List generateResetStatementList(List stmts) { 123 if ( null != _precedents ) 124 { 125 Iterator iter = _precedents.iterator(); 126 while (iter.hasNext()) { 127 ((SqlExpression)iter.next()).generateResetStatementList(stmts); 128 } 129 } 130 return getResetStatementList(stmts); 131 } 132 133 protected List getResetStatementList(List stmts) { return stmts;} 134 135 public final StatementInfo toSql() { 136 if (null == _sql) { 137 Context context = new Context(); 138 context.currentStmt.sStmt = toSql(context); 139 _sql = context.currentStmt; 140 } 141 return _sql; 142 } 143 144 public String toSql(Context context) {return toString ();} 145 146 public List getRequestList() { 147 if (_requestList == null) 148 generateRequestList(); 149 return _requestList; 150 } 151 152 public void generateRequestList() { 153 _requestList = new ArrayList (); 154 generateRequestList(_requestList); 155 } 156 157 protected void generateRequestList(List requestList) 158 { 159 if (null != _precedents && ! _precedents.isEmpty()) { 160 SqlExpression precedent = null; 161 for (int i = 0; i < _precedents.size(); i++) { 162 precedent = (SqlExpression) _precedents.get(i); 163 precedent.generateRequestList(requestList); 164 } 165 } 166 requestList.add(toSql()); 167 } 168 169 public String pprint() { 170 StringBuffer retVal = new StringBuffer (); 171 List requestList = getRequestList(); 172 173 String request = null; 174 for (int i = 0; i < requestList.size(); i++) { 175 request = (String ) requestList.get(i); 176 retVal.append(request); 177 retVal.append('\n'); 178 } 179 180 return retVal.toString(); 181 } 182 183 public String toString() { return toSql().sStmt;} 184 } 185 | Popular Tags |