-
Notifications
You must be signed in to change notification settings - Fork 435
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
Fix parameter meta data with whitespace characters #371
Merged
xiangyushawn
merged 4 commits into
microsoft:dev
from
xiangyushawn:newdev-fix-ParameterMetaData-with-whitespace-characters
Jul 21, 2017
Merged
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
145 changes: 145 additions & 0 deletions
145
.../java/com/microsoft/sqlserver/jdbc/parametermetadata/ParameterMetaDataWhiteSpaceTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
/* | ||
* 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.parametermetadata; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
import java.sql.DriverManager; | ||
import java.sql.PreparedStatement; | ||
import java.sql.ResultSet; | ||
import java.sql.SQLException; | ||
import java.sql.Statement; | ||
|
||
import org.junit.jupiter.api.AfterAll; | ||
import org.junit.jupiter.api.BeforeAll; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.platform.runner.JUnitPlatform; | ||
import org.junit.runner.RunWith; | ||
|
||
import com.microsoft.sqlserver.jdbc.SQLServerConnection; | ||
import com.microsoft.sqlserver.testframework.AbstractTest; | ||
import com.microsoft.sqlserver.testframework.Utils; | ||
import com.microsoft.sqlserver.testframework.util.RandomUtil; | ||
|
||
@RunWith(JUnitPlatform.class) | ||
public class ParameterMetaDataWhiteSpaceTest extends AbstractTest { | ||
private static final String tableName = "[" + RandomUtil.getIdentifier("ParameterMetaDataWhiteSpaceTest") + "]"; | ||
|
||
private static Statement stmt = null; | ||
|
||
@BeforeAll | ||
public static void BeforeTests() throws SQLException { | ||
connection = (SQLServerConnection) DriverManager.getConnection(connectionString); | ||
stmt = connection.createStatement(); | ||
createCharTable(); | ||
} | ||
|
||
@AfterAll | ||
public static void dropTables() throws SQLException { | ||
Utils.dropTableIfExists(tableName, stmt); | ||
|
||
if (null != stmt) { | ||
stmt.close(); | ||
} | ||
|
||
if (null != connection) { | ||
connection.close(); | ||
} | ||
} | ||
|
||
private static void createCharTable() throws SQLException { | ||
stmt.execute("Create table " + tableName + " (c1 int)"); | ||
} | ||
|
||
/** | ||
* Test regular simple query | ||
* | ||
* @throws SQLException | ||
*/ | ||
@Test | ||
public void NormalTest() throws SQLException { | ||
testUpdateWithTwoParameters("update " + tableName + " set c1 = ? where c1 = ?"); | ||
testInsertWithOneParameter("insert into " + tableName + " (c1) values (?)"); | ||
} | ||
|
||
/** | ||
* Test query with new line character | ||
* | ||
* @throws SQLException | ||
*/ | ||
@Test | ||
public void NewLineTest() throws SQLException { | ||
testQueriesWithWhiteSpaces("\n"); | ||
} | ||
|
||
/** | ||
* Test query with tab character | ||
* | ||
* @throws SQLException | ||
*/ | ||
@Test | ||
public void TabTest() throws SQLException { | ||
testQueriesWithWhiteSpaces("\t"); | ||
} | ||
|
||
/** | ||
* Test query with form feed character | ||
* | ||
* @throws SQLException | ||
*/ | ||
@Test | ||
public void FormFeedTest() throws SQLException { | ||
testQueriesWithWhiteSpaces("\f"); | ||
} | ||
|
||
private void testQueriesWithWhiteSpaces(String whiteSpace) throws SQLException { | ||
testUpdateWithTwoParameters("update" + whiteSpace + tableName + " set c1 = ? where c1 = ?"); | ||
testUpdateWithTwoParameters("update " + tableName + " set" + whiteSpace + "c1 = ? where c1 = ?"); | ||
testUpdateWithTwoParameters("update " + tableName + " set c1 = ? where" + whiteSpace + "c1 = ?"); | ||
|
||
testInsertWithOneParameter("insert into " + tableName + "(c1) values (?)"); // no space between table name and column name | ||
testInsertWithOneParameter("insert into" + whiteSpace + tableName + " (c1) values (?)"); | ||
} | ||
|
||
private void testUpdateWithTwoParameters(String sql) throws SQLException { | ||
insertTestRow(1); | ||
try (PreparedStatement ps = connection.prepareStatement(sql)) { | ||
ps.setInt(1, 2); | ||
ps.setInt(2, 1); | ||
ps.executeUpdate(); | ||
assertTrue(isIdPresentInTable(2), "Expected ID is not present"); | ||
assertEquals(2, ps.getParameterMetaData().getParameterCount(), "Parameter count mismatch"); | ||
} | ||
} | ||
|
||
private void testInsertWithOneParameter(String sql) throws SQLException { | ||
try (PreparedStatement ps = connection.prepareStatement(sql)) { | ||
ps.setInt(1, 1); | ||
ps.executeUpdate(); | ||
assertTrue(isIdPresentInTable(1), "Insert statement did not work"); | ||
assertEquals(1, ps.getParameterMetaData().getParameterCount(), "Parameter count mismatch"); | ||
} | ||
} | ||
|
||
private void insertTestRow(int id) throws SQLException { | ||
try (PreparedStatement ps = connection.prepareStatement("insert into " + tableName + " (c1) values (?)")) { | ||
ps.setInt(1, id); | ||
ps.executeUpdate(); | ||
} | ||
} | ||
|
||
private boolean isIdPresentInTable(int id) throws SQLException { | ||
try (PreparedStatement ps = connection.prepareStatement("select c1 from " + tableName + " where c1 = ?")) { | ||
ps.setInt(1, id); | ||
try (ResultSet rs = ps.executeQuery()) { | ||
return rs.next(); | ||
} | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we need a null check here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In this case, if we look at the caller of the method, the variable will never be null since it's a token returned from StringTokenizer (instead of returning null, NoSuchElementException will be thrown if there are no more tokens in this tokenizer).
You are right, it's a good practice to always check for null.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, currently
firstToken
cannot benull
, but there might be more caller methods in the future :)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, you are right :) Done