forked from microsoft/mssql-jdbc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSQLServerDriver.java
652 lines (562 loc) · 33.4 KB
/
SQLServerDriver.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
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
/*
* 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.DriverManager;
import java.sql.DriverPropertyInfo;
import java.sql.SQLException;
import java.sql.SQLFeatureNotSupportedException;
import java.text.MessageFormat;
import java.util.Enumeration;
import java.util.Locale;
import java.util.Properties;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.ietf.jgss.GSSCredential;
/**
* SQLServerDriver implements the java.sql.Driver for SQLServerConnect.
*
*/
final class SQLServerDriverPropertyInfo {
private final String name;
final String getName() {
return name;
}
private final String description;
private final String defaultValue;
private final boolean required;
private final String[] choices;
SQLServerDriverPropertyInfo(String name,
String defaultValue,
boolean required,
String[] choices) {
this.name = name;
this.description = SQLServerResource.getResource("R_" + name + "PropertyDescription");
this.defaultValue = defaultValue;
this.required = required;
this.choices = choices;
}
DriverPropertyInfo build(Properties connProperties) {
String propValue = name.equals(SQLServerDriverStringProperty.PASSWORD.toString()) ? "" : connProperties.getProperty(name);
if (null == propValue)
propValue = defaultValue;
DriverPropertyInfo info = new DriverPropertyInfo(name, propValue);
info.description = description;
info.required = required;
info.choices = choices;
return info;
}
}
enum SqlAuthentication {
NotSpecified,
SqlPassword,
ActiveDirectoryPassword,
ActiveDirectoryIntegrated;
static SqlAuthentication valueOfString(String value) throws SQLServerException {
SqlAuthentication method = null;
if (value.toLowerCase(Locale.US).equalsIgnoreCase(SqlAuthentication.NotSpecified.toString())) {
method = SqlAuthentication.NotSpecified;
}
else if (value.toLowerCase(Locale.US).equalsIgnoreCase(SqlAuthentication.SqlPassword.toString())) {
method = SqlAuthentication.SqlPassword;
}
else if (value.toLowerCase(Locale.US).equalsIgnoreCase(SqlAuthentication.ActiveDirectoryPassword.toString())) {
method = SqlAuthentication.ActiveDirectoryPassword;
}
else if (value.toLowerCase(Locale.US).equalsIgnoreCase(SqlAuthentication.ActiveDirectoryIntegrated.toString())) {
method = SqlAuthentication.ActiveDirectoryIntegrated;
}
else {
MessageFormat form = new MessageFormat(SQLServerException.getErrString("R_InvalidConnectionSetting"));
Object[] msgArgs = {"authentication", value};
throw new SQLServerException(null, form.format(msgArgs), null, 0, false);
}
return method;
}
}
enum ColumnEncryptionSetting {
Enabled,
Disabled;
static ColumnEncryptionSetting valueOfString(String value) throws SQLServerException {
ColumnEncryptionSetting method = null;
if (value.toLowerCase(Locale.US).equalsIgnoreCase(ColumnEncryptionSetting.Enabled.toString())) {
method = ColumnEncryptionSetting.Enabled;
}
else if (value.toLowerCase(Locale.US).equalsIgnoreCase(ColumnEncryptionSetting.Disabled.toString())) {
method = ColumnEncryptionSetting.Disabled;
}
else {
MessageFormat form = new MessageFormat(SQLServerException.getErrString("R_InvalidConnectionSetting"));
Object[] msgArgs = {"columnEncryptionSetting", value};
throw new SQLServerException(form.format(msgArgs), null);
}
return method;
}
}
enum KeyStoreAuthentication {
JavaKeyStorePassword;
static KeyStoreAuthentication valueOfString(String value) throws SQLServerException {
KeyStoreAuthentication method = null;
if (value.toLowerCase(Locale.US).equalsIgnoreCase(KeyStoreAuthentication.JavaKeyStorePassword.toString())) {
method = KeyStoreAuthentication.JavaKeyStorePassword;
}
else {
MessageFormat form = new MessageFormat(SQLServerException.getErrString("R_InvalidConnectionSetting"));
Object[] msgArgs = {"keyStoreAuthentication", value};
throw new SQLServerException(form.format(msgArgs), null);
}
return method;
}
}
enum AuthenticationScheme {
nativeAuthentication,
javaKerberos;
static AuthenticationScheme valueOfString(String value) throws SQLServerException {
AuthenticationScheme scheme;
if (value.toLowerCase(Locale.US).equalsIgnoreCase(AuthenticationScheme.javaKerberos.toString())) {
scheme = AuthenticationScheme.javaKerberos;
}
else if (value.toLowerCase(Locale.US).equalsIgnoreCase(AuthenticationScheme.nativeAuthentication.toString())) {
scheme = AuthenticationScheme.nativeAuthentication;
}
else {
MessageFormat form = new MessageFormat(SQLServerException.getErrString("R_invalidAuthenticationScheme"));
Object[] msgArgs = {value};
throw new SQLServerException(null, form.format(msgArgs), null, 0, false);
}
return scheme;
}
}
enum ApplicationIntent {
READ_WRITE("readwrite"),
READ_ONLY("readonly");
// the value of the enum
private String value;
// constructor that sets the string value of the enum
private ApplicationIntent(String value) {
this.value = value;
}
// returns the string value of enum
public String toString() {
return value;
}
static ApplicationIntent valueOfString(String value) throws SQLServerException {
ApplicationIntent applicationIntent = ApplicationIntent.READ_WRITE;
assert value != null;
// handling turkish i issues
value = value.toUpperCase(Locale.US).toLowerCase(Locale.US);
if (value.equalsIgnoreCase(ApplicationIntent.READ_ONLY.toString())) {
applicationIntent = ApplicationIntent.READ_ONLY;
}
else if (value.equalsIgnoreCase(ApplicationIntent.READ_WRITE.toString())) {
applicationIntent = ApplicationIntent.READ_WRITE;
}
else {
MessageFormat form = new MessageFormat(SQLServerException.getErrString("R_invalidapplicationIntent"));
Object[] msgArgs = {value};
throw new SQLServerException(null, form.format(msgArgs), null, 0, false);
}
return applicationIntent;
}
}
enum SQLServerDriverObjectProperty {
GSS_CREDENTIAL("gsscredential", null);
private String name;
private Object defaultValue;
private SQLServerDriverObjectProperty(String name,
Object defaultValue) {
this.name = name;
this.defaultValue = defaultValue;
}
/**
* returning string due to structure of DRIVER_PROPERTIES_PROPERTY_ONLY
* @return
*/
public String getDefaultValue() {
return null;
}
public String toString() {
return name;
}
}
enum SQLServerDriverStringProperty
{
APPLICATION_INTENT ("applicationIntent", ApplicationIntent.READ_WRITE.toString()),
APPLICATION_NAME ("applicationName", SQLServerDriver.DEFAULT_APP_NAME),
DATABASE_NAME ("databaseName", ""),
FAILOVER_PARTNER ("failoverPartner", ""),
HOSTNAME_IN_CERTIFICATE ("hostNameInCertificate", ""),
INSTANCE_NAME ("instanceName", ""),
PASSWORD ("password", ""),
RESPONSE_BUFFERING ("responseBuffering", "adaptive"),
SELECT_METHOD ("selectMethod", "direct"),
SERVER_NAME ("serverName", ""),
SERVER_SPN ("serverSpn", ""),
TRUST_STORE_TYPE ("trustStoreType", "JKS"),
TRUST_STORE ("trustStore", ""),
TRUST_STORE_PASSWORD ("trustStorePassword", ""),
USER ("user", ""),
WORKSTATION_ID ("workstationID", Util.WSIDNotAvailable),
AUTHENTICATION_SCHEME ("authenticationScheme", AuthenticationScheme.nativeAuthentication.toString()),
AUTHENTICATION ("authentication", SqlAuthentication.NotSpecified.toString()),
ACCESS_TOKEN ("accessToken", ""),
COLUMN_ENCRYPTION ("columnEncryptionSetting", ColumnEncryptionSetting.Disabled.toString()),
KEY_STORE_AUTHENTICATION ("keyStoreAuthentication", ""),
KEY_STORE_SECRET ("keyStoreSecret", ""),
KEY_STORE_LOCATION ("keyStoreLocation", ""),
FIPS_PROVIDER ("fipsProvider", ""),
;
private String name;
private String defaultValue;
private SQLServerDriverStringProperty(String name,
String defaultValue) {
this.name = name;
this.defaultValue = defaultValue;
}
String getDefaultValue() {
return defaultValue;
}
public String toString() {
return name;
}
}
enum SQLServerDriverIntProperty {
PACKET_SIZE ("packetSize", TDS.DEFAULT_PACKET_SIZE),
LOCK_TIMEOUT ("lockTimeout", -1),
LOGIN_TIMEOUT ("loginTimeout", 15),
QUERY_TIMEOUT ("queryTimeout", -1),
PORT_NUMBER ("portNumber", 1433),
SOCKET_TIMEOUT ("socketTimeout", 0);
private String name;
private int defaultValue;
private SQLServerDriverIntProperty(String name,
int defaultValue) {
this.name = name;
this.defaultValue = defaultValue;
}
int getDefaultValue() {
return defaultValue;
}
public String toString() {
return name;
}
}
enum SQLServerDriverBooleanProperty
{
DISABLE_STATEMENT_POOLING ("disableStatementPooling", true),
ENCRYPT ("encrypt", false),
INTEGRATED_SECURITY ("integratedSecurity", false),
LAST_UPDATE_COUNT ("lastUpdateCount", true),
MULTI_SUBNET_FAILOVER ("multiSubnetFailover", false),
SERVER_NAME_AS_ACE ("serverNameAsACE", false),
SEND_STRING_PARAMETERS_AS_UNICODE ("sendStringParametersAsUnicode", true),
SEND_TIME_AS_DATETIME ("sendTimeAsDatetime", true),
TRANSPARENT_NETWORK_IP_RESOLUTION ("TransparentNetworkIPResolution", true),
TRUST_SERVER_CERTIFICATE ("trustServerCertificate", false),
XOPEN_STATES ("xopenStates", false),
FIPS ("fips", false);
private String name;
private boolean defaultValue;
private SQLServerDriverBooleanProperty(String name,
boolean defaultValue) {
this.name = name;
this.defaultValue = defaultValue;
}
boolean getDefaultValue() {
return defaultValue;
}
public String toString() {
return name;
}
}
public final class SQLServerDriver implements java.sql.Driver {
static final String PRODUCT_NAME = "Microsoft JDBC Driver " + SQLJdbcVersion.major + "." + SQLJdbcVersion.minor + " for SQL Server";
static final String DEFAULT_APP_NAME = "Microsoft JDBC Driver for SQL Server";
private static final String[] TRUE_FALSE = {"true", "false"};
private static final SQLServerDriverPropertyInfo[] DRIVER_PROPERTIES =
{
// default required available choices
// property name value property (if appropriate)
new SQLServerDriverPropertyInfo(SQLServerDriverStringProperty.APPLICATION_INTENT.toString(), SQLServerDriverStringProperty.APPLICATION_INTENT.getDefaultValue(), false, new String[]{ApplicationIntent.READ_ONLY.toString(), ApplicationIntent.READ_WRITE.toString()}),
new SQLServerDriverPropertyInfo(SQLServerDriverStringProperty.APPLICATION_NAME.toString(), SQLServerDriverStringProperty.APPLICATION_NAME.getDefaultValue(), false, null),
new SQLServerDriverPropertyInfo(SQLServerDriverStringProperty.COLUMN_ENCRYPTION.toString(), SQLServerDriverStringProperty.COLUMN_ENCRYPTION.getDefaultValue(), false, new String[] {ColumnEncryptionSetting.Disabled.toString(), ColumnEncryptionSetting.Enabled.toString()}),
new SQLServerDriverPropertyInfo(SQLServerDriverStringProperty.DATABASE_NAME.toString(), SQLServerDriverStringProperty.DATABASE_NAME.getDefaultValue(), false, null),
new SQLServerDriverPropertyInfo(SQLServerDriverBooleanProperty.DISABLE_STATEMENT_POOLING.toString(), Boolean.toString(SQLServerDriverBooleanProperty.DISABLE_STATEMENT_POOLING.getDefaultValue()), false, new String[] {"true"}),
new SQLServerDriverPropertyInfo(SQLServerDriverBooleanProperty.ENCRYPT.toString(), Boolean.toString(SQLServerDriverBooleanProperty.ENCRYPT.getDefaultValue()), false, TRUE_FALSE),
new SQLServerDriverPropertyInfo(SQLServerDriverStringProperty.FAILOVER_PARTNER.toString(), SQLServerDriverStringProperty.FAILOVER_PARTNER.getDefaultValue(), false, null),
new SQLServerDriverPropertyInfo(SQLServerDriverStringProperty.HOSTNAME_IN_CERTIFICATE.toString(), SQLServerDriverStringProperty.HOSTNAME_IN_CERTIFICATE.getDefaultValue(), false, null),
new SQLServerDriverPropertyInfo(SQLServerDriverStringProperty.INSTANCE_NAME.toString(), SQLServerDriverStringProperty.INSTANCE_NAME.getDefaultValue(), false, null),
new SQLServerDriverPropertyInfo(SQLServerDriverBooleanProperty.INTEGRATED_SECURITY.toString(), Boolean.toString(SQLServerDriverBooleanProperty.INTEGRATED_SECURITY.getDefaultValue()), false, TRUE_FALSE),
new SQLServerDriverPropertyInfo(SQLServerDriverStringProperty.KEY_STORE_AUTHENTICATION.toString(), SQLServerDriverStringProperty.KEY_STORE_AUTHENTICATION.getDefaultValue(), false, new String[] {KeyStoreAuthentication.JavaKeyStorePassword.toString()}),
new SQLServerDriverPropertyInfo(SQLServerDriverStringProperty.KEY_STORE_SECRET .toString(), SQLServerDriverStringProperty.KEY_STORE_SECRET.getDefaultValue(), false, null),
new SQLServerDriverPropertyInfo(SQLServerDriverStringProperty.KEY_STORE_LOCATION .toString(), SQLServerDriverStringProperty.KEY_STORE_LOCATION.getDefaultValue(), false, null),
new SQLServerDriverPropertyInfo(SQLServerDriverBooleanProperty.LAST_UPDATE_COUNT.toString(), Boolean.toString(SQLServerDriverBooleanProperty.LAST_UPDATE_COUNT.getDefaultValue()), false, TRUE_FALSE),
new SQLServerDriverPropertyInfo(SQLServerDriverIntProperty.LOCK_TIMEOUT.toString(), Integer.toString(SQLServerDriverIntProperty.LOCK_TIMEOUT.getDefaultValue()), false, null),
new SQLServerDriverPropertyInfo(SQLServerDriverIntProperty.LOGIN_TIMEOUT.toString(), Integer.toString(SQLServerDriverIntProperty.LOGIN_TIMEOUT.getDefaultValue()), false, null),
new SQLServerDriverPropertyInfo(SQLServerDriverBooleanProperty.MULTI_SUBNET_FAILOVER.toString(), Boolean.toString(SQLServerDriverBooleanProperty.MULTI_SUBNET_FAILOVER.getDefaultValue()), false, TRUE_FALSE),
new SQLServerDriverPropertyInfo(SQLServerDriverIntProperty.PACKET_SIZE.toString(), Integer.toString(SQLServerDriverIntProperty.PACKET_SIZE.getDefaultValue()), false, null),
new SQLServerDriverPropertyInfo(SQLServerDriverStringProperty.PASSWORD.toString(), SQLServerDriverStringProperty.PASSWORD.getDefaultValue(), true, null),
new SQLServerDriverPropertyInfo(SQLServerDriverIntProperty.PORT_NUMBER.toString(), Integer.toString(SQLServerDriverIntProperty.PORT_NUMBER.getDefaultValue()), false, null),
new SQLServerDriverPropertyInfo(SQLServerDriverIntProperty.QUERY_TIMEOUT.toString(), Integer.toString(SQLServerDriverIntProperty.QUERY_TIMEOUT.getDefaultValue()), false, null),
new SQLServerDriverPropertyInfo(SQLServerDriverStringProperty.RESPONSE_BUFFERING.toString(), SQLServerDriverStringProperty.RESPONSE_BUFFERING.getDefaultValue(), false, new String[] {"adaptive", "full"}),
new SQLServerDriverPropertyInfo(SQLServerDriverStringProperty.SELECT_METHOD.toString(), SQLServerDriverStringProperty.SELECT_METHOD.getDefaultValue(), false, new String[] {"direct", "cursor"}),
new SQLServerDriverPropertyInfo(SQLServerDriverBooleanProperty.SEND_STRING_PARAMETERS_AS_UNICODE.toString(), Boolean.toString(SQLServerDriverBooleanProperty.SEND_STRING_PARAMETERS_AS_UNICODE.getDefaultValue()), false, TRUE_FALSE),
new SQLServerDriverPropertyInfo(SQLServerDriverBooleanProperty.SERVER_NAME_AS_ACE.toString(), Boolean.toString(SQLServerDriverBooleanProperty.SERVER_NAME_AS_ACE.getDefaultValue()), false, TRUE_FALSE),
new SQLServerDriverPropertyInfo(SQLServerDriverStringProperty.SERVER_NAME.toString(), SQLServerDriverStringProperty.SERVER_NAME.getDefaultValue(), false, null),
new SQLServerDriverPropertyInfo(SQLServerDriverStringProperty.SERVER_SPN.toString(), SQLServerDriverStringProperty.SERVER_SPN.getDefaultValue(), false, null),
new SQLServerDriverPropertyInfo(SQLServerDriverBooleanProperty.TRANSPARENT_NETWORK_IP_RESOLUTION.toString(), Boolean.toString(SQLServerDriverBooleanProperty.TRANSPARENT_NETWORK_IP_RESOLUTION.getDefaultValue()), false, TRUE_FALSE),
new SQLServerDriverPropertyInfo(SQLServerDriverBooleanProperty.TRUST_SERVER_CERTIFICATE.toString(), Boolean.toString(SQLServerDriverBooleanProperty.TRUST_SERVER_CERTIFICATE.getDefaultValue()), false, TRUE_FALSE),
new SQLServerDriverPropertyInfo(SQLServerDriverStringProperty.TRUST_STORE_TYPE.toString(), SQLServerDriverStringProperty.TRUST_STORE_TYPE.getDefaultValue(), false, null),
new SQLServerDriverPropertyInfo(SQLServerDriverStringProperty.TRUST_STORE.toString(), SQLServerDriverStringProperty.TRUST_STORE.getDefaultValue(), false, null),
new SQLServerDriverPropertyInfo(SQLServerDriverStringProperty.TRUST_STORE_PASSWORD.toString(), SQLServerDriverStringProperty.TRUST_STORE_PASSWORD.getDefaultValue(), false, null),
new SQLServerDriverPropertyInfo(SQLServerDriverBooleanProperty.SEND_TIME_AS_DATETIME.toString(), Boolean.toString(SQLServerDriverBooleanProperty.SEND_TIME_AS_DATETIME.getDefaultValue()), false, TRUE_FALSE),
new SQLServerDriverPropertyInfo(SQLServerDriverStringProperty.USER.toString(), SQLServerDriverStringProperty.USER.getDefaultValue(), true, null),
new SQLServerDriverPropertyInfo(SQLServerDriverStringProperty.WORKSTATION_ID.toString(), SQLServerDriverStringProperty.WORKSTATION_ID.getDefaultValue(), false, null),
new SQLServerDriverPropertyInfo(SQLServerDriverBooleanProperty.XOPEN_STATES.toString(), Boolean.toString(SQLServerDriverBooleanProperty.XOPEN_STATES.getDefaultValue()), false, TRUE_FALSE),
new SQLServerDriverPropertyInfo(SQLServerDriverStringProperty.AUTHENTICATION_SCHEME.toString(), SQLServerDriverStringProperty.AUTHENTICATION_SCHEME.getDefaultValue(), false, new String[] {AuthenticationScheme.javaKerberos.toString(),AuthenticationScheme.nativeAuthentication.toString()}),
new SQLServerDriverPropertyInfo(SQLServerDriverStringProperty.AUTHENTICATION.toString(), SQLServerDriverStringProperty.AUTHENTICATION.getDefaultValue(), false, new String[] {SqlAuthentication.NotSpecified.toString(),SqlAuthentication.SqlPassword.toString(),SqlAuthentication.ActiveDirectoryPassword.toString(),SqlAuthentication.ActiveDirectoryIntegrated.toString()}),
new SQLServerDriverPropertyInfo(SQLServerDriverStringProperty.FIPS_PROVIDER.toString(), SQLServerDriverStringProperty.FIPS_PROVIDER.getDefaultValue(), false, null),
new SQLServerDriverPropertyInfo(SQLServerDriverIntProperty.SOCKET_TIMEOUT.toString(), Integer.toString(SQLServerDriverIntProperty.SOCKET_TIMEOUT.getDefaultValue()), false, null),
new SQLServerDriverPropertyInfo(SQLServerDriverBooleanProperty.FIPS.toString(), Boolean.toString(SQLServerDriverBooleanProperty.FIPS.getDefaultValue()), false, TRUE_FALSE),
};
// Properties that can only be set by using Properties.
// Cannot set in connection string
private static final SQLServerDriverPropertyInfo[] DRIVER_PROPERTIES_PROPERTY_ONLY = {
// default required available choices
// property name value property (if appropriate)
new SQLServerDriverPropertyInfo(SQLServerDriverStringProperty.ACCESS_TOKEN.toString(), SQLServerDriverStringProperty.ACCESS_TOKEN.getDefaultValue(), false, null),
new SQLServerDriverPropertyInfo(SQLServerDriverObjectProperty.GSS_CREDENTIAL.toString(), SQLServerDriverObjectProperty.GSS_CREDENTIAL.getDefaultValue(), false, null),
};
private static final String driverPropertiesSynonyms[][] = {
{"database", SQLServerDriverStringProperty.DATABASE_NAME.toString()},
{"userName",SQLServerDriverStringProperty.USER.toString()},
{"server",SQLServerDriverStringProperty.SERVER_NAME.toString()},
{"port", SQLServerDriverIntProperty.PORT_NUMBER.toString()}
};
static private final AtomicInteger baseID = new AtomicInteger(0); // Unique id generator for each instance (used for logging).
final private int instanceID; // Unique id for this instance.
final private String traceID;
// Returns unique id for each instance.
private static int nextInstanceID() {
return baseID.incrementAndGet();
}
final public String toString() {
return traceID;
}
static final private java.util.logging.Logger loggerExternal = java.util.logging.Logger.getLogger("com.microsoft.sqlserver.jdbc.Driver");
static final private java.util.logging.Logger parentLogger = java.util.logging.Logger.getLogger("com.microsoft.sqlserver.jdbc");
final private String loggingClassName;
String getClassNameLogging() {
return loggingClassName;
}
private final static java.util.logging.Logger drLogger = java.util.logging.Logger
.getLogger("com.microsoft.sqlserver.jdbc.internals.SQLServerDriver");
// Register with the DriverManager
static {
try {
java.sql.DriverManager.registerDriver(new SQLServerDriver());
}
catch (SQLException e) {
e.printStackTrace();
}
}
public SQLServerDriver() {
instanceID = nextInstanceID();
traceID = "SQLServerDriver:" + instanceID;
loggingClassName = "com.microsoft.sqlserver.jdbc." + "SQLServerDriver:" + instanceID;
}
// Helper function used to fixup the case sensitivity, synonyms and remove unknown tokens from the
// properties
static Properties fixupProperties(Properties props) throws SQLServerException {
// assert props !=null
Properties fixedup = new Properties();
Enumeration<?> e = props.keys();
while (e.hasMoreElements()) {
String name = (String) e.nextElement();
String newname = getNormalizedPropertyName(name, drLogger);
if (null == newname) {
newname = getPropertyOnlyName(name, drLogger);
}
if (null != newname) {
String val = props.getProperty(name);
if (null != val) {
// replace with the driver approved name
fixedup.setProperty(newname, val);
}
else if(newname.equalsIgnoreCase("gsscredential") && (props.get(name) instanceof GSSCredential)){
fixedup.put(newname, props.get(name));
}
else {
MessageFormat form = new MessageFormat(SQLServerException.getErrString("R_invalidpropertyValue"));
Object[] msgArgs = {name};
throw new SQLServerException(null, form.format(msgArgs), null, 0, false);
}
}
}
return fixedup;
}
// Helper function used to merge together the property set extracted from the url and the
// user supplied property set passed in by the caller. This function is used by both SQLServerDriver.connect
// and SQLServerDataSource.getConnectionInternal to centralize this property merging code.
static Properties mergeURLAndSuppliedProperties(Properties urlProps,
Properties suppliedProperties) throws SQLServerException {
if (null == suppliedProperties)
return urlProps;
if (suppliedProperties.isEmpty())
return urlProps;
Properties suppliedPropertiesFixed = fixupProperties(suppliedProperties);
// Merge URL properties and supplied properties.
for (int i = 0; i < DRIVER_PROPERTIES.length; i++) {
String sProp = DRIVER_PROPERTIES[i].getName();
String sPropVal = suppliedPropertiesFixed.getProperty(sProp); // supplied properties have precedence
if (null != sPropVal) {
// overwrite the property in urlprops if already exists. supp prop has more precedence
urlProps.put(sProp, sPropVal);
}
}
// Merge URL properties with property-only properties
for (int i = 0; i < DRIVER_PROPERTIES_PROPERTY_ONLY.length; i++) {
String sProp = DRIVER_PROPERTIES_PROPERTY_ONLY[i].getName();
Object oPropVal = suppliedPropertiesFixed.get(sProp); // supplied properties have precedence
if (null != oPropVal) {
// overwrite the property in urlprops if already exists. supp prop has more precedence
urlProps.put(sProp, oPropVal);
}
}
return urlProps;
}
/**
* normalize the property names
*
* @param name
* name to normalize
* @param logger
* @return the normalized property name
*/
static String getNormalizedPropertyName(String name,
Logger logger) {
if (null == name)
return name;
for (int i = 0; i < driverPropertiesSynonyms.length; i++) {
if (driverPropertiesSynonyms[i][0].equalsIgnoreCase(name)) {
return driverPropertiesSynonyms[i][1];
}
}
for (int i = 0; i < DRIVER_PROPERTIES.length; i++) {
if (DRIVER_PROPERTIES[i].getName().equalsIgnoreCase(name)) {
return DRIVER_PROPERTIES[i].getName();
}
}
if (logger.isLoggable(Level.FINER))
logger.finer("Unknown property" + name);
return null;
}
/**
* get property-only names that do not work with connection string
*
* @param name
* to normalize
* @param logger
* @return the normalized property name
*/
static String getPropertyOnlyName(String name,
Logger logger) {
if (null == name)
return name;
for (int i = 0; i < DRIVER_PROPERTIES_PROPERTY_ONLY.length; i++) {
if (DRIVER_PROPERTIES_PROPERTY_ONLY[i].getName().equalsIgnoreCase(name)) {
return DRIVER_PROPERTIES_PROPERTY_ONLY[i].getName();
}
}
if (logger.isLoggable(Level.FINER))
logger.finer("Unknown property" + name);
return null;
}
/* L0 */ public java.sql.Connection connect(String Url,
Properties suppliedProperties) throws SQLServerException {
loggerExternal.entering(getClassNameLogging(), "connect", "Arguments not traced.");
SQLServerConnection result = null;
// Merge connectProperties (from URL) and supplied properties from user.
Properties connectProperties = parseAndMergeProperties(Url, suppliedProperties);
if (connectProperties != null) {
result = new SQLServerConnection(toString());
result.connect(connectProperties, null);
}
loggerExternal.exiting(getClassNameLogging(), "connect", result);
return result;
}
private Properties parseAndMergeProperties(String Url,
Properties suppliedProperties) throws SQLServerException {
if (Url == null) {
throw new SQLServerException(null, SQLServerException.getErrString("R_nullConnection"), null, 0, false);
}
Properties connectProperties = Util.parseUrl(Url, drLogger);
if (connectProperties == null)
return null; // If we are the wrong driver dont throw an exception
// put the user properties into the connect properties
int nTimeout = DriverManager.getLoginTimeout();
if (nTimeout > 0) {
connectProperties.put(SQLServerDriverIntProperty.LOGIN_TIMEOUT.toString(), new Integer(nTimeout).toString());
}
// Merge connectProperties (from URL) and supplied properties from user.
connectProperties = mergeURLAndSuppliedProperties(connectProperties, suppliedProperties);
return connectProperties;
}
/* L0 */ public boolean acceptsURL(String url) throws SQLServerException {
loggerExternal.entering(getClassNameLogging(), "acceptsURL", "Arguments not traced.");
if (null == url) {
throw new SQLServerException(null, SQLServerException.getErrString("R_nullConnection"), null, 0, false);
}
boolean result = false;
try {
result = (Util.parseUrl(url, drLogger) != null);
}
catch (SQLServerException e) {
// ignore the exception from the parse URL failure, if we cant parse the URL we do not accept em
result = false;
}
loggerExternal.exiting(getClassNameLogging(), "acceptsURL", result);
return result;
}
public DriverPropertyInfo[] getPropertyInfo(String Url,
Properties Info) throws SQLServerException {
loggerExternal.entering(getClassNameLogging(), "getPropertyInfo", "Arguments not traced.");
Properties connProperties = parseAndMergeProperties(Url, Info);
// This means we are not the right driver throw an exception.
if (null == connProperties)
throw new SQLServerException(null, SQLServerException.getErrString("R_invalidConnection"), null, 0, false);
DriverPropertyInfo[] properties = getPropertyInfoFromProperties(connProperties);
loggerExternal.exiting(getClassNameLogging(), "getPropertyInfo");
return properties;
}
static final DriverPropertyInfo[] getPropertyInfoFromProperties(Properties props) {
DriverPropertyInfo[] properties = new DriverPropertyInfo[DRIVER_PROPERTIES.length];
for (int i = 0; i < DRIVER_PROPERTIES.length; i++)
properties[i] = DRIVER_PROPERTIES[i].build(props);
return properties;
}
public int getMajorVersion() {
loggerExternal.entering(getClassNameLogging(), "getMajorVersion");
loggerExternal.exiting(getClassNameLogging(), "getMajorVersion", new Integer(SQLJdbcVersion.major));
return SQLJdbcVersion.major;
}
public int getMinorVersion() {
loggerExternal.entering(getClassNameLogging(), "getMinorVersion");
loggerExternal.exiting(getClassNameLogging(), "getMinorVersion", new Integer(SQLJdbcVersion.minor));
return SQLJdbcVersion.minor;
}
public Logger getParentLogger() throws SQLFeatureNotSupportedException {
DriverJDBCVersion.checkSupportsJDBC41();
return parentLogger;
}
/* L0 */ public boolean jdbcCompliant() {
loggerExternal.entering(getClassNameLogging(), "jdbcCompliant");
loggerExternal.exiting(getClassNameLogging(), "jdbcCompliant", Boolean.valueOf(true));
return true;
}
}