-
Notifications
You must be signed in to change notification settings - Fork 435
/
Copy pathSQLServerDataTable.java
282 lines (248 loc) · 9.41 KB
/
SQLServerDataTable.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
//---------------------------------------------------------------------------------------------------------------------------------
// File: SQLServerDataTable.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.jdbc;
import java.math.BigDecimal;
import java.text.MessageFormat;
import java.time.OffsetDateTime;
import java.time.OffsetTime;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.UUID;
public final class SQLServerDataTable {
int rowCount = 0;
int columnCount = 0;
Map<Integer, SQLServerDataColumn> columnMetadata = null;
Map<Integer,Object[]> rows = null;
private String tvpName = null;
/**
* The constant in the Java programming language, sometimes referred to as a type code,
* that identifies the type TVP.
*
* @throws SQLServerException when an error occurs
*/
// Name used in CREATE TYPE
public SQLServerDataTable() throws SQLServerException
{
columnMetadata = new LinkedHashMap<Integer, SQLServerDataColumn>();
rows = new HashMap<Integer,Object[]>();
}
/**
* Clears this data table.
*/
public synchronized void clear()
{
rowCount = 0;
columnCount = 0;
columnMetadata.clear();
rows.clear();
}
/**
* Retrieves an iterator on the rows of the data table.
* @return an iterator on the rows of the data table.
*/
public synchronized Iterator<Entry<Integer,Object[]>> getIterator()
{
if ((null != rows) && (null != rows.entrySet()))
{
return rows.entrySet().iterator();
}
return null;
}
/**
* Adds meta data for the specified column
* @param columnName the name of the column
* @param sqlType the sql type of the column
* @throws SQLServerException when an error occurs
*/
public synchronized void addColumnMetadata(String columnName, int sqlType) throws SQLServerException
{
//column names must be unique
Util.checkDuplicateColumnName(columnName, columnMetadata);
columnMetadata.put(columnCount++, new SQLServerDataColumn(columnName,sqlType));
}
/**
* Adds meta data for the specified column
* @param column the name of the column
* @throws SQLServerException when an error occurs
*/
public synchronized void addColumnMetadata(SQLServerDataColumn column) throws SQLServerException
{
//column names must be unique
Util.checkDuplicateColumnName(column.columnName, columnMetadata);
columnMetadata.put(columnCount++, column);
}
/**
* Adds one row of data to the data table.
* @param values values to be added in one row of data to the data table.
* @throws SQLServerException when an error occurs
*/
public synchronized void addRow(Object... values) throws SQLServerException
{
try
{
int columnCount = columnMetadata.size();
if ( (null != values) && values.length > columnCount)
{
MessageFormat form = new MessageFormat(SQLServerException.getErrString("R_moreDataInRowThanColumnInTVP"));
Object[] msgArgs = {};
throw new SQLServerException(null , form.format(msgArgs) , null, 0 , false);
}
Iterator<Entry<Integer, SQLServerDataColumn>> columnsIterator = columnMetadata.entrySet().iterator();
Object[] rowValues = new Object[columnCount];
int currentColumn = 0;
while(columnsIterator.hasNext())
{
Object val = null;
boolean bValueNull ;
int nValueLen ;
if((null != values) && (currentColumn < values.length) && (null != values[currentColumn]))
val = (null == values[currentColumn]) ? null : values[currentColumn] ;
currentColumn++;
Map.Entry<Integer, SQLServerDataColumn> pair = (Map.Entry<Integer, SQLServerDataColumn>)columnsIterator.next();
SQLServerDataColumn currentColumnMetadata = pair.getValue();
JDBCType jdbcType = JDBCType.of(pair.getValue().javaSqlType);
boolean isColumnMetadataUpdated = false;
switch (jdbcType)
{
case BIGINT:
rowValues[pair.getKey()] = (null == val) ? null : Long.parseLong(val.toString());
break;
case BIT:
rowValues[pair.getKey()] = (null == val) ? null : Boolean.parseBoolean(val.toString());
break;
case INTEGER:
rowValues[pair.getKey()] = (null == val) ? null : Integer.parseInt(val.toString());
break;
case SMALLINT:
case TINYINT:
rowValues[pair.getKey()] = (null == val) ? null : Short.parseShort(val.toString());
break;
case DECIMAL:
case NUMERIC:
BigDecimal bd = null;
if(null != val)
{
bd = new BigDecimal(val.toString());
if (bd.scale() > currentColumnMetadata.scale)
{
currentColumnMetadata.scale = bd.scale();
isColumnMetadataUpdated = true;
}
if (bd.precision() > currentColumnMetadata.precision)
{
currentColumnMetadata.precision = bd.precision();
isColumnMetadataUpdated = true;
}
if(isColumnMetadataUpdated)
columnMetadata.put(pair.getKey(), currentColumnMetadata);
}
rowValues[pair.getKey()] = bd;
break;
case DOUBLE:
rowValues[pair.getKey()] = (null == val) ? null : Double.parseDouble(val.toString());
break;
case FLOAT:
case REAL:
rowValues[pair.getKey()] = (null == val) ? null : Float.parseFloat(val.toString());
break;
case TIMESTAMP_WITH_TIMEZONE:
case TIME_WITH_TIMEZONE:
DriverJDBCVersion.checkSupportsJDBC42();
case DATE:
case TIME:
case TIMESTAMP:
case DATETIMEOFFSET:
// Sending temporal types as string. Error from database is thrown if parsing fails
// no need to send precision for temporal types, string literal will never exceed DataTypes.SHORT_VARTYPE_MAX_BYTES
if (null == val)
rowValues[pair.getKey()] = null;
//java.sql.Date, java.sql.Time and java.sql.Timestamp are subclass of java.util.Date
else if (val instanceof java.util.Date)
rowValues[pair.getKey()] = ((java.util.Date) val).toString();
else if(val instanceof microsoft.sql.DateTimeOffset)
rowValues[pair.getKey()] = ((microsoft.sql.DateTimeOffset) val).toString();
else if(val instanceof OffsetDateTime)
rowValues[pair.getKey()] = ((OffsetDateTime) val).toString();
else if(val instanceof OffsetTime)
rowValues[pair.getKey()] = ((OffsetTime) val).toString();
else
rowValues[pair.getKey()] = (null == val) ? null : (String) val;
break;
case BINARY:
case VARBINARY:
bValueNull = (null == val);
nValueLen = bValueNull ? 0 : ((byte[]) val).length;
if (nValueLen > currentColumnMetadata.precision)
{
currentColumnMetadata.precision = nValueLen;
columnMetadata.put(pair.getKey(), currentColumnMetadata);
}
rowValues[pair.getKey()] = (bValueNull) ? null : (byte[]) val;
break;
case CHAR:
if(val instanceof UUID && (val != null))
val = ((UUID)val).toString();
case VARCHAR:
case NCHAR:
case NVARCHAR:
bValueNull = (null == val);
nValueLen = bValueNull ? 0 : (2 * ((String)val).length());
if (nValueLen > currentColumnMetadata.precision)
{
currentColumnMetadata.precision = nValueLen;
columnMetadata.put(pair.getKey(), currentColumnMetadata);
}
rowValues[pair.getKey()] = (bValueNull) ? null : (String) val;
break;
default:
MessageFormat form = new MessageFormat(SQLServerException.getErrString("R_unsupportedDataTypeTVP"));
Object[] msgArgs = {jdbcType};
throw new SQLServerException(null , form.format(msgArgs) , null, 0 , false);
}
}
rows.put(rowCount++, rowValues);
}
catch(NumberFormatException e)
{
throw new SQLServerException(SQLServerException.getErrString("R_TVPInvalidColumnValue"), e);
}
catch(ClassCastException e)
{
throw new SQLServerException(SQLServerException.getErrString("R_TVPInvalidColumnValue"), e);
}
}
public synchronized Map<Integer, SQLServerDataColumn> getColumnMetadata()
{
return columnMetadata;
}
public String getTvpName()
{
return tvpName;
}
/**
* Retrieves the column meta data of this data table.
* @return the column meta data of this data table.
*/
public void setTvpName(String tvpName)
{
this.tvpName = tvpName;
}
}