Skip to content

Commit 85dd53d

Browse files
Suraiya Hameedv-nisidh
Suraiya Hameed
authored andcommitted
Binary and Varbinary types for framework (#119)
* Binary and Varbinary types for framework * updates to precision to fit row in SQL Server 8K page * modularizing the if block * added documentation
1 parent c49f28e commit 85dd53d

File tree

8 files changed

+161
-16
lines changed

8 files changed

+161
-16
lines changed

src/test/java/com/microsoft/sqlserver/testframework/DBColumn.java

+9
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
package com.microsoft.sqlserver.testframework;
2727

28+
import java.sql.JDBCType;
2829
import java.util.ArrayList;
2930
import java.util.List;
3031

@@ -71,6 +72,14 @@ SqlType getSqlType() {
7172
return sqlType;
7273
}
7374

75+
/**
76+
*
77+
* @return JDBCType for the column
78+
*/
79+
JDBCType getJdbctype() {
80+
return sqlType.getJdbctype();
81+
}
82+
7483
/**
7584
*
7685
* @param sqlType

src/test/java/com/microsoft/sqlserver/testframework/DBSchema.java

+6-1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import java.util.List;
3030

3131
import com.microsoft.sqlserver.testframework.sqlType.SqlBigInt;
32+
import com.microsoft.sqlserver.testframework.sqlType.SqlBinary;
3233
import com.microsoft.sqlserver.testframework.sqlType.SqlBit;
3334
import com.microsoft.sqlserver.testframework.sqlType.SqlChar;
3435
import com.microsoft.sqlserver.testframework.sqlType.SqlDate;
@@ -49,6 +50,7 @@
4950
import com.microsoft.sqlserver.testframework.sqlType.SqlTime;
5051
import com.microsoft.sqlserver.testframework.sqlType.SqlTinyInt;
5152
import com.microsoft.sqlserver.testframework.sqlType.SqlType;
53+
import com.microsoft.sqlserver.testframework.sqlType.SqlVarBinary;
5254
import com.microsoft.sqlserver.testframework.sqlType.SqlVarChar;
5355

5456
/**
@@ -94,8 +96,11 @@ public class DBSchema {
9496
sqlTypes.add(new SqlSmallDateTime());
9597
sqlTypes.add(new SqlDateTime2());
9698
sqlTypes.add(new SqlDateTimeOffset());
97-
// TODO:
9899
// Binary
100+
sqlTypes.add(new SqlBinary());
101+
sqlTypes.add(new SqlVarBinary());
102+
103+
// TODO:
99104
// Other types
100105
}
101106
}

src/test/java/com/microsoft/sqlserver/testframework/DBTable.java

+35-9
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@
3535
import java.util.logging.Level;
3636
import java.util.logging.Logger;
3737

38+
import org.apache.commons.codec.binary.Hex;
39+
3840
import com.microsoft.sqlserver.testframework.sqlType.SqlType;
3941
import com.microsoft.sqlserver.testframework.sqlType.VariableLengthType;
4042
import com.microsoft.sqlserver.testframework.util.RandomUtil;
@@ -250,17 +252,16 @@ String populateTableSql() {
250252
sb.add(OPEN_BRACKET);
251253
for (int colNum = 0; colNum < totalColumns; colNum++) {
252254

253-
// TODO: add betterway to enclose data
254-
if (JDBCType.CHAR == getColumn(colNum).getSqlType().getJdbctype()
255-
|| JDBCType.VARCHAR == getColumn(colNum).getSqlType().getJdbctype()
256-
|| JDBCType.NCHAR == getColumn(colNum).getSqlType().getJdbctype()
257-
|| JDBCType.NVARCHAR == getColumn(colNum).getSqlType().getJdbctype()
258-
|| JDBCType.TIMESTAMP == getColumn(colNum).getSqlType().getJdbctype()
259-
|| JDBCType.DATE == getColumn(colNum).getSqlType().getJdbctype()
260-
|| JDBCType.TIME == getColumn(colNum).getSqlType().getJdbctype())
255+
// TODO: consider how to enclose data in case of preparedStatemets
256+
if (passDataAsString(colNum)) {
261257
sb.add("'" + String.valueOf(getColumn(colNum).getRowValue(i)) + "'");
262-
else
258+
}
259+
else if (passDataAsHex(colNum)) {
260+
sb.add("0X" + Hex.encodeHexString((byte[]) (getColumn(colNum).getRowValue(i))));
261+
}
262+
else {
263263
sb.add(String.valueOf(getColumn(colNum).getRowValue(i)));
264+
}
264265

265266
if (colNum < totalColumns - 1) {
266267
sb.add(COMMA);
@@ -333,4 +334,29 @@ public void addColumn(SqlType sqlType) {
333334
DBColumn getColumn(int index) {
334335
return columns.get(index);
335336
}
337+
338+
/**
339+
*
340+
* @param colNum
341+
* @return <code>true</code> if value can be passed as String for the column
342+
*/
343+
boolean passDataAsString(int colNum){
344+
return (JDBCType.CHAR == getColumn(colNum).getJdbctype()
345+
|| JDBCType.VARCHAR == getColumn(colNum).getJdbctype()
346+
|| JDBCType.NCHAR == getColumn(colNum).getJdbctype()
347+
|| JDBCType.NVARCHAR == getColumn(colNum).getJdbctype()
348+
|| JDBCType.TIMESTAMP == getColumn(colNum).getJdbctype()
349+
|| JDBCType.DATE == getColumn(colNum).getJdbctype()
350+
|| JDBCType.TIME == getColumn(colNum).getJdbctype());
351+
}
352+
353+
/**
354+
*
355+
* @param colNum
356+
* @return <code>true</code> if value can be passed as Hex for the column
357+
*/
358+
boolean passDataAsHex(int colNum){
359+
return (JDBCType.BINARY == getColumn(colNum).getJdbctype()
360+
|| JDBCType.VARBINARY == getColumn(colNum).getJdbctype());
361+
}
336362
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
// ---------------------------------------------------------------------------------------------------------------------------------
2+
// File: SqlBinary.java
3+
//
4+
//
5+
// Microsoft JDBC Driver for SQL Server
6+
// Copyright(c) Microsoft Corporation
7+
// All rights reserved.
8+
// MIT License
9+
// Permission is hereby granted, free of charge, to any person obtaining a copy
10+
// of this software and associated documentation files(the "Software"),
11+
// to deal in the Software without restriction, including without limitation the
12+
// rights to use, copy, modify, merge, publish, distribute, sublicense,
13+
// and / or sell copies of the Software, and to permit persons to whom the
14+
// Software is furnished to do so, subject to the following conditions :
15+
// The above copyright notice and this permission notice shall be included in
16+
// all copies or substantial portions of the Software.
17+
// THE SOFTWARE IS PROVIDED *AS IS*, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
20+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
23+
// IN THE SOFTWARE.
24+
// ---------------------------------------------------------------------------------------------------------------------------------
25+
26+
package com.microsoft.sqlserver.testframework.sqlType;
27+
28+
import java.sql.JDBCType;
29+
import java.util.concurrent.ThreadLocalRandom;
30+
31+
/**
32+
* Contains name, jdbctype, precision, scale for binary data type
33+
*/
34+
public class SqlBinary extends SqlType {
35+
36+
/**
37+
* set JDBCType and precision for SqlBinary
38+
*/
39+
public SqlBinary() {
40+
this("binary", JDBCType.BINARY, 2000);
41+
}
42+
43+
/**
44+
*
45+
* @param name binary or varbinary
46+
* @param jdbctype
47+
* @param precision
48+
*/
49+
SqlBinary(String name, JDBCType jdbctype, int precision) {
50+
super(name, jdbctype, precision, 0, SqlTypeValue.BINARY.minValue, SqlTypeValue.BINARY.maxValue, SqlTypeValue.BINARY.nullValue,
51+
VariableLengthType.Precision);
52+
generatePrecision();
53+
}
54+
55+
/**
56+
* create random data for binary and varbinary column
57+
*/
58+
public Object createdata() {
59+
int dataLength = ThreadLocalRandom.current().nextInt(precision);
60+
byte[] bytes = new byte[dataLength];
61+
ThreadLocalRandom.current().nextBytes(bytes);
62+
return bytes;
63+
}
64+
}

src/test/java/com/microsoft/sqlserver/testframework/sqlType/SqlChar.java

+3-4
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,13 @@
3232
import org.apache.commons.lang3.RandomStringUtils;
3333

3434
/*
35-
* Restricting the size of char/varchar to 4000 and nchar/nvarchar to 2000 to
36-
* accommodate SQL Sever limitation of having of having maximum allowable table
37-
* row size to 8060
35+
* Restricting the size of char/binary to 2000 and nchar to 1000 to accommodate SQL Sever limitation of having of having maximum allowable
36+
* table row size to 8060
3837
*/
3938
public class SqlChar extends SqlType {
4039

4140
public SqlChar() {
42-
this("char", JDBCType.CHAR, 4000);
41+
this("char", JDBCType.CHAR, 2000);
4342
}
4443

4544
SqlChar(String name, JDBCType jdbctype, int precision) {

src/test/java/com/microsoft/sqlserver/testframework/sqlType/SqlNChar.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public class SqlNChar extends SqlChar {
3737
}
3838

3939
public SqlNChar() {
40-
this("nchar", JDBCType.NCHAR, 2000);
40+
this("nchar", JDBCType.NCHAR, 1000);
4141
}
4242

4343
public Object createdata() {

src/test/java/com/microsoft/sqlserver/testframework/sqlType/SqlTypeValue.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,9 @@ enum SqlTypeValue {
4545
FLOAT (new Double(-1.79E308), new Double(+1.79E308), new Double(0)),
4646
REAL (new Float(-3.4E38), new Float(+3.4E38), new Float(0)),
4747
CHAR (null, null, null),// CHAR used by char, nchar, varchar, nvarchar
48+
BINARY (null, null, null),
4849
DATETIME ("17530101T00:00:00.000", "99991231T23:59:59.997", null),
49-
DATE ("00010101", "99991231", null),
50+
DATE ("00010101", "99991231", null),
5051
TIME ("00:00:00.0000000", "23:59:59.9999999", null),
5152
SMALLDATETIME ("19000101T00:00:00", "20790606T23:59:59", null),
5253
DATETIME2 ("00010101T00:00:00.0000000", "99991231T23:59:59.9999999", null),
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// ---------------------------------------------------------------------------------------------------------------------------------
2+
// File: SqlVarBinary.java
3+
//
4+
//
5+
// Microsoft JDBC Driver for SQL Server
6+
// Copyright(c) Microsoft Corporation
7+
// All rights reserved.
8+
// MIT License
9+
// Permission is hereby granted, free of charge, to any person obtaining a copy
10+
// of this software and associated documentation files(the "Software"),
11+
// to deal in the Software without restriction, including without limitation the
12+
// rights to use, copy, modify, merge, publish, distribute, sublicense,
13+
// and / or sell copies of the Software, and to permit persons to whom the
14+
// Software is furnished to do so, subject to the following conditions :
15+
// The above copyright notice and this permission notice shall be included in
16+
// all copies or substantial portions of the Software.
17+
// THE SOFTWARE IS PROVIDED *AS IS*, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
20+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
23+
// IN THE SOFTWARE.
24+
// ---------------------------------------------------------------------------------------------------------------------------------
25+
26+
package com.microsoft.sqlserver.testframework.sqlType;
27+
28+
import java.sql.JDBCType;
29+
30+
/**
31+
* Contains name, jdbctype, precision, scale for varbinary data type
32+
*/
33+
public class SqlVarBinary extends SqlBinary {
34+
35+
/**
36+
* set JDBCType and precision for SqlVarBinary
37+
*/
38+
public SqlVarBinary() {
39+
super("varbinary", JDBCType.VARBINARY, 4000);
40+
}
41+
}

0 commit comments

Comments
 (0)