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

Binary and Varbinary types for framework #119

Merged
merged 4 commits into from
Jan 18, 2017
Merged
Show file tree
Hide file tree
Changes from 2 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
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import java.util.List;

import com.microsoft.sqlserver.testframework.sqlType.SqlBigInt;
import com.microsoft.sqlserver.testframework.sqlType.SqlBinary;
import com.microsoft.sqlserver.testframework.sqlType.SqlBit;
import com.microsoft.sqlserver.testframework.sqlType.SqlChar;
import com.microsoft.sqlserver.testframework.sqlType.SqlDate;
Expand All @@ -49,6 +50,7 @@
import com.microsoft.sqlserver.testframework.sqlType.SqlTime;
import com.microsoft.sqlserver.testframework.sqlType.SqlTinyInt;
import com.microsoft.sqlserver.testframework.sqlType.SqlType;
import com.microsoft.sqlserver.testframework.sqlType.SqlVarBinary;
import com.microsoft.sqlserver.testframework.sqlType.SqlVarChar;

/**
Expand Down Expand Up @@ -94,8 +96,11 @@ public class DBSchema {
sqlTypes.add(new SqlSmallDateTime());
sqlTypes.add(new SqlDateTime2());
sqlTypes.add(new SqlDateTimeOffset());
// TODO:
// Binary
sqlTypes.add(new SqlBinary());
sqlTypes.add(new SqlVarBinary());

// TODO:
// Other types
}
}
Expand Down
14 changes: 11 additions & 3 deletions src/test/java/com/microsoft/sqlserver/testframework/DBTable.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
import java.util.logging.Level;
import java.util.logging.Logger;

import org.apache.commons.codec.binary.Hex;

import com.microsoft.sqlserver.testframework.sqlType.SqlType;
import com.microsoft.sqlserver.testframework.sqlType.VariableLengthType;
import com.microsoft.sqlserver.testframework.util.RandomUtil;
Expand Down Expand Up @@ -251,16 +253,22 @@ String populateTableSql() {
for (int colNum = 0; colNum < totalColumns; colNum++) {

// TODO: add betterway to enclose data
if (JDBCType.CHAR == getColumn(colNum).getSqlType().getJdbctype()
if (JDBCType.CHAR == getColumn(colNum).getSqlType().getJdbctype()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion: Can we have one utility method to get JDBCType

public JDBCType getJDBCType(DBColumn column) {
 return column.getSqlType().getJdbcType();
}
```

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ya, that sounds good.

|| JDBCType.VARCHAR == getColumn(colNum).getSqlType().getJdbctype()
|| JDBCType.NCHAR == getColumn(colNum).getSqlType().getJdbctype()
|| JDBCType.NVARCHAR == getColumn(colNum).getSqlType().getJdbctype()
|| JDBCType.TIMESTAMP == getColumn(colNum).getSqlType().getJdbctype()
|| JDBCType.DATE == getColumn(colNum).getSqlType().getJdbctype()
|| JDBCType.TIME == getColumn(colNum).getSqlType().getJdbctype())
|| JDBCType.TIME == getColumn(colNum).getSqlType().getJdbctype()) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SUGGESTION: In order to reduce complexity it is better to create new method having all conditions and call that method in your if condition.
If method is reusable then you can move this method to Util class. Otherwise create this helper method in same class.

sb.add("'" + String.valueOf(getColumn(colNum).getRowValue(i)) + "'");
else
}
else if (JDBCType.BINARY == getColumn(colNum).getSqlType().getJdbctype()
|| JDBCType.VARBINARY == getColumn(colNum).getSqlType().getJdbctype()) {
sb.add("0X" + Hex.encodeHexString((byte[]) (getColumn(colNum).getRowValue(i))));
}
else {
sb.add(String.valueOf(getColumn(colNum).getRowValue(i)));
}

if (colNum < totalColumns - 1) {
sb.add(COMMA);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// ---------------------------------------------------------------------------------------------------------------------------------
// File: SqlBinary.java
//
//
// Microsoft JDBC Driver for SQL Server
// Copyright(c) Microsoft Corporation
// All rights reserved.
// MIT License
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files(the "Software"),
// to deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense,
// and / or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions :
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED *AS IS*, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.
// ---------------------------------------------------------------------------------------------------------------------------------

package com.microsoft.sqlserver.testframework.sqlType;

import java.sql.JDBCType;
import java.util.concurrent.ThreadLocalRandom;

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add appropriate java docs?

public class SqlBinary extends SqlType {

public SqlBinary() {
this("binary", JDBCType.BINARY, 2000);
}

SqlBinary(String name, JDBCType jdbctype, int precision) {
super(name, jdbctype, precision, 0, SqlTypeValue.BINARY.minValue, SqlTypeValue.BINARY.maxValue, SqlTypeValue.BINARY.nullValue,
VariableLengthType.Precision);
generatePrecision();
}

public Object createdata() {
int dataLength = ThreadLocalRandom.current().nextInt(precision);
byte[] bytes = new byte[dataLength];
ThreadLocalRandom.current().nextBytes(bytes);
return bytes;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,13 @@
import org.apache.commons.lang3.RandomStringUtils;

/*
* Restricting the size of char/varchar to 4000 and nchar/nvarchar to 2000 to
* accommodate SQL Sever limitation of having of having maximum allowable table
* row size to 8060
* Restricting the size of char/binary to 2000 and nchar to 1000 to accommodate SQL Sever limitation of having of having maximum allowable
* table row size to 8060
*/
public class SqlChar extends SqlType {

public SqlChar() {
this("char", JDBCType.CHAR, 4000);
this("char", JDBCType.CHAR, 2000);
}

SqlChar(String name, JDBCType jdbctype, int precision) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public class SqlNChar extends SqlChar {
}

public SqlNChar() {
this("nchar", JDBCType.NCHAR, 2000);
this("nchar", JDBCType.NCHAR, 1000);
}

public Object createdata() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,9 @@ enum SqlTypeValue {
FLOAT (new Double(-1.79E308), new Double(+1.79E308), new Double(0)),
REAL (new Float(-3.4E38), new Float(+3.4E38), new Float(0)),
CHAR (null, null, null),// CHAR used by char, nchar, varchar, nvarchar
BINARY (null, null, null),
DATETIME ("17530101T00:00:00.000", "99991231T23:59:59.997", null),
DATE ("00010101", "99991231", null),
DATE ("00010101", "99991231", null),
TIME ("00:00:00.0000000", "23:59:59.9999999", null),
SMALLDATETIME ("19000101T00:00:00", "20790606T23:59:59", null),
DATETIME2 ("00010101T00:00:00.0000000", "99991231T23:59:59.9999999", null),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// ---------------------------------------------------------------------------------------------------------------------------------
// File: SqlVarBinary.java
//
//
// Microsoft JDBC Driver for SQL Server
// Copyright(c) Microsoft Corporation
// All rights reserved.
// MIT License
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files(the "Software"),
// to deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense,
// and / or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions :
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED *AS IS*, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.
// ---------------------------------------------------------------------------------------------------------------------------------

package com.microsoft.sqlserver.testframework.sqlType;

import java.sql.JDBCType;

public class SqlVarBinary extends SqlBinary {

public SqlVarBinary() {
super("varbinary", JDBCType.VARBINARY, 4000);
}
}