001    /*
002    // $Id: //open/mondrian/src/main/mondrian/olap4j/EmptyResultSet.java#1 $
003    // This software is subject to the terms of the Common Public License
004    // Agreement, available at the following URL:
005    // http://www.opensource.org/licenses/cpl.html.
006    // Copyright (C) 2007-2007 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/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            throw new UnsupportedOperationException();
147        }
148    
149        public byte[] getBytes(int columnIndex) throws SQLException {
150            Object o = getColumn(columnIndex - 1);
151            return (byte[]) o;
152        }
153    
154        public Date getDate(int columnIndex) throws SQLException {
155            Object o = getColumn(columnIndex - 1);
156            return (Date) o;
157        }
158    
159        public Time getTime(int columnIndex) throws SQLException {
160            Object o = getColumn(columnIndex - 1);
161            return (Time) o;
162        }
163    
164        public Timestamp getTimestamp(int columnIndex) throws SQLException {
165            Object o = getColumn(columnIndex - 1);
166            return (Timestamp) o;
167        }
168    
169        public InputStream getAsciiStream(int columnIndex) throws SQLException {
170            throw new UnsupportedOperationException();
171        }
172    
173        public InputStream getUnicodeStream(int columnIndex) throws SQLException {
174            throw new UnsupportedOperationException();
175        }
176    
177        public InputStream getBinaryStream(int columnIndex) throws SQLException {
178            throw new UnsupportedOperationException();
179        }
180    
181        public String getString(String columnLabel) throws SQLException {
182            Object o = getColumn(columnLabel);
183            return String.valueOf(o);
184        }
185    
186        public boolean getBoolean(String columnLabel) throws SQLException {
187            Object o = getColumn(columnLabel);
188            if (o instanceof Boolean) {
189                return (Boolean) o;
190            } else if (o instanceof String) {
191                return Boolean.valueOf((String) o);
192            } else {
193                return !o.equals(0);
194            }
195        }
196    
197        public byte getByte(String columnLabel) throws SQLException {
198            Object o = getColumn(columnLabel);
199            return ((Number) o).byteValue();
200        }
201    
202        public short getShort(String columnLabel) throws SQLException {
203            Object o = getColumn(columnLabel);
204            return ((Number) o).shortValue();
205        }
206    
207        public int getInt(String columnLabel) throws SQLException {
208            Object o = getColumn(columnLabel);
209            return ((Number) o).intValue();
210        }
211    
212        public long getLong(String columnLabel) throws SQLException {
213            Object o = getColumn(columnLabel);
214            return ((Number) o).longValue();
215        }
216    
217        public float getFloat(String columnLabel) throws SQLException {
218            Object o = getColumn(columnLabel);
219            return ((Number) o).floatValue();
220        }
221    
222        public double getDouble(String columnLabel) throws SQLException {
223            Object o = getColumn(columnLabel);
224            return ((Number) o).doubleValue();
225        }
226    
227        public BigDecimal getBigDecimal(
228            String columnLabel, int scale) throws SQLException {
229            throw new UnsupportedOperationException();
230        }
231    
232        public byte[] getBytes(String columnLabel) throws SQLException {
233            Object o = getColumn(columnLabel);
234            return (byte[]) o;
235        }
236    
237        public Date getDate(String columnLabel) throws SQLException {
238            Object o = getColumn(columnLabel);
239            return (Date) o;
240        }
241    
242        public Time getTime(String columnLabel) throws SQLException {
243            Object o = getColumn(columnLabel);
244            return (Time) o;
245        }
246    
247        public Timestamp getTimestamp(String columnLabel) throws SQLException {
248            Object o = getColumn(columnLabel);
249            return (Timestamp) o;
250        }
251    
252        public InputStream getAsciiStream(String columnLabel) throws SQLException {
253            throw new UnsupportedOperationException();
254        }
255    
256        public InputStream getUnicodeStream(String columnLabel) throws SQLException {
257            throw new UnsupportedOperationException();
258        }
259    
260        public InputStream getBinaryStream(String columnLabel) throws SQLException {
261            throw new UnsupportedOperationException();
262        }
263    
264        public SQLWarning getWarnings() throws SQLException {
265            throw new UnsupportedOperationException();
266        }
267    
268        public void clearWarnings() throws SQLException {
269            throw new UnsupportedOperationException();
270        }
271    
272        public String getCursorName() throws SQLException {
273            throw new UnsupportedOperationException();
274        }
275    
276        public ResultSetMetaData getMetaData() throws SQLException {
277            return metaData;
278        }
279    
280        public Object getObject(int columnIndex) throws SQLException {
281            throw new UnsupportedOperationException();
282        }
283    
284        public Object getObject(String columnLabel) throws SQLException {
285            throw new UnsupportedOperationException();
286        }
287    
288        public int findColumn(String columnLabel) throws SQLException {
289            throw new UnsupportedOperationException();
290        }
291    
292        public Reader getCharacterStream(int columnIndex) throws SQLException {
293            throw new UnsupportedOperationException();
294        }
295    
296        public Reader getCharacterStream(String columnLabel) throws SQLException {
297            throw new UnsupportedOperationException();
298        }
299    
300        public BigDecimal getBigDecimal(int columnIndex) throws SQLException {
301            throw new UnsupportedOperationException();
302        }
303    
304        public BigDecimal getBigDecimal(String columnLabel) throws SQLException {
305            throw new UnsupportedOperationException();
306        }
307    
308        public boolean isBeforeFirst() throws SQLException {
309            return rowOrdinal < 0;
310        }
311    
312        public boolean isAfterLast() throws SQLException {
313            return rowOrdinal >= rowList.size();
314        }
315    
316        public boolean isFirst() throws SQLException {
317            return rowOrdinal == 0;
318        }
319    
320        public boolean isLast() throws SQLException {
321            return rowOrdinal == rowList.size() - 1;
322        }
323    
324        public void beforeFirst() throws SQLException {
325            rowOrdinal = -1;
326        }
327    
328        public void afterLast() throws SQLException {
329            rowOrdinal = rowList.size();
330        }
331    
332        public boolean first() throws SQLException {
333            if (rowList.size() == 0) {
334                return false;
335            } else {
336                rowOrdinal = 0;
337                return true;
338            }
339        }
340    
341        public boolean last() throws SQLException {
342            if (rowList.size() == 0) {
343                return false;
344            } else {
345                rowOrdinal = rowList.size() - 1;
346                return true;
347            }
348        }
349    
350        public int getRow() throws SQLException {
351            return rowOrdinal + 1; // 1-based
352        }
353    
354        public boolean absolute(int row) throws SQLException {
355            int newRowOrdinal = row - 1;// convert to 0-based
356            if (newRowOrdinal >= 0 && newRowOrdinal < rowList.size()) {
357                rowOrdinal = newRowOrdinal;
358                return true;
359            } else {
360                return false;
361            }
362        }
363    
364        public boolean relative(int rows) throws SQLException {
365            int newRowOrdinal = rowOrdinal + (rows - 1);
366            if (newRowOrdinal >= 0 && newRowOrdinal < rowList.size()) {
367                rowOrdinal = newRowOrdinal;
368                return true;
369            } else {
370                return false;
371            }
372        }
373    
374        public boolean previous() throws SQLException {
375            // converse of next(); note that if rowOrdinal == 0, we decrement
376            // but return false
377            if (rowOrdinal >= 0) {
378                --rowOrdinal;
379            }
380            return rowOrdinal >= 0;
381        }
382    
383        public void setFetchDirection(int direction) throws SQLException {
384            throw new UnsupportedOperationException();
385        }
386    
387        public int getFetchDirection() throws SQLException {
388            throw new UnsupportedOperationException();
389        }
390    
391        public void setFetchSize(int rows) throws SQLException {
392            throw new UnsupportedOperationException();
393        }
394    
395        public int getFetchSize() throws SQLException {
396            throw new UnsupportedOperationException();
397        }
398    
399        public int getType() throws SQLException {
400            throw new UnsupportedOperationException();
401        }
402    
403        public int getConcurrency() throws SQLException {
404            throw new UnsupportedOperationException();
405        }
406    
407        public boolean rowUpdated() throws SQLException {
408            throw new UnsupportedOperationException();
409        }
410    
411        public boolean rowInserted() throws SQLException {
412            throw new UnsupportedOperationException();
413        }
414    
415        public boolean rowDeleted() throws SQLException {
416            throw new UnsupportedOperationException();
417        }
418    
419        public void updateNull(int columnIndex) throws SQLException {
420            throw new UnsupportedOperationException();
421        }
422    
423        public void updateBoolean(int columnIndex, boolean x) throws SQLException {
424            throw new UnsupportedOperationException();
425        }
426    
427        public void updateByte(int columnIndex, byte x) throws SQLException {
428            throw new UnsupportedOperationException();
429        }
430    
431        public void updateShort(int columnIndex, short x) throws SQLException {
432            throw new UnsupportedOperationException();
433        }
434    
435        public void updateInt(int columnIndex, int x) throws SQLException {
436            throw new UnsupportedOperationException();
437        }
438    
439        public void updateLong(int columnIndex, long x) throws SQLException {
440            throw new UnsupportedOperationException();
441        }
442    
443        public void updateFloat(int columnIndex, float x) throws SQLException {
444            throw new UnsupportedOperationException();
445        }
446    
447        public void updateDouble(int columnIndex, double x) throws SQLException {
448            throw new UnsupportedOperationException();
449        }
450    
451        public void updateBigDecimal(
452            int columnIndex, BigDecimal x) throws SQLException {
453            throw new UnsupportedOperationException();
454        }
455    
456        public void updateString(int columnIndex, String x) throws SQLException {
457            throw new UnsupportedOperationException();
458        }
459    
460        public void updateBytes(int columnIndex, byte x[]) throws SQLException {
461            throw new UnsupportedOperationException();
462        }
463    
464        public void updateDate(int columnIndex, Date x) throws SQLException {
465            throw new UnsupportedOperationException();
466        }
467    
468        public void updateTime(int columnIndex, Time x) throws SQLException {
469            throw new UnsupportedOperationException();
470        }
471    
472        public void updateTimestamp(
473            int columnIndex, Timestamp x) throws SQLException {
474            throw new UnsupportedOperationException();
475        }
476    
477        public void updateAsciiStream(
478            int columnIndex, InputStream x, int length) throws SQLException {
479            throw new UnsupportedOperationException();
480        }
481    
482        public void updateBinaryStream(
483            int columnIndex, InputStream x, int length) throws SQLException {
484            throw new UnsupportedOperationException();
485        }
486    
487        public void updateCharacterStream(
488            int columnIndex, Reader x, int length) throws SQLException {
489            throw new UnsupportedOperationException();
490        }
491    
492        public void updateObject(
493            int columnIndex, Object x, int scaleOrLength) throws SQLException {
494            throw new UnsupportedOperationException();
495        }
496    
497        public void updateObject(int columnIndex, Object x) throws SQLException {
498            throw new UnsupportedOperationException();
499        }
500    
501        public void updateNull(String columnLabel) throws SQLException {
502            throw new UnsupportedOperationException();
503        }
504    
505        public void updateBoolean(
506            String columnLabel, boolean x) throws SQLException {
507            throw new UnsupportedOperationException();
508        }
509    
510        public void updateByte(String columnLabel, byte x) throws SQLException {
511            throw new UnsupportedOperationException();
512        }
513    
514        public void updateShort(String columnLabel, short x) throws SQLException {
515            throw new UnsupportedOperationException();
516        }
517    
518        public void updateInt(String columnLabel, int x) throws SQLException {
519            throw new UnsupportedOperationException();
520        }
521    
522        public void updateLong(String columnLabel, long x) throws SQLException {
523            throw new UnsupportedOperationException();
524        }
525    
526        public void updateFloat(String columnLabel, float x) throws SQLException {
527            throw new UnsupportedOperationException();
528        }
529    
530        public void updateDouble(String columnLabel, double x) throws SQLException {
531            throw new UnsupportedOperationException();
532        }
533    
534        public void updateBigDecimal(
535            String columnLabel, BigDecimal x) throws SQLException {
536            throw new UnsupportedOperationException();
537        }
538    
539        public void updateString(String columnLabel, String x) throws SQLException {
540            throw new UnsupportedOperationException();
541        }
542    
543        public void updateBytes(String columnLabel, byte x[]) throws SQLException {
544            throw new UnsupportedOperationException();
545        }
546    
547        public void updateDate(String columnLabel, Date x) throws SQLException {
548            throw new UnsupportedOperationException();
549        }
550    
551        public void updateTime(String columnLabel, Time x) throws SQLException {
552            throw new UnsupportedOperationException();
553        }
554    
555        public void updateTimestamp(
556            String columnLabel, Timestamp x) throws SQLException {
557            throw new UnsupportedOperationException();
558        }
559    
560        public void updateAsciiStream(
561            String columnLabel, InputStream x, int length) throws SQLException {
562            throw new UnsupportedOperationException();
563        }
564    
565        public void updateBinaryStream(
566            String columnLabel, InputStream x, int length) throws SQLException {
567            throw new UnsupportedOperationException();
568        }
569    
570        public void updateCharacterStream(
571            String columnLabel, Reader reader, int length) throws SQLException {
572            throw new UnsupportedOperationException();
573        }
574    
575        public void updateObject(
576            String columnLabel, Object x, int scaleOrLength) throws SQLException {
577            throw new UnsupportedOperationException();
578        }
579    
580        public void updateObject(String columnLabel, Object x) throws SQLException {
581            throw new UnsupportedOperationException();
582        }
583    
584        public void insertRow() throws SQLException {
585            throw new UnsupportedOperationException();
586        }
587    
588        public void updateRow() throws SQLException {
589            throw new UnsupportedOperationException();
590        }
591    
592        public void deleteRow() throws SQLException {
593            throw new UnsupportedOperationException();
594        }
595    
596        public void refreshRow() throws SQLException {
597            throw new UnsupportedOperationException();
598        }
599    
600        public void cancelRowUpdates() throws SQLException {
601            throw new UnsupportedOperationException();
602        }
603    
604        public void moveToInsertRow() throws SQLException {
605            throw new UnsupportedOperationException();
606        }
607    
608        public void moveToCurrentRow() throws SQLException {
609            throw new UnsupportedOperationException();
610        }
611    
612        public Statement getStatement() throws SQLException {
613            throw new UnsupportedOperationException();
614        }
615    
616        public Object getObject(
617            int columnIndex, Map<String, Class<?>> map) throws SQLException {
618            throw new UnsupportedOperationException();
619        }
620    
621        public Ref getRef(int columnIndex) throws SQLException {
622            throw new UnsupportedOperationException();
623        }
624    
625        public Blob getBlob(int columnIndex) throws SQLException {
626            throw new UnsupportedOperationException();
627        }
628    
629        public Clob getClob(int columnIndex) throws SQLException {
630            throw new UnsupportedOperationException();
631        }
632    
633        public Array getArray(int columnIndex) throws SQLException {
634            throw new UnsupportedOperationException();
635        }
636    
637        public Object getObject(
638            String columnLabel, Map<String, Class<?>> map) throws SQLException {
639            throw new UnsupportedOperationException();
640        }
641    
642        public Ref getRef(String columnLabel) throws SQLException {
643            throw new UnsupportedOperationException();
644        }
645    
646        public Blob getBlob(String columnLabel) throws SQLException {
647            throw new UnsupportedOperationException();
648        }
649    
650        public Clob getClob(String columnLabel) throws SQLException {
651            throw new UnsupportedOperationException();
652        }
653    
654        public Array getArray(String columnLabel) throws SQLException {
655            throw new UnsupportedOperationException();
656        }
657    
658        public Date getDate(int columnIndex, Calendar cal) throws SQLException {
659            throw new UnsupportedOperationException();
660        }
661    
662        public Date getDate(String columnLabel, Calendar cal) throws SQLException {
663            throw new UnsupportedOperationException();
664        }
665    
666        public Time getTime(int columnIndex, Calendar cal) throws SQLException {
667            throw new UnsupportedOperationException();
668        }
669    
670        public Time getTime(String columnLabel, Calendar cal) throws SQLException {
671            throw new UnsupportedOperationException();
672        }
673    
674        public Timestamp getTimestamp(
675            int columnIndex, Calendar cal) throws SQLException {
676            throw new UnsupportedOperationException();
677        }
678    
679        public Timestamp getTimestamp(
680            String columnLabel, Calendar cal) throws SQLException {
681            throw new UnsupportedOperationException();
682        }
683    
684        public URL getURL(int columnIndex) throws SQLException {
685            throw new UnsupportedOperationException();
686        }
687    
688        public URL getURL(String columnLabel) throws SQLException {
689            throw new UnsupportedOperationException();
690        }
691    
692        public void updateRef(int columnIndex, Ref x) throws SQLException {
693            throw new UnsupportedOperationException();
694        }
695    
696        public void updateRef(String columnLabel, Ref x) throws SQLException {
697            throw new UnsupportedOperationException();
698        }
699    
700        public void updateBlob(int columnIndex, Blob x) throws SQLException {
701            throw new UnsupportedOperationException();
702        }
703    
704        public void updateBlob(String columnLabel, Blob x) throws SQLException {
705            throw new UnsupportedOperationException();
706        }
707    
708        public void updateClob(int columnIndex, Clob x) throws SQLException {
709            throw new UnsupportedOperationException();
710        }
711    
712        public void updateClob(String columnLabel, Clob x) throws SQLException {
713            throw new UnsupportedOperationException();
714        }
715    
716        public void updateArray(int columnIndex, Array x) throws SQLException {
717            throw new UnsupportedOperationException();
718        }
719    
720        public void updateArray(String columnLabel, Array x) throws SQLException {
721            throw new UnsupportedOperationException();
722        }
723    
724        // implement Wrapper
725    
726        public <T> T unwrap(Class<T> iface) throws SQLException {
727            if (iface.isInstance(this)) {
728                return iface.cast(this);
729            }
730            throw olap4jConnection.helper.createException("cannot cast");
731        }
732    
733        public boolean isWrapperFor(Class<?> iface) throws SQLException {
734            return iface.isInstance(this);
735        }
736    }
737    
738    // End EmptyResultSet.java