2 * Firebird ADO.NET Data provider for .NET and Mono
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
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.
15 * Copyright (c) 2002, 2005 Carlos Guzman Alvarez
16 * All Rights Reserved.
20 using System.Globalization;
22 namespace FirebirdSql.Data.Common
24 internal sealed class DbValue
28 private StatementBase statement;
29 private DbField field;
38 get { return this.field; }
43 get { return this.GetValue(); }
44 set { this.value = value; }
51 public DbValue(DbField field, object value)
54 this.value = (value == null) ? System.DBNull.Value : value;
57 public DbValue(StatementBase statement, DbField field)
59 this.statement = statement;
61 this.value = field.Value;
64 public DbValue(StatementBase statement, DbField field, object value)
66 this.statement = statement;
68 this.value = (value == null) ? System.DBNull.Value : value;
75 public bool IsDBNull()
77 if (this.value == null || this.value == System.DBNull.Value)
87 public string GetString()
89 if (this.Field.DbDataType == DbDataType.Text && this.value is long)
91 this.value = this.GetClobData((long)this.value);
94 return this.value.ToString();
99 return Convert.ToChar(this.value, CultureInfo.CurrentCulture);
102 public bool GetBoolean()
104 return Convert.ToBoolean(this.value, CultureInfo.InvariantCulture);
107 public byte GetByte()
109 return Convert.ToByte(this.value, CultureInfo.InvariantCulture);
112 public short GetInt16()
114 return Convert.ToInt16(this.value, CultureInfo.InvariantCulture);
117 public int GetInt32()
119 return Convert.ToInt32(this.value, CultureInfo.InvariantCulture);
122 public long GetInt64()
124 return Convert.ToInt64(this.value, CultureInfo.InvariantCulture);
127 public decimal GetDecimal()
129 return Convert.ToDecimal(this.value, CultureInfo.InvariantCulture);
132 public float GetFloat()
134 return Convert.ToSingle(this.value, CultureInfo.InvariantCulture);
137 public Guid GetGuid()
139 if (this.Value is Guid)
141 return (Guid)this.Value;
143 else if (this.Value is byte[])
145 return new Guid((byte[])this.value);
148 throw new InvalidOperationException("Incorrect Guid value");
151 public double GetDouble()
153 return Convert.ToDouble(this.value, CultureInfo.InvariantCulture);
156 public DateTime GetDateTime()
158 return Convert.ToDateTime(this.value, CultureInfo.CurrentCulture.DateTimeFormat);
161 public Array GetArray()
163 if (this.value is long)
165 this.value = this.GetArrayData((long)this.value);
168 return (Array)this.value;
171 public byte[] GetBinary()
173 if (this.value is long)
175 this.value = this.GetBlobData((long)this.value);
177 return (byte[])this.value;
180 public int EncodeDate()
182 return TypeEncoder.EncodeDate(this.GetDateTime());
185 public int EncodeTime()
187 return TypeEncoder.EncodeTime(this.GetDateTime());
192 #region Private Methods
194 private object GetValue()
198 return System.DBNull.Value;
201 switch (this.field.DbDataType)
203 case DbDataType.Text:
204 if (this.statement == null)
206 return this.GetInt64();
210 return this.GetString();
213 case DbDataType.Binary:
214 if (this.statement == null)
216 return this.GetInt64();
220 return this.GetBinary();
223 case DbDataType.Array:
224 if (this.statement == null)
226 return this.GetInt64();
230 return this.GetArray();
238 private string GetClobData(long blobId)
240 BlobBase clob = this.statement.CreateBlob(blobId);
242 return clob.ReadString();
245 private byte[] GetBlobData(long blobId)
247 BlobBase blob = this.statement.CreateBlob(blobId);
252 private Array GetArrayData(long handle)
254 if (this.field.ArrayHandle == null)
256 this.field.ArrayHandle = this.statement.CreateArray(handle, this.Field.Relation, this.Field.Name);
259 ArrayBase gdsArray = this.statement.CreateArray(this.field.ArrayHandle.Descriptor);
261 gdsArray.Handle = handle;
262 gdsArray.DB = this.statement.DB;
263 gdsArray.Transaction = this.statement.Transaction;
265 return gdsArray.Read();