001    /*
002    // $Id: //open/mondrian-release/3.2/src/main/mondrian/olap4j/EmptyResultSet.java#1 $
003    // This software is subject to the terms of the Eclipse Public License v1.0
004    // Agreement, available at the following URL:
005    // http://www.eclipse.org/legal/epl-v10.html.
006    // Copyright (C) 2007-2009 Julian Hyde
007    // All Rights Reserved.
008    // You must accept the terms of that agreement to use this software.
009    */
010    package mondrian.olap4j;
011    
012    import org.olap4j.OlapWrapper;
013    
014    import javax.sql.rowset.RowSetMetaDataImpl;
015    import java.sql.*;
016    import java.sql.Date;
017    import java.math.BigDecimal;
018    import java.io.InputStream;
019    import java.io.Reader;
020    import java.util.*;
021    import java.net.URL;
022    
023    /**
024     * Implementation of {@link ResultSet} which returns 0 rows.
025     *
026     * <p>This class is used to implement {@link java.sql.DatabaseMetaData}
027     * methods for querying object types where those object types never have
028     * any instances for this particular driver.</p>
029     *
030     * <p>This class has sub-classes which implement JDBC 3.0 and JDBC 4.0 APIs;
031     * it is instantiated using {@link Factory#newEmptyResultSet}.</p>
032     *
033     * @author jhyde
034     * @version $Id: //open/mondrian-release/3.2/src/main/mondrian/olap4j/EmptyResultSet.java#1 $
035     * @since May 24, 2007
036     */
037    abstract class EmptyResultSet implements ResultSet, OlapWrapper {
038        final MondrianOlap4jConnection olap4jConnection;
039        private final List<String> headerList;
040        private final List<List<Object>> rowList;
041        private int rowOrdinal = -1;
042        private final RowSetMetaDataImpl metaData = new RowSetMetaDataImpl();
043    
044        EmptyResultSet(
045            MondrianOlap4jConnection olap4jConnection,
046            List<String> headerList,
047            List<List<Object>> rowList)
048        {
049            this.olap4jConnection = olap4jConnection;
050            this.headerList = headerList;
051            this.rowList = rowList;
052            try {
053                metaData.setColumnCount(headerList.size());
054                for (int i = 0; i < headerList.size(); i++) {
055                    metaData.setColumnName(i + 1, headerList.get(i));
056                }
057            } catch (SQLException e) {
058                throw new RuntimeException(e);
059            }
060        }
061    
062        // helper methods
063    
064        /**
065         * Returns the value of a given column
066         * @param columnOrdinal 0-based ordinal
067         * @return Value
068         */
069        private Object getColumn(int columnOrdinal) {
070            return rowList.get(rowOrdinal).get(columnOrdinal);
071        }
072    
073        private Object getColumn(String columnLabel) throws SQLException {
074            int column = headerList.indexOf(columnLabel);
075            if (column < 0) {
076                throw new SQLException("Column not found: " + columnLabel);
077            }
078            return rowList.get(rowOrdinal).get(column);
079        }
080    
081        // implement ResultSet
082    
083        public boolean next() throws SQLException {
084            // note that if rowOrdinal == rowList.size - 1, we move but then return
085            // false
086            if (rowOrdinal < rowList.size()) {
087                ++rowOrdinal;
088            }
089            return rowOrdinal < rowList.size();
090        }
091    
092        public void close() throws SQLException {
093        }
094    
095        public boolean wasNull() throws SQLException {
096            throw new UnsupportedOperationException();
097        }
098    
099        public String getString(int columnIndex) throws SQLException {
100            return String.valueOf(getColumn(columnIndex - 1));
101        }
102    
103        public boolean getBoolean(int columnIndex) throws SQLException {
104            Object o = getColumn(columnIndex - 1);
105            if (o instanceof Boolean) {
106                return (Boolean) o;
107            } else if (o instanceof String) {
108                return Boolean.valueOf((String) o);
109            } else {
110                return !o.equals(0);
111            }
112        }
113    
114        public byte getByte(int columnIndex) throws SQLException {
115            Object o = getColumn(columnIndex - 1);
116            return ((Number) o).byteValue();
117        }
118    
119        public short getShort(int columnIndex) throws SQLException {
120            Object o = getColumn(columnIndex - 1);
121            return ((Number) o).shortValue();
122        }
123    
124        public int getInt(int columnIndex) throws SQLException {
125            Object o = getColumn(columnIndex - 1);
126            return ((Number) o).intValue();
127        }
128    
129        public long getLong(int columnIndex) throws SQLException {
130            Object o = getColumn(columnIndex - 1);
131            return ((Number) o).longValue();
132        }
133    
134        public float getFloat(int columnIndex) throws SQLException {
135            Object o = getColumn(columnIndex - 1);
136            return ((Number) o).floatValue();
137        }
138    
139        public double getDouble(int columnIndex) throws SQLException {
140            Object o = getColumn(columnIndex - 1);
141            return ((Number) o).doubleValue();
142        }
143    
144        public BigDecimal getBigDecimal(
145            int columnIndex, int scale) throws SQLException
146        {
147            throw new UnsupportedOperationException();
148        }
149    
150        public byte[] getBytes(int columnIndex) throws SQLException {
151            Object o = getColumn(columnIndex - 1);
152            return (byte[]) o;
153        }
154    
155        public Date getDate(int columnIndex) throws SQLException {
156            Object o = getColumn(columnIndex - 1);
157            return (Date) o;
158        }
159    
160        public Time getTime(int columnIndex) throws SQLException {
161            Object o = getColumn(columnIndex - 1);
162            return (Time) o;
163        }
164    
165        public Timestamp getTimestamp(int columnIndex) throws SQLException {
166            Object o = getColumn(columnIndex - 1);
167            return (Timestamp) o;
168        }
169    
170        public InputStream getAsciiStream(int columnIndex) throws SQLException {
171            throw new UnsupportedOperationException();
172        }
173    
174        public InputStream getUnicodeStream(int columnIndex) throws SQLException {
175            throw new UnsupportedOperationException();
176        }
177    
178        public InputStream getBinaryStream(int columnIndex) throws SQLException {
179            throw new UnsupportedOperationException();
180        }
181    
182        public String getString(String columnLabel) throws SQLException {
183            Object o = getColumn(columnLabel);
184            return String.valueOf(o);
185        }
186    
187        public boolean getBoolean(String columnLabel) throws SQLException {
188            Object o = getColumn(columnLabel);
189            if (o instanceof Boolean) {
190                return (Boolean) o;
191            } else if (o instanceof String) {
192                return Boolean.valueOf((String) o);
193            } else {
194                return !o.equals(0);
195            }
196        }
197    
198        public byte getByte(String columnLabel) throws SQLException {
199            Object o = getColumn(columnLabel);
200            return ((Number) o).byteValue();
201        }
202    
203        public short getShort(String columnLabel) throws SQLException {
204            Object o = getColumn(columnLabel);
205            return ((Number) o).shortValue();
206        }
207    
208        public int getInt(String columnLabel) throws SQLException {
209            Object o = getColumn(columnLabel);
210            return ((Number) o).intValue();
211        }
212    
213        public long getLong(String columnLabel) throws SQLException {
214            Object o = getColumn(columnLabel);
215            return ((Number) o).longValue();
216        }
217    
218        public float getFloat(String columnLabel) throws SQLException {
219            Object o = getColumn(columnLabel);
220            return ((Number) o).floatValue();
221        }
222    
223        public double getDouble(String columnLabel) throws SQLException {
224            Object o = getColumn(columnLabel);
225            return ((Number) o).doubleValue();
226        }
227    
228        public BigDecimal getBigDecimal(
229            String columnLabel, int scale) throws SQLException
230        {
231            throw new UnsupportedOperationException();
232        }
233    
234        public byte[] getBytes(String columnLabel) throws SQLException {
235            Object o = getColumn(columnLabel);
236            return (byte[]) o;
237        }
238    
239        public Date getDate(String columnLabel) throws SQLException {
240            Object o = getColumn(columnLabel);
241            return (Date) o;
242        }
243    
244        public Time getTime(String columnLabel) throws SQLException {
245            Object o = getColumn(columnLabel);
246            return (Time) o;
247        }
248    
249        public Timestamp getTimestamp(String columnLabel) throws SQLException {
250            Object o = getColumn(columnLabel);
251            return (Timestamp) o;
252        }
253    
254        public InputStream getAsciiStream(String columnLabel) throws SQLException {
255            throw new UnsupportedOperationException();
256        }
257    
258        public InputStream getUnicodeStream(String columnLabel) throws SQLException
259        {
260            throw new UnsupportedOperationException();
261        }
262    
263        public InputStream getBinaryStream(String columnLabel) throws SQLException {
264            throw new UnsupportedOperationException();
265        }
266    
267        public SQLWarning getWarnings() throws SQLException {
268            throw new UnsupportedOperationException();
269        }
270    
271        public void clearWarnings() throws SQLException {
272            throw new UnsupportedOperationException();
273        }
274    
275        public String getCursorName() throws SQLException {
276            throw new UnsupportedOperationException();
277        }
278    
279        public ResultSetMetaData getMetaData() throws SQLException {
280            return metaData;
281        }
282    
283        public Object getObject(int columnIndex) throws SQLException {
284            throw new UnsupportedOperationException();
285        }
286    
287        public Object getObject(String columnLabel) throws SQLException {
288            throw new UnsupportedOperationException();
289        }
290    
291        public int findColumn(String columnLabel) throws SQLException {
292            throw new UnsupportedOperationException();
293        }
294    
295        public Reader getCharacterStream(int columnIndex) throws SQLException {
296            throw new UnsupportedOperationException();
297        }
298    
299        public Reader getCharacterStream(String columnLabel) throws SQLException {
300            throw new UnsupportedOperationException();
301        }
302    
303        public BigDecimal getBigDecimal(int columnIndex) throws SQLException {
304            throw new UnsupportedOperationException();
305        }
306    
307        public BigDecimal getBigDecimal(String columnLabel) throws SQLException {
308            throw new UnsupportedOperationException();
309        }
310    
311        public boolean isBeforeFirst() throws SQLException {
312            return rowOrdinal < 0;
313        }
314    
315        public boolean isAfterLast() throws SQLException {
316            return rowOrdinal >= rowList.size();
317        }
318    
319        public boolean isFirst() throws SQLException {
320            return rowOrdinal == 0;
321        }
322    
323        public boolean isLast() throws SQLException {
324            return rowOrdinal == rowList.size() - 1;
325        }
326    
327        public void beforeFirst() throws SQLException {
328            rowOrdinal = -1;
329        }
330    
331        public void afterLast() throws SQLException {
332            rowOrdinal = rowList.size();
333        }
334    
335        public boolean first() throws SQLException {
336            if (rowList.size() == 0) {
337                return false;
338            } else {
339                rowOrdinal = 0;
340                return true;
341            }
342        }
343    
344        public boolean last() throws SQLException {
345            if (rowList.size() == 0) {
346                return false;
347            } else {
348                rowOrdinal = rowList.size() - 1;
349                return true;
350            }
351        }
352    
353        public int getRow() throws SQLException {
354            return rowOrdinal + 1; // 1-based
355        }
356    
357        public boolean absolute(int row) throws SQLException {
358            int newRowOrdinal = row - 1;// convert to 0-based
359            if (newRowOrdinal >= 0 && newRowOrdinal < rowList.size()) {
360                rowOrdinal = newRowOrdinal;
361                return true;
362            } else {
363                return false;
364            }
365        }
366    
367        public boolean relative(int rows) throws SQLException {
368            int newRowOrdinal = rowOrdinal + (rows - 1);
369            if (newRowOrdinal >= 0 && newRowOrdinal < rowList.size()) {
370                rowOrdinal = newRowOrdinal;
371                return true;
372            } else {
373                return false;
374            }
375        }
376    
377        public boolean previous() throws SQLException {
378            // converse of next(); note that if rowOrdinal == 0, we decrement
379            // but return false
380            if (rowOrdinal >= 0) {
381                --rowOrdinal;
382            }
383            return rowOrdinal >= 0;
384        }
385    
386        public void setFetchDirection(int direction) throws SQLException {
387            throw new UnsupportedOperationException();
388        }
389    
390        public int getFetchDirection() throws SQLException {
391            throw new UnsupportedOperationException();
392        }
393    
394        public void setFetchSize(int rows) throws SQLException {
395            throw new UnsupportedOperationException();
396        }
397    
398        public int getFetchSize() throws SQLException {
399            throw new UnsupportedOperationException();
400        }
401    
402        public int getType() throws SQLException {
403            throw new UnsupportedOperationException();
404        }
405    
406        public int getConcurrency() throws SQLException {
407            throw new UnsupportedOperationException();
408        }
409    
410        public boolean rowUpdated() throws SQLException {
411            throw new UnsupportedOperationException();
412        }
413    
414        public boolean rowInserted() throws SQLException {
415            throw new UnsupportedOperationException();
416        }
417    
418        public boolean rowDeleted() throws SQLException {
419            throw new UnsupportedOperationException();
420        }
421    
422        public void updateNull(int columnIndex) throws SQLException {
423            throw new UnsupportedOperationException();
424        }
425    
426        public void updateBoolean(int columnIndex, boolean x) throws SQLException {
427            throw new UnsupportedOperationException();
428        }
429    
430        public void updateByte(int columnIndex, byte x) throws SQLException {
431            throw new UnsupportedOperationException();
432        }
433    
434        public void updateShort(int columnIndex, short x) throws SQLException {
435            throw new UnsupportedOperationException();
436        }
437    
438        public void updateInt(int columnIndex, int x) throws SQLException {
439            throw new UnsupportedOperationException();
440        }
441    
442        public void updateLong(int columnIndex, long x) throws SQLException {
443            throw new UnsupportedOperationException();
444        }
445    
446        public void updateFloat(int columnIndex, float x) throws SQLException {
447            throw new UnsupportedOperationException();
448        }
449    
450        public void updateDouble(int columnIndex, double x) throws SQLException {
451            throw new UnsupportedOperationException();
452        }
453    
454        public void updateBigDecimal(
455            int columnIndex, BigDecimal x) throws SQLException
456        {
457            throw new UnsupportedOperationException();
458        }
459    
460        public void updateString(int columnIndex, String x) throws SQLException {
461            throw new UnsupportedOperationException();
462        }
463    
464        public void updateBytes(int columnIndex, byte x[]) throws SQLException {
465            throw new UnsupportedOperationException();
466        }
467    
468        public void updateDate(int columnIndex, Date x) throws SQLException {
469            throw new UnsupportedOperationException();
470        }
471    
472        public void updateTime(int columnIndex, Time x) throws SQLException {
473            throw new UnsupportedOperationException();
474        }
475    
476        public void updateTimestamp(
477            int columnIndex, Timestamp x) throws SQLException
478        {
479            throw new UnsupportedOperationException();
480        }
481    
482        public void updateAsciiStream(
483            int columnIndex, InputStream x, int length) throws SQLException
484        {
485            throw new UnsupportedOperationException();
486        }
487    
488        public void updateBinaryStream(
489            int columnIndex, InputStream x, int length) throws SQLException
490        {
491            throw new UnsupportedOperationException();
492        }
493    
494        public void updateCharacterStream(
495            int columnIndex, Reader x, int length) throws SQLException
496        {
497            throw new UnsupportedOperationException();
498        }
499    
500        public void updateObject(
501            int columnIndex, Object x, int scaleOrLength) throws SQLException
502        {
503            throw new UnsupportedOperationException();
504        }
505    
506        public void updateObject(int columnIndex, Object x) throws SQLException {
507            throw new UnsupportedOperationException();
508        }
509    
510        public void updateNull(String columnLabel) throws SQLException {
511            throw new UnsupportedOperationException();
512        }
513    
514        public void updateBoolean(
515            String columnLabel, boolean x) throws SQLException
516        {
517            throw new UnsupportedOperationException();
518        }
519    
520        public void updateByte(String columnLabel, byte x) throws SQLException {
521            throw new UnsupportedOperationException();
522        }
523    
524        public void updateShort(String columnLabel, short x) throws SQLException {
525            throw new UnsupportedOperationException();
526        }
527    
528        public void updateInt(String columnLabel, int x) throws SQLException {
529            throw new UnsupportedOperationException();
530        }
531    
532        public void updateLong(String columnLabel, long x) throws SQLException {
533            throw new UnsupportedOperationException();
534        }
535    
536        public void updateFloat(String columnLabel, float x) throws SQLException {
537            throw new UnsupportedOperationException();
538        }
539    
540        public void updateDouble(String columnLabel, double x) throws SQLException {
541            throw new UnsupportedOperationException();
542        }
543    
544        public void updateBigDecimal(
545            String columnLabel, BigDecimal x) throws SQLException
546        {
547            throw new UnsupportedOperationException();
548        }
549    
550        public void updateString(String columnLabel, String x) throws SQLException {
551            throw new UnsupportedOperationException();
552        }
553    
554        public void updateBytes(String columnLabel, byte x[]) throws SQLException {
555            throw new UnsupportedOperationException();
556        }
557    
558        public void updateDate(String columnLabel, Date x) throws SQLException {
559            throw new UnsupportedOperationException();
560        }
561    
562        public void updateTime(String columnLabel, Time x) throws SQLException {
563            throw new UnsupportedOperationException();
564        }
565    
566        public void updateTimestamp(
567            String columnLabel, Timestamp x) throws SQLException
568        {
569            throw new UnsupportedOperationException();
570        }
571    
572        public void updateAsciiStream(
573            String columnLabel, InputStream x, int length) throws SQLException
574        {
575            throw new UnsupportedOperationException();
576        }
577    
578        public void updateBinaryStream(
579            String columnLabel, InputStream x, int length) throws SQLException
580        {
581            throw new UnsupportedOperationException();
582        }
583    
584        public void updateCharacterStream(
585            String columnLabel, Reader reader, int length) throws SQLException
586        {
587            throw new UnsupportedOperationException();
588        }
589    
590        public void updateObject(
591            String columnLabel, Object x, int scaleOrLength) throws SQLException
592        {
593            throw new UnsupportedOperationException();
594        }
595    
596        public void updateObject(String columnLabel, Object x) throws SQLException {
597            throw new UnsupportedOperationException();
598        }
599    
600        public void insertRow() throws SQLException {
601            throw new UnsupportedOperationException();
602        }
603    
604        public void updateRow() throws SQLException {
605            throw new UnsupportedOperationException();
606        }
607    
608        public void deleteRow() throws SQLException {
609            throw new UnsupportedOperationException();
610        }
611    
612        public void refreshRow() throws SQLException {
613            throw new UnsupportedOperationException();
614        }
615    
616        public void cancelRowUpdates() throws SQLException {
617            throw new UnsupportedOperationException();
618        }
619    
620        public void moveToInsertRow() throws SQLException {
621            throw new UnsupportedOperationException();
622        }
623    
624        public void moveToCurrentRow() throws SQLException {
625            throw new UnsupportedOperationException();
626        }
627    
628        public Statement getStatement() throws SQLException {
629            throw new UnsupportedOperationException();
630        }
631    
632        public Object getObject(
633            int columnIndex, Map<String, Class<?>> map) throws SQLException
634        {
635            throw new UnsupportedOperationException();
636        }
637    
638        public Ref getRef(int columnIndex) throws SQLException {
639            throw new UnsupportedOperationException();
640        }
641    
642        public Blob getBlob(int columnIndex) throws SQLException {
643            throw new UnsupportedOperationException();
644        }
645    
646        public Clob getClob(int columnIndex) throws SQLException {
647            throw new UnsupportedOperationException();
648        }
649    
650        public Array getArray(int columnIndex) throws SQLException {
651            throw new UnsupportedOperationException();
652        }
653    
654        public Object getObject(
655            String columnLabel, Map<String, Class<?>> map) throws SQLException
656        {
657            throw new UnsupportedOperationException();
658        }
659    
660        public Ref getRef(String columnLabel) throws SQLException {
661            throw new UnsupportedOperationException();
662        }
663    
664        public Blob getBlob(String columnLabel) throws SQLException {
665            throw new UnsupportedOperationException();
666        }
667    
668        public Clob getClob(String columnLabel) throws SQLException {
669            throw new UnsupportedOperationException();
670        }
671    
672        public Array getArray(String columnLabel) throws SQLException {
673            throw new UnsupportedOperationException();
674        }
675    
676        public Date getDate(int columnIndex, Calendar cal) throws SQLException {
677            throw new UnsupportedOperationException();
678        }
679    
680        public Date getDate(String columnLabel, Calendar cal) throws SQLException {
681            throw new UnsupportedOperationException();
682        }
683    
684        public Time getTime(int columnIndex, Calendar cal) throws SQLException {
685            throw new UnsupportedOperationException();
686        }
687    
688        public Time getTime(String columnLabel, Calendar cal) throws SQLException {
689            throw new UnsupportedOperationException();
690        }
691    
692        public Timestamp getTimestamp(
693            int columnIndex, Calendar cal) throws SQLException
694        {
695            throw new UnsupportedOperationException();
696        }
697    
698        public Timestamp getTimestamp(
699            String columnLabel, Calendar cal) throws SQLException
700        {
701            throw new UnsupportedOperationException();
702        }
703    
704        public URL getURL(int columnIndex) throws SQLException {
705            throw new UnsupportedOperationException();
706        }
707    
708        public URL getURL(String columnLabel) throws SQLException {
709            throw new UnsupportedOperationException();
710        }
711    
712        public void updateRef(int columnIndex, Ref x) throws SQLException {
713            throw new UnsupportedOperationException();
714        }
715    
716        public void updateRef(String columnLabel, Ref x) throws SQLException {
717            throw new UnsupportedOperationException();
718        }
719    
720        public void updateBlob(int columnIndex, Blob x) throws SQLException {
721            throw new UnsupportedOperationException();
722        }
723    
724        public void updateBlob(String columnLabel, Blob x) throws SQLException {
725            throw new UnsupportedOperationException();
726        }
727    
728        public void updateClob(int columnIndex, Clob x) throws SQLException {
729            throw new UnsupportedOperationException();
730        }
731    
732        public void updateClob(String columnLabel, Clob x) throws SQLException {
733            throw new UnsupportedOperationException();
734        }
735    
736        public void updateArray(int columnIndex, Array x) throws SQLException {
737            throw new UnsupportedOperationException();
738        }
739    
740        public void updateArray(String columnLabel, Array x) throws SQLException {
741            throw new UnsupportedOperationException();
742        }
743    
744        // implement Wrapper
745    
746        public <T> T unwrap(Class<T> iface) throws SQLException {
747            if (iface.isInstance(this)) {
748                return iface.cast(this);
749            }
750            throw olap4jConnection.helper.createException("cannot cast");
751        }
752    
753        public boolean isWrapperFor(Class<?> iface) throws SQLException {
754            return iface.isInstance(this);
755        }
756    }
757    
758    // End EmptyResultSet.java