-
Notifications
You must be signed in to change notification settings - Fork 435
/
Copy pathSQLServerParameterMetaData.java
571 lines (519 loc) · 24.1 KB
/
SQLServerParameterMetaData.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
/*
* Microsoft JDBC Driver for SQL Server Copyright(c) Microsoft Corporation All rights reserved. This program is made
* available under the terms of the MIT License. See the LICENSE file in the project root for more information.
*/
package com.microsoft.sqlserver.jdbc;
import java.sql.ParameterMetaData;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* Provides meta data for prepared statement parameters.
*
* The API javadoc for JDBC API methods that this class implements are not repeated here. Please see Sun's JDBC API
* interfaces javadoc for those details.
*
* Prepared statements are executed with SET FMT ONLY to retrieve column meta data Callable statements :
* sp_sp_sproc_columns is called to retrieve names and meta data for the procedures params.
*/
public final class SQLServerParameterMetaData implements ParameterMetaData {
private final static int SQL_SERVER_2012_VERSION = 11;
private final SQLServerPreparedStatement stmtParent;
private SQLServerConnection con;
private List<Map<String, Object>> procMetadata;
protected boolean procedureIsFound = false;
static final private java.util.logging.Logger logger = java.util.logging.Logger
.getLogger("com.microsoft.sqlserver.jdbc.internals.SQLServerParameterMetaData");
static private final AtomicInteger baseID = new AtomicInteger(0); // Unique id generator for each instance (used for
// logging).
final private String traceID = " SQLServerParameterMetaData:" + nextInstanceID();
boolean isTVP = false;
// Returns unique id for each instance.
private static int nextInstanceID() {
return baseID.incrementAndGet();
}
/**
* Provides a helper function to provide an ID string suitable for tracing.
*
* @return traceID string
*/
@Override
final public String toString() {
return traceID;
}
/* Used for prepared statement meta data */
class QueryMeta {
String parameterClassName = null;
int parameterType = 0;
String parameterTypeName = null;
int precision = 0;
int scale = 0;
int isNullable = ParameterMetaData.parameterNullableUnknown;
boolean isSigned = false;
}
Map<Integer, QueryMeta> queryMetaMap = null;
/*
* Parses query metadata.
*/
private void parseQueryMeta(ResultSet rsQueryMeta) throws SQLServerException {
Pattern datatypePattern = Pattern.compile("(.*)\\((.*)(\\)|,(.*)\\))");
try {
if (null != rsQueryMeta) {
while (rsQueryMeta.next()) {
QueryMeta qm = new QueryMeta();
SSType ssType = null;
int paramOrdinal = rsQueryMeta.getInt("parameter_ordinal");
String typename = rsQueryMeta.getString("suggested_system_type_name");
if (null == typename) {
typename = rsQueryMeta.getString("suggested_user_type_name");
try (SQLServerPreparedStatement pstmt = (SQLServerPreparedStatement) con.prepareStatement(
"select max_length, precision, scale, is_nullable from sys.assembly_types where name = ?")) {
pstmt.setNString(1, typename);
try (ResultSet assemblyRs = pstmt.executeQuery()) {
if (assemblyRs.next()) {
qm.parameterTypeName = typename;
qm.precision = assemblyRs.getInt("max_length");
qm.scale = assemblyRs.getInt("scale");
ssType = SSType.UDT;
}
}
}
} else {
qm.precision = rsQueryMeta.getInt("suggested_precision");
qm.scale = rsQueryMeta.getInt("suggested_scale");
Matcher matcher = datatypePattern.matcher(typename);
if (matcher.matches()) {
// the datatype has some precision/scale defined explicitly.
ssType = SSType.of(matcher.group(1));
if ("varchar(max)".equalsIgnoreCase(typename)
|| "varbinary(max)".equalsIgnoreCase(typename)) {
qm.precision = SQLServerDatabaseMetaData.MAXLOBSIZE;
} else if ("nvarchar(max)".equalsIgnoreCase(typename)) {
qm.precision = SQLServerDatabaseMetaData.MAXLOBSIZE / 2;
} else if (SSType.Category.CHARACTER == ssType.category
|| SSType.Category.BINARY == ssType.category
|| SSType.Category.NCHARACTER == ssType.category) {
try {
// For character/binary data types "suggested_precision" is 0. So get the precision
// from
// the type itself.
qm.precision = Integer.parseInt(matcher.group(2));
} catch (NumberFormatException e) {
MessageFormat form = new MessageFormat(
SQLServerException.getErrString("R_metaDataErrorForParameter"));
Object[] msgArgs = {paramOrdinal};
SQLServerException.makeFromDriverError(con, stmtParent,
form.format(msgArgs) + " " + e.getMessage(), null, false);
}
}
} else
ssType = SSType.of(typename);
// For float and real types suggested_precision returns the number of bits, not digits.
if (SSType.FLOAT == ssType) {
// https://msdn.microsoft.com/en-CA/library/ms173773.aspx
// real is float(24) and is 7 digits. Float is 15 digits.
qm.precision = 15;
} else if (SSType.REAL == ssType) {
qm.precision = 7;
} else if (SSType.TEXT == ssType) {
qm.precision = SQLServerDatabaseMetaData.MAXLOBSIZE;
} else if (SSType.NTEXT == ssType) {
qm.precision = SQLServerDatabaseMetaData.MAXLOBSIZE / 2;
} else if (SSType.IMAGE == ssType) {
qm.precision = SQLServerDatabaseMetaData.MAXLOBSIZE;
} else if (SSType.GUID == ssType) {
qm.precision = SQLServerDatabaseMetaData.uniqueidentifierSize;
} else if (SSType.TIMESTAMP == ssType) {
qm.precision = 8;
} else if (SSType.XML == ssType) {
qm.precision = SQLServerDatabaseMetaData.MAXLOBSIZE / 2;
}
qm.parameterTypeName = ssType.toString();
}
// Check if ssType is null. Was caught by static analysis.
if (null == ssType) {
throw new SQLServerException(SQLServerException.getErrString("R_metaDataErrorForParameter"),
null);
}
JDBCType jdbcType = ssType.getJDBCType();
qm.parameterClassName = jdbcType.className();
qm.parameterType = jdbcType.getIntValue();
// The parameter can be signed if it is a NUMERIC type (except bit or tinyint).
qm.isSigned = ((SSType.Category.NUMERIC == ssType.category) && (SSType.BIT != ssType)
&& (SSType.TINYINT != ssType));
queryMetaMap.put(paramOrdinal, qm);
}
}
} catch (SQLException e) {
throw new SQLServerException(SQLServerException.getErrString("R_metaDataErrorForParameter"), e);
}
}
private void parseFMTQueryMeta(ResultSetMetaData md, SQLServerFMTQuery f) throws SQLServerException {
try {
// Gets the list of parsed column names/targets
List<String> columns = f.getColumns();
// Gets VALUES(?,?,?...) list. The internal list corresponds to the parameters in the bracket after VALUES.
List<List<String>> params = f.getValuesList();
int valueListOffset = 0;
int mdIndex = 1;
int mapIndex = 1;
for (int i = 0; i < columns.size(); i++) {
/**
* For INSERT table VALUES(?,?,?...) scenarios where the column names are not specifically defined after
* the table name, the parser adds a '*' followed by '?'s equal to the number of parameters in the
* values bracket. The '*' will retrieve all values from the table and we'll use the '?'s to match their
* position here
*/
if ("*".equals(columns.get(i))) {
for (int j = 0; j < params.get(valueListOffset).size(); j++) {
if ("?".equals(params.get(valueListOffset).get(j))) {
if (!md.isAutoIncrement(mdIndex + j)) {
QueryMeta qm = getQueryMetaFromResultSetMetaData(md, mdIndex + j);
queryMetaMap.put(mapIndex++, qm);
i++;
}
}
}
mdIndex += params.get(valueListOffset).size();
valueListOffset++;
} else {
/*
* If this is not a INSERT table VALUES(...) situation, just add the entry.
*/
QueryMeta qm = getQueryMetaFromResultSetMetaData(md, mdIndex);
queryMetaMap.put(mapIndex++, qm);
mdIndex++;
}
}
} catch (SQLException e) {
throw new SQLServerException(SQLServerException.getErrString("R_metaDataErrorForParameter"), e);
}
}
private QueryMeta getQueryMetaFromResultSetMetaData(ResultSetMetaData md, int index) throws SQLException {
QueryMeta qm = new QueryMeta();
qm.parameterClassName = md.getColumnClassName(index);
qm.parameterType = md.getColumnType(index);
qm.parameterTypeName = md.getColumnTypeName(index);
qm.precision = md.getPrecision(index);
qm.scale = md.getScale(index);
qm.isNullable = md.isNullable(index);
qm.isSigned = md.isSigned(index);
return qm;
}
String parseProcIdentifier(String procIdentifier) throws SQLServerException {
ThreePartName threePartName = ThreePartName.parse(procIdentifier);
StringBuilder sb = new StringBuilder();
if (threePartName.getDatabasePart() != null) {
sb.append("@procedure_qualifier=");
sb.append(threePartName.getDatabasePart());
sb.append(", ");
}
if (threePartName.getOwnerPart() != null) {
sb.append("@procedure_owner=");
sb.append(threePartName.getOwnerPart());
sb.append(", ");
}
if (threePartName.getProcedurePart() != null) {
sb.append("@procedure_name=");
sb.append(threePartName.getProcedurePart());
} else {
SQLServerException.makeFromDriverError(con, stmtParent, SQLServerException.getErrString("R_noMetadata"),
null, false);
}
return sb.toString();
}
private void checkClosed() throws SQLServerException {
// stmtParent does not seem to be re-used, should just verify connection is not closed.
// stmtParent.checkClosed();
con.checkClosed();
}
/**
* Construct a SQLServerParameterMetaData parameter meta data.
*
* @param st
* the prepared statement
* @param sProcString
* the procedure name
* @throws SQLServerException
*/
@SuppressWarnings("serial")
SQLServerParameterMetaData(SQLServerPreparedStatement st, String sProcString) throws SQLServerException {
assert null != st;
stmtParent = st;
con = st.connection;
if (logger.isLoggable(java.util.logging.Level.FINE)) {
logger.fine(toString() + " created by (" + st.toString() + ")");
}
try {
// If the CallableStatement/PreparedStatement is a stored procedure call
// then we can extract metadata using sp_sproc_columns
if (null != st.procedureName) {
String sProc = parseProcIdentifier(st.procedureName);
try (SQLServerStatement s = (SQLServerStatement) con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
SQLServerResultSet rsProcedureMeta = s.executeQueryInternal(
con.isKatmaiOrLater() ? "exec sp_sproc_columns_100 " + sProc + ", @ODBCVer=3, @fUsePattern=0"
: "exec sp_sproc_columns " + sProc + ", @ODBCVer=3, @fUsePattern=0")) {
// if rsProcedureMeta has next row, it means the stored procedure is found
if (rsProcedureMeta.next()) {
procedureIsFound = true;
} else {
procedureIsFound = false;
}
rsProcedureMeta.beforeFirst();
// Sixth is DATA_TYPE
rsProcedureMeta.getColumn(6).setFilter(new DataTypeFilter());
if (con.isKatmaiOrLater()) {
rsProcedureMeta.getColumn(8).setFilter(new ZeroFixupFilter());
rsProcedureMeta.getColumn(9).setFilter(new ZeroFixupFilter());
rsProcedureMeta.getColumn(17).setFilter(new ZeroFixupFilter());
}
procMetadata = new ArrayList<>();
// Process ResultSet Procedure Metadata for API usage
while (rsProcedureMeta.next()) {
procMetadata.add(new HashMap<String, Object>() {
{
put("DATA_TYPE", rsProcedureMeta.getShort("DATA_TYPE"));
put("COLUMN_TYPE", rsProcedureMeta.getInt("COLUMN_TYPE"));
put("TYPE_NAME", rsProcedureMeta.getString("TYPE_NAME"));
put("PRECISION", rsProcedureMeta.getInt("PRECISION"));
put("SCALE", rsProcedureMeta.getInt("SCALE"));
put("NULLABLE", rsProcedureMeta.getInt("NULLABLE"));
put("SS_TYPE_SCHEMA_NAME", rsProcedureMeta.getString("SS_TYPE_SCHEMA_NAME"));
}
});
}
}
}
// Otherwise we just have a parameterized statement.
// if SQL server version is 2012 and above use stored
// procedure "sp_describe_undeclared_parameters" to retrieve parameter meta data
// if SQL server version is 2008, then use FMTONLY
else {
queryMetaMap = new HashMap<>();
if (con.getServerMajorVersion() >= SQL_SERVER_2012_VERSION && !st.getUseFmtOnly()) {
String preparedSQL = con.replaceParameterMarkers(stmtParent.userSQL,
stmtParent.userSQLParamPositions, stmtParent.inOutParam, stmtParent.bReturnValueSyntax);
try (SQLServerCallableStatement cstmt = (SQLServerCallableStatement) con
.prepareCall("exec sp_describe_undeclared_parameters ?")) {
cstmt.setNString(1, preparedSQL);
parseQueryMeta(cstmt.executeQueryInternal());
}
} else {
SQLServerFMTQuery f = new SQLServerFMTQuery(sProcString);
try (SQLServerStatement stmt = (SQLServerStatement) con.createStatement();
ResultSet rs = stmt.executeQuery(f.getFMTQuery())) {
parseFMTQueryMeta(rs.getMetaData(), f);
}
}
}
}
// Do not need to wrapper SQLServerException again
catch (SQLServerException e) {
throw e;
} catch (SQLException e) {
SQLServerException.makeFromDriverError(con, stmtParent, e.getMessage(), null, false);
} catch (StringIndexOutOfBoundsException e) {
SQLServerException.makeFromDriverError(con, stmtParent, e.getMessage(), null, false);
}
}
@Override
public boolean isWrapperFor(Class<?> iface) throws SQLException {
boolean f = iface.isInstance(this);
return f;
}
@Override
public <T> T unwrap(Class<T> iface) throws SQLException {
T t;
try {
t = iface.cast(this);
} catch (ClassCastException e) {
throw new SQLServerException(e.getMessage(), e);
}
return t;
}
private Map<String, Object> getParameterInfo(int param) {
if (stmtParent.bReturnValueSyntax && isTVP) {
return procMetadata.get(param - 1);
} else {
// Note row 1 is the 'return value' meta data
return procMetadata.get(param);
}
}
private boolean isValidParamProc(int n) {
// Note row 1 is the 'return value' meta data
return ((stmtParent.bReturnValueSyntax && isTVP && procMetadata.size() >= n) || procMetadata.size() > n);
}
private boolean isValidParamQuery(int n) {
return (null != queryMetaMap && queryMetaMap.containsKey(n));
}
/**
* Checks if the @param passed is valid for either procedure metadata or query metadata.
*
* @param param
* @throws SQLServerException
*/
private void checkParam(int param) throws SQLServerException {
// Check if Procedure Metadata is not available
if (null == procMetadata) {
// Check if Query Metadata is also not available
if (!isValidParamQuery(param)) {
SQLServerException.makeFromDriverError(con, stmtParent, SQLServerException.getErrString("R_noMetadata"),
null, false);
}
} else if (!isValidParamProc(param)) {
// Throw exception if @param index not found
MessageFormat form = new MessageFormat(SQLServerException.getErrString("R_invalidParameterNumber"));
Object[] msgArgs = {param};
SQLServerException.makeFromDriverError(con, stmtParent, form.format(msgArgs), null, false);
}
}
@Override
public String getParameterClassName(int param) throws SQLServerException {
checkClosed();
checkParam(param);
try {
if (null == procMetadata) {
return queryMetaMap.get(param).parameterClassName;
} else {
return JDBCType.of((short) getParameterInfo(param).get("DATA_TYPE")).className();
}
} catch (SQLServerException e) {
SQLServerException.makeFromDriverError(con, stmtParent, e.getMessage(), null, false);
return null;
}
}
@Override
public int getParameterCount() throws SQLServerException {
checkClosed();
if (null == procMetadata) {
return queryMetaMap.size();
} else {
// Row 1 is Return Type metadata
return (procMetadata.size() == 0 ? 0 : procMetadata.size() - 1);
}
}
@Override
public int getParameterMode(int param) throws SQLServerException {
checkClosed();
checkParam(param);
if (null == procMetadata) {
// if it is not a stored procedure, the @param can only be input.
return parameterModeIn;
} else {
int n = (int) getParameterInfo(param).get("COLUMN_TYPE");
if (n == 1)
return parameterModeIn;
else if (n == 2)
return parameterModeOut;
else
return parameterModeUnknown;
}
}
@Override
public int getParameterType(int param) throws SQLServerException {
checkClosed();
checkParam(param);
int parameterType = 0;
if (null == procMetadata) {
parameterType = queryMetaMap.get(param).parameterType;
} else {
parameterType = (short) getParameterInfo(param).get("DATA_TYPE");
}
if (0 != parameterType) {
switch (parameterType) {
case microsoft.sql.Types.DATETIME:
case microsoft.sql.Types.SMALLDATETIME:
parameterType = SSType.DATETIME2.getJDBCType().asJavaSqlType();
break;
case microsoft.sql.Types.MONEY:
case microsoft.sql.Types.SMALLMONEY:
parameterType = SSType.DECIMAL.getJDBCType().asJavaSqlType();
break;
case microsoft.sql.Types.GUID:
parameterType = SSType.CHAR.getJDBCType().asJavaSqlType();
break;
default:
break;
}
}
return parameterType;
}
@Override
public String getParameterTypeName(int param) throws SQLServerException {
checkClosed();
checkParam(param);
if (null == procMetadata) {
return queryMetaMap.get(param).parameterTypeName;
} else {
return getParameterInfo(param).get("TYPE_NAME").toString();
}
}
@Override
public int getPrecision(int param) throws SQLServerException {
checkClosed();
checkParam(param);
if (null == procMetadata) {
return queryMetaMap.get(param).precision;
} else {
return (int) getParameterInfo(param).get("PRECISION");
}
}
@Override
public int getScale(int param) throws SQLServerException {
checkClosed();
checkParam(param);
if (null == procMetadata) {
return queryMetaMap.get(param).scale;
} else {
return (int) getParameterInfo(param).get("SCALE");
}
}
@Override
public int isNullable(int param) throws SQLServerException {
checkClosed();
checkParam(param);
if (procMetadata == null) {
return queryMetaMap.get(param).isNullable;
} else {
return (int) getParameterInfo(param).get("NULLABLE");
}
}
/**
* Returns if a supplied parameter index is valid.
*
* @param param
* the @param index
* @throws SQLServerException
* when an error occurs
* @return boolean
*/
@Override
public boolean isSigned(int param) throws SQLServerException {
checkClosed();
checkParam(param);
try {
if (null == procMetadata) {
return queryMetaMap.get(param).isSigned;
} else {
return JDBCType.of((short) getParameterInfo(param).get("DATA_TYPE")).isSigned();
}
} catch (SQLException e) {
SQLServerException.makeFromDriverError(con, stmtParent, e.getMessage(), null, false);
return false;
}
}
String getTVPSchemaFromStoredProcedure(int param) throws SQLServerException {
checkClosed();
checkParam(param);
return (String) getParameterInfo(param).get("SS_TYPE_SCHEMA_NAME");
}
}