merge r98600
[mono.git] / mcs / class / System.Data / System.Data.Odbc / OdbcColumn.cs
1
2 //
3 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
4 //
5 // Permission is hereby granted, free of charge, to any person obtaining
6 // a copy of this software and associated documentation files (the
7 // "Software"), to deal in the Software without restriction, including
8 // without limitation the rights to use, copy, modify, merge, publish,
9 // distribute, sublicense, and/or sell copies of the Software, and to
10 // permit persons to whom the Software is furnished to do so, subject to
11 // the following conditions:
12 // 
13 // The above copyright notice and this permission notice shall be
14 // included in all copies or substantial portions of the Software.
15 // 
16 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 //
24 using System;
25
26 namespace System.Data.Odbc
27 {
28         /// <summary>
29         /// Summary description for OdbcColumn.
30         /// </summary>
31         internal class OdbcColumn
32         {
33                 internal string ColumnName;
34                 internal OdbcType OdbcType;
35                 private SQL_TYPE _sqlType = SQL_TYPE.UNASSIGNED;
36                 private SQL_C_TYPE _sqlCType = SQL_C_TYPE.UNASSIGNED;
37                 internal bool AllowDBNull;
38                 internal int MaxLength;
39                 internal int Digits;
40                 internal object Value;
41
42                 internal OdbcColumn (string Name, OdbcType Type)
43                 {
44                         this.ColumnName = Name;
45                         this.OdbcType = Type;           
46                         AllowDBNull = false;
47                         MaxLength = 0;
48                         Digits = 0;
49                         Value = null;
50                 }
51
52                 internal OdbcColumn (string Name, SQL_TYPE type)
53                 {
54                         this.ColumnName = Name;
55                         AllowDBNull = false;
56                         MaxLength = 0;
57                         Digits = 0;
58                         Value = null;
59                         UpdateTypes (type);
60
61                 }
62
63
64                 internal Type DataType
65                 {
66                         get
67                         {
68                                 switch (OdbcType)
69                                 {
70                                         case OdbcType.TinyInt:
71                                                 return typeof (System.Byte);
72                                         case OdbcType.BigInt: 
73                                                 return typeof (System.Int64);
74                                         case OdbcType.Image:
75                                         case OdbcType.VarBinary:
76                                         case OdbcType.Binary:
77                                                 return typeof (byte[]);
78                                         case OdbcType.Bit:
79                                                 return typeof (bool);
80                                         case OdbcType.NChar:
81                                         case OdbcType.Char:
82                                                 return typeof (string);
83                                         case OdbcType.Time:
84                                                 return typeof (TimeSpan);
85                                         case OdbcType.Timestamp:
86                                         case OdbcType.DateTime:
87                                         case OdbcType.Date:
88                                         case OdbcType.SmallDateTime:
89                                                 return typeof (DateTime);
90                                         case OdbcType.Decimal:
91                                                 return typeof (Decimal);
92                                         case OdbcType.Numeric:
93                                         case OdbcType.Double:
94                                                 return typeof (Double);
95                                         case OdbcType.Int:
96                                                 return typeof (System.Int32);
97                                         case OdbcType.Text:
98                                         case OdbcType.NText:
99                                         case OdbcType.NVarChar:
100                                         case OdbcType.VarChar:
101                                                 return typeof (string);
102                                         case OdbcType.Real:
103                                                 return typeof (float);
104                                         case OdbcType.SmallInt:
105                                                 return typeof (System.Int16);
106                                         case OdbcType.UniqueIdentifier:
107                                                 return typeof (Guid);
108                                 }
109                                 throw new InvalidCastException();
110                         }
111                 }
112
113                 internal bool IsDateType
114                 {
115                         get
116                         {
117                                 switch (OdbcType)
118                                 {
119                                         case OdbcType.Time:
120                                         case OdbcType.Timestamp:
121                                         case OdbcType.DateTime:
122                                         case OdbcType.Date:
123                                         case OdbcType.SmallDateTime:
124                                                 return true;
125                                         default:
126                                                 return false;
127                                 }
128                         }
129                 }
130
131                 internal bool IsStringType
132                 {
133                         get
134                         {
135                                 switch (OdbcType)
136                                 {
137                                         case OdbcType.Char:
138                                         case OdbcType.Text:
139                                         case OdbcType.NText:
140                                         case OdbcType.NVarChar:
141                                         case OdbcType.VarChar:
142                                                 return true;
143                                         default:
144                                                 return false;
145                                 }
146                         }
147                 }
148
149                 internal bool IsVariableSizeType {
150                         get {
151                                 if (IsStringType)
152                                         return true;
153                                 switch (OdbcType) {
154                                 case OdbcType.Binary :
155                                 case OdbcType.VarBinary :
156                                 case OdbcType.Image :
157                                         return true;
158                                 default : 
159                                         return false;
160                                 }
161                         }
162                 }
163
164                 internal SQL_TYPE SqlType
165                 {
166                         get {
167                                 if ( _sqlType == SQL_TYPE.UNASSIGNED)
168                                         _sqlType = OdbcTypeConverter.GetTypeMap (OdbcType).SqlType;
169                                 return _sqlType;
170                         }
171
172                         set {_sqlType = value;}
173                 }
174
175                 internal SQL_C_TYPE SqlCType
176                 {
177                         get {
178                                 
179                                 if ( _sqlCType == SQL_C_TYPE.UNASSIGNED)
180                                         _sqlCType = OdbcTypeConverter.GetTypeMap (OdbcType).NativeType;
181                                 return _sqlCType;
182                         }
183                         set {_sqlCType = value;}
184                 }
185
186                 internal void UpdateTypes (SQL_TYPE sqlType)
187                 {
188                         SqlType = sqlType;
189                         OdbcTypeMap map = OdbcTypeConverter.GetTypeMap (SqlType);
190                         OdbcType = map.OdbcType;
191                         SqlCType = map.NativeType;
192                 }
193         }
194 }