wasCSharpSQLite – Blame information for rev

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 office 1 //
2 // System.Data.Common.DbDataRecord.cs
3 //
4 // Author:
5 // Tim Coleman (tim@timcoleman.com)
6 //
7 // Copyright (C) Tim Coleman, 2002-2003
8 //
9  
10 //
11 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
12 //
13 // Permission is hereby granted, free of charge, to any person obtaining
14 // a copy of this software and associated documentation files (the
15 // "Software"), to deal in the Software without restriction, including
16 // without limitation the rights to use, copy, modify, merge, publish,
17 // distribute, sublicense, and/or sell copies of the Software, and to
18 // permit persons to whom the Software is furnished to do so, subject to
19 // the following conditions:
20 //
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
23 //
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 //
32  
33 using System.Collections;
34 using System.ComponentModel;
35 using System.Data;
36 using System.Runtime.CompilerServices;
37  
38 namespace System.Data.Common
39 {
40 public abstract class DbDataRecord : IDataRecord
41 {
42 protected DbDataRecord ()
43 {
44 }
45  
46 public abstract int FieldCount { get; }
47 public abstract object this [string name] { get; }
48 public abstract object this [int i] { get; }
49  
50 public abstract bool GetBoolean (int i);
51 public abstract byte GetByte (int i);
52 public abstract long GetBytes (int i, long dataIndex, byte [] buffer, int bufferIndex,int length);
53 public abstract char GetChar (int i);
54 public abstract long GetChars (int i, long dataIndex, char [] buffer, int bufferIndex, int length);
55 public abstract string GetDataTypeName (int i);
56 public abstract DateTime GetDateTime (int i);
57 public abstract decimal GetDecimal (int i);
58 public abstract double GetDouble (int i);
59 public abstract Type GetFieldType (int i);
60 public abstract float GetFloat (int i);
61 public abstract Guid GetGuid (int i);
62 public abstract short GetInt16 (int i);
63 public abstract int GetInt32 (int i);
64 public abstract long GetInt64 (int i);
65 public abstract string GetName (int i);
66 public abstract int GetOrdinal (string name);
67 public abstract string GetString (int i);
68 public abstract object GetValue (int i);
69 public abstract int GetValues (object [] values);
70 public abstract bool IsDBNull (int i);
71  
72 public IDataReader GetData (int i)
73 {
74 return (IDataReader) GetValue (i);
75 }
76  
77 }
78  
79 class DbDataRecordImpl : DbDataRecord
80 {
81 #region Fields
82  
83 readonly SchemaInfo [] schema;
84 readonly object [] values;
85 readonly int fieldCount;
86  
87 #endregion
88  
89 #region Constructors
90  
91 // FIXME: this class should actually be reimplemented to be one
92 // of the derived classes of DbDataRecord, which should become
93 // almost abstract.
94 internal DbDataRecordImpl (SchemaInfo[] schema, object[] values)
95 {
96 this.schema = schema;
97 this.values = values;
98 this.fieldCount = values.Length;
99 }
100  
101 #endregion
102  
103 #region Properties
104  
105 public override int FieldCount {
106 get { return fieldCount; }
107 }
108  
109 public override object this [string name] {
110 get { return this [GetOrdinal (name)]; }
111 }
112  
113 public override object this [int i] {
114 get { return GetValue (i); }
115 }
116  
117 #endregion
118  
119 #region Methods
120  
121 public override bool GetBoolean (int i)
122 {
123 return (bool) GetValue (i);
124 }
125  
126 public override byte GetByte (int i)
127 {
128 return (byte) GetValue (i);
129 }
130  
131 public override long GetBytes (int i, long dataIndex, byte[] buffer, int bufferIndex, int length)
132 {
133 object value = GetValue (i);
134 if (!(value is byte []))
135 throw new InvalidCastException ("Type is " + value.GetType ().ToString ());
136  
137 if ( buffer == null ) {
138 // Return length of data
139 return ((byte []) value).Length;
140 } else {
141 // Copy data into buffer
142 Array.Copy ((byte []) value, (int) dataIndex, buffer, bufferIndex, length);
143 return ((byte []) value).Length - dataIndex;
144 }
145 }
146  
147 public override char GetChar (int i)
148 {
149 return (char) GetValue (i);
150 }
151  
152 public override long GetChars (int i, long dataIndex, char[] buffer, int bufferIndex, int length)
153 {
154 object value = GetValue (i);
155 char [] valueBuffer;
156  
157 if (value is char[])
158 valueBuffer = (char []) value;
159 else if (value is string)
160 valueBuffer = ((string) value).ToCharArray ();
161 else
162 throw new InvalidCastException ("Type is " + value.GetType ().ToString ());
163  
164 if (buffer == null) {
165 // Return length of data
166 return valueBuffer.Length;
167 } else {
168 // Copy data into buffer
169 Array.Copy (valueBuffer, (int) dataIndex, buffer, bufferIndex, length);
170 return valueBuffer.Length - dataIndex;
171 }
172 }
173  
174 public override string GetDataTypeName (int i)
175 {
176 return schema[i].DataTypeName;
177 }
178  
179 public override DateTime GetDateTime (int i)
180 {
181 return (DateTime) GetValue (i);
182 }
183  
184 public override decimal GetDecimal (int i)
185 {
186 return (decimal) GetValue (i);
187 }
188  
189 public override double GetDouble (int i)
190 {
191 return (double) GetValue (i);
192 }
193  
194 public override Type GetFieldType (int i)
195 {
196 return schema[i].FieldType;
197 }
198  
199 public override float GetFloat (int i)
200 {
201 return (float) GetValue (i);
202 }
203  
204 public override Guid GetGuid (int i)
205 {
206 return (Guid) GetValue (i);
207 }
208  
209 public override short GetInt16 (int i)
210 {
211 return (short) GetValue (i);
212 }
213  
214 public override int GetInt32 (int i)
215 {
216 return (int) GetValue (i);
217 }
218  
219 public override long GetInt64 (int i)
220 {
221 return (long) GetValue (i);
222 }
223  
224 public override string GetName (int i)
225 {
226 return schema [i].ColumnName;
227 }
228  
229 public override int GetOrdinal (string name)
230 {
231 for (int i = 0; i < FieldCount; i++)
232 if (schema [i].ColumnName == name)
233 return i;
234 return -1;
235 }
236  
237 public override string GetString (int i)
238 {
239 return (string) GetValue (i);
240 }
241  
242 public override object GetValue (int i)
243 {
244 if (i < 0 || i > fieldCount)
245 throw new IndexOutOfRangeException ();
246 return values [i];
247 }
248  
249 public override int GetValues (object[] values)
250 {
251 if (values == null)
252 throw new ArgumentNullException("values");
253  
254 int count = values.Length > this.values.Length ? this.values.Length : values.Length;
255 for(int i = 0; i < count; i++)
256 values [i] = this.values [i];
257 return count;
258 }
259  
260 public override bool IsDBNull (int i)
261 {
262 return GetValue (i) == DBNull.Value;
263 }
264  
265 #endregion // Methods
266 }
267 }