| 1 package com.quadcap.sql; 2 3 40 41 import java.io.BufferedInputStream ; 42 import java.io.BufferedOutputStream ; 43 import java.io.ByteArrayInputStream ; 44 import java.io.ByteArrayOutputStream ; 45 import java.io.Externalizable ; 46 import java.io.InputStream ; 47 import java.io.IOException ; 48 import java.io.ObjectInput ; 49 import java.io.ObjectOutput ; 50 import java.io.OutputStream ; 51 52 import java.util.Enumeration ; 53 import java.util.Vector ; 54 55 import java.sql.SQLException ; 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 OrderByCursor extends BC_Cursor { 69 TempTable tempTable; 70 int[] kcols; 71 int rowCount = 0; 72 73 public OrderByCursor(Session session, Cursor c, Vector v) 74 throws SQLException , IOException  75 { 76 super(session, c.getName(), null); 77 this.session = session; 78 addColumns(session, c); 79 80 this.kcols = new int[v.size()]; 82 83 int kcnt = 0; 84 boolean[] asc = new boolean[v.size() + 1]; 85 for (int i = 0; i < v.size(); i++) { 86 OrderElement elem = (OrderElement)v.elementAt(i); 87 asc[i] = elem.isAscending(); 88 int num = elem.getColumn(); 89 if (num < 0) { 90 String name = elem.getColName(); 91 Column col = c.getColumn(name); 92 if (col == null) { 93 Debug.println("bad col: " + name + ", c = " + c); 95 throw new SQLException ("Bad column name: " + name); 97 } 98 num = col.getColumn(); 99 elem.setColumn(num); 100 } 101 kcols[kcnt++] = num; 102 } 103 asc[v.size()] = true; 104 Key compare = new Key(asc); 105 106 this.tempTable = new TempTable(session, compare, kcols); 107 tempTable.addRows(c); 108 c.close(); 109 this.row = new LazyRow(c.getColumnCount()); 110 beforeFirst(); 111 } 112 113 public void checkCursor() throws SQLException , IOException { 114 if (bc == null) { 115 bc = tempTable.getCursor(); 116 } 117 } 118 119 public long getCurrentRowId() throws SQLException { 120 return ByteUtil.getLong(bc.getValBuf(), 1); 121 } 122 123 public void fetchCurrentRow() throws SQLException , IOException { 124 tempTable.getRow(bc.getValBuf(), row); 125 rowValid = true; 126 } 127 128 public void close() throws SQLException { 129 try { 130 super.close(); 131 } finally { 132 try { 133 if (tempTable != null) tempTable.release(); 134 } catch (IOException ex) { 135 throw DbException.wrapThrowable(ex); 136 } finally { 137 tempTable = null; 138 } 139 } 140 } 141 } 142 | Popular Tags |