KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > xquark > extractor > sql > SqlExpression


1 /*
2  * This file belongs to the XQuark distribution.
3  * Copyright (C) 2003 Universite de Versailles Saint-Quentin.
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307.
18  * You can also get it at http://www.gnu.org/licenses/lgpl.html
19  *
20  * For more information on this software, see http://www.xquark.org.
21  */

22
23 package org.xquark.extractor.sql;
24
25 import java.sql.ResultSet JavaDoc;
26 import java.sql.SQLException JavaDoc;
27 import java.sql.Statement JavaDoc;
28 import java.util.ArrayList JavaDoc;
29 import java.util.Iterator JavaDoc;
30 import java.util.List JavaDoc;
31
32
33 public class SqlExpression implements Constants
34 {
35
36     private static final String JavaDoc RCSRevision = "$Revision: 1.10 $";
37     private static final String JavaDoc RCSName = "$Name: $";
38
39
40     private List JavaDoc _precedents = null;;
41
42     private StatementInfo _sql = null; // TODO: check that this field is not set somewhere before generation (2times)
43

44     private List JavaDoc _requestList = null; // contains either Strings or StatementInfos (according to the accessor used)
45
private List JavaDoc _resetStatementList = null;
46
47     public SqlExpression() {}
48
49     public String JavaDoc getName()
50     {
51         /* this is a default implemetaion for most of subclasses of SqlExpression
52         * SqlTable, SqlAttributeExpression, SqlRenameItem, SqlRenameRelation
53         *re-implemente this function */

54         return null;
55     }
56
57     /**
58      * Access method for the _precedents property.
59      *
60      * @return the current value of the _precedents property
61      */

62     public List JavaDoc getPrecedents()
63     {
64         return _precedents;
65     }
66
67     /**
68      * Sets the value of the _precedents property.
69      *
70      * @param aPrecedents the new value of the _precedents property
71      */

72     public void setPrecedents(List JavaDoc aPrecedents)
73     {
74         _precedents = aPrecedents;
75     }
76
77     public void appendPrecedent(SqlExpression precedent)
78     {
79         if (null == _precedents)
80             _precedents = new ArrayList JavaDoc();
81         
82         _precedents.add(precedent);
83     }
84
85     public ResultSet JavaDoc Execute(Statement JavaDoc statement) throws SQLException JavaDoc
86     {
87         if ( null != _precedents )
88         {
89             Iterator JavaDoc 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 JavaDoc statement) throws SQLException JavaDoc {
98         if ( null != _precedents )
99         {
100             Iterator JavaDoc 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 JavaDoc statement) throws SQLException JavaDoc {}
109
110
111     public List JavaDoc getResetStatementList() {
112         if (_resetStatementList == null)
113             generateResetStatementList();
114         return _resetStatementList;
115     }
116     
117     public void generateResetStatementList() {
118         _resetStatementList = new ArrayList JavaDoc();
119         generateResetStatementList(_resetStatementList);
120     }
121     
122     protected List JavaDoc generateResetStatementList(List JavaDoc stmts) {
123         if ( null != _precedents )
124         {
125             Iterator JavaDoc iter = _precedents.iterator();
126             while (iter.hasNext()) {
127                 ((SqlExpression)iter.next()).generateResetStatementList(stmts);
128             }
129         }
130         return getResetStatementList(stmts);
131     }
132
133     protected List JavaDoc getResetStatementList(List JavaDoc 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 JavaDoc toSql(Context context) {return toString ();}
145
146     public List JavaDoc getRequestList() {
147         if (_requestList == null)
148             generateRequestList();
149         return _requestList;
150     }
151
152     public void generateRequestList() {
153         _requestList = new ArrayList JavaDoc();
154         generateRequestList(_requestList);
155     }
156
157     protected void generateRequestList(List JavaDoc 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 JavaDoc pprint() {
170         StringBuffer JavaDoc retVal = new StringBuffer JavaDoc();
171         List JavaDoc requestList = getRequestList();
172
173         String JavaDoc request = null;
174         for (int i = 0; i < requestList.size(); i++) {
175             request = (String JavaDoc) requestList.get(i);
176             retVal.append(request);
177             retVal.append('\n');
178         }
179
180         return retVal.toString();
181     }
182
183     public String JavaDoc toString() { return toSql().sStmt;}
184 }
185
Popular Tags