Skip to content

Commit 05bbf2d

Browse files
committed
Add more methods to ValuesMetadata while extending java.sql.ParameterMetaData.
1 parent 59c0a45 commit 05bbf2d

File tree

6 files changed

+123
-34
lines changed

6 files changed

+123
-34
lines changed

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

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -262,20 +262,25 @@ public static String f27()
262262
return context.getBody();
263263
}
264264

265-
public static String f28(int i1, int i2)
265+
public static String f28(int i1, int i2) throws SQLException
266266
{
267267
CallableRoutineContext context = CallableRoutineContext.get();
268-
return context.getInputMetadata().getCount() + ", " + context.getOutputMetadata().getCount();
268+
return context.getInputMetadata().getParameterCount() + ", " + context.getOutputMetadata().getParameterCount();
269269
}
270270

271-
public static String f29(int i1, int i2)
271+
public static String f29(int i1, int i2) throws SQLException
272272
{
273273
CallableRoutineContext context = CallableRoutineContext.get();
274274
ValuesMetadata input = context.getInputMetadata();
275275
ValuesMetadata output = context.getOutputMetadata();
276276

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) + "), ";
277+
return getValuesInfo(input, 1) + ", " + getValuesInfo(input, 2) + ", " + getValuesInfo(output, 1);
278+
}
279+
280+
static String getValuesInfo(ValuesMetadata valuesMetadata, int index) throws SQLException
281+
{
282+
return valuesMetadata.getName(index) + ": " + valuesMetadata.getJavaClass(index).toString() +
283+
" (" + valuesMetadata.getParameterType(index) + ", " + valuesMetadata.getPrecision(index) + ", " +
284+
valuesMetadata.getScale(index) + ", " + valuesMetadata.isNullable(index) + ")";
280285
}
281286
}

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

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

21+
import java.sql.SQLException;
22+
2123
import org.firebirdsql.fbjava.CallableRoutineContext;
2224
import org.firebirdsql.fbjava.ExternalResultSet;
2325
import org.firebirdsql.fbjava.ValuesMetadata;
@@ -120,20 +122,29 @@ public void close()
120122
};
121123
}
122124

123-
public static void p8(int i1, int i2, String[] o1, int[] o2, int[] o3)
125+
public static void p8(int i1, int i2, String[] o1, int[] o2, int[] o3) throws SQLException
126+
{
127+
CallableRoutineContext context = CallableRoutineContext.get();
128+
o1[0] = context.getInputMetadata().getParameterCount() + ", " + context.getOutputMetadata().getParameterCount();
129+
}
130+
131+
public static void p9(int i1, Integer i2, String[] o1) throws SQLException
124132
{
125133
CallableRoutineContext context = CallableRoutineContext.get();
126-
o1[0] = context.getInputMetadata().getCount() + ", " + context.getOutputMetadata().getCount();
134+
ValuesMetadata input = context.getInputMetadata();
135+
ValuesMetadata output = context.getOutputMetadata();
136+
137+
o1[0] = Functions.getValuesInfo(input, 1) + ", " + Functions.getValuesInfo(input, 2) + ", " +
138+
Functions.getValuesInfo(output, 1);
127139
}
128140

129-
public static void p9(int i1, Integer i2, String[] o1)
141+
public static void p10(Object i1, Object i2, String[] o1) throws SQLException
130142
{
131143
CallableRoutineContext context = CallableRoutineContext.get();
132144
ValuesMetadata input = context.getInputMetadata();
133145
ValuesMetadata output = context.getOutputMetadata();
134146

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) + ")";
147+
o1[0] = Functions.getValuesInfo(input, 1) + ", " + Functions.getValuesInfo(input, 2) + ", " +
148+
Functions.getValuesInfo(output, 1);
138149
}
139150
}

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

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,21 @@
1818
*/
1919
package org.firebirdsql.fbjava;
2020

21+
import java.sql.ParameterMetaData;
22+
2123

