Skip to content

Commit

Permalink
Fix conflict issues
Browse files Browse the repository at this point in the history
  • Loading branch information
cheenamalhotra committed Sep 22, 2018
1 parent e9b7806 commit 8118d6c
Show file tree
Hide file tree
Showing 10 changed files with 37 additions and 22 deletions.
6 changes: 3 additions & 3 deletions src/test/java/com/microsoft/sqlserver/jdbc/TestUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ public static void dropProcedureIfExists(String procName, java.sql.Statement stm
}

public static void dropDatabaseIfExists(String databaseName, java.sql.Statement stmt) throws SQLException {
stmt.executeUpdate("USE MASTER; IF EXISTS(SELECT * from sys.databases WHERE name='" + databaseName
stmt.executeUpdate("USE MASTER; IF EXISTS(SELECT * from sys.databases WHERE name='" + escapeSingleQuotes(databaseName)
+ "') DROP DATABASE [" + databaseName + "]");
}

Expand All @@ -304,7 +304,7 @@ private static void dropObjectIfExists(String objectName, String objectProperty,
String bracketedObjectName = sb.toString();
String sql = String.format("IF EXISTS " + "( " + "SELECT * from sys.objects "
+ "WHERE object_id = OBJECT_ID(N'%s') AND OBJECTPROPERTY(object_id, N'%s') = 1 " + ") " + "DROP %s %s ",
bracketedObjectName, objectProperty, "IsProcedure".equals(objectProperty) ? "PROCEDURE" : "TABLE",
escapeSingleQuotes(bracketedObjectName), objectProperty, "IsProcedure".equals(objectProperty) ? "PROCEDURE" : "TABLE",
bracketedObjectName);
try {
stmt.executeUpdate(sql);
Expand Down Expand Up @@ -696,7 +696,7 @@ public static boolean supportJDBC43(Connection con) throws SQLException {
* Object name to be passed as String
* @return Converted object name
*/
public static String escapeQuotes(String name) {
public static String escapeSingleQuotes(String name) {
return name.replace("'", "''");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import com.microsoft.sqlserver.testframework.DBResultSet;
import com.microsoft.sqlserver.testframework.DBResultSetTypes;
import com.microsoft.sqlserver.testframework.DBStatement;
import com.microsoft.sqlserver.testframework.util.Util;;


@RunWith(JUnitPlatform.class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import com.microsoft.sqlserver.jdbc.RandomUtil;
import com.microsoft.sqlserver.jdbc.SQLServerDataSource;
import com.microsoft.sqlserver.jdbc.TestUtils;
import com.microsoft.sqlserver.testframework.AbstractTest;
import com.microsoft.sqlserver.testframework.DBTable;

Expand All @@ -26,12 +27,12 @@ public class DBMetadataTest extends AbstractTest {
public void testDatabaseMetaData() throws SQLException {
String functionName = RandomUtil.getIdentifier("proc");
functionName = DBTable.escapeIdentifier(functionName);

SQLServerDataSource ds = new SQLServerDataSource();
ds.setURL(connectionString);

String sqlDropFunction = "if exists (select * from dbo.sysobjects where id = object_id(N'[dbo]." + functionName
+ "')" + "and xtype in (N'FN', N'IF', N'TF'))" + "drop function " + functionName;
String sqlDropFunction = "if exists (select * from dbo.sysobjects where id = object_id(N'[dbo]."
+ TestUtils.escapeSingleQuotes(functionName) + "')" + "and xtype in (N'FN', N'IF', N'TF'))" + "drop function "
+ functionName;
String sqlCreateFunction = "CREATE FUNCTION " + functionName
+ " (@text varchar(8000), @delimiter varchar(20) = ' ') RETURNS @Strings TABLE "
+ "(position int IDENTITY PRIMARY KEY, value varchar(8000)) AS BEGIN INSERT INTO @Strings VALUES ('DDD') RETURN END ";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import com.microsoft.sqlserver.jdbc.RandomUtil;
import com.microsoft.sqlserver.jdbc.SQLServerXADataSource;
import com.microsoft.sqlserver.jdbc.TestResource;
import com.microsoft.sqlserver.jdbc.TestUtils;
import com.microsoft.sqlserver.testframework.AbstractTest;
import com.microsoft.sqlserver.testframework.DBConnection;
import com.microsoft.sqlserver.testframework.DBTable;
Expand Down Expand Up @@ -102,11 +103,11 @@ public void testConnectionPoolConnFunctions() throws SQLException {
String tableName = RandomUtil.getIdentifier("table");
tableName = DBTable.escapeIdentifier(tableName);

String sql1 = "if exists (select * from dbo.sysobjects where name = '" + tableName + "' and type = 'U')\n"
+ "drop table " + tableName + "\n" + "create table " + tableName + "\n" + "(\n"
String sql1 = "if exists (select * from dbo.sysobjects where name = '" + TestUtils.escapeSingleQuotes(tableName)
+ "' and type = 'U')\n" + "drop table " + tableName + "\n" + "create table " + tableName + "\n" + "(\n"
+ "wibble_id int primary key not null,\n" + "counter int null\n" + ");";
String sql2 = "if exists (select * from dbo.sysobjects where name = '" + tableName + "' and type = 'U')\n"
+ "drop table " + tableName + "\n";
String sql2 = "if exists (select * from dbo.sysobjects where name = '" + TestUtils.escapeSingleQuotes(tableName)
+ "' and type = 'U')\n" + "drop table " + tableName + "\n";

SQLServerXADataSource ds = new SQLServerXADataSource();
ds.setURL(connectionString);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,7 @@ public void testNewMethods() {
private SQLServerConnection connect() throws SQLException {
SQLServerConnection connection = null;
try {
connection = PrepUtil.getConnection(getConfiguredProperty("mssql_jdbc_test_connection_properties"));
connection = PrepUtil.getConnection(connectionString);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ public void testParameterMetaData() throws SQLException {
assert (metadata.getScale(2) == 5);
}
} finally {
Utils.dropTableIfExists(tableName, stmt);
TestUtils.dropTableIfExists(tableName, stmt);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -756,8 +756,8 @@ private static DBTable createTable(DBTable table, String[] types, boolean popula
}

private static void dropTables(DBTable table) throws SQLException {
stmt.executeUpdate("if object_id('" + table.getEscapedTableName() + "','U') is not null" + " drop table "
+ table.getEscapedTableName());
stmt.executeUpdate("if object_id('" + TestUtils.escapeSingleQuotes(table.getEscapedTableName()) + "','U') is not null"
+ " drop table " + table.getEscapedTableName());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -630,13 +630,14 @@ private static void selectDateAndTime() throws SQLException {
}

private static void createTablesForCompexQueries() throws SQLException {
stmt.executeUpdate("if object_id('" + nameTable + "','U') is not null" + " drop table " + nameTable);

stmt.executeUpdate(
"if object_id('" + phoneNumberTable + "','U') is not null" + " drop table " + phoneNumberTable);
"if object_id('" + TestUtils.escapeSingleQuotes(nameTable) + "','U') is not null" + " drop table " + nameTable);

stmt.executeUpdate(
"if object_id('" + mergeNameDesTable + "','U') is not null" + " drop table " + mergeNameDesTable);
stmt.executeUpdate("if object_id('" + TestUtils.escapeSingleQuotes(phoneNumberTable) + "','U') is not null"
+ " drop table " + phoneNumberTable);

stmt.executeUpdate("if object_id('" + TestUtils.escapeSingleQuotes(mergeNameDesTable) + "','U') is not null"
+ " drop table " + mergeNameDesTable);

String sql = "create table " + nameTable + " ("
// + "ID int NOT NULL,"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

package com.microsoft.sqlserver.testframework;

import com.microsoft.sqlserver.testframework.util.Util;
import com.microsoft.sqlserver.jdbc.TestUtils;


/**
Expand Down Expand Up @@ -61,7 +61,7 @@ public static void setWrapIdentifier(String wrapIdentifier) {
public String wrapName(String name) {
StringBuffer wrap = new StringBuffer();
wrap.append(getWrapIdentifier());
wrap.append(Util.escapeQuotes(name));
wrap.append(TestUtils.escapeSingleQuotes(name));
wrap.append(getWrapIdentifier());
return wrap.toString();
}
Expand Down
13 changes: 13 additions & 0 deletions src/test/java/com/microsoft/sqlserver/testframework/DBTable.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import java.util.logging.Logger;

import com.microsoft.sqlserver.jdbc.RandomUtil;
import com.microsoft.sqlserver.jdbc.TestUtils;
import com.microsoft.sqlserver.testframework.sqlType.SqlType;
import com.microsoft.sqlserver.testframework.sqlType.VariableLengthType;

Expand All @@ -29,6 +30,7 @@ public class DBTable extends AbstractSQLGenerator {
public static final Logger log = Logger.getLogger("DBTable");
String tableName;
String escapedTableName;
String escapedQuotesTableName;
String tableDefinition;
List<DBColumn> columns;
int totalColumns;
Expand Down Expand Up @@ -71,6 +73,7 @@ public DBTable(boolean autoGenerateSchema, boolean unicode, boolean alternateSch

this.tableName = RandomUtil.getIdentifier("table");
this.escapedTableName = escapeIdentifier(tableName);
this.escapedQuotesTableName = TestUtils.escapeSingleQuotes(escapedTableName);
this.schema = new DBSchema(autoGenerateSchema, alternateSchema);
if (autoGenerateSchema) {
if (unicode)
Expand All @@ -91,6 +94,7 @@ public DBTable(boolean autoGenerateSchema, boolean unicode, boolean alternateSch
private DBTable(DBTable sourceTable) {
this.tableName = RandomUtil.getIdentifier("table");
this.escapedTableName = escapeIdentifier(tableName);
this.escapedQuotesTableName = TestUtils.escapeSingleQuotes(escapedTableName);
this.columns = sourceTable.columns;
this.totalColumns = columns.size();
this.schema = sourceTable.schema;
Expand Down Expand Up @@ -159,6 +163,15 @@ public String getEscapedTableName() {
return escapedTableName;
}

/**
* gets escaped table name of the {@link DBTable} object to be used within single quotes
*
* @return {@link String} escaped table name
*/
public String getEscapedQuotesTableName() {
return escapedQuotesTableName;
}

public String getDefinitionOfColumns() {
return tableDefinition;
}
Expand Down

0 comments on commit 8118d6c

Please sign in to comment.