1 package com.quadcap.sql; 2 3 40 41 import java.io.ByteArrayOutputStream ; 42 import java.io.Externalizable ; 43 import java.io.IOException ; 44 import java.io.ObjectInput ; 45 import java.io.ObjectOutput ; 46 47 import java.util.Enumeration ; 48 import java.util.Hashtable ; 49 50 import java.util.Vector ; 51 52 import java.sql.SQLException ; 53 54 import com.quadcap.sql.io.ObjectInputStream; 55 import com.quadcap.sql.io.ObjectOutputStream; 56 57 import com.quadcap.sql.file.ByteUtil; 58 59 import com.quadcap.util.Debug; 60 import com.quadcap.util.Util; 61 62 68 public class DistinctCursor extends BC_Cursor { 69 Cursor sourceCursor; 70 Vector orderBy = null; 71 TempTable tempTable; 72 boolean distinct = true; 73 74 77 public DistinctCursor(Session session, Cursor cursor) throws SQLException { 78 super(session, null, null); 79 this.sourceCursor = cursor; 80 addColumns(session, cursor); 81 } 82 83 public final void checkCursor() throws SQLException , IOException { 84 if (bc == null) { 85 try { 86 makeTempTable(); 87 } finally { 88 try { 89 sourceCursor.close(); 90 } catch (Throwable t) { 91 } finally { 92 sourceCursor = null; 93 } 94 } 95 } 96 } 97 98 99 private void makeTempTable() throws IOException , SQLException { 100 int size = getColumnCount(); 101 if (orderBy == null) { 102 if (distinct) { 103 tempTable = new TempTable(session, new Key(size), null); 104 } else { 105 tempTable = new TempTable(session, new Key(1), new int[0]); 106 } 107 } else { 108 tempTable = makeObTemp(); 109 } 110 tempTable.addRows(sourceCursor); 111 row = new LazyRow(size); 112 bc = tempTable.getCursor(); 113 } 114 115 TempTable makeObTemp() throws SQLException , IOException { 116 int[] map = new int[getColumnCount()]; 117 boolean[] rmap = new boolean[map.length+1]; 118 boolean[] asc = new boolean[map.length]; 119 for (int i = 0; i < orderBy.size(); i++) { 120 OrderElement elem = (OrderElement)orderBy.elementAt(i); 121 int num = elem.getColumn(); 122 if (num < 0) { 123 String name = elem.getColName(); 124 Column col = getColumn(name); 125 if (col == null) { 126 throw new SQLException ("Bad column name: " + name); 127 } 128 num = col.getColumn(); 129 elem.setColumn(num); 130 } 131 map[i] = num; 132 rmap[num] = true; 133 asc[i] = elem.isAscending(); 134 } 135 int cnt = orderBy.size(); 136 for (int i = 1; i < rmap.length; i++) { 137 if (!rmap[i]) { 138 map[cnt] = i; 139 asc[cnt] = true; 140 cnt++; 141 } 142 } 143 Key compare = new Key(asc); 144 return new TempTable(session, compare, map); 145 } 146 147 public void setOrder(Vector v) { 148 this.orderBy = v; 149 } 150 151 public void setDistinct(boolean b) { 152 this.distinct = b; 153 } 154 155 public long getCurrentRowId() throws SQLException { 156 return ByteUtil.getLong(bc.getValBuf(), 1); 157 } 158 159 public void fetchCurrentRow() throws SQLException , IOException { 160 tempTable.getRow(bc.getValBuf(), row); 161 rowValid = true; 162 } 163 164 167 public void close() throws SQLException { 168 try { 169 super.close(); 170 } finally { 171 try { 172 if (sourceCursor != null) sourceCursor.close(); 173 } finally { 174 sourceCursor = null; 175 try { 176 if (tempTable != null) tempTable.release(); 177 } catch (IOException ex) { 178 throw DbException.wrapThrowable(ex); 179 } finally { 180 tempTable = null; 181 } 182 } 183 } 184 } 185 186 public String toString() { 188 return super.toString() + "\n\t[source cursor = " + sourceCursor + "]\n"; 189 } 190 } 192 | Popular Tags |