KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > xquark > extractor > mysql > sql > SqlCreateTempTableAs


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.mysql.sql;
24
25 import java.sql.ResultSet JavaDoc;
26 import java.sql.SQLException JavaDoc;
27 import java.sql.Statement JavaDoc;
28 import java.util.Iterator JavaDoc;
29 import java.util.List JavaDoc;
30
31 import org.xquark.extractor.sql.Context;
32 import org.xquark.extractor.sql.SqlExpression;
33 import org.xquark.extractor.sql.StatementInfo;
34
35 public class SqlCreateTempTableAs extends SqlExpression {
36     private static final String JavaDoc RCSRevision = "$Revision: 1.6 $";
37     private static final String JavaDoc RCSName = "$Name: $";
38
39     protected String JavaDoc _name = null;
40     protected List JavaDoc _attributeNameList = null;
41     protected SqlExpression _query = null;
42
43     public SqlCreateTempTableAs() {
44     }
45
46     public SqlCreateTempTableAs(String JavaDoc name, SqlExpression query) {
47         setName(name);
48         setquery(query);
49     }
50
51     public SqlCreateTempTableAs(String JavaDoc name, List JavaDoc attributeNameList, SqlExpression query) {
52         setName(name);
53         setAttributeNameList(attributeNameList);
54         setquery(query);
55     }
56
57     public void setName(String JavaDoc name) {
58         _name = name;
59     }
60
61     public String JavaDoc getName() {
62         return _name;
63     }
64
65     public void setAttributeNameList(List JavaDoc attributeNameListList) {
66         _attributeNameList = attributeNameListList;
67     }
68
69     public List JavaDoc getAttributeNameList() {
70         return _attributeNameList;
71     }
72
73     public void setquery(SqlExpression query) {
74         _query = query;
75     }
76
77     public SqlExpression getquery() {
78         return _query;
79     }
80
81     public ResultSet JavaDoc Execute(Statement JavaDoc statement) throws SQLException JavaDoc {
82         ResultSet JavaDoc retResultSet = null;
83         List JavaDoc prcedents = getPrecedents();
84         if (null != prcedents) {
85             Iterator JavaDoc iter = prcedents.iterator();
86             while (iter.hasNext()) {
87                 ((SqlExpression) iter.next()).Execute(statement);
88             }
89         }
90         String JavaDoc query = this.toSql(new Context());
91         try {
92             statement.executeUpdate(query);
93         } catch (SQLException JavaDoc ex) {
94             if (true /** @todo the view exists alreay */
95                 ) {
96                 statement.executeUpdate("DROP TABLE " + _name);
97                 statement.executeUpdate(query);
98             }
99         }
100
101         return retResultSet;
102     }
103
104     public String JavaDoc toSql(Context context) {
105         StringBuffer JavaDoc retVal = new StringBuffer JavaDoc();
106         retVal.append("CREATE TEMPORARY TABLE ");
107         retVal.append(_name);
108         attributeNameListToSql(retVal, context);
109         retVal.append(" ");
110         retVal.append(_query.toSql(context));
111
112         return retVal.toString();
113     }
114
115     protected StringBuffer JavaDoc attributeNameListToSql(StringBuffer JavaDoc statementBuffer, Context context) {
116         StringBuffer JavaDoc retVal = statementBuffer;
117         if (null != _attributeNameList && !_attributeNameList.isEmpty()) {
118             statementBuffer.append('(');
119             String JavaDoc attrName = null;
120             for (int i = 0; i < _attributeNameList.size(); i++) {
121                 attrName = (String JavaDoc) _attributeNameList.get(i);
122                 statementBuffer.append(attrName);
123                 if (i < _attributeNameList.size() - 1) {
124                     statementBuffer.append(", ");
125                 }
126             }
127             statementBuffer.append(')');
128         }
129         return statementBuffer;
130     }
131
132     protected void clean(Statement JavaDoc statement) throws SQLException JavaDoc {
133         statement.execute("DROP TABLE " + _name);
134     }
135     
136     protected List JavaDoc getResetStatementList(List JavaDoc stmts)
137     {
138         StringBuffer JavaDoc buf = new StringBuffer JavaDoc("TRUNCATE TABLE ");
139         buf.append(_name);
140         stmts.add(new StatementInfo(buf.toString()));
141         buf.setLength(0);
142         buf.append("INSERT INTO ");
143         buf.append(_name);
144         buf.append(' ');
145         Context context = new Context();
146         buf.append(_query.toSql(context));
147         context.currentStmt.sStmt = buf.toString();
148         stmts.add(context.currentStmt);
149         return stmts;
150     }
151 }
152
Popular Tags