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