Skip to content

Commit b6e47c3

Browse files
committed
Work in progress with ValuesMetadata.
1 parent a8d7e81 commit b6e47c3

File tree

7 files changed

+142
-32
lines changed

7 files changed

+142
-32
lines changed

src/fbjava-tests/src/main/java/example/Functions.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import org.firebirdsql.fbjava.CallableRoutineContext;
3434
import org.firebirdsql.fbjava.Context;
3535
import org.firebirdsql.fbjava.FunctionContext;
36+
import org.firebirdsql.fbjava.ValuesMetadata;
3637

3738

3839
public class Functions
@@ -266,4 +267,15 @@ public static String f28(int i1, int i2)
266267
CallableRoutineContext context = CallableRoutineContext.get();
267268
return context.getInputMetadata().getCount() + ", " + context.getOutputMetadata().getCount();
268269
}
270+
271+
public static String f29(int i1, int i2)
272+
{
273+
CallableRoutineContext context = CallableRoutineContext.get();
274+
ValuesMetadata input = context.getInputMetadata();
275+
ValuesMetadata output = context.getOutputMetadata();
276+
277+
return input.getName(0) + ": " + input.getJavaClass(0).toString() + " (" + input.getSqlType(0) + "), " +
278+
input.getName(1) + ": " + input.getJavaClass(1).toString() + " (" + input.getSqlType(1) + "), " +
279+
output.getName(0) + ": " + output.getJavaClass(0).toString() + " (" + output.getSqlType(0) + "), ";
280+
}
269281
}

src/fbjava-tests/src/main/java/example/Procedures.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,8 @@ public static void p9(int i1, Integer i2, String[] o1)
132132
ValuesMetadata input = context.getInputMetadata();
133133
ValuesMetadata output = context.getOutputMetadata();
134134

135-
o1[0] = input.getJavaClass(0).toString() + ", " + input.getJavaClass(1).toString() + ", " +
136-
output.getJavaClass(0).toString();
135+
o1[0] = input.getName(0) + ": " + input.getJavaClass(0).toString() + " (" + input.getSqlType(0) + "), " +
136+
input.getName(1) + ": " + input.getJavaClass(1).toString() + " (" + input.getSqlType(1) + "), " +
137+
output.getName(0) + ": " + output.getJavaClass(0).toString() + " (" + output.getSqlType(0) + ")";
137138
}
138139
}

src/fbjava/src/main/java/org/firebirdsql/fbjava/ValuesMetadata.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,22 @@ public interface ValuesMetadata
3131
*/
3232
public int getCount();
3333

34+
/**
35+
* Gets the name for a given value index.
36+
* Returns null in function output metadata.
37+
*/
38+
public String getName(int index);
39+
3440
/**
3541
* Gets the Java Class for a given value index.
3642
*/
3743
public Class<?> getJavaClass(int index);
3844

45+
/**
46+
* Gets the Java SQL type (java.sql.Types.*) for a given value index.
47+
* It returns the raw type (i.e. not NUMERIC or DECIMAL, but INTERGER, SMALLINT, etc).
48+
*/
49+
public int getSqlType(int index);
50+
3951
//// TODO: more methods
4052
}

src/fbjava/src/main/java/org/firebirdsql/fbjava/impl/ExternalEngine.java

Lines changed: 51 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,13 @@
3636
import java.sql.Blob;
3737
import java.sql.SQLException;
3838
import java.sql.Time;
39+
import java.sql.Types;
3940
import java.util.ArrayList;
4041
import java.util.Arrays;
4142
import java.util.Calendar;
4243
import java.util.HashMap;
4344
import java.util.Map;
45+
import java.util.Optional;
4446
import java.util.concurrent.ConcurrentHashMap;
4547
import java.util.concurrent.atomic.AtomicInteger;
4648

