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