forked from microsoft/mssql-jdbc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSQLServerMetaData.java
197 lines (179 loc) · 5.63 KB
/
SQLServerMetaData.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
/*
* Microsoft JDBC Driver for SQL Server
*
* Copyright(c) Microsoft Corporation All rights reserved.
*
* This program is made available under the terms of the MIT License. See the LICENSE file in the project root for more information.
*/
package com.microsoft.sqlserver.jdbc;
import java.text.MessageFormat;
/**
*
* This class represents metadata for a column. It is used in the ISQLServerDataRecord interface to pass column metadata to the table-valued
* parameter.
*
*/
public class SQLServerMetaData {
String columnName = null;
int javaSqlType;
int precision = 0;
int scale = 0;
boolean useServerDefault = false;
boolean isUniqueKey = false;
SQLServerSortOrder sortOrder = SQLServerSortOrder.Unspecified;
int sortOrdinal;
static final int defaultSortOrdinal = -1;
/**
* Creates a new SQLServerMetaData
*
* @param columnName
* the name of the column
* @param sqlType
* the SQL type of the column
*/
public SQLServerMetaData(String columnName,
int sqlType) {
this.columnName = columnName;
this.javaSqlType = sqlType;
}
/**
* creates a new SQLServerMetaData
*
* @param columnName
* the name of the column
* @param sqlType
* the SQL type of the column
* @param precision
* the precision of the column
* @param scale
* the scale of the column
*/
public SQLServerMetaData(String columnName,
int sqlType,
int precision,
int scale) {
this.columnName = columnName;
this.javaSqlType = sqlType;
this.precision = precision;
this.scale = scale;
}
/**
* Creates a new SQLServerMetaData
*
* @param columnName
* the name of the column
* @param sqlType
* the sql type of the column
* @param precision
* the precision of the column
* @param scale
* the scale of the column
* @param useServerDefault
* specifies if this column should use the default server value; Default value is false.
* @param isUniqueKey
* indicates if the column in the table-valued parameter is unique; Default value is false.
* @param sortOrder
* indicates the sort order for a column; Default value is SQLServerSortOrder.Unspecified.
* @param sortOrdinal
* specifies ordinal of the sort column; sortOrdinal starts from 0; Default value is -1.
* @throws SQLServerException
* when an error occurs
*/
public SQLServerMetaData(String columnName,
int sqlType,
int precision,
int scale,
boolean useServerDefault,
boolean isUniqueKey,
SQLServerSortOrder sortOrder,
int sortOrdinal) throws SQLServerException {
this.columnName = columnName;
this.javaSqlType = sqlType;
this.precision = precision;
this.scale = scale;
this.useServerDefault = useServerDefault;
this.isUniqueKey = isUniqueKey;
this.sortOrder = sortOrder;
this.sortOrdinal = sortOrdinal;
validateSortOrder();
}
/**
* Initializes a new instance of SQLServerMetaData from another SQLServerMetaData object.
*
* @param sqlServerMetaData
* the object passed to initialize a new instance of SQLServerMetaData
*/
public SQLServerMetaData(SQLServerMetaData sqlServerMetaData) {
this.columnName = sqlServerMetaData.columnName;
this.javaSqlType = sqlServerMetaData.javaSqlType;
this.precision = sqlServerMetaData.precision;
this.scale = sqlServerMetaData.scale;
this.useServerDefault = sqlServerMetaData.useServerDefault;
this.isUniqueKey = sqlServerMetaData.isUniqueKey;
this.sortOrder = sqlServerMetaData.sortOrder;
this.sortOrdinal = sqlServerMetaData.sortOrdinal;
}
/**
*
* @return Retrieves the column name.
*/
public String getColumName() {
return columnName;
}
/**
*
* @return Retrieves the java sql type.
*/
public int getSqlType() {
return javaSqlType;
}
/**
*
* @return retrieves the precision of the type passed to the column.
*/
public int getPrecision() {
return precision;
}
/**
*
* @return retrieves the scale of the type passed to the column.
*/
public int getScale() {
return scale;
}
/**
*
* @return returns whether the column uses the default server value.
*/
public boolean useServerDefault() {
return useServerDefault;
}
/**
*
* @return retrieves the whether the column is unique.
*/
public boolean isUniqueKey() {
return isUniqueKey;
}
/**
*
* @return retrieves the sort order.
*/
public SQLServerSortOrder getSortOrder() {
return sortOrder;
}
/**
*
* @return retrieves the sort ordinal.
*/
public int getSortOrdinal() {
return sortOrdinal;
}
void validateSortOrder() throws SQLServerException {
// should specify both sort order and ordinal, or neither
if ((SQLServerSortOrder.Unspecified == sortOrder) != (defaultSortOrdinal == sortOrdinal)) {
MessageFormat form = new MessageFormat(SQLServerException.getErrString("R_TVPMissingSortOrderOrOrdinal"));
throw new SQLServerException(form.format(new Object[] {sortOrder, sortOrdinal}), null, 0, null);
}
}
}