2002-08-12 Franklin Wise <gracenote@earthlink.net>
[mono.git] / mcs / class / System.Data / System.Data / DataColumn.cs
1 //
2 // System.Data.DataColumn.cs
3 //
4 // Author:
5 //   Christopher Podurgiel (cpodurgiel@msn.com)
6 //   Rodrigo Moya (rodrigo@ximian.com)
7 //   Daniel Morgan (danmorg@sc.rr.com)
8 //
9 // (C) Chris Podurgiel
10 // (C) Ximian, Inc 2002
11 //
12
13
14 using System;
15 using System.ComponentModel;
16
17 namespace System.Data
18 {
19         /// <summary>
20         /// Summary description for DataColumn.
21         /// </summary>
22         public class DataColumn : MarshalByValueComponent
23         {               
24                 #region Fields
25
26                 private bool allowDBNull = true;
27                 private bool autoIncrement = false;
28                 private long autoIncrementSeed = 0;
29                 private long autoIncrementStep = 1;
30                 private string caption = null;
31                 private MappingType columnMapping = MappingType.Element;
32                 private string columnName = null;
33                 private Type dataType = null;
34                 private object defaultValue = null;
35                 private string expression = null;
36                 private PropertyCollection extendedProperties = null;
37                 private int maxLength = -1;
38                 private string nameSpace = null;
39                 private int ordinal = -1;
40                 private string prefix = null;
41                 private bool readOnly = false;
42                 private DataTable _table = null;
43                 private bool unique = false;
44
45                 #endregion // Fields
46
47                 #region Constructors
48
49                 public DataColumn()
50                 {
51                 }
52
53                 public DataColumn(string columnName): this()
54                 {
55                         ColumnName = columnName;
56                 }
57
58                 public DataColumn(string columnName, Type dataType): this(columnName)
59                 {
60                         if(dataType == null) {
61                                 throw new ArgumentNullException();
62                         }
63                         
64                         DataType = dataType;
65
66                 }
67
68                 public DataColumn( string columnName, Type dataType, 
69                         string expr): this(columnName, dataType)
70                 {
71                         Expression = expr;
72                 }
73
74                 public DataColumn(string columnName, Type dataType, 
75                         string expr, MappingType type): this(columnName, dataType, expr)
76                 {
77                         ColumnMapping = type;
78                 }
79                 #endregion
80
81                 #region Properties
82                 
83                 public bool AllowDBNull
84                 {
85                         get {
86                                 return allowDBNull;
87                         }
88                         set {
89                                 allowDBNull = value;
90                         }
91                 }
92         
93                 /// <summary>
94                 /// Gets or sets a value indicating whether the column automatically increments the value of the column for new rows added to the table.
95                 /// </summary>
96                 /// <remarks>
97                 ///             If the type of this column is not Int16, Int32, or Int64 when this property is set, 
98                 ///             the DataType property is coerced to Int32. An exception is generated if this is a computed column 
99                 ///             (that is, the Expression property is set.) The incremented value is used only if the row's value for this column, 
100                 ///             when added to the columns collection, is equal to the default value.
101                 ///     </remarks>
102                 public bool AutoIncrement
103                 {
104                         get {
105                                 return autoIncrement;
106                         }
107                         set {
108                                 autoIncrement = value;
109                                 if(autoIncrement == true)
110                                 {
111                                         if(Expression != null)
112                                         {
113                                                 throw new Exception();
114                                         }
115                                         if(Type.GetTypeCode(dataType) != TypeCode.Int16 && 
116                                            Type.GetTypeCode(dataType) != TypeCode.Int32 && 
117                                            Type.GetTypeCode(dataType) != TypeCode.Int64)
118                                         {
119                                                 Int32 dtInt = new Int32();
120                                                 dataType = dtInt.GetType();
121                                         }
122                                 }
123                         }
124                 }
125
126                 public long AutoIncrementSeed
127                 {
128                         get {
129                                 return autoIncrementSeed;
130                         }
131                         set {
132                                 autoIncrementSeed = value;
133                         }
134                 }
135
136                 public long AutoIncrementStep
137                 {
138                         get {
139                                 return autoIncrementStep;
140                         }
141                         set {
142                                 autoIncrementStep = value;
143                         }
144                 }
145
146                 public string Caption 
147                 {
148                         get {
149                                 if(caption == null)
150                                         return columnName;
151                                 else
152                                         return caption;
153                         }
154                         set {
155                                 caption = value;
156                         }
157                 }
158
159                 public virtual MappingType ColumnMapping
160                 {
161                         get {
162                                 return columnMapping;
163                         }
164                         set {
165                                 columnMapping = value;
166                         }
167                 }
168
169                 public string ColumnName
170                 {
171                         get {
172                                 return columnName;
173                         }
174                         set {
175                                 columnName = value;
176                         }
177                 }
178
179                 public Type DataType
180                 {
181                         get {
182                                 return dataType;
183                         }
184                         set {
185                                 if(AutoIncrement == true && 
186                                    Type.GetTypeCode(value) != TypeCode.Int32)
187                                 {
188                                         throw new Exception();
189                                 }
190                                 dataType = value;
191                         }
192                 }
193
194                 /// <summary>
195                 /// 
196                 /// </summary>
197                 /// <remarks>When AutoIncrement is set to true, there can be no default value.</remarks>
198                 public object DefaultValue
199                 {
200                         get {
201                                 return defaultValue;
202                         }
203                         set {
204                                 defaultValue = value;
205                         }
206                 }
207
208                 public string Expression
209                 {
210                         get {
211                                 return expression;
212                         }
213                         set {
214                                 expression = value;
215                         }
216                 }
217
218                 public PropertyCollection ExtendedProperties
219                 {
220                         get {
221                                 return extendedProperties;
222                         }
223                 }
224
225                 public int MaxLength
226                 {
227                         get {
228                                 return maxLength;
229                         }
230                         set {
231                                 maxLength = value;
232                         }
233                 }
234
235                 public string Namespace
236                 {
237                         get {
238                                 return nameSpace;
239                         }
240                         set {
241                                 nameSpace = value;
242                         }
243                 }
244
245                 //Need a good way to set the Ordinal when the column is added to a columnCollection.
246                 public int Ordinal
247                 {
248                         get {
249                                 return ordinal;
250                         }
251                 }
252
253                 public string Prefix
254                 {
255                         get {
256                                 return prefix;
257                         }
258                         set {
259                                 prefix = value;
260                         }
261                 }
262
263                 public bool ReadOnly
264                 {
265                         get {
266                                 return readOnly;
267                         }
268                         set {
269                                 readOnly = value;
270                         }
271                 }
272         
273                 public DataTable Table
274                 {
275                         get {
276                                 return _table;
277                         }
278                 }
279
280                 public bool Unique
281                 {
282                         get {
283                                 return unique;
284                         }
285                         set {
286                                 unique = value;
287                         }
288                 }
289
290                 #endregion // Properties
291
292                 #region Methods
293
294                 [MonoTODO]
295                 protected internal void CheckNotAllowNull() {
296                 }
297
298                 [MonoTODO]
299                 protected void CheckUnique() {
300                 }
301
302                 [MonoTODO]
303                 protected internal virtual void 
304                 OnPropertyChanging (PropertyChangedEventArgs pcevent) {
305                 }
306
307                 [MonoTODO]
308                 protected internal void RaisePropertyChanging(string name) {
309                 }
310
311                 /// <summary>
312                 /// Gets the Expression of the column, if one exists.
313                 /// </summary>
314                 /// <returns>The Expression value, if the property is set; 
315                 /// otherwise, the ColumnName property.</returns>
316                 [MonoTODO]
317                 public override string ToString()
318                 {
319                         if (expression != null)
320                                 return expression;
321                         
322                         return columnName;
323                 }
324
325                 [MonoTODO]
326                 internal void SetTable(DataTable table) {
327                         _table = table; 
328                         // FIXME: this will get called by DataTable 
329                         // and DataColumnCollection
330                 }
331
332                 #endregion // Methods
333
334         }
335 }