2224
/**
2325
* This interface represents a Firebird Values (set of input/output parameters or trigger's old/new fields) metadata.
2426
*
2527
* @author <a href="mailto:adrianosf@gmail.com">Adriano dos Santos Fernandes</a>
2628
*/
27-
public interface ValuesMetadata
29+
public interface ValuesMetadata extends ParameterMetaData
2830
{
2931
/**
30-
* Gets the number of values.
32+
* Gets the index for a given name.
33+
* Returns -1 if the name is not found.
3134
*/
32-
public int getCount();
35+
public int getIndex(String name);
3336

3437
/**
3538
* Gets the name for a given value index.
@@ -41,12 +44,4 @@ public interface ValuesMetadata
4144
* Gets the Java Class for a given value index.
4245
*/
4346
public Class<?> getJavaClass(int index);
44-
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-
51-
//// TODO: more methods
5247
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ final class Parameter
2525
{
2626
this.dataType = dataType;
2727
this.javaClass = javaClass;
28-
this.type = type;
2928
}
3029

3130
DataType dataType;
@@ -36,4 +35,6 @@ final class Parameter
3635
int nullOffset;
3736
int offset;
3837
int length;
38+
int scale;
39+
boolean isNullable;
3940
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@ void setupParameters(IStatus status, List<Parameter> parameters, ValuesMetadataI
8181
parameter.nullOffset = builtMetadata.getNullOffset(status, i);
8282
parameter.offset = builtMetadata.getOffset(status, i);
8383
parameter.length = builtMetadata.getLength(status, i);
84+
parameter.scale = builtMetadata.getScale(status, i);
85+
parameter.isNullable = builtMetadata.isNullable(status, i);
8486
}
8587

8688
valuesMetadata.setup(parameters);

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

Lines changed: 85 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
*/
1919
package org.firebirdsql.fbjava.impl;
2020

21+
import java.sql.ParameterMetaData;
22+
import java.sql.SQLException;
2123
import java.util.List;
2224

2325
import org.firebirdsql.fbjava.ValuesMetadata;
@@ -26,47 +28,120 @@
2628
final class ValuesMetadataImpl implements ValuesMetadata
2729
{
2830
private List<Parameter> parameters;
29-
private int count;
3031

3132
void setup(List<Parameter> parameters)
3233
{
3334
this.parameters = parameters;
34-
count = parameters.size();
3535
}
3636

3737
@Override
38-
public int getCount()
38+
public int getIndex(String name)
3939
{
40-
return count;
40+
for (int i = 0; i < parameters.size(); ++i)
41+
{
42+
if (name.equals(parameters.get(i).name))
43+
return i + 1;
44+
}
45+
46+
return -1;
4147
}
4248

4349
@Override
4450
public String getName(int index)
4551
{
4652
checkIndex(index);
47-
return parameters.get(index).name;
53+
return parameters.get(index - 1).name;
4854
}
4955

5056
@Override
5157
public Class<?> getJavaClass(int index)
5258
{
5359
checkIndex(index);
54-
return parameters.get(index).javaClass;
60+
return parameters.get(index - 1).javaClass;
61+
}
62+
63+
@Override
64+
public int getParameterCount()
65+
{
66+
return parameters.size();
67+
}
68+
69+
@Override
70+
public int getParameterType(int index)
71+
{
72+
checkIndex(index);
73+
return parameters.get(index - 1).type.getSecond();
74+
}
75+
76+
@Override
77+
public int getPrecision(int index)
78+
{
79+
checkIndex(index);
80+
//// FIXME: Incorrect for multi-byte strings, for numerics etc.
81+
return parameters.get(index - 1).length;
82+
}
83+
84+
@Override
85+
public int getScale(int index)
86+
{
87+
checkIndex(index);
88+
return parameters.get(index - 1).scale;
89+
}
90+
91+
@Override
92+
public int isNullable(int index)
93+
{
94+
checkIndex(index);
95+
return parameters.get(index - 1).isNullable ?
96+
ParameterMetaData.parameterNullable : ParameterMetaData.parameterNoNulls;
97+
}
98+
99+
@Override
100+
public boolean isSigned(int index) throws SQLException
101+
{
102+
checkIndex(index);
103+
return true;
55104
}
56105

57106
@Override
58-
public int getSqlType(int index)
107+
public String getParameterTypeName(int index) throws SQLException
59108
{
60109
checkIndex(index);
61-
return parameters.get(index).type.getSecond();
110+
return null; //// FIXME:
111+
}
112+
113+
@Override
114+
public String getParameterClassName(int index) throws SQLException
115+
{
116+
checkIndex(index);
117+
return parameters.get(index - 1).javaClass.getName();
118+
}
119+
120+
@Override
121+
public int getParameterMode(int index) throws SQLException
122+
{
123+
checkIndex(index);
124+
return ParameterMetaData.parameterModeUnknown;
125+
}
126+
127+
@Override
128+
public <T> T unwrap(Class<T> iface) throws SQLException
129+
{
130+
return null;
131+
}
132+
133+
@Override
134+
public boolean isWrapperFor(Class<?> iface) throws SQLException
135+
{
136+
return false;
62137
}
63138

64139
private void checkIndex(int index)
65140
{
66-
if (index < 0 || index >= count)
141+
if (index < 1 || index > parameters.size())
67142
{
68143
throw new IndexOutOfBoundsException(
69-
String.format("ValuesMetadata index out of bounds: Index: %d, Size: %d", index, count));
144+
String.format("ValuesMetadata index out of bounds: Index: %d, Size: %d", index, parameters.size()));
70145
}
71146
}
72147
}

0 commit comments

Comments
 (0)