Skip to content

Commit f791207

Browse files
committed
Add Values interface and methods for dynamic read/write parameter values.
1 parent 05bbf2d commit f791207

File tree

14 files changed

+236
-20
lines changed

14 files changed

+236
-20
lines changed

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

Lines changed: 26 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.Values;
3637
import org.firebirdsql.fbjava.ValuesMetadata;
3738

3839

@@ -277,10 +278,35 @@ public static String f29(int i1, int i2) throws SQLException
277278
return getValuesInfo(input, 1) + ", " + getValuesInfo(input, 2) + ", " + getValuesInfo(output, 1);
278279
}
279280

281+
public static String f30(Object i1, Object i2) throws SQLException
282+
{
283+
FunctionContext context = FunctionContext.get();
284+
ValuesMetadata inputMetadata = context.getInputMetadata();
285+
Values input = context.getInputValues();
286+
287+
return Functions.getValues(inputMetadata, input);
288+
}
289+
280290
static String getValuesInfo(ValuesMetadata valuesMetadata, int index) throws SQLException
281291
{
282292
return valuesMetadata.getName(index) + ": " + valuesMetadata.getJavaClass(index).toString() +
283293
" (" + valuesMetadata.getParameterType(index) + ", " + valuesMetadata.getPrecision(index) + ", " +
284294
valuesMetadata.getScale(index) + ", " + valuesMetadata.isNullable(index) + ")";
285295
}
296+
297+
static String getValues(ValuesMetadata valuesMetadata, Values values) throws SQLException
298+
{
299+
int count = valuesMetadata.getParameterCount();
300+
StringBuilder sb = new StringBuilder();
301+
302+
for (int i = 1; i <= count; ++i)
303+
{
304+
if (i != 1)
305+
sb.append(", ");
306+
307+
sb.append(values.get(i));
308+
}
309+
310+
return sb.toString();
311+
}
286312
}

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222

2323
import org.firebirdsql.fbjava.CallableRoutineContext;
2424
import org.firebirdsql.fbjava.ExternalResultSet;
25+
import org.firebirdsql.fbjava.ProcedureContext;
26+
import org.firebirdsql.fbjava.Values;
2527
import org.firebirdsql.fbjava.ValuesMetadata;
2628

2729

@@ -147,4 +149,28 @@ public static void p10(Object i1, Object i2, String[] o1) throws SQLException
147149
o1[0] = Functions.getValuesInfo(input, 1) + ", " + Functions.getValuesInfo(input, 2) + ", " +
148150
Functions.getValuesInfo(output, 1);
149151
}
152+
153+
public static void p11(Object i1, Object i2, String[] o1) throws SQLException
154+
{
155+
ProcedureContext context = ProcedureContext.get();
156+
ValuesMetadata inputMetadata = context.getInputMetadata();
157+
ValuesMetadata outputMetadata = context.getOutputMetadata();
158+
Values input = context.getInputValues();
159+
Values output = context.getOutputValues();
160+
161+
if (o1[0] != output.get(1))
162+
throw new RuntimeException("Error");
163+
164+
o1[0] = "a";
165+
166+
if (o1[0] != output.get(1))
167+
throw new RuntimeException("Error");
168+
169+
output.set(1, "b");
170+
171+
if (o1[0] != output.get(1))
172+
throw new RuntimeException("Error");
173+
174+
output.set(1, Functions.getValues(inputMetadata, input) + " / " + Functions.getValues(outputMetadata, output));
175+
}
150176
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,9 @@ public static CallableRoutineContext get()
5050
* For functions, it always returns a ValuesMetadata with a single entry.
5151
*/
5252
public ValuesMetadata getOutputMetadata();
53+
54+
/**
55+
* Gets the input values.
56+
*/
57+
public Values getInputValues();
5358
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,9 @@ public static ProcedureContext get()
3333
{
3434
return (ProcedureContext) Context.get();
3535
}
36+
37+
/**
38+
* Gets the output values.
39+
*/
40+
public Values getOutputValues();
3641
}
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) data.
24+
*
25+
* @author <a href="mailto:adrianosf@gmail.com">Adriano dos Santos Fernandes</a>
26+
*/
27+
public interface Values
28+
{
29+
/**
30+
* Gets the value for a given index.
31+
* Index starts from 1.
32+
*/
33+
public Object get(int index);
34+
35+
/**
36+
* Sets the value for a given index.
37+
* Index starts from 1.
38+
*/
39+
public Object set(int index, Object value);
40+
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,14 @@ public interface ValuesMetadata extends ParameterMetaData
3636

3737
/**
3838
* Gets the name for a given value index.
39+
* Index starts from 1.
3940
* Returns null in function output metadata.
4041
*/
4142
public String getName(int index);
4243

4344
/**
4445
* Gets the Java Class for a given value index.
46+
* Index starts from 1.
4547
*/
4648
public Class<?> getJavaClass(int index);
4749
}

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

Lines changed: 7 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.Values;
2223
import org.firebirdsql.fbjava.ValuesMetadata;
2324

2425

@@ -48,4 +49,10 @@ public ValuesMetadata getOutputMetadata()
4849
{
4950
return internalContext.getRoutine().outputMetadata;
5051
}
52+
53+
@Override
54+
public Values getInputValues()
55+
{
56+
return internalContext.getInValues();
57+
}
5158
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ static synchronized Subject getUserSubject(IStatus status, IExternalContext cont
127127

128128
String roleName;
129129

130-
try (InternalContext internalContext = InternalContext.create(status, context, null))
130+
try (InternalContext internalContext = InternalContext.create(status, context, null, null, null))
131131
{
132132
try (Connection conn = internalContext.getConnection())
133133
{

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,12 @@ public void execute(IStatus status, IExternalContext context, Pointer inMsg, Poi
6262
{
6363
try
6464
{
65-
try (InternalContext internalContext = InternalContext.create(status, context, routine))
65+
Object[] in = new Object[routine.inputParameters.size()];
66+
67+
try (InternalContext internalContext = InternalContext.create(status, context, routine,
68+
new ValuesImpl(in, in.length), null))
6669
{
67-
Object[] in = routine.getFromMessage(status, context, routine.inputParameters, inMsg);
70+
routine.getFromMessage(status, context, routine.inputParameters, inMsg, in);
6871
Object[] out = {routine.run(status, context, in)};
6972

7073
routine.putInMessage(status, context, routine.outputParameters, out, 0, outMsg);

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

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -67,13 +67,15 @@ public IExternalResultSet open(IStatus status, IExternalContext context, Pointer
6767
{
6868
try
6969
{
70-
try (InternalContext internalContext = InternalContext.create(status, context, routine))
70+
int inCount = routine.inputParameters.size();
71+
int outCount = routine.outputParameters.size();
72+
Object[] inOut = new Object[inCount + outCount];
73+
Object[] inOut2 = new Object[inCount + outCount];
74+
75+
try (InternalContext internalContext = InternalContext.create(status, context, routine,
76+
new ValuesImpl(inOut, inCount),
77+
new ValuesImpl(inOut, inCount, outCount)))
7178
{
72-
int inCount = routine.inputParameters.size();
73-
int outCount = routine.outputParameters.size();
74-
Object[] inOut = new Object[inCount + outCount];
75-
Object[] inOut2 = new Object[inCount + outCount];
76-
7779
routine.getFromMessage(status, context, routine.inputParameters, inMsg, inOut);
7880

7981
for (int i = inCount; i < inOut.length; ++i)

0 commit comments

Comments
 (0)