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 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
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

package com.microsoft.sqlserver.testframework;

import java.sql.JDBCType;
import java.util.ArrayList;
import java.util.List;

Expand Down Expand Up @@ -71,6 +72,14 @@ SqlType getSqlType() {
return sqlType;
}

/**
*
* @return JDBCType for the column
*/
JDBCType getJdbctype() {
return sqlType.getJdbctype();
}

/**
*
* @param sqlType
Expand Down
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
44 changes: 35 additions & 9 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 @@ -250,17 +252,16 @@ String populateTableSql() {
sb.add(OPEN_BRACKET);
for (int colNum = 0; colNum < totalColumns; colNum++) {

// TODO: add betterway to enclose data
if (JDBCType.CHAR == getColumn(colNum).getSqlType().getJdbctype()
|| 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())
// TODO: consider how to enclose data in case of preparedStatemets
if (passDataAsString(colNum)) {
sb.add("'" + String.valueOf(getColumn(colNum).getRowValue(i)) + "'");
else
}
else if (passDataAsHex(colNum)) {
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 Expand Up @@ -333,4 +334,29 @@ public void addColumn(SqlType sqlType) {
DBColumn getColumn(int index) {
return columns.get(index);
}

/**
*
* @param colNum
* @return <code>true</code> if value can be passed as String for the column
*/
boolean passDataAsString(int colNum){
return (JDBCType.CHAR == getColumn(colNum).getJdbctype()
|| JDBCType.VARCHAR == getColumn(colNum).getJdbctype()
|| JDBCType.NCHAR == getColumn(colNum).getJdbctype()
|| JDBCType.NVARCHAR == getColumn(colNum).getJdbctype()
|| JDBCType.TIMESTAMP == getColumn(colNum).getJdbctype()
|| JDBCType.DATE == getColumn(colNum).getJdbctype()
|| JDBCType.TIME == getColumn(colNum).getJdbctype());
}

/**
*
* @param colNum
* @return <code>true</code> if value can be passed as Hex for the column
*/
boolean passDataAsHex(int colNum){
return (JDBCType.BINARY == getColumn(colNum).getJdbctype()
|| JDBCType.VARBINARY == getColumn(colNum).getJdbctype());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// ---------------------------------------------------------------------------------------------------------------------------------
// 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?

/**
* Contains name, jdbctype, precision, scale for binary data type
*/
public class SqlBinary extends SqlType {

/**
* set JDBCType and precision for SqlBinary
*/
public SqlBinary() {
this("binary", JDBCType.BINARY, 2000);
}

/**
*
* @param name binary or varbinary
* @param jdbctype
* @param precision
*/
SqlBinary(String name, JDBCType jdbctype, int precision) {
super(name, jdbctype, precision, 0, SqlTypeValue.BINARY.minValue, SqlTypeValue.BINARY.maxValue, SqlTypeValue.BINARY.nullValue,
VariableLengthType.Precision);
generatePrecision();
}

/**
* create random data for binary and varbinary column
*/
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,41 @@
// ---------------------------------------------------------------------------------------------------------------------------------
// 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;

/**
* Contains name, jdbctype, precision, scale for varbinary data type
*/
public class SqlVarBinary extends SqlBinary {

/**
* set JDBCType and precision for SqlVarBinary
*/
public SqlVarBinary() {
super("varbinary", JDBCType.VARBINARY, 4000);
}
}