2 // System.Data.OleDb.OleDbConvert
5 // Konstantin Triger <kostat@mainsoft.com>
6 // Boris Kirzner <borisk@mainsoft.com>
8 // (C) 2005 Mainsoft Corporation (http://www.mainsoft.com)
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
34 using System
.Collections
;
35 using System
.Data
.Common
;
36 using System
.Data
.ProviderBase
;
40 namespace System
.Data
.OleDb
42 internal sealed class OleDbConvert
: DbConvert
46 private static Hashtable _typeNamesMap
;
54 _typeNamesMap
= new Hashtable(30,CaseInsensitiveHashCodeProvider
.DefaultInvariant
, CaseInsensitiveComparer
.DefaultInvariant
);
56 // SqlServer types mapping
57 // _typeNamesMap.Add("numeric(3, 0)1","DBTYPE_I1");
58 _typeNamesMap
.Add("smallint","DBTYPE_I2");
59 _typeNamesMap
.Add("Int","DBTYPE_I4");
60 _typeNamesMap
.Add("bigint","DBTYPE_I8");
61 _typeNamesMap
.Add("tinyint","DBTYPE_UI1");
62 // _typeNamesMap.Add("numeric(5,0)","DBTYPE_UI2");
63 // _typeNamesMap.Add("numeric(10,0)","DBTYPE_UI4");
64 // _typeNamesMap.Add("numeric(20,0)","DBTYPE_UI8");
65 _typeNamesMap
.Add("Float","DBTYPE_R8");
66 _typeNamesMap
.Add("Real","DBTYPE_R4");
67 _typeNamesMap
.Add("numeric","DBTYPE_NUMERIC");
68 _typeNamesMap
.Add("decimal","DBTYPE_NUMERIC");
69 _typeNamesMap
.Add("money","DBTYPE_CY");
70 _typeNamesMap
.Add("smallmoney","DBTYPE_CY");
71 _typeNamesMap
.Add("ntext","DBTYPE_WLONGVARCHAR");
72 _typeNamesMap
.Add("nchar","DBTYPE_WCHAR");
73 _typeNamesMap
.Add("nvarchar","DBTYPE_WVARCHAR");
74 _typeNamesMap
.Add("Bit","DBTYPE_BOOL");
75 // _typeNamesMap.Add("nvarchar(4000)","DBTYPE_VARIANT");
76 _typeNamesMap
.Add("sql_variant","DBTYPE_VARIANT");
77 _typeNamesMap
.Add("uniqueidentifier","DBTYPE_GUID");
78 _typeNamesMap
.Add("image","DBTYPE_LONGVARBINARY");
79 _typeNamesMap
.Add("timestamp","DBTYPE_BINARY");
80 _typeNamesMap
.Add("binary","DBTYPE_BINARY");
81 _typeNamesMap
.Add("varbinary","DBTYPE_VARBINARY");
82 _typeNamesMap
.Add("char","DBTYPE_CHAR");
83 _typeNamesMap
.Add("varchar","DBTYPE_VARCHAR");
84 _typeNamesMap
.Add("text","DBTYPE_LONGVARCHAR");
85 // _typeNamesMap.Add("nchar","DBTYPE_WSTR");
86 // _typeNamesMap.Add("nvarchar","DBTYPE_WSTR");
87 // _typeNamesMap.Add("ntext","DBTYPE_WSTR");
88 // _typeNamesMap.Add("datetime","DBTYPE_DATE");
89 _typeNamesMap
.Add("datetime","DBTYPE_DBTIMESTAMP");
90 _typeNamesMap
.Add("smalldatetime","DBTYPE_DBTIMESTAMP");
91 _typeNamesMap
.Add("Ignored","DBTYPE_BYREF");
94 #endregion //Constructors
98 internal static String
JdbcTypeNameToDbTypeName(string jdbcTypeName
)
100 jdbcTypeName
= jdbcTypeName
.Trim();
101 string dbTypeName
= (string)_typeNamesMap
[jdbcTypeName
];
103 return (dbTypeName
!= null) ? dbTypeName
: jdbcTypeName
;
106 internal static OleDbType
JdbcTypeToOleDbType(int jdbcType
)
108 switch ((JavaSqlTypes
)jdbcType
) {
109 case JavaSqlTypes
.ARRAY
: return OleDbType
.Binary
;
110 case JavaSqlTypes
.BIGINT
: return OleDbType
.BigInt
;
111 case JavaSqlTypes
.BINARY
: return OleDbType
.Binary
;
112 case JavaSqlTypes
.BIT
: return OleDbType
.Boolean
;
113 case JavaSqlTypes
.BLOB
: return OleDbType
.Binary
;
114 case JavaSqlTypes
.BOOLEAN
: return OleDbType
.Boolean
;
115 case JavaSqlTypes
.CHAR
: return OleDbType
.Char
;
116 case JavaSqlTypes
.CLOB
: return OleDbType
.LongVarWChar
;
117 case JavaSqlTypes
.DATALINK
: return OleDbType
.IUnknown
;
118 case JavaSqlTypes
.DATE
: return OleDbType
.DBDate
;
119 case JavaSqlTypes
.DECIMAL
: return OleDbType
.Decimal
;
120 case JavaSqlTypes
.DISTINCT
: return OleDbType
.IUnknown
;
121 case JavaSqlTypes
.DOUBLE
: return OleDbType
.Double
;
122 case JavaSqlTypes
.FLOAT
: return OleDbType
.Double
;
123 case JavaSqlTypes
.INTEGER
: return OleDbType
.Integer
;
124 case JavaSqlTypes
.JAVA_OBJECT
: return OleDbType
.IUnknown
;
125 case JavaSqlTypes
.LONGVARBINARY
: return OleDbType
.LongVarBinary
;
126 case JavaSqlTypes
.LONGVARCHAR
: return OleDbType
.LongVarWChar
;
127 case JavaSqlTypes
.NULL
: return OleDbType
.Empty
;
128 case JavaSqlTypes
.NUMERIC
: return OleDbType
.Numeric
;
130 case JavaSqlTypes
.OTHER
: return OleDbType
.IUnknown
;
131 case JavaSqlTypes
.REAL
: return OleDbType
.Single
;
132 case JavaSqlTypes
.REF
: return OleDbType
.IUnknown
;
133 case JavaSqlTypes
.SMALLINT
: return OleDbType
.SmallInt
;
134 case JavaSqlTypes
.STRUCT
: return OleDbType
.IUnknown
;
135 case JavaSqlTypes
.TIME
: return OleDbType
.DBTime
;
136 case JavaSqlTypes
.TIMESTAMP
: return OleDbType
.DBTimeStamp
;
137 case JavaSqlTypes
.TINYINT
: return OleDbType
.TinyInt
;
138 case JavaSqlTypes
.VARBINARY
: return OleDbType
.VarBinary
;
139 case JavaSqlTypes
.VARCHAR
: return OleDbType
.VarChar
;
143 internal static OleDbType
ValueTypeToOleDbType(Type type
)
145 switch (Type
.GetTypeCode(type
)) {
146 case TypeCode
.Boolean
: return OleDbType
.Boolean
;
147 case TypeCode
.Byte
: return OleDbType
.UnsignedTinyInt
;
148 case TypeCode
.Char
: return OleDbType
.Char
;
149 case TypeCode
.DateTime
: return OleDbType
.Date
;
150 case TypeCode
.DBNull
: return OleDbType
.Empty
;
151 case TypeCode
.Decimal
: return OleDbType
.Decimal
;
152 case TypeCode
.Double
: return OleDbType
.Double
;
153 case TypeCode
.Empty
: return OleDbType
.Empty
;
154 case TypeCode
.Int16
: return OleDbType
.SmallInt
;
155 case TypeCode
.Int32
: return OleDbType
.Integer
;
156 case TypeCode
.Int64
: return OleDbType
.BigInt
;
158 case TypeCode
.Object
: {
159 if (type
.Equals(DbTypes
.TypeOfByteArray
)) return OleDbType
.Binary
;
160 if (type
.Equals(DbTypes
.TypeOfTimespan
)) return OleDbType
.DBTime
;
161 if (type
.Equals(DbTypes
.TypeOfGuid
)) return OleDbType
.Guid
;
164 return ValueTypeToOleDbType (Enum
.GetUnderlyingType (type
));
166 return OleDbType
.IUnknown
;
168 case TypeCode
.SByte
: return OleDbType
.TinyInt
;
169 case TypeCode
.Single
: return OleDbType
.Single
;
170 case TypeCode
.String
: return OleDbType
.VarWChar
;
171 case TypeCode
.UInt16
: return OleDbType
.UnsignedSmallInt
;
172 case TypeCode
.UInt32
: return OleDbType
.UnsignedInt
;
173 case TypeCode
.UInt64
: return OleDbType
.UnsignedBigInt
;
177 internal static Type
OleDbTypeToValueType(OleDbType oleDbType
)
180 case OleDbType
.BigInt
: return DbTypes
.TypeOfInt64
;// typeof(long);
181 case OleDbType
.Binary
: return DbTypes
.TypeOfByteArray
;
182 case OleDbType
.Boolean
: return DbTypes
.TypeOfBoolean
;
183 case OleDbType
.BSTR
: return DbTypes
.TypeOfString
;
184 case OleDbType
.Char
: return DbTypes
.TypeOfString
;
185 case OleDbType
.Currency
: return DbTypes
.TypeOfDecimal
;
186 case OleDbType
.Date
: return DbTypes
.TypeOfDateTime
;
187 case OleDbType
.DBDate
: return DbTypes
.TypeOfDateTime
;
188 case OleDbType
.DBTime
: return DbTypes
.TypeOfTimespan
;
189 case OleDbType
.DBTimeStamp
: return DbTypes
.TypeOfDateTime
;
190 case OleDbType
.Decimal
: return DbTypes
.TypeOfDecimal
;
191 case OleDbType
.Double
: return DbTypes
.TypeOfDouble
;
192 case OleDbType
.Empty
: return null; //typeof(DBNull);
193 case OleDbType
.Error
: return typeof(Exception
);
194 case OleDbType
.Filetime
: return DbTypes
.TypeOfDateTime
;
195 case OleDbType
.Guid
: return DbTypes
.TypeOfGuid
;
196 case OleDbType
.IDispatch
: return DbTypes
.TypeOfObject
;
197 case OleDbType
.Integer
: return DbTypes
.TypeOfInt32
;
198 case OleDbType
.IUnknown
: return DbTypes
.TypeOfObject
;
199 case OleDbType
.LongVarBinary
: return DbTypes
.TypeOfByteArray
;
200 case OleDbType
.LongVarChar
: return DbTypes
.TypeOfString
;
201 case OleDbType
.LongVarWChar
: return DbTypes
.TypeOfString
;
202 case OleDbType
.Numeric
: return DbTypes
.TypeOfDecimal
;
203 case OleDbType
.PropVariant
: return DbTypes
.TypeOfObject
;
204 case OleDbType
.Single
: return DbTypes
.TypeOfFloat
;
205 case OleDbType
.SmallInt
: return DbTypes
.TypeOfInt16
;
206 case OleDbType
.TinyInt
: return DbTypes
.TypeOfSByte
;
207 case OleDbType
.UnsignedBigInt
: return DbTypes
.TypeOfUInt64
;
208 case OleDbType
.UnsignedInt
: return DbTypes
.TypeOfUInt32
;
209 case OleDbType
.UnsignedSmallInt
: return DbTypes
.TypeOfUInt16
;
210 case OleDbType
.UnsignedTinyInt
: return DbTypes
.TypeOfByte
;
211 case OleDbType
.VarBinary
: return DbTypes
.TypeOfByteArray
;
212 case OleDbType
.VarChar
: return DbTypes
.TypeOfString
;
213 case OleDbType
.Variant
: return DbTypes
.TypeOfObject
;
214 case OleDbType
.VarNumeric
: return DbTypes
.TypeOfDecimal
;
215 case OleDbType
.VarWChar
: return DbTypes
.TypeOfString
;
216 case OleDbType
.WChar
: return DbTypes
.TypeOfString
;
217 default : return DbTypes
.TypeOfObject
;
221 internal static OleDbType
DbTypeToOleDbType(DbType dbType
)
224 case DbType
.AnsiString
: return OleDbType
.VarChar
;
225 case DbType
.Binary
: return OleDbType
.VarBinary
;
226 case DbType
.Byte
: return OleDbType
.UnsignedTinyInt
;
227 case DbType
.Boolean
: return OleDbType
.Boolean
;
228 case DbType
.Currency
: return OleDbType
.Currency
;
229 case DbType
.Date
: return OleDbType
.DBDate
;
230 case DbType
.DateTime
: return OleDbType
.DBTimeStamp
;
231 case DbType
.Decimal
: return OleDbType
.Decimal
;
232 case DbType
.Double
: return OleDbType
.Double
;
233 case DbType
.Guid
: return OleDbType
.Guid
;
234 case DbType
.Int16
: return OleDbType
.SmallInt
;
235 case DbType
.Int32
: return OleDbType
.Integer
;
236 case DbType
.Int64
: return OleDbType
.BigInt
;
237 case DbType
.Object
: return OleDbType
.Variant
;
238 case DbType
.SByte
: return OleDbType
.TinyInt
;
239 case DbType
.Single
: return OleDbType
.Single
;
240 case DbType
.String
: return OleDbType
.VarWChar
;
241 case DbType
.Time
: return OleDbType
.DBTime
;
242 case DbType
.UInt16
: return OleDbType
.UnsignedSmallInt
;
243 case DbType
.UInt32
: return OleDbType
.UnsignedInt
;
244 case DbType
.UInt64
: return OleDbType
.UnsignedBigInt
;
245 case DbType
.VarNumeric
: return OleDbType
.VarNumeric
;
246 case DbType
.AnsiStringFixedLength
: return OleDbType
.Char
;
247 case DbType
.StringFixedLength
: return OleDbType
.WChar
;
248 default : throw ExceptionHelper
.InvalidDbType((int)dbType
);
252 internal static DbType
OleDbTypeToDbType(OleDbType oleDbType
)
255 case OleDbType
.Empty
: return DbType
.Object
;
256 case OleDbType
.SmallInt
: return DbType
.Int16
;
257 case OleDbType
.Integer
: return DbType
.Int32
;
258 case OleDbType
.Single
: return DbType
.Single
;
259 case OleDbType
.Double
: return DbType
.Double
;
260 case OleDbType
.Currency
: return DbType
.Currency
;
261 case OleDbType
.Date
: return DbType
.DateTime
;
262 case OleDbType
.BSTR
: return DbType
.String
;
263 case OleDbType
.IDispatch
: return DbType
.Object
;
264 case OleDbType
.Error
: return DbType
.Object
;
265 case OleDbType
.Boolean
: return DbType
.Boolean
;
266 case OleDbType
.Variant
: return DbType
.Object
;
267 case OleDbType
.IUnknown
: return DbType
.Object
;
268 case OleDbType
.Decimal
: return DbType
.Decimal
;
269 case OleDbType
.TinyInt
: return DbType
.SByte
;
270 case OleDbType
.UnsignedTinyInt
: return DbType
.Byte
;
271 case OleDbType
.UnsignedSmallInt
: return DbType
.UInt16
;
272 case OleDbType
.UnsignedInt
: return DbType
.UInt32
;
273 case OleDbType
.BigInt
: return DbType
.Int64
;
274 case OleDbType
.UnsignedBigInt
: return DbType
.UInt64
;
275 case OleDbType
.Filetime
: return DbType
.DateTime
;
276 case OleDbType
.Guid
: return DbType
.Guid
;
277 case OleDbType
.Binary
: return DbType
.Binary
;
278 case OleDbType
.Char
: return DbType
.AnsiStringFixedLength
;
279 case OleDbType
.WChar
: return DbType
.StringFixedLength
;
280 case OleDbType
.Numeric
: return DbType
.Decimal
;
281 case OleDbType
.DBDate
: return DbType
.Date
;
282 case OleDbType
.DBTime
: return DbType
.Time
;
283 case OleDbType
.DBTimeStamp
: return DbType
.DateTime
;
284 case OleDbType
.PropVariant
: return DbType
.Object
;
285 case OleDbType
.VarNumeric
: return DbType
.VarNumeric
;
286 case OleDbType
.VarChar
: return DbType
.AnsiString
;
287 case OleDbType
.LongVarChar
: return DbType
.AnsiString
;
288 case OleDbType
.VarWChar
: return DbType
.String
;
289 case OleDbType
.LongVarWChar
: return DbType
.String
;
290 case OleDbType
.VarBinary
: return DbType
.Binary
;
291 case OleDbType
.LongVarBinary
: return DbType
.Binary
;
292 default : throw ExceptionHelper
.InvalidOleDbType((int)oleDbType
);
296 internal static int OleDbTypeToJdbcType(OleDbType oleDbType
)
299 case OleDbType
.BigInt
: return Types
.BIGINT
;
300 case OleDbType
.Binary
: return Types
.BINARY
;
301 case OleDbType
.Boolean
: return Types
.BIT
;
302 case OleDbType
.BSTR
: return Types
.VARCHAR
;
303 case OleDbType
.Char
: return Types
.CHAR
;
304 case OleDbType
.Currency
: return Types
.DECIMAL
;
305 case OleDbType
.Date
: return Types
.TIMESTAMP
;
306 case OleDbType
.DBDate
: return Types
.DATE
;
307 case OleDbType
.DBTime
: return Types
.TIME
;
308 case OleDbType
.DBTimeStamp
: return Types
.TIMESTAMP
;
309 case OleDbType
.Decimal
: return Types
.DECIMAL
;
310 case OleDbType
.Double
: return Types
.DOUBLE
;
311 case OleDbType
.Empty
: return Types
.NULL
;
312 case OleDbType
.Error
: return Types
.OTHER
;
313 case OleDbType
.Filetime
: return Types
.TIMESTAMP
;
314 case OleDbType
.Guid
: return Types
.CHAR
;
315 case OleDbType
.IDispatch
: return Types
.OTHER
; //throw new ArgumentException("The " + oleDbType + " OleDbType value is not supported.");
316 case OleDbType
.Integer
: return Types
.INTEGER
;
317 case OleDbType
.IUnknown
: return Types
.OTHER
; //throw new ArgumentException("The " + oleDbType + " OleDbType value is not supported.");
318 case OleDbType
.LongVarBinary
: return Types
.LONGVARBINARY
;
319 case OleDbType
.LongVarChar
: return Types
.LONGVARCHAR
;
320 case OleDbType
.LongVarWChar
: return Types
.LONGVARCHAR
;
321 case OleDbType
.Numeric
: return Types
.NUMERIC
;
322 case OleDbType
.PropVariant
: return Types
.OTHER
;
323 case OleDbType
.Single
: return Types
.FLOAT
;
324 case OleDbType
.SmallInt
: return Types
.SMALLINT
;
325 case OleDbType
.TinyInt
: return Types
.TINYINT
;
326 case OleDbType
.UnsignedBigInt
: return Types
.BIGINT
;
327 case OleDbType
.UnsignedInt
: return Types
.INTEGER
;
328 case OleDbType
.UnsignedSmallInt
: return Types
.SMALLINT
;
329 case OleDbType
.UnsignedTinyInt
: return Types
.TINYINT
;
330 case OleDbType
.VarBinary
: return Types
.VARBINARY
;
331 case OleDbType
.VarChar
: return Types
.VARCHAR
;
332 case OleDbType
.Variant
: return Types
.VARCHAR
;
333 case OleDbType
.VarNumeric
: return Types
.DECIMAL
;
334 case OleDbType
.VarWChar
: return Types
.VARCHAR
;
335 case OleDbType
.WChar
: return Types
.VARCHAR
;
336 default : throw ExceptionHelper
.InvalidOleDbType((int)oleDbType
);
339 #endregion // Methods