SqlConvert.cs 10 KB

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