1 22 23 package org.xquark.extractor.mysql.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 import org.xquark.extractor.sql.StatementInfo; 34 35 public class SqlCreateTempTableAs extends SqlExpression { 36 private static final String RCSRevision = "$Revision: 1.6 $"; 37 private static final String RCSName = "$Name: $"; 38 39 protected String _name = null; 40 protected List _attributeNameList = null; 41 protected SqlExpression _query = null; 42 43 public SqlCreateTempTableAs() { 44 } 45 46 public SqlCreateTempTableAs(String name, SqlExpression query) { 47 setName(name); 48 setquery(query); 49 } 50 51 public SqlCreateTempTableAs(String name, List attributeNameList, SqlExpression query) { 52 setName(name); 53 setAttributeNameList(attributeNameList); 54 setquery(query); 55 } 56 57 public void setName(String name) { 58 _name = name; 59 } 60 61 public String getName() { 62 return _name; 63 } 64 65 public void setAttributeNameList(List attributeNameListList) { 66 _attributeNameList = attributeNameListList; 67 } 68 69 public List 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 Execute(Statement statement) throws SQLException { 82 ResultSet retResultSet = null; 83 List prcedents = getPrecedents(); 84 if (null != prcedents) { 85 Iterator iter = prcedents.iterator(); 86 while (iter.hasNext()) { 87 ((SqlExpression) iter.next()).Execute(statement); 88 } 89 } 90 String query = this.toSql(new Context()); 91 try { 92 statement.executeUpdate(query); 93 } catch (SQLException ex) { 94 if (true 95 ) { 96 statement.executeUpdate("DROP TABLE " + _name); 97 statement.executeUpdate(query); 98 } 99 } 100 101 return retResultSet; 102 } 103 104 public String toSql(Context context) { 105 StringBuffer retVal = new StringBuffer (); 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 attributeNameListToSql(StringBuffer statementBuffer, Context context) { 116 StringBuffer retVal = statementBuffer; 117 if (null != _attributeNameList && !_attributeNameList.isEmpty()) { 118 statementBuffer.append('('); 119 String attrName = null; 120 for (int i = 0; i < _attributeNameList.size(); i++) { 121 attrName = (String ) _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 statement) throws SQLException { 133 statement.execute("DROP TABLE " + _name); 134 } 135 136 protected List getResetStatementList(List stmts) 137 { 138 StringBuffer buf = new StringBuffer ("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 |