1 22 23 package org.xquark.extractor.sybase.sql; 24 25 import java.sql.ResultSet ; 26 import java.sql.SQLException ; 27 import java.sql.Statement ; 28 import java.util.Iterator ; 29 import java.util.List ; 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 RCSRevision = "$Revision: 1.6 $"; 37 private static final String RCSName = "$Name: $"; 38 39 40 protected String _name = null; 41 protected List _attributeNameList = null; 42 protected SqlExpression _query = null; 43 44 public SqlCreateView() { 45 } 46 47 public SqlCreateView(String name, SqlExpression query) { 48 setName(name); 49 setquery(query); 50 } 51 52 public SqlCreateView(String name, List attributeNameList, SqlExpression query) { 53 setName(name); 54 setAttributeNameList(attributeNameList); 55 setquery(query); 56 } 57 58 public void setName(String name) { 59 _name = name; 60 } 61 62 public String getName() { 63 return _name; 64 } 65 66 public void setAttributeNameList(List attributeNameListList) { 67 _attributeNameList = attributeNameListList; 68 } 69 70 public List 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 Execute(Statement statement) throws SQLException 83 { 84 ResultSet retResultSet = null; 85 List prcedents = getPrecedents(); 86 if ( null != prcedents ) 87 { 88 Iterator iter = prcedents.iterator(); 89 while (iter.hasNext()) { 90 ((SqlExpression)iter.next()).Execute( statement ); 91 } 92 } 93 String query = this.toSql(new Context()); 94 try { 96 statement.executeUpdate(query); 97 } 98 catch (SQLException ex) { 99 if (true ) { 100 statement.executeUpdate("DROP VIEW " + _name) ; 101 statement.executeUpdate(query) ; 102 } 103 } 104 105 return retResultSet; 106 } 107 108 public String toSql(Context context) 109 { 110 StringBuffer retVal = new StringBuffer ("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 attributeNameListToSql(StringBuffer statementBuffer, Context context) { 120 StringBuffer retVal = statementBuffer; 121 if (null != _attributeNameList && !_attributeNameList.isEmpty()) { 122 statementBuffer.append('('); 123 String attrName = null; 124 for (int i = 0; i < _attributeNameList.size(); i++) { 125 attrName = (String )_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 statement) throws SQLException { 138 statement.execute("DROP VIEW " + _name); 139 } 140 } 141 | Popular Tags |