Skip to content

Commit fc9f1a2

Browse files
KowsarAtzbusbey
authored andcommitted
[jdbc] Add mssql support for scan operation (#1350)
If the JDBC connection string is MsSQL, then change our SQL dialect to something MsSQL can use.
1 parent 2ff658c commit fc9f1a2

File tree

3 files changed

+27
-10
lines changed

3 files changed

+27
-10
lines changed

jdbc/src/main/java/com/yahoo/ycsb/db/JdbcDBClient.java

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright (c) 2010 - 2016 Yahoo! Inc., 2016 YCSB contributors. All rights reserved.
2+
* Copyright (c) 2010 - 2016 Yahoo! Inc., 2016, 2019 YCSB contributors. All rights reserved.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License"); you
55
* may not use this file except in compliance with the License. You
@@ -82,6 +82,7 @@ public class JdbcDBClient extends DB {
8282
/** The field name prefix in the table. */
8383
public static final String COLUMN_PREFIX = "FIELD";
8484

85+
private boolean sqlserver = false;
8586
private List<Connection> conns;
8687
private boolean initialized = false;
8788
private Properties props;
@@ -183,6 +184,10 @@ public void init() throws DBException {
183184
String passwd = props.getProperty(CONNECTION_PASSWD, DEFAULT_PROP);
184185
String driver = props.getProperty(DRIVER_CLASS);
185186

187+
if (driver.contains("sqlserver")) {
188+
sqlserver = true;
189+
}
190+
186191
this.jdbcFetchSize = getIntProperty(props, JDBC_FETCH_SIZE);
187192
this.batchSize = getIntProperty(props, DB_BATCH_SIZE);
188193

@@ -299,7 +304,7 @@ private PreparedStatement createAndCacheUpdateStatement(StatementType updateType
299304

300305
private PreparedStatement createAndCacheScanStatement(StatementType scanType, String key)
301306
throws SQLException {
302-
String select = dbFlavor.createScanStatement(scanType, key);
307+
String select = dbFlavor.createScanStatement(scanType, key, sqlserver);
303308
PreparedStatement scanStatement = getShardConnectionByKey(key).prepareStatement(select);
304309
if (this.jdbcFetchSize > 0) {
305310
scanStatement.setFetchSize(this.jdbcFetchSize);
@@ -348,8 +353,13 @@ public Status scan(String tableName, String startKey, int recordcount, Set<Strin
348353
if (scanStatement == null) {
349354
scanStatement = createAndCacheScanStatement(type, startKey);
350355
}
351-
scanStatement.setString(1, startKey);
352-
scanStatement.setInt(2, recordcount);
356+
if (sqlserver) {
357+
scanStatement.setInt(1, recordcount);
358+
scanStatement.setString(2, startKey);
359+
} else {
360+
scanStatement.setString(1, startKey);
361+
scanStatement.setInt(2, recordcount);
362+
}
353363
ResultSet resultSet = scanStatement.executeQuery();
354364
for (int i = 0; i < recordcount && resultSet.next(); i++) {
355365
if (result != null && fields != null) {

jdbc/src/main/java/com/yahoo/ycsb/db/flavors/DBFlavor.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright (c) 2016 YCSB contributors. All rights reserved.
2+
* Copyright (c) 2016, 2019 YCSB contributors. All rights reserved.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License"); you
55
* may not use this file except in compliance with the License. You
@@ -65,5 +65,5 @@ public static DBFlavor fromJdbcUrl(String url) {
6565
/**
6666
* Create and return a SQL statement for scanning data.
6767
*/
68-
public abstract String createScanStatement(StatementType scanType, String key);
68+
public abstract String createScanStatement(StatementType scanType, String key, boolean sqlserver);
6969
}

jdbc/src/main/java/com/yahoo/ycsb/db/flavors/DefaultDBFlavor.java

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright (c) 2016 YCSB contributors. All rights reserved.
2+
* Copyright (c) 2016, 2019 YCSB contributors. All rights reserved.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License"); you
55
* may not use this file except in compliance with the License. You
@@ -84,15 +84,22 @@ public String createUpdateStatement(StatementType updateType, String key) {
8484
}
8585

8686
@Override
87-
public String createScanStatement(StatementType scanType, String key) {
88-
StringBuilder select = new StringBuilder("SELECT * FROM ");
87+
public String createScanStatement(StatementType scanType, String key, boolean sqlserver) {
88+
StringBuilder select;
89+
if (sqlserver) {
90+
select = new StringBuilder("SELECT TOP (?) * FROM ");
91+
} else {
92+
select = new StringBuilder("SELECT * FROM ");
93+
}
8994
select.append(scanType.getTableName());
9095
select.append(" WHERE ");
9196
select.append(JdbcDBClient.PRIMARY_KEY);
9297
select.append(" >= ?");
9398
select.append(" ORDER BY ");
9499
select.append(JdbcDBClient.PRIMARY_KEY);
95-
select.append(" LIMIT ?");
100+
if (!sqlserver) {
101+
select.append(" LIMIT ?");
102+
}
96103
return select.toString();
97104
}
98105
}

0 commit comments

Comments
 (0)