KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > xquark > extractor > sybase > sql > SqlCreateView


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