@@ -75,7 +77,7 @@ final class ExternalEngine implements IExternalEngineIntf
7577
{
7678
private static final IEncodingFactory encodingFactory = EncodingFactory.getPlatformDefault();
7779
private static Map<String, SharedData> sharedDataMap = new ConcurrentHashMap<>();
78-
private static Map<Integer, String> fbTypeNames;
80+
private static Map<Integer, Pair<String, Integer>> fbTypeNames;
7981
private static Map<Class<?>, DataType> dataTypesByClass;
8082
private static Map<String, Class<?>> javaClassesByName;
8183
private static Map<Integer, DataType> defaultDataTypes;
@@ -137,22 +139,22 @@ private ExternalEngine(String securityDatabase)
137139
static
138140
{
139141
fbTypeNames = new HashMap<>();
140-
fbTypeNames.put(ISCConstants.SQL_TEXT, "CHAR");
141-
fbTypeNames.put(ISCConstants.SQL_VARYING, "VARCHAR");
142-
fbTypeNames.put(ISCConstants.SQL_SHORT, "SMALLINT");
143-
fbTypeNames.put(ISCConstants.SQL_LONG, "INTEGER");
144-
fbTypeNames.put(ISCConstants.SQL_FLOAT, "FLOAT");
145-
fbTypeNames.put(ISCConstants.SQL_DOUBLE, "DOUBLE PRECISION");
146-
fbTypeNames.put(ISCConstants.SQL_D_FLOAT, "FLOAT");
147-
fbTypeNames.put(ISCConstants.SQL_TIMESTAMP, "TIMESTAMP");
148-
fbTypeNames.put(ISCConstants.SQL_BLOB, "BLOB");
149-
fbTypeNames.put(ISCConstants.SQL_ARRAY, "ARRAY");
150-
fbTypeNames.put(ISCConstants.SQL_QUAD, "QUAD");
151-
fbTypeNames.put(ISCConstants.SQL_TYPE_TIME, "TIME");
152-
fbTypeNames.put(ISCConstants.SQL_TYPE_DATE, "DATE");
153-
fbTypeNames.put(ISCConstants.SQL_INT64, "BIGINT");
154-
fbTypeNames.put(ISCConstants.SQL_BOOLEAN, "BOOLEAN");
155-
fbTypeNames.put(ISCConstants.SQL_NULL, "NULL");
142+
fbTypeNames.put(ISCConstants.SQL_TEXT, Pair.of("CHAR", Types.CHAR));
143+
fbTypeNames.put(ISCConstants.SQL_VARYING, Pair.of("VARCHAR", Types.VARCHAR));
144+
fbTypeNames.put(ISCConstants.SQL_SHORT, Pair.of("SMALLINT", Types.SMALLINT));
145+
fbTypeNames.put(ISCConstants.SQL_LONG, Pair.of("INTEGER", Types.INTEGER));
146+
fbTypeNames.put(ISCConstants.SQL_FLOAT, Pair.of("FLOAT", Types.FLOAT));
147+
fbTypeNames.put(ISCConstants.SQL_DOUBLE, Pair.of("DOUBLE PRECISION", Types.DOUBLE));
148+
fbTypeNames.put(ISCConstants.SQL_D_FLOAT, Pair.of("FLOAT", Types.FLOAT));
149+
fbTypeNames.put(ISCConstants.SQL_TIMESTAMP, Pair.of("TIMESTAMP", Types.TIMESTAMP));
150+
fbTypeNames.put(ISCConstants.SQL_BLOB, Pair.of("BLOB", Types.BLOB));
151+
fbTypeNames.put(ISCConstants.SQL_ARRAY, Pair.of("ARRAY", Types.ARRAY));
152+
fbTypeNames.put(ISCConstants.SQL_QUAD, Pair.of("QUAD", -1));
153+
fbTypeNames.put(ISCConstants.SQL_TYPE_TIME, Pair.of("TIME", Types.TIME));
154+
fbTypeNames.put(ISCConstants.SQL_TYPE_DATE, Pair.of("DATE", Types.DATE));
155+
fbTypeNames.put(ISCConstants.SQL_INT64, Pair.of("BIGINT", Types.BIGINT));
156+
fbTypeNames.put(ISCConstants.SQL_BOOLEAN, Pair.of("BOOLEAN", Types.BOOLEAN));
157+
fbTypeNames.put(ISCConstants.SQL_NULL, Pair.of("NULL", Types.NULL));
156158

157159
dataTypesByClass = new HashMap<>();
158160
javaClassesByName = new HashMap<>();
@@ -228,9 +230,9 @@ void putInMessage(IExternalContext context, Pointer message, int nullOffset, int
228230

229231
default:
230232
{
231-
String typeName = fbTypeNames.get(type);
232-
if (typeName == null)
233-
typeName = String.valueOf(type);
233+
String typeName = Optional.of(fbTypeNames.get(type))
234+
.map(Pair::getFirst)
235+
.orElse(String.valueOf(type));
234236

235237
throw new FbException(
236238
String.format("Cannot use Java String type for the Firebird type '%s'.", typeName));
@@ -297,9 +299,9 @@ Conversion setupConversion(IStatus status, Class<?> javaClass, IMessageMetadata
297299

298300
default:
299301
{
300-
String typeName = fbTypeNames.get(type);
301-
if (typeName == null)
302-
typeName = String.valueOf(type);
302+
String typeName = Optional.of(fbTypeNames.get(type))
303+
.map(Pair::getFirst)
304+
.orElse(String.valueOf(type));
303305

304306
throw new FbException(
305307
String.format("Cannot use Java byte[] type for the Firebird type '%s'.", typeName));
@@ -419,9 +421,9 @@ Conversion setupConversion(IStatus status, Class<?> javaClass, IMessageMetadata
419421

420422
if (type != ISCConstants.SQL_BLOB)
421423
{
422-
String typeName = fbTypeNames.get(type);
423-
if (typeName == null)
424-
typeName = String.valueOf(type);
424+
String typeName = Optional.of(fbTypeNames.get(type))
425+
.map(Pair::getFirst)
426+
.orElse(String.valueOf(type));
425427

426428
throw new FbException(
427429
String.format("Cannot use Java java.sql.Blob type for the Firebird type '%s'.", typeName));
@@ -938,9 +940,9 @@ Conversion setupConversion(IStatus status, Class<?> javaClass, IMessageMetadata
938940

939941
if (defaultType == null)
940942
{
941-
String typeName = fbTypeNames.get(type);
942-
if (typeName == null)
943-
typeName = String.valueOf(type);
943+
String typeName = Optional.of(fbTypeNames.get(type))
944+
.map(Pair::getFirst)
945+
.orElse(String.valueOf(type));
944946

945947
throw new FbException(
946948
String.format("Cannot use Java Object type for the Firebird type '%s'.", typeName));
@@ -1185,10 +1187,24 @@ private Routine getRoutine(IStatus status, IExternalContext context, IRoutineMet
11851187
boolean isOutput = n > inCount && n <= inCount + outCount;
11861188
Parameter parameter = getDataType(entryPoint, pos, isOutput);
11871189

1190+
IMessageMetadata inOutMetadata;
1191+
int index;
1192+
11881193
if (isOutput)
1194+
{
11891195
routine.outputParameters.add(parameter);
1196+
inOutMetadata = outMetadata;
1197+
index = n - 1 - inCount;
1198+
}
11901199
else
1200+
{
11911201
routine.inputParameters.add(parameter);
1202+
inOutMetadata = inMetadata;
1203+
index = n - 1;
1204+
}
1205+
1206+
parameter.name = inOutMetadata.getField(status, index);
1207+
parameter.type = fbTypeNames.get(inOutMetadata.getType(status, index));
11921208

11931209
paramTypes.add(parameter.javaClass);
11941210

@@ -1224,6 +1240,7 @@ else if (pos[0] != entryPoint.length())
12241240
switch (type)
12251241
{
12261242
case FUNCTION:
1243+
{
12271244
assert outCount == 1;
12281245

12291246
if (paramTypes.size() != inCount)
@@ -1243,8 +1260,12 @@ else if (pos[0] != entryPoint.length())
12431260
routine.method.getReturnType().getName()));
12441261
}
12451262

1246-
routine.outputParameters.add(new Parameter(returnType, routine.method.getReturnType()));
1263+
Parameter parameter = new Parameter(returnType, routine.method.getReturnType());
1264+
parameter.type = fbTypeNames.get(outMetadata.getType(status, 0));
1265+
1266+
routine.outputParameters.add(parameter);
12471267
break;
1268+
}
12481269

12491270
case PROCEDURE:
12501271
if (paramTypes.size() != inCount + outCount)
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* FB/Java plugin
3+
*
4+
* Distributable under LGPL license.
5+
* You may obtain a copy of the License at http://www.gnu.org/copyleft/lgpl.html
6+
*
7+
* This program is distributed in the hope that it will be useful,
8+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
9+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10+
* LGPL License for more details.
11+
*
12+
* This file was created by members of the Firebird development team.
13+
* All individual contributions remain the Copyright (C) of those
14+
* individuals. Contributors to this file are either listed here or
15+
* can be obtained from a git log command.
16+
*
17+
* All rights reserved.
18+
*/
19+
package org.firebirdsql.fbjava.impl;
20+
21+
22+
public class Pair<T1, T2>
23+
{
24+
private T1 first;
25+
private T2 second;
26+
27+
public Pair(T1 first, T2 second)
28+
{
29+
this.first = first;
30+
this.second = second;
31+
}
32+
33+
public static <T1, T2> Pair<T1, T2> of(T1 first, T2 second)
34+
{
35+
return new Pair<T1, T2>(first, second);
36+
}
37+
38+
public T1 getFirst()
39+
{
40+
return first;
41+
}
42+
43+
public T2 getSecond()
44+
{
45+
return second;
46+
}
47+
}

src/fbjava/src/main/java/org/firebirdsql/fbjava/impl/Parameter.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,14 @@ final class Parameter
2525
{
2626
this.dataType = dataType;
2727
this.javaClass = javaClass;
28+
this.type = type;
2829
}
2930

3031
DataType dataType;
3132
Class<?> javaClass;
3233
DataType.Conversion conversion;
34+
String name;
35+
Pair<String, Integer> type; // Pair of String, java.sql.Types.*
3336
int nullOffset;
3437
int offset;
3538
int length;

src/fbjava/src/main/java/org/firebirdsql/fbjava/impl/ValuesMetadataImpl.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,27 @@ public int getCount()
4040
return count;
4141
}
4242

43+
@Override
44+
public String getName(int index)
45+
{
46+
checkIndex(index);
47+
return parameters.get(index).name;
48+
}
49+
4350
@Override
4451
public Class<?> getJavaClass(int index)
4552
{
4653
checkIndex(index);
4754
return parameters.get(index).javaClass;
4855
}
4956

57+
@Override
58+
public int getSqlType(int index)
59+
{
60+
checkIndex(index);
61+
return parameters.get(index).type.getSecond();
62+
}
63+
5064
private void checkIndex(int index)
5165
{
5266
if (index < 0 || index >= count)

0 commit comments

Comments
 (0)