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