2007-05-10 Nagappan A <anagappan@novell.com>
[mono.git] / mcs / class / FirebirdSql.Data.Firebird / FirebirdSql.Data.Common / Descriptor.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.IO;
21
22 namespace FirebirdSql.Data.Common
23 {
24         /// <summary>
25         /// Descriptor of query input and output parameters.
26         /// </summary>
27         /// <remarks>
28         /// This is similar to the XSQLDA structure described 
29         /// in the Interbase 6.0 API docs.
30         /// </remarks>
31         internal sealed class Descriptor : ICloneable
32         {
33                 #region Fields
34
35                 private short version;
36                 private short count;
37                 private short actualCount;
38                 private DbField[] fields;
39
40                 #endregion
41
42                 #region Properties
43
44                 public short Version
45                 {
46                         get
47                         {
48                                 return this.version;
49                         }
50                         set
51                         {
52                                 this.version = value;
53                         }
54                 }
55
56                 public short Count
57                 {
58                         get { return this.count; }
59                 }
60
61                 public short ActualCount
62                 {
63                         get { return this.actualCount; }
64                         set { this.actualCount = value; }
65                 }
66
67                 #endregion
68
69                 #region Indexers
70
71                 public DbField this[int index]
72                 {
73                         get { return this.fields[index]; }
74                 }
75
76                 #endregion
77
78                 #region Constructors
79
80                 public Descriptor(int count)
81                 {
82                         this.version            = IscCodes.SQLDA_VERSION1;
83                         this.count                      = (short)count;
84                         this.actualCount        = (short)count;
85                         this.fields                     = new DbField[count];
86
87                         for (int i = 0; i < this.fields.Length; i++)
88                         {
89                                 this.fields[i] = new DbField();
90                         }
91                 }
92
93                 #endregion
94
95                 #region ICloneable Methods
96
97                 public object Clone()
98                 {
99                         Descriptor descriptor = new Descriptor(this.Count);
100                         descriptor.Version = this.version;
101
102                         for (int i = 0; i < descriptor.Count; i++)
103                         {
104                                 descriptor[i].DataType  = this.fields[i].DataType;
105                                 descriptor[i].NumericScale = this.fields[i].NumericScale;
106                                 descriptor[i].SubType   = this.fields[i].SubType;
107                                 descriptor[i].Length    = this.fields[i].Length;
108                                 descriptor[i].Value             = this.fields[i].Value;
109                                 descriptor[i].NullFlag  = this.fields[i].NullFlag;
110                                 descriptor[i].Name              = this.fields[i].Name;
111                                 descriptor[i].Relation  = this.fields[i].Relation;
112                                 descriptor[i].Owner             = this.fields[i].Owner;
113                                 descriptor[i].Alias             = this.fields[i].Alias;
114                         }
115
116                         return descriptor;
117                 }
118
119                 #endregion
120
121                 #region Methods
122
123                 public void ResetValues()
124                 {
125                         for (int i = 0; i < this.fields.Length; i++)
126                         {
127                                 this.fields[i].Value = null;
128                         }
129                 }
130
131                 public byte[] ToBlrArray()
132                 {
133                         MemoryStream blr = new MemoryStream();
134                         int par_count = this.Count * 2;
135
136                         blr.WriteByte(IscCodes.blr_version5);
137                         blr.WriteByte(IscCodes.blr_begin);
138                         blr.WriteByte(IscCodes.blr_message);
139                         blr.WriteByte(0);
140                         blr.WriteByte((byte)(par_count & 255));
141                         blr.WriteByte((byte)(par_count >> 8));
142
143                         for (int i = 0; i < this.fields.Length; i++)
144                         {
145                                 int dtype = this.fields[i].SqlType;
146                                 int len = this.fields[i].Length;
147
148                                 switch (dtype)
149                                 {
150                                         case IscCodes.SQL_VARYING:
151                                                 blr.WriteByte(IscCodes.blr_varying);
152                                                 blr.WriteByte((byte)(len & 255));
153                                                 blr.WriteByte((byte)(len >> 8));
154                                                 break;
155
156                                         case IscCodes.SQL_TEXT:
157                                                 blr.WriteByte(IscCodes.blr_text);
158                                                 blr.WriteByte((byte)(len & 255));
159                                                 blr.WriteByte((byte)(len >> 8));
160                                                 break;
161
162                                         case IscCodes.SQL_DOUBLE:
163                                                 blr.WriteByte(IscCodes.blr_double);
164                                                 break;
165
166                                         case IscCodes.SQL_FLOAT:
167                                                 blr.WriteByte(IscCodes.blr_float);
168                                                 break;
169
170                                         case IscCodes.SQL_D_FLOAT:
171                                                 blr.WriteByte(IscCodes.blr_d_float);
172                                                 break;
173
174                                         case IscCodes.SQL_TYPE_DATE:
175                                                 blr.WriteByte(IscCodes.blr_sql_date);
176                                                 break;
177
178                                         case IscCodes.SQL_TYPE_TIME:
179                                                 blr.WriteByte(IscCodes.blr_sql_time);
180                                                 break;
181
182                                         case IscCodes.SQL_TIMESTAMP:
183                                                 blr.WriteByte(IscCodes.blr_timestamp);
184                                                 break;
185
186                                         case IscCodes.SQL_BLOB:
187                                                 blr.WriteByte(IscCodes.blr_quad);
188                                                 blr.WriteByte(0);
189                                                 break;
190
191                                         case IscCodes.SQL_ARRAY:
192                                                 blr.WriteByte(IscCodes.blr_quad);
193                                                 blr.WriteByte(0);
194                                                 break;
195
196                                         case IscCodes.SQL_LONG:
197                                                 blr.WriteByte(IscCodes.blr_long);
198                                                 blr.WriteByte((byte)this.fields[i].NumericScale);
199                                                 break;
200
201                                         case IscCodes.SQL_SHORT:
202                                                 blr.WriteByte(IscCodes.blr_short);
203                                                 blr.WriteByte((byte)this.fields[i].NumericScale);
204                                                 break;
205
206                                         case IscCodes.SQL_INT64:
207                                                 blr.WriteByte(IscCodes.blr_int64);
208                                                 blr.WriteByte((byte)this.fields[i].NumericScale);
209                                                 break;
210
211                                         case IscCodes.SQL_QUAD:
212                                                 blr.WriteByte(IscCodes.blr_quad);
213                                                 blr.WriteByte((byte)this.fields[i].NumericScale);
214                                                 break;
215                                 }
216
217                                 blr.WriteByte(IscCodes.blr_short);
218                                 blr.WriteByte(0);
219                         }
220
221                         blr.WriteByte(IscCodes.blr_end);
222                         blr.WriteByte(IscCodes.blr_eoc);
223
224                         return blr.ToArray();
225                 }
226
227                 #endregion
228         }
229 }