SqlConvert.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. //
  2. // System.Data.SqlClient.SqlConvert
  3. //
  4. //
  5. // Authors:
  6. // Konstantin Triger <[email protected]>
  7. // Boris Kirzner <[email protected]>
  8. //
  9. // (C) 2005 Mainsoft Corporation (http://www.mainsoft.com)
  10. //
  11. //
  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:
  19. //
  20. // The above copyright notice and this permission notice shall be
  21. // included in all copies or substantial portions of the Software.
  22. //
  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.
  30. //
  31. using System;
  32. using System.Data.Common;
  33. using java.sql;
  34. namespace System.Data.SqlClient
  35. {
  36. internal sealed class SqlConvert : DbConvert
  37. {
  38. #region Methods
  39. internal static String JdbcTypeNameToDbTypeName(string jdbcTypeName)
  40. {
  41. return jdbcTypeName.Trim();
  42. }
  43. internal static SqlDbType JdbcTypeToSqlDbType(int jdbcType)
  44. {
  45. // FIXME : other java.sql.Type
  46. // Types.ARRAY
  47. if(Types.BIGINT == jdbcType) return SqlDbType.BigInt;
  48. if(Types.BINARY == jdbcType) return SqlDbType.Binary;
  49. if(Types.BIT == jdbcType) return SqlDbType.Bit;
  50. if(Types.BLOB == jdbcType) return SqlDbType.Binary;
  51. // Types.BOOLEAN
  52. if(Types.CHAR == jdbcType) return SqlDbType.Char;
  53. if(Types.CLOB == jdbcType) return SqlDbType.Binary;
  54. if(Types.DATE == jdbcType) return SqlDbType.DateTime;
  55. if(Types.DECIMAL == jdbcType) return SqlDbType.Decimal;
  56. // Types.DISTINCT
  57. if(Types.DOUBLE == jdbcType) return SqlDbType.Float;
  58. if(Types.FLOAT == jdbcType) return SqlDbType.Float;
  59. if(Types.INTEGER == jdbcType) return SqlDbType.Int;
  60. // Types.JAVA_OBJECT
  61. if(Types.LONGVARBINARY == jdbcType) return SqlDbType.Image;
  62. if(Types.LONGVARCHAR == jdbcType) return SqlDbType.Text;
  63. // Types.NULL
  64. if(Types.NUMERIC == jdbcType) return SqlDbType.Decimal;
  65. if(Types.REAL == jdbcType) return SqlDbType.Real;
  66. // Types.REF
  67. if(Types.SMALLINT == jdbcType) return SqlDbType.SmallInt;
  68. // Types.STRUCT
  69. if(Types.TIME == jdbcType) return SqlDbType.Timestamp;
  70. if(Types.TIMESTAMP == jdbcType) return SqlDbType.Timestamp;
  71. if(Types.TINYINT == jdbcType) return SqlDbType.TinyInt;
  72. if(Types.VARBINARY == jdbcType) return SqlDbType.VarBinary;
  73. if(Types.VARCHAR == jdbcType) return SqlDbType.VarChar;
  74. return SqlDbType.Variant;
  75. }
  76. internal static SqlDbType ValueTypeToSqlDbType(Type type)
  77. {
  78. switch (Type.GetTypeCode(type)) {
  79. case TypeCode.Boolean: return SqlDbType.Bit;
  80. case TypeCode.Byte: return SqlDbType.TinyInt;
  81. case TypeCode.Char: return SqlDbType.Char;
  82. case TypeCode.DateTime: return SqlDbType.DateTime;
  83. case TypeCode.DBNull: return SqlDbType.Variant;
  84. case TypeCode.Decimal: return SqlDbType.Decimal;
  85. case TypeCode.Double: return SqlDbType.Float;
  86. case TypeCode.Empty: return SqlDbType.Variant;
  87. case TypeCode.Int16: return SqlDbType.SmallInt;
  88. case TypeCode.Int32: return SqlDbType.Int;
  89. case TypeCode.Int64: return SqlDbType.BigInt;
  90. default:
  91. case TypeCode.Object: {
  92. if (type.Equals(DbTypes.TypeOfByteArray)) return SqlDbType.VarBinary;
  93. //if (type.Equals(DbTypes.TypeOfTimespan)) return OleDbType.DBTime;
  94. if (type.Equals(DbTypes.TypeOfGuid)) return SqlDbType.UniqueIdentifier;
  95. if (type.IsEnum)
  96. return ValueTypeToSqlDbType (Enum.GetUnderlyingType (type));
  97. return SqlDbType.Variant;
  98. }
  99. case TypeCode.SByte: return SqlDbType.TinyInt;
  100. case TypeCode.Single: return SqlDbType.Float;
  101. case TypeCode.String: return SqlDbType.VarChar;
  102. case TypeCode.UInt16: return SqlDbType.SmallInt;
  103. case TypeCode.UInt32: return SqlDbType.Int;
  104. case TypeCode.UInt64: return SqlDbType.BigInt;
  105. }
  106. }
  107. internal static Type SqlDbTypeToValueType(SqlDbType sqlDbType)
  108. {
  109. switch (sqlDbType) {
  110. case SqlDbType.BigInt : return typeof(long);
  111. case SqlDbType.Binary : return typeof(byte[]);
  112. case SqlDbType.Bit : return typeof(bool);
  113. case SqlDbType.Char : return typeof(string);
  114. case SqlDbType.DateTime : return typeof(DateTime);
  115. case SqlDbType.Decimal : return typeof(decimal);
  116. case SqlDbType.Float : return typeof(double);
  117. case SqlDbType.Image : return typeof(byte[]);
  118. case SqlDbType.Int : return typeof(int);
  119. case SqlDbType.Money : return typeof(decimal);
  120. case SqlDbType.NChar : return typeof(string);
  121. case SqlDbType.NText : return typeof(string);
  122. case SqlDbType.NVarChar : return typeof(string);
  123. case SqlDbType.Real : return typeof(Single);
  124. case SqlDbType.UniqueIdentifier : return typeof(Guid);
  125. case SqlDbType.SmallDateTime : return typeof(DateTime);
  126. case SqlDbType.SmallInt : return typeof(Int16);
  127. case SqlDbType.SmallMoney : return typeof(decimal);
  128. case SqlDbType.Text : return typeof(string);
  129. case SqlDbType.Timestamp : return typeof(byte[]);
  130. case SqlDbType.TinyInt : return typeof(byte);
  131. case SqlDbType.VarBinary : return typeof(byte[]);
  132. case SqlDbType.VarChar : return typeof(string);
  133. case SqlDbType.Variant : return typeof(object);
  134. default : throw ExceptionHelper.InvalidSqlDbType((int)sqlDbType);
  135. }
  136. }
  137. internal static SqlDbType DbTypeToSqlDbType(DbType dbType)
  138. {
  139. switch (dbType) {
  140. case DbType.AnsiString : return SqlDbType.VarChar;
  141. case DbType.Binary : return SqlDbType.VarBinary;
  142. case DbType.Byte : return SqlDbType.TinyInt;
  143. case DbType.Boolean : return SqlDbType.Bit;
  144. case DbType.Currency : return SqlDbType.Money;
  145. case DbType.Date : return SqlDbType.DateTime;
  146. case DbType.DateTime : return SqlDbType.DateTime;
  147. case DbType.Decimal : return SqlDbType.Decimal;
  148. case DbType.Double : return SqlDbType.Float;
  149. case DbType.Guid : return SqlDbType.UniqueIdentifier;
  150. case DbType.Int16 : return SqlDbType.SmallInt;
  151. case DbType.Int32 : return SqlDbType.Int;
  152. case DbType.Int64 : return SqlDbType.BigInt;
  153. case DbType.Object : return SqlDbType.Variant;
  154. case DbType.SByte : throw ExceptionHelper.UnknownDataType(dbType.ToString(),"SqlDbType");
  155. case DbType.Single : return SqlDbType.Real;
  156. case DbType.String : return SqlDbType.NVarChar;
  157. case DbType.UInt16 : throw ExceptionHelper.UnknownDataType(dbType.ToString(),"SqlDbType");
  158. case DbType.UInt32 : throw ExceptionHelper.UnknownDataType(dbType.ToString(),"SqlDbType");
  159. case DbType.UInt64 : throw ExceptionHelper.UnknownDataType(dbType.ToString(),"SqlDbType");
  160. case DbType.VarNumeric : throw ExceptionHelper.UnknownDataType(dbType.ToString(),"SqlDbType");
  161. case DbType.AnsiStringFixedLength : return SqlDbType.Char;
  162. case DbType.StringFixedLength : return SqlDbType.NChar;
  163. default : throw ExceptionHelper.InvalidDbType((int)dbType);
  164. }
  165. }
  166. internal static DbType SqlDbTypeToDbType(SqlDbType sqlDbType)
  167. {
  168. switch (sqlDbType) {
  169. case SqlDbType.BigInt : return DbType.Int64;
  170. case SqlDbType.Binary : return DbType.Binary;
  171. case SqlDbType.Bit : return DbType.Boolean;
  172. case SqlDbType.Char : return DbType.AnsiStringFixedLength;
  173. case SqlDbType.DateTime : return DbType.DateTime;
  174. case SqlDbType.Decimal : return DbType.Decimal;
  175. case SqlDbType.Float : return DbType.Double;
  176. case SqlDbType.Image : return DbType.Binary;
  177. case SqlDbType.Int : return DbType.Int32;
  178. case SqlDbType.Money : return DbType.Currency;
  179. case SqlDbType.NChar : return DbType.StringFixedLength;
  180. case SqlDbType.NText : return DbType.String;
  181. case SqlDbType.NVarChar : return DbType.String;
  182. case SqlDbType.Real : return DbType.Single;
  183. case SqlDbType.UniqueIdentifier : return DbType.Guid;
  184. case SqlDbType.SmallDateTime : return DbType.DateTime;
  185. case SqlDbType.SmallInt : return DbType.Int16;
  186. case SqlDbType.SmallMoney : return DbType.Currency;
  187. case SqlDbType.Text : return DbType.AnsiString;
  188. case SqlDbType.Timestamp : return DbType.Binary;
  189. case SqlDbType.TinyInt : return DbType.Byte;
  190. case SqlDbType.VarBinary : return DbType.Binary;
  191. case SqlDbType.VarChar : return DbType.AnsiString;
  192. case SqlDbType.Variant : return DbType.Object;
  193. default : throw ExceptionHelper.InvalidSqlDbType((int)sqlDbType);
  194. }
  195. }
  196. internal static int SqlDbTypeToJdbcType(SqlDbType sqlDbType)
  197. {
  198. switch(sqlDbType) {
  199. case SqlDbType.BigInt : return Types.BIGINT;
  200. case SqlDbType.Binary : return Types.BINARY;
  201. case SqlDbType.Bit : return Types.BIT;
  202. case SqlDbType.Char : return Types.CHAR;
  203. case SqlDbType.DateTime : return Types.TIMESTAMP;
  204. case SqlDbType.Decimal : return Types.DECIMAL;
  205. case SqlDbType.Float : return Types.FLOAT;
  206. case SqlDbType.Image : return Types.LONGVARBINARY;
  207. case SqlDbType.Int : return Types.INTEGER;
  208. case SqlDbType.Money : return Types.DECIMAL;
  209. case SqlDbType.NChar : return Types.CHAR;
  210. case SqlDbType.NText : return Types.LONGVARCHAR;
  211. case SqlDbType.NVarChar : return Types.VARCHAR;
  212. case SqlDbType.Real : return Types.REAL;
  213. case SqlDbType.UniqueIdentifier : return Types.CHAR;
  214. case SqlDbType.SmallDateTime : return Types.DATE;
  215. case SqlDbType.SmallInt : return Types.SMALLINT;
  216. case SqlDbType.SmallMoney : return Types.DECIMAL;
  217. case SqlDbType.Text : return Types.LONGVARCHAR;
  218. case SqlDbType.Timestamp : return Types.TIMESTAMP;
  219. case SqlDbType.TinyInt : return Types.TINYINT;
  220. case SqlDbType.VarBinary : return Types.VARBINARY;
  221. case SqlDbType.VarChar : return Types.VARCHAR;
  222. case SqlDbType.Variant : return Types.VARCHAR; // note : ms jdbc driver recognize this sqlserver as varchar
  223. default : throw ExceptionHelper.InvalidSqlDbType((int)sqlDbType);
  224. }
  225. }
  226. #endregion // Methods
  227. }
  228. }