1 24 25 package com.sqlmagic.tinysql; 26 27 import java.util.*; 28 import java.lang.*; 29 import java.io.*; 30 import java.sql.*; 31 32 41 public class tsResultSet { 42 43 private Vector rows; private Vector tsColumns; private Vector columns; private Vector tables; private tinySQLWhere whereC; 48 49 private int fetchsize; 50 private int windowStart; 51 private int level; 52 private tinySQL dbengine; 53 private Hashtable sTables; 54 private Vector groupByColumns; 55 private Vector orderByColumns; 56 private String orderType; 57 private int type; 58 private boolean eof; 59 private boolean groupedColumns = false; 60 public String newLine = System.getProperty("line.separator"); 61 62 public tsResultSet (Vector t, Vector c, tinySQLWhere w, tinySQL dbeng) 63 { 64 dbengine = dbeng; 65 windowStart = 0; 66 tables = t; 67 columns = c; 68 whereC = w; 69 rows = new Vector (); 70 tsColumns = new Vector (); 71 } 72 public void setState (int pstate, Hashtable ptables, Vector inputGroupBy, 73 Vector inputOrderBy,String inputOrderType) 74 { 75 sTables = ptables; 76 groupByColumns = inputGroupBy; 77 orderByColumns = inputOrderBy; 78 orderType = inputOrderType; 79 level = pstate; 80 } 81 public void setType (int type) 82 { 83 if ((type == ResultSet.TYPE_FORWARD_ONLY) || 84 (type == ResultSet.TYPE_SCROLL_SENSITIVE) || 85 (type == ResultSet.TYPE_SCROLL_INSENSITIVE)) 86 87 this.type = type; 88 } 89 public int getType () 90 { 91 return type; 92 } 93 public void setFetchSize (int i) 94 { 95 fetchsize = i; 96 } 97 public int getFetchSize () 98 { 99 return fetchsize; 100 } 101 public void addColumn (tsColumn col) 102 { 103 tsColumns.addElement (col); 104 if ( col.isGroupedColumn() ) groupedColumns = true; 105 } 106 public boolean isGrouped() 107 { 108 return groupedColumns; 109 } 110 public boolean getMoreResults (int newPos, int fetchsize) 111 { 112 this.fetchsize = fetchsize; 113 if (dbengine != null) 114 { 115 try 116 { 117 if (type != ResultSet.TYPE_SCROLL_INSENSITIVE) 118 { 119 rows.removeAllElements(); 120 windowStart = newPos; 121 } 122 dbengine.contSelectStatement (this); 123 if (level != 0) 124 { 125 eof = false; 126 return eof; 127 } 128 } catch (tinySQLException e) { 129 } 130 } 131 eof = true; 132 return eof; 133 } 134 public boolean addRow (tsRow row) 135 { 136 return addRow(row,"ASC"); 137 } 138 public boolean addRow (tsRow row,String orderType) 139 { 140 int i; 141 boolean sortUp=true; 142 tsRow sortRow; 143 if ( orderType != (String )null ) 144 if ( orderType.startsWith("DESC") ) sortUp = false; 145 if ( rows.size() > 0 ) 146 { 147 151 if ( sortUp ) 152 { 153 for ( i = rows.size() -1; i > -1; i-- ) 154 { 155 sortRow = (tsRow)rows.elementAt(i); 156 if ( row.compareTo(sortRow) < 0 ) continue; 157 if ( i == rows.size() - 1 ) 158 rows.addElement(row); 159 else 160 rows.insertElementAt(row,i+1); 161 return true; 162 } 163 } else { 164 for ( i = rows.size() -1; i > -1; i-- ) 165 { 166 sortRow = (tsRow)rows.elementAt(i); 167 if ( row.compareTo(sortRow) > 0 ) continue; 168 if ( i == rows.size() - 1 ) 169 rows.addElement(row); 170 else 171 rows.insertElementAt(row,i+1); 172 return true; 173 } 174 } 175 rows.insertElementAt(row,0); 176 return true; 177 } 178 rows.addElement (row); 179 if ((fetchsize > 0) && (rows.size () >= fetchsize)) return false; 180 return true; 181 } 182 185 public void updateRow(tsRow inputRow) 186 { 187 updateRow(inputRow,rows.size()-1); 188 } 189 public void updateRow(tsRow inputRow, int rowIndex) 190 { 191 rows.setElementAt(inputRow,rowIndex); 192 } 193 public Vector getTables () 194 { 195 return tables; 196 } 197 public int getLevel () 198 { 199 return level; 200 } 201 public void setLevel (int l) 202 { 203 level = l; 204 } 205 public Hashtable getTableState () 206 { 207 return sTables; 208 } 209 public Vector getGroupByColumns () 210 { 211 return groupByColumns; 212 } 213 public Vector getOrderByColumns () 214 { 215 return orderByColumns; 216 } 217 public String getOrderType() 218 { 219 return orderType; 220 } 221 public tinySQLWhere getWhereClause () 222 { 223 return whereC; 224 } 225 228 public int numcols() 229 { 230 return tsColumns.size(); 231 } 232 235 public void updateColumns(String inputColumnName,String inputColumnValue) 236 throws tinySQLException 237 { 238 int i; 239 tsColumn tcol; 240 for ( i = 0; i < tsColumns.size(); i++ ) 241 { 242 tcol = (tsColumn)tsColumns.elementAt(i); 243 tcol.update(inputColumnName,inputColumnValue); 244 } 245 } 246 249 public int size() 250 { 251 return rows.size(); 252 } 253 258 public tsRow rowAt(int row) 259 { 260 int i; 261 if (row >= (windowStart + rows.size())) 262 { 263 getMoreResults (row, fetchsize); 264 } 265 i = row - windowStart; 266 if ( i < rows.size () ) 267 { 268 return (tsRow) rows.elementAt(i); 269 } 270 return null; 271 } 272 277 public tsColumn columnAtIndex (int i) 278 { 279 return (tsColumn) tsColumns.elementAt(i); 280 } 281 284 public String toString() 285 { 286 int i; 287 tsColumn tcol; 288 StringBuffer outputBuffer; 289 292 outputBuffer = new StringBuffer (newLine + "Columns in ResultSet" 293 + newLine); 294 for ( i = 0; i < tsColumns.size(); i++ ) 295 { 296 tcol = (tsColumn)tsColumns.elementAt(i); 297 outputBuffer.append(tcol.toString()); 298 } 299 outputBuffer.append(newLine + "Rows in tsResultSet" + newLine); 300 for (i = 0; i < size(); i++) 301 { 302 tsRow row = rowAt(i); 303 outputBuffer.append(row.toString() + newLine); 304 } 305 return outputBuffer.toString(); 306 } 307 } 308 | Popular Tags |