New test.
[mono.git] / mcs / class / FirebirdSql.Data.Firebird / FirebirdSql.Data.Common / DbValue.cs
1 /*
2  *  Firebird ADO.NET Data provider for .NET and Mono 
3  * 
4  *     The contents of this file are subject to the Initial 
5  *     Developer's Public License Version 1.0 (the "License"); 
6  *     you may not use this file except in compliance with the 
7  *     License. You may obtain a copy of the License at 
8  *     http://www.firebirdsql.org/index.php?op=doc&id=idpl
9  *
10  *     Software distributed under the License is distributed on 
11  *     an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either 
12  *     express or implied.  See the License for the specific 
13  *     language governing rights and limitations under the License.
14  * 
15  *  Copyright (c) 2002, 2005 Carlos Guzman Alvarez
16  *  All Rights Reserved.
17  */
18
19 using System;
20 using System.Globalization;
21
22 namespace FirebirdSql.Data.Common
23 {
24         internal sealed class DbValue
25         {
26                 #region Fields
27
28                 private StatementBase   statement;
29                 private DbField                 field;
30                 private object                  value;
31
32                 #endregion
33
34                 #region Properties
35
36                 public DbField Field
37                 {
38                         get { return this.field; }
39                 }
40
41                 public object Value
42                 {
43                         get { return this.GetValue(); }
44                         set { this.value = value; }
45                 }
46
47                 #endregion
48
49                 #region Constructor
50
51                 public DbValue(DbField field, object value)
52                 {
53                         this.field = field;
54                         this.value = (value == null) ? System.DBNull.Value : value;
55                 }
56
57                 public DbValue(StatementBase statement, DbField field)
58                 {
59                         this.statement  = statement;
60                         this.field              = field;
61                         this.value              = field.Value;
62                 }
63
64                 public DbValue(StatementBase statement, DbField field, object value)
65                 {
66                         this.statement  = statement;
67                         this.field              = field;
68                         this.value              = (value == null) ? System.DBNull.Value : value;
69                 }
70
71                 #endregion
72
73                 #region Methods
74
75                 public bool IsDBNull()
76                 {
77                         if (this.value == null || this.value == System.DBNull.Value)
78                         {
79                                 return true;
80                         }
81                         else
82                         {
83                                 return false;
84                         }
85                 }
86
87                 public string GetString()
88                 {
89                         if (this.Field.DbDataType == DbDataType.Text && this.value is long)
90                         {
91                                 this.value = this.GetClobData((long)this.value);
92                         }
93
94                         return this.value.ToString();
95                 }
96
97                 public char GetChar()
98                 {
99                         return Convert.ToChar(this.value, CultureInfo.CurrentCulture);
100                 }
101
102                 public bool GetBoolean()
103                 {
104                         return Convert.ToBoolean(this.value, CultureInfo.InvariantCulture);
105                 }
106
107                 public byte GetByte()
108                 {
109                         return Convert.ToByte(this.value, CultureInfo.InvariantCulture);
110                 }
111
112                 public short GetInt16()
113                 {
114                         return Convert.ToInt16(this.value, CultureInfo.InvariantCulture);
115                 }
116
117                 public int GetInt32()
118                 {
119                         return Convert.ToInt32(this.value, CultureInfo.InvariantCulture);
120                 }
121
122                 public long GetInt64()
123                 {
124                         return Convert.ToInt64(this.value, CultureInfo.InvariantCulture);
125                 }
126
127                 public decimal GetDecimal()
128                 {
129                         return Convert.ToDecimal(this.value, CultureInfo.InvariantCulture);
130                 }
131
132                 public float GetFloat()
133                 {
134                         return Convert.ToSingle(this.value, CultureInfo.InvariantCulture);
135                 }
136
137                 public Guid GetGuid()
138                 {
139                         if (this.Value is Guid)
140                         {
141                                 return (Guid)this.Value;
142                         }
143                         else if (this.Value is byte[])
144                         {
145                                 return new Guid((byte[])this.value);
146                         }
147
148                         throw new InvalidOperationException("Incorrect Guid value");
149                 }
150
151                 public double GetDouble()
152                 {
153                         return Convert.ToDouble(this.value, CultureInfo.InvariantCulture);
154                 }
155
156                 public DateTime GetDateTime()
157                 {
158                         return Convert.ToDateTime(this.value, CultureInfo.CurrentCulture.DateTimeFormat);
159                 }
160
161                 public Array GetArray()
162                 {
163                         if (this.value is long)
164                         {
165                                 this.value = this.GetArrayData((long)this.value);
166                         }
167
168                         return (Array)this.value;
169                 }
170
171                 public byte[] GetBinary()
172                 {
173                         if (this.value is long)
174                         {
175                                 this.value = this.GetBlobData((long)this.value);
176                         }
177                         return (byte[])this.value;
178                 }
179
180                 public int EncodeDate()
181                 {
182                         return TypeEncoder.EncodeDate(this.GetDateTime());
183                 }
184
185                 public int EncodeTime()
186                 {
187                         return TypeEncoder.EncodeTime(this.GetDateTime());
188                 }
189
190                 #endregion
191
192                 #region Private Methods
193
194                 private object GetValue()
195                 {
196                         if (this.IsDBNull())
197                         {
198                                 return System.DBNull.Value;
199                         }
200
201                         switch (this.field.DbDataType)
202                         {
203                                 case DbDataType.Text:
204                                         if (this.statement == null)
205                                         {
206                                                 return this.GetInt64();
207                                         }
208                                         else
209                                         {
210                                                 return this.GetString();
211                                         }
212
213                                 case DbDataType.Binary:
214                                         if (this.statement == null)
215                                         {
216                                                 return this.GetInt64();
217                                         }
218                                         else
219                                         {
220                                                 return this.GetBinary();
221                                         }
222
223                                 case DbDataType.Array:
224                                         if (this.statement == null)
225                                         {
226                                                 return this.GetInt64();
227                                         }
228                                         else
229                                         {
230                                                 return this.GetArray();
231                                         }
232
233                                 default:
234                                         return this.value;
235                         }
236                 }
237
238                 private string GetClobData(long blobId)
239                 {
240                         BlobBase clob = this.statement.CreateBlob(blobId);
241
242                         return clob.ReadString();
243                 }
244
245                 private byte[] GetBlobData(long blobId)
246                 {
247                         BlobBase blob = this.statement.CreateBlob(blobId);
248
249                         return blob.Read();
250                 }
251
252                 private Array GetArrayData(long handle)
253                 {
254                         if (this.field.ArrayHandle == null)
255                         {
256                                 this.field.ArrayHandle = this.statement.CreateArray(handle, this.Field.Relation, this.Field.Name);
257                         }
258
259                         ArrayBase gdsArray = this.statement.CreateArray(this.field.ArrayHandle.Descriptor);
260                         
261                         gdsArray.Handle                 = handle;
262                         gdsArray.DB                             = this.statement.DB;
263                         gdsArray.Transaction    = this.statement.Transaction;
264
265                         return gdsArray.Read();
266                 }
267
268                 #endregion
269         }
270 }