Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[jdbc] Add mssql support for scan operation #1350

Merged
merged 2 commits into from
Sep 8, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions jdbc/src/main/java/com/yahoo/ycsb/db/JdbcDBClient.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright (c) 2010 - 2016 Yahoo! Inc., 2016 YCSB contributors. All rights reserved.
* Copyright (c) 2010 - 2016 Yahoo! Inc., 2016, 2019 YCSB contributors. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you
* may not use this file except in compliance with the License. You
Expand Down Expand Up @@ -82,6 +82,7 @@ public class JdbcDBClient extends DB {
/** The field name prefix in the table. */
public static final String COLUMN_PREFIX = "FIELD";

private boolean sqlserver = false;
private List<Connection> conns;
private boolean initialized = false;
private Properties props;
Expand Down Expand Up @@ -183,6 +184,10 @@ public void init() throws DBException {
String passwd = props.getProperty(CONNECTION_PASSWD, DEFAULT_PROP);
String driver = props.getProperty(DRIVER_CLASS);

if (driver.contains("sqlserver")) {
sqlserver = true;
}

this.jdbcFetchSize = getIntProperty(props, JDBC_FETCH_SIZE);
this.batchSize = getIntProperty(props, DB_BATCH_SIZE);

Expand Down Expand Up @@ -299,7 +304,7 @@ private PreparedStatement createAndCacheUpdateStatement(StatementType updateType

private PreparedStatement createAndCacheScanStatement(StatementType scanType, String key)
throws SQLException {
String select = dbFlavor.createScanStatement(scanType, key);
String select = dbFlavor.createScanStatement(scanType, key, sqlserver);
PreparedStatement scanStatement = getShardConnectionByKey(key).prepareStatement(select);
if (this.jdbcFetchSize > 0) {
scanStatement.setFetchSize(this.jdbcFetchSize);
Expand Down Expand Up @@ -348,8 +353,13 @@ public Status scan(String tableName, String startKey, int recordcount, Set<Strin
if (scanStatement == null) {
scanStatement = createAndCacheScanStatement(type, startKey);
}
scanStatement.setString(1, startKey);
scanStatement.setInt(2, recordcount);
if (sqlserver) {
scanStatement.setInt(1, recordcount);
scanStatement.setString(2, startKey);
} else {
scanStatement.setString(1, startKey);
scanStatement.setInt(2, recordcount);
}
ResultSet resultSet = scanStatement.executeQuery();
for (int i = 0; i < recordcount && resultSet.next(); i++) {
if (result != null && fields != null) {
Expand Down
4 changes: 2 additions & 2 deletions jdbc/src/main/java/com/yahoo/ycsb/db/flavors/DBFlavor.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright (c) 2016 YCSB contributors. All rights reserved.
* Copyright (c) 2016, 2019 YCSB contributors. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you
* may not use this file except in compliance with the License. You
Expand Down Expand Up @@ -65,5 +65,5 @@ public static DBFlavor fromJdbcUrl(String url) {
/**
* Create and return a SQL statement for scanning data.
*/
public abstract String createScanStatement(StatementType scanType, String key);
public abstract String createScanStatement(StatementType scanType, String key, boolean sqlserver);
}
15 changes: 11 additions & 4 deletions jdbc/src/main/java/com/yahoo/ycsb/db/flavors/DefaultDBFlavor.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright (c) 2016 YCSB contributors. All rights reserved.
* Copyright (c) 2016, 2019 YCSB contributors. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you
* may not use this file except in compliance with the License. You
Expand Down Expand Up @@ -84,15 +84,22 @@ public String createUpdateStatement(StatementType updateType, String key) {
}

@Override
public String createScanStatement(StatementType scanType, String key) {
StringBuilder select = new StringBuilder("SELECT * FROM ");
public String createScanStatement(StatementType scanType, String key, boolean sqlserver) {
StringBuilder select;
if (sqlserver) {
select = new StringBuilder("SELECT TOP (?) * FROM ");
} else {
select = new StringBuilder("SELECT * FROM ");
}
select.append(scanType.getTableName());
select.append(" WHERE ");
select.append(JdbcDBClient.PRIMARY_KEY);
select.append(" >= ?");
select.append(" ORDER BY ");
select.append(JdbcDBClient.PRIMARY_KEY);
select.append(" LIMIT ?");
if (!sqlserver) {
select.append(" LIMIT ?");
}
return select.toString();
}
}