Skip to content

Commit a8d7e81

Browse files
committed
Work in progress with functions/procedures input/output metadata.
1 parent 6e0e478 commit a8d7e81

File tree

8 files changed

+166
-11
lines changed

8 files changed

+166
-11
lines changed

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import java.sql.SQLException;
3131
import java.sql.Statement;
3232

33+
import org.firebirdsql.fbjava.CallableRoutineContext;
3334
import org.firebirdsql.fbjava.Context;
3435
import org.firebirdsql.fbjava.FunctionContext;
3536

@@ -259,4 +260,10 @@ public static String f27()
259260
Context context = Context.get();
260261
return context.getBody();
261262
}
263+
264+
public static String f28(int i1, int i2)
265+
{
266+
CallableRoutineContext context = CallableRoutineContext.get();
267+
return context.getInputMetadata().getCount() + ", " + context.getOutputMetadata().getCount();
268+
}
262269
}

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@
1818
*/
1919
package example;
2020

21+
import org.firebirdsql.fbjava.CallableRoutineContext;
2122
import org.firebirdsql.fbjava.ExternalResultSet;
23+
import org.firebirdsql.fbjava.ValuesMetadata;
2224

2325

2426
public class Procedures
@@ -117,4 +119,20 @@ public void close()
117119
}
118120
};
119121
}
122+
123+
public static void p8(int i1, int i2, String[] o1, int[] o2, int[] o3)
124+
{
125+
CallableRoutineContext context = CallableRoutineContext.get();
126+
o1[0] = context.getInputMetadata().getCount() + ", " + context.getOutputMetadata().getCount();
127+
}
128+
129+
public static void p9(int i1, Integer i2, String[] o1)
130+
{
131+
CallableRoutineContext context = CallableRoutineContext.get();
132+
ValuesMetadata input = context.getInputMetadata();
133+
ValuesMetadata output = context.getOutputMetadata();
134+
135+
o1[0] = input.getJavaClass(0).toString() + ", " + input.getJavaClass(1).toString() + ", " +
136+
output.getJavaClass(0).toString();
137+
}
120138
}

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,15 @@ public static CallableRoutineContext get()
3939
* For unpackaged routines, return null.
4040
*/
4141
public String getPackageName();
42+
43+
/**
44+
* Gets the input values metadata.
45+
*/
46+
public ValuesMetadata getInputMetadata();
47+
48+
/**
49+
* Gets the output values metadata.
50+
* For functions, it always returns a ValuesMetadata with a single entry.
51+
*/
52+
public ValuesMetadata getOutputMetadata();
4253
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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;
20+
21+
22+
/**
23+
* This interface represents a Firebird Values (set of input/output parameters or trigger's old/new fields) metadata.
24+
*
25+
* @author <a href="mailto:adrianosf@gmail.com">Adriano dos Santos Fernandes</a>
26+
*/
27+
public interface ValuesMetadata
28+
{
29+
/**
30+
* Gets the number of values.
31+
*/
32+
public int getCount();
33+
34+
/**
35+
* Gets the Java Class for a given value index.
36+
*/
37+
public Class<?> getJavaClass(int index);
38+
39+
//// TODO: more methods
40+
}

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
package org.firebirdsql.fbjava.impl;
2020

2121
import org.firebirdsql.fbjava.CallableRoutineContext;
22+
import org.firebirdsql.fbjava.ValuesMetadata;
2223

2324

2425
abstract class CallableRoutineContextImpl extends ContextImpl implements CallableRoutineContext
@@ -35,4 +36,16 @@ public String getPackageName()
3536
{
3637
return internalContext.getRoutine().packageName;
3738
}
39+
40+
@Override
41+
public ValuesMetadata getInputMetadata()
42+
{
43+
return internalContext.getRoutine().inputMetadata;
44+
}
45+
46+
@Override
47+
public ValuesMetadata getOutputMetadata()
48+
{
49+
return internalContext.getRoutine().outputMetadata;
50+
}
3851
}

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1155,8 +1155,7 @@ private Routine getRoutine(IStatus status, IExternalContext context, IRoutineMet
11551155
Class<?> clazz = runInClassLoader(status, context, className, methodName,
11561156
() -> Class.forName(className, true, sharedData.classLoader));
11571157

1158-
Routine routine = new Routine(type, this, metadata.getName(status), metadata.getPackage(status),
1159-
metadata.getBody(status));
1158+
Routine routine = new Routine(status, metadata, this, type);
11601159
ArrayList<Class<?>> paramTypes = new ArrayList<>();
11611160

11621161
int[] pos = {paramsStart + 1};
@@ -1279,8 +1278,11 @@ else if (pos[0] != entryPoint.length())
12791278
break;
12801279
}
12811280

1282-
routine.setupParameters(status, routine.inputParameters, inMetadata, inBuilder);
1283-
routine.setupParameters(status, routine.outputParameters, outMetadata, outBuilder);
1281+
routine.setupParameters(status, routine.inputParameters, routine.inputMetadata,
1282+
inMetadata, inBuilder);
1283+
1284+
routine.setupParameters(status, routine.outputParameters, routine.outputMetadata,
1285+
outMetadata, outBuilder);
12841286
}
12851287
finally
12861288
{

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

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import org.firebirdsql.fbjava.impl.FbClientLibrary.IExternalContext;
2626
import org.firebirdsql.fbjava.impl.FbClientLibrary.IMessageMetadata;
2727
import org.firebirdsql.fbjava.impl.FbClientLibrary.IMetadataBuilder;
28+
import org.firebirdsql.fbjava.impl.FbClientLibrary.IRoutineMetadata;
2829
import org.firebirdsql.fbjava.impl.FbClientLibrary.IStatus;
2930

3031
import com.sun.jna.Pointer;
@@ -48,18 +49,21 @@ static enum Type
4849
Method method;
4950
final List<Parameter> inputParameters = new ArrayList<>();
5051
final List<Parameter> outputParameters = new ArrayList<>();
52+
final ValuesMetadataImpl inputMetadata = new ValuesMetadataImpl();
53+
final ValuesMetadataImpl outputMetadata = new ValuesMetadataImpl();
5154

52-
Routine(Type type, ExternalEngine engine, String objectName, String packageName, String body)
55+
Routine(IStatus status, IRoutineMetadata metadata, ExternalEngine engine, Type type) throws FbException
5356
{
54-
this.type = type;
5557
this.engine = engine;
56-
this.objectName = objectName;
57-
this.packageName = packageName;
58-
this.body = body;
58+
this.type = type;
59+
60+
objectName = metadata.getName(status);
61+
packageName = metadata.getPackage(status);
62+
body = metadata.getBody(status);
5963
}
6064

61-
void setupParameters(IStatus status, List<Parameter> parameters, IMessageMetadata metadata,
62-
IMetadataBuilder builder) throws FbException
65+
void setupParameters(IStatus status, List<Parameter> parameters, ValuesMetadataImpl valuesMetadata,
66+
IMessageMetadata metadata, IMetadataBuilder builder) throws FbException
6367
{
6468
for (int i = 0; i < parameters.size(); ++i)
6569
{
@@ -78,6 +82,8 @@ void setupParameters(IStatus status, List<Parameter> parameters, IMessageMetadat
7882
parameter.offset = builtMetadata.getOffset(status, i);
7983
parameter.length = builtMetadata.getLength(status, i);
8084
}
85+
86+
valuesMetadata.setup(parameters);
8187
}
8288
finally
8389
{
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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+
import java.util.List;
22+
23+
import org.firebirdsql.fbjava.ValuesMetadata;
24+
25+
26+
final class ValuesMetadataImpl implements ValuesMetadata
27+
{
28+
private List<Parameter> parameters;
29+
private int count;
30+
31+
void setup(List<Parameter> parameters)
32+
{
33+
this.parameters = parameters;
34+
count = parameters.size();
35+
}
36+
37+
@Override
38+
public int getCount()
39+
{
40+
return count;
41+
}
42+
43+
@Override
44+
public Class<?> getJavaClass(int index)
45+
{
46+
checkIndex(index);
47+
return parameters.get(index).javaClass;
48+
}
49+
50+
private void checkIndex(int index)
51+
{
52+
if (index < 0 || index >= count)
53+
{
54+
throw new IndexOutOfBoundsException(
55+
String.format("ValuesMetadata index out of bounds: Index: %d, Size: %d", index, count));
56+
}
57+
}
58+
}

0 commit comments

Comments
 (0)