Skip to contentMethod: isBound(int)
1: /**
2: * Copyright (C) 2016 Czech Technical University in Prague
3: * <p>
4: * This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public
5: * License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later
6: * version.
7: * <p>
8: * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
9: * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
10: * details. You should have received a copy of the GNU General Public License along with this program. If not, see
11: * <http://www.gnu.org/licenses/>.
12: */
13: package cz.cvut.kbss.ontodriver.sesame.query;
14:
15: import cz.cvut.kbss.ontodriver.Statement;
16: import cz.cvut.kbss.ontodriver.exception.OntoDriverException;
17: import cz.cvut.kbss.ontodriver.sesame.exceptions.SesameDriverException;
18:
19: public class AskResultSet extends AbstractResultSet {
20:
21: private final boolean result;
22:
23: public AskResultSet(boolean result, Statement statement) {
24: super(statement);
25: this.result = result;
26: }
27:
28: @Override
29: public int findColumn(String columnLabel) {
30: return 0;
31: }
32:
33: @Override
34: public int getColumnCount() {
35: return 1;
36: }
37:
38: @Override
39: public boolean isBound(int variableIndex) {
40: // Return always true, as we do not care about index/name for ASK results
41: return true;
42: }
43:
44: @Override
45: public boolean isBound(String variableName) {
46: // Return always true, as we do not care about index/name for ASK results
47: return true;
48: }
49:
50: // We discard column index and column name, because in a boolean result, there is no such concept. Therefore,
51: // the result is returned for any column index and column name.
52:
53: @Override
54: public boolean getBoolean(int columnIndex) throws OntoDriverException {
55: ensureState();
56: return result;
57: }
58:
59: private void ensureState() throws OntoDriverException {
60: ensureOpen();
61: if (!isFirst()) {
62: throw new IllegalStateException("Must call next before getting the first value.");
63: }
64: }
65:
66: @Override
67: public boolean getBoolean(String columnLabel) throws OntoDriverException {
68: ensureState();
69: return result;
70: }
71:
72: @Override
73: public byte getByte(int columnIndex) throws OntoDriverException {
74: ensureState();
75: throw unsupported("byte");
76: }
77:
78: private static UnsupportedOperationException unsupported(String type) {
79: return new UnsupportedOperationException("ASK query results cannot return " + type + "values.");
80: }
81:
82: @Override
83: public byte getByte(String columnLabel) throws OntoDriverException {
84: ensureState();
85: throw unsupported("byte");
86: }
87:
88: @Override
89: public double getDouble(int columnIndex) throws OntoDriverException {
90: ensureState();
91: throw unsupported("double");
92: }
93:
94: @Override
95: public double getDouble(String columnLabel) throws OntoDriverException {
96: ensureState();
97: throw unsupported("double");
98: }
99:
100: @Override
101: public float getFloat(int columnIndex) throws OntoDriverException {
102: ensureState();
103: throw unsupported("float");
104: }
105:
106: @Override
107: public float getFloat(String columnLabel) throws OntoDriverException {
108: ensureState();
109: throw unsupported("float");
110: }
111:
112: @Override
113: public int getInt(int columnIndex) throws OntoDriverException {
114: ensureState();
115: throw unsupported("int");
116: }
117:
118: @Override
119: public int getInt(String columnLabel) throws OntoDriverException {
120: ensureState();
121: throw unsupported("int");
122: }
123:
124: @Override
125: public long getLong(int columnIndex) throws OntoDriverException {
126: ensureState();
127: throw unsupported("long");
128: }
129:
130: @Override
131: public long getLong(String columnLabel) throws OntoDriverException {
132: ensureState();
133: throw unsupported("long");
134: }
135:
136: @Override
137: public Object getObject(int columnIndex) throws OntoDriverException {
138: ensureState();
139: return result;
140: }
141:
142: @Override
143: public Object getObject(String columnLabel) throws OntoDriverException {
144: ensureState();
145: return result;
146: }
147:
148: @Override
149: public <T> T getObject(int columnIndex, Class<T> cls) throws OntoDriverException {
150: ensureState();
151: return toType(cls);
152: }
153:
154: @Override
155: public <T> T getObject(String columnLabel, Class<T> cls) throws OntoDriverException {
156: ensureState();
157: return toType(cls);
158: }
159:
160: private <T> T toType(Class<T> type) throws OntoDriverException {
161: if (type.isAssignableFrom(Boolean.class)) {
162: return type.cast(result);
163: }
164: if (type.isAssignableFrom(String.class)) {
165: return type.cast(getString(0));
166: }
167: throw new SesameDriverException("Unable to return boolean result as type " + type);
168: }
169:
170: @Override
171: public short getShort(int columnIndex) throws OntoDriverException {
172: ensureState();
173: throw unsupported("short");
174: }
175:
176: @Override
177: public short getShort(String columnLabel) throws OntoDriverException {
178: ensureState();
179: throw unsupported("short");
180: }
181:
182: @Override
183: public String getString(int columnIndex) throws OntoDriverException {
184: ensureState();
185: return Boolean.toString(result);
186: }
187:
188: @Override
189: public String getString(String columnLabel) throws OntoDriverException {
190: ensureState();
191: return Boolean.toString(result);
192: }
193:
194: @Override
195: public boolean hasNext() throws OntoDriverException {
196: ensureOpen();
197: return !isFirst();
198: }
199: }