2 // System.Data.SqlClient.SqlConvert
6 // Konstantin Triger <kostat@mainsoft.com>
7 // Boris Kirzner <borisk@mainsoft.com>
9 // (C) 2005 Mainsoft Corporation (http://www.mainsoft.com)
13 // Permission is hereby granted, free of charge, to any person obtaining
14 // a copy of this software and associated documentation files (the
15 // "Software"), to deal in the Software without restriction, including
16 // without limitation the rights to use, copy, modify, merge, publish,
17 // distribute, sublicense, and/or sell copies of the Software, and to
18 // permit persons to whom the Software is furnished to do so, subject to
19 // the following conditions:
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
34 using System
.Data
.Common
;
35 using System
.Data
.ProviderBase
;
39 namespace System
.Data
.SqlClient
41 internal sealed class SqlConvert
: DbConvert
45 internal static String
JdbcTypeNameToDbTypeName(string jdbcTypeName
)
47 return jdbcTypeName
.Trim();
50 internal static SqlDbType
JdbcTypeToSqlDbType(int jdbcType
)
52 // FIXME : other java.sql.Type
54 if(Types
.BIGINT
== jdbcType
) return SqlDbType
.BigInt
;
55 if(Types
.BINARY
== jdbcType
) return SqlDbType
.Binary
;
56 if(Types
.BIT
== jdbcType
) return SqlDbType
.Bit
;
57 if(Types
.BLOB
== jdbcType
) return SqlDbType
.Binary
;
59 if(Types
.CHAR
== jdbcType
) return SqlDbType
.Char
;
60 if(Types
.CLOB
== jdbcType
) return SqlDbType
.Binary
;
61 if(Types
.DATE
== jdbcType
) return SqlDbType
.DateTime
;
62 if(Types
.DECIMAL
== jdbcType
) return SqlDbType
.Decimal
;
64 if(Types
.DOUBLE
== jdbcType
) return SqlDbType
.Float
;
65 if(Types
.FLOAT
== jdbcType
) return SqlDbType
.Float
;
66 if(Types
.INTEGER
== jdbcType
) return SqlDbType
.Int
;
68 if(Types
.LONGVARBINARY
== jdbcType
) return SqlDbType
.Image
;
69 if(Types
.LONGVARCHAR
== jdbcType
) return SqlDbType
.Text
;
71 if(Types
.NUMERIC
== jdbcType
) return SqlDbType
.Decimal
;
72 if(Types
.REAL
== jdbcType
) return SqlDbType
.Real
;
74 if(Types
.SMALLINT
== jdbcType
) return SqlDbType
.SmallInt
;
76 if(Types
.TIME
== jdbcType
) return SqlDbType
.DateTime
;
77 if(Types
.TIMESTAMP
== jdbcType
) return SqlDbType
.DateTime
;
78 if(Types
.TINYINT
== jdbcType
) return SqlDbType
.TinyInt
;
79 if(Types
.VARBINARY
== jdbcType
) return SqlDbType
.VarBinary
;
80 if(Types
.VARCHAR
== jdbcType
) return SqlDbType
.NVarChar
;
81 return SqlDbType
.Variant
;
84 internal static SqlDbType
ValueTypeToSqlDbType(Type type
)
86 switch (Type
.GetTypeCode(type
)) {
87 case TypeCode
.Boolean
: return SqlDbType
.Bit
;
88 case TypeCode
.Byte
: return SqlDbType
.TinyInt
;
89 case TypeCode
.Char
: return SqlDbType
.Char
;
90 case TypeCode
.DateTime
: return SqlDbType
.DateTime
;
91 case TypeCode
.DBNull
: return SqlDbType
.Variant
;
92 case TypeCode
.Decimal
: return SqlDbType
.Decimal
;
93 case TypeCode
.Double
: return SqlDbType
.Float
;
94 case TypeCode
.Empty
: return SqlDbType
.Variant
;
95 case TypeCode
.Int16
: return SqlDbType
.SmallInt
;
96 case TypeCode
.Int32
: return SqlDbType
.Int
;
97 case TypeCode
.Int64
: return SqlDbType
.BigInt
;
99 case TypeCode
.Object
: {
100 if (type
.Equals(DbTypes
.TypeOfByteArray
)) return SqlDbType
.VarBinary
;
101 //if (type.Equals(DbTypes.TypeOfTimespan)) return OleDbType.DBTime;
102 if (type
.Equals(DbTypes
.TypeOfGuid
)) return SqlDbType
.UniqueIdentifier
;
105 return ValueTypeToSqlDbType (Enum
.GetUnderlyingType (type
));
107 return SqlDbType
.Variant
;
109 case TypeCode
.SByte
: return SqlDbType
.TinyInt
;
110 case TypeCode
.Single
: return SqlDbType
.Float
;
111 case TypeCode
.String
: return SqlDbType
.NVarChar
;
112 case TypeCode
.UInt16
: return SqlDbType
.SmallInt
;
113 case TypeCode
.UInt32
: return SqlDbType
.Int
;
114 case TypeCode
.UInt64
: return SqlDbType
.BigInt
;
118 internal static Type
SqlDbTypeToValueType(SqlDbType sqlDbType
)
121 case SqlDbType
.BigInt
: return typeof(long);
122 case SqlDbType
.Binary
: return typeof(byte[]);
123 case SqlDbType
.Bit
: return typeof(bool);
124 case SqlDbType
.Char
: return typeof(string);
125 case SqlDbType
.DateTime
: return typeof(DateTime
);
126 case SqlDbType
.Decimal
: return typeof(decimal);
127 case SqlDbType
.Float
: return typeof(double);
128 case SqlDbType
.Image
: return typeof(byte[]);
129 case SqlDbType
.Int
: return typeof(int);
130 case SqlDbType
.Money
: return typeof(decimal);
131 case SqlDbType
.NChar
: return typeof(string);
132 case SqlDbType
.NText
: return typeof(string);
133 case SqlDbType
.NVarChar
: return typeof(string);
134 case SqlDbType
.Real
: return typeof(Single
);
135 case SqlDbType
.UniqueIdentifier
: return typeof(Guid
);
136 case SqlDbType
.SmallDateTime
: return typeof(DateTime
);
137 case SqlDbType
.SmallInt
: return typeof(Int16
);
138 case SqlDbType
.SmallMoney
: return typeof(decimal);
139 case SqlDbType
.Text
: return typeof(string);
140 case SqlDbType
.Timestamp
: return typeof(byte[]);
141 case SqlDbType
.TinyInt
: return typeof(byte);
142 case SqlDbType
.VarBinary
: return typeof(byte[]);
143 case SqlDbType
.VarChar
: return typeof(string);
144 case SqlDbType
.Variant
: return typeof(object);
145 default : throw ExceptionHelper
.InvalidSqlDbType((int)sqlDbType
);
149 internal static SqlDbType
DbTypeToSqlDbType(DbType dbType
)
152 case DbType
.AnsiString
: return SqlDbType
.VarChar
;
153 case DbType
.Binary
: return SqlDbType
.VarBinary
;
154 case DbType
.Byte
: return SqlDbType
.TinyInt
;
155 case DbType
.Boolean
: return SqlDbType
.Bit
;
156 case DbType
.Currency
: return SqlDbType
.Money
;
157 case DbType
.Date
: return SqlDbType
.DateTime
;
158 case DbType
.DateTime
: return SqlDbType
.DateTime
;
159 case DbType
.Decimal
: return SqlDbType
.Decimal
;
160 case DbType
.Double
: return SqlDbType
.Float
;
161 case DbType
.Guid
: return SqlDbType
.UniqueIdentifier
;
162 case DbType
.Int16
: return SqlDbType
.SmallInt
;
163 case DbType
.Int32
: return SqlDbType
.Int
;
164 case DbType
.Int64
: return SqlDbType
.BigInt
;
165 case DbType
.Object
: return SqlDbType
.Variant
;
166 case DbType
.SByte
: throw ExceptionHelper
.UnknownDataType(dbType
.ToString(),"SqlDbType");
167 case DbType
.Single
: return SqlDbType
.Real
;
168 case DbType
.String
: return SqlDbType
.NVarChar
;
169 case DbType
.UInt16
: throw ExceptionHelper
.UnknownDataType(dbType
.ToString(),"SqlDbType");
170 case DbType
.UInt32
: throw ExceptionHelper
.UnknownDataType(dbType
.ToString(),"SqlDbType");
171 case DbType
.UInt64
: throw ExceptionHelper
.UnknownDataType(dbType
.ToString(),"SqlDbType");
172 case DbType
.VarNumeric
: throw ExceptionHelper
.UnknownDataType(dbType
.ToString(),"SqlDbType");
173 case DbType
.AnsiStringFixedLength
: return SqlDbType
.Char
;
174 case DbType
.StringFixedLength
: return SqlDbType
.NChar
;
175 default : throw ExceptionHelper
.InvalidDbType((int)dbType
);
179 internal static DbType
SqlDbTypeToDbType(SqlDbType sqlDbType
)
182 case SqlDbType
.BigInt
: return DbType
.Int64
;
183 case SqlDbType
.Binary
: return DbType
.Binary
;
184 case SqlDbType
.Bit
: return DbType
.Boolean
;
185 case SqlDbType
.Char
: return DbType
.AnsiStringFixedLength
;
186 case SqlDbType
.DateTime
: return DbType
.DateTime
;
187 case SqlDbType
.Decimal
: return DbType
.Decimal
;
188 case SqlDbType
.Float
: return DbType
.Double
;
189 case SqlDbType
.Image
: return DbType
.Binary
;
190 case SqlDbType
.Int
: return DbType
.Int32
;
191 case SqlDbType
.Money
: return DbType
.Currency
;
192 case SqlDbType
.NChar
: return DbType
.StringFixedLength
;
193 case SqlDbType
.NText
: return DbType
.String
;
194 case SqlDbType
.NVarChar
: return DbType
.String
;
195 case SqlDbType
.Real
: return DbType
.Single
;
196 case SqlDbType
.UniqueIdentifier
: return DbType
.Guid
;
197 case SqlDbType
.SmallDateTime
: return DbType
.DateTime
;
198 case SqlDbType
.SmallInt
: return DbType
.Int16
;
199 case SqlDbType
.SmallMoney
: return DbType
.Currency
;
200 case SqlDbType
.Text
: return DbType
.AnsiString
;
201 case SqlDbType
.Timestamp
: return DbType
.Binary
;
202 case SqlDbType
.TinyInt
: return DbType
.Byte
;
203 case SqlDbType
.VarBinary
: return DbType
.Binary
;
204 case SqlDbType
.VarChar
: return DbType
.AnsiString
;
205 case SqlDbType
.Variant
: return DbType
.Object
;
206 default : throw ExceptionHelper
.InvalidSqlDbType((int)sqlDbType
);
210 internal static int SqlDbTypeToJdbcType(SqlDbType sqlDbType
)
213 case SqlDbType
.BigInt
: return Types
.BIGINT
;
214 case SqlDbType
.Binary
: return Types
.BINARY
;
215 case SqlDbType
.Bit
: return Types
.BIT
;
216 case SqlDbType
.Char
: return Types
.CHAR
;
217 case SqlDbType
.DateTime
: return Types
.TIMESTAMP
;
218 case SqlDbType
.Decimal
: return Types
.DECIMAL
;
219 case SqlDbType
.Float
: return Types
.FLOAT
;
220 case SqlDbType
.Image
: return Types
.LONGVARBINARY
;
221 case SqlDbType
.Int
: return Types
.INTEGER
;
222 case SqlDbType
.Money
: return Types
.DECIMAL
;
223 case SqlDbType
.NChar
: return Types
.CHAR
;
224 case SqlDbType
.NText
: return Types
.LONGVARCHAR
;
225 case SqlDbType
.NVarChar
: return Types
.VARCHAR
;
226 case SqlDbType
.Real
: return Types
.REAL
;
227 case SqlDbType
.UniqueIdentifier
: return Types
.CHAR
;
228 case SqlDbType
.SmallDateTime
: return Types
.DATE
;
229 case SqlDbType
.SmallInt
: return Types
.SMALLINT
;
230 case SqlDbType
.SmallMoney
: return Types
.DECIMAL
;
231 case SqlDbType
.Text
: return Types
.LONGVARCHAR
;
232 case SqlDbType
.Timestamp
: return Types
.TIMESTAMP
;
233 case SqlDbType
.TinyInt
: return Types
.TINYINT
;
234 case SqlDbType
.VarBinary
: return Types
.VARBINARY
;
235 case SqlDbType
.VarChar
: return Types
.VARCHAR
;
236 case SqlDbType
.Variant
: return Types
.VARCHAR
; // note : ms jdbc driver recognize this sqlserver as varchar
237 default : throw ExceptionHelper
.InvalidSqlDbType((int)sqlDbType
);
241 #endregion // Methods