KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > quadcap > sql > OrderByCursor


1 package com.quadcap.sql;
2
3 /* Copyright 1999 - 2003 Quadcap Software. All rights reserved.
4  *
5  * This software is distributed under the Quadcap Free Software License.
6  * This software may be used or modified for any purpose, personal or
7  * commercial. Open Source redistributions are permitted. Commercial
8  * redistribution of larger works derived from, or works which bundle
9  * this software requires a "Commercial Redistribution License"; see
10  * http://www.quadcap.com/purchase.
11  *
12  * Redistributions qualify as "Open Source" under one of the following terms:
13  *
14  * Redistributions are made at no charge beyond the reasonable cost of
15  * materials and delivery.
16  *
17  * Redistributions are accompanied by a copy of the Source Code or by an
18  * irrevocable offer to provide a copy of the Source Code for up to three
19  * years at the cost of materials and delivery. Such redistributions
20  * must allow further use, modification, and redistribution of the Source
21  * Code under substantially the same terms as this license.
22  *
23  * Redistributions of source code must retain the copyright notices as they
24  * appear in each source code file, these license terms, and the
25  * disclaimer/limitation of liability set forth as paragraph 6 below.
26  *
27  * Redistributions in binary form must reproduce this Copyright Notice,
28  * these license terms, and the disclaimer/limitation of liability set
29  * forth as paragraph 6 below, in the documentation and/or other materials
30  * provided with the distribution.
31  *
32  * The Software is provided on an "AS IS" basis. No warranty is
33  * provided that the Software is free of defects, or fit for a
34  * particular purpose.
35  *
36  * Limitation of Liability. Quadcap Software shall not be liable
37  * for any damages suffered by the Licensee or any third party resulting
38  * from use of the Software.
39  */

40
41 import java.io.BufferedInputStream JavaDoc;
42 import java.io.BufferedOutputStream JavaDoc;
43 import java.io.ByteArrayInputStream JavaDoc;
44 import java.io.ByteArrayOutputStream JavaDoc;
45 import java.io.Externalizable JavaDoc;
46 import java.io.InputStream JavaDoc;
47 import java.io.IOException JavaDoc;
48 import java.io.ObjectInput JavaDoc;
49 import java.io.ObjectOutput JavaDoc;
50 import java.io.OutputStream JavaDoc;
51
52 import java.util.Enumeration JavaDoc;
53 import java.util.Vector JavaDoc;
54
55 import java.sql.SQLException JavaDoc;
56
57 import com.quadcap.sql.file.ByteUtil;
58
59 import com.quadcap.util.Debug;
60 import com.quadcap.util.Util;
61
62 /**
63  * Cursor implementing the <b>ORDER BY</b> clause, uses a temporary table
64  * with the order-by columns as the key, then iterates the table's index.
65  *
66  * @author Stan Bailes
67  */

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 JavaDoc v)
74     throws SQLException JavaDoc, IOException JavaDoc
75     {
76     super(session, c.getName(), null);
77     this.session = session;
78     addColumns(session, c);
79
80     // key columns
81
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 JavaDoc name = elem.getColName();
91         Column col = c.getColumn(name);
92         if (col == null) {
93                     //#ifdef DEBUG
94
Debug.println("bad col: " + name + ", c = " + c);
95                     //#endif
96
throw new SQLException JavaDoc("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 JavaDoc, IOException JavaDoc {
114         if (bc == null) {
115             bc = tempTable.getCursor();
116         }
117     }
118
119     public long getCurrentRowId() throws SQLException JavaDoc {
120         return ByteUtil.getLong(bc.getValBuf(), 1);
121     }
122
123     public void fetchCurrentRow() throws SQLException JavaDoc, IOException JavaDoc {
124         tempTable.getRow(bc.getValBuf(), row);
125         rowValid = true;
126     }
127     
128     public void close() throws SQLException JavaDoc {
129         try {
130             super.close();
131         } finally {
132             try {
133                 if (tempTable != null) tempTable.release();
134             } catch (IOException JavaDoc ex) {
135                 throw DbException.wrapThrowable(ex);
136         } finally {
137         tempTable = null;
138         }
139     }
140     }
141 }
142
Popular Tags