Welcome to mirror list, hosted at ThFree Co, Russian Federation.

OdbcDataReader.cs « System.Data.Odbc « System.Data « class « mcs - github.com/mono/mono.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 61b49d55481c3ba10a9da6785d5cf1a422fa5712 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
//
// System.Data.Odbc.OdbcDataReader
//
// Author:
//   Brian Ritchie (brianlritchie@hotmail.com) 
//   Daniel Morgan <danmorg@sc.rr.com>
//
// Copyright (C) Brian Ritchie, 2002
// Copyright (C) Daniel Morgan, 2002
//

//
// Copyright (C) 2004 Novell, Inc (http://www.novell.com)
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
// 
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//

using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.Common;
using System.Text;

namespace System.Data.Odbc
{
	public sealed class OdbcDataReader : MarshalByRefObject, IDataReader, IDisposable, IDataRecord, IEnumerable
	{
		#region Fields
		
		private OdbcCommand command;
		private bool open;
		private int currentRow;
		private OdbcColumn[] cols;
		private IntPtr hstmt;
		private CommandBehavior behavior;

		#endregion

		#region Constructors

		internal OdbcDataReader (OdbcCommand command, CommandBehavior behavior) 
		{
			this.command = command;
			this.behavior=behavior;
			open = true;
			currentRow = -1;
			hstmt=command.hStmt;
			// Init columns array;
			short colcount=0;
			libodbc.SQLNumResultCols(hstmt, ref colcount);
			cols=new OdbcColumn[colcount];
		}

		#endregion

		#region Properties

		public int Depth {
			get {
				return 0; // no nested selects supported
			}
		}

		public int FieldCount {
			get {
				return cols.Length;
			}
		}

		public bool IsClosed {
			get {
				return !open;
			}
		}

		public object this[string name] {
			get {
				int pos;

				if (currentRow == -1)
					throw new InvalidOperationException ();

				pos = ColIndex(name);
				
				if (pos == -1)
					throw new IndexOutOfRangeException ();

				return this[pos];
			}
		}

		public object this[int index] {
			get {
				return (object) GetValue (index);
			}
		}

		public int RecordsAffected {
			get {
				return -1;
			}
		}

		[MonoTODO]
		public bool HasRows {
			get { throw new NotImplementedException(); }
		}

		#endregion

		#region Methods
		
		private int ColIndex(string colname)
		{
			int i=0;
			foreach (OdbcColumn col in cols)
			{
				if (col.ColumnName==colname)
					return i;
				i++;
			}
			return 0;
		}

		// Dynamically load column descriptions as needed.
		private OdbcColumn GetColumn(int ordinal)
		{
			if (cols[ordinal]==null)
			{
				short bufsize=255;
				byte[] colname_buffer=new byte[bufsize];
				string colname;
				short colname_size=0;
				uint ColSize=0;
				short DecDigits=0, Nullable=0, dt=0;
				OdbcReturn ret=libodbc.SQLDescribeCol(hstmt, Convert.ToUInt16(ordinal+1), 
					colname_buffer, bufsize, ref colname_size, ref dt, ref ColSize, 
					ref DecDigits, ref Nullable);
				if ((ret!=OdbcReturn.Success) && (ret!=OdbcReturn.SuccessWithInfo)) 
					throw new OdbcException(new OdbcError("SQLDescribeCol",OdbcHandleType.Stmt,hstmt));
				colname=System.Text.Encoding.Default.GetString(colname_buffer);
				colname=colname.Replace((char) 0,' ').Trim();
				OdbcColumn c=new OdbcColumn(colname, (OdbcType) dt);
				c.AllowDBNull=(Nullable!=0);
				c.Digits=DecDigits;
				if (c.IsStringType)
					c.MaxLength=(int)ColSize;
				cols[ordinal]=c;
			}
			return cols[ordinal];
		}

		public void Close ()
		{
		
			OdbcReturn ret=libodbc.SQLCloseCursor(hstmt);
			if ((ret!=OdbcReturn.Success) && (ret!=OdbcReturn.SuccessWithInfo)) 
				throw new OdbcException(new OdbcError("SQLCloseCursor",OdbcHandleType.Stmt,hstmt));
	
			open = false;
			currentRow = -1;

			if ((behavior & CommandBehavior.CloseConnection)==CommandBehavior.CloseConnection)
				this.command.Connection.Close();
			ret = libodbc.SQLFreeHandle( (ushort) OdbcHandleType.Stmt, hstmt);
			if ((ret!=OdbcReturn.Success) && (ret!=OdbcReturn.SuccessWithInfo)) 
				throw new OdbcException(new OdbcError("SQLFreeHandle",OdbcHandleType.Stmt,hstmt));
		}

		~OdbcDataReader ()
		{
			if (open)
				Close ();
		}

		public bool GetBoolean (int ordinal)
		{
			return (bool) GetValue(ordinal);
		}

		public byte GetByte (int ordinal)
		{
			return (byte) Convert.ToByte(GetValue(ordinal));
		}

		public long GetBytes (int ordinal, long dataIndex, byte[] buffer, int bufferIndex, int length)
		{
                        OdbcReturn ret = OdbcReturn.Error;
                        bool copyBuffer = false;
                        int returnVal = 0, outsize = 0;
                        byte [] tbuff = new byte [length+1];

                        length = buffer == null ? 0 : length;
                        ret=libodbc.SQLGetData (hstmt, (ushort) (ordinal+1), OdbcType.Binary, tbuff, length, 
                                        ref outsize);

                        if (ret == OdbcReturn.NoData)
                                return 0;

                        if ( (ret != OdbcReturn.Success) && (ret != OdbcReturn.SuccessWithInfo)) 
                                throw new OdbcException (new OdbcError ("SQLGetData", OdbcHandleType.Stmt, hstmt));

			OdbcError odbcErr = null;
			if ( (ret == OdbcReturn.SuccessWithInfo))
				odbcErr = new OdbcError ("SQLGetData", OdbcHandleType.Stmt, hstmt);

                        if (buffer == null)
                                return outsize; //if buffer is null,return length of the field
                        
                        if (ret == OdbcReturn.SuccessWithInfo) {
                                if (outsize == (int) OdbcLengthIndicator.NoTotal)
                                        copyBuffer = true;
                                else if (outsize == (int) OdbcLengthIndicator.NullData) {
                                        copyBuffer = false;
                                        returnVal = -1;
                                } else {
                                        string sqlstate = odbcErr.SQLState;
                                        //SQLState: String Data, Right truncated
                                        if (sqlstate != libodbc.SQLSTATE_RIGHT_TRUNC) 
                                                throw new OdbcException ( odbcErr);
                                        copyBuffer = true;
                                }
                        } else {
                                copyBuffer = outsize == -1 ? false : true;
                                returnVal = outsize;
                        }

                        if (copyBuffer) {
                                int i = 0;
                                while (tbuff [i] != libodbc.C_NULL) {
                                        buffer [bufferIndex + i] = tbuff [i];
                                        i++;
                                }
                                returnVal = i;
                        }
                        return returnVal;
		}
		
		[MonoTODO]
		public char GetChar (int ordinal)
		{
			throw new NotImplementedException ();
		}

		[MonoTODO]
		public long GetChars (int ordinal, long dataIndex, char[] buffer, int bufferIndex, int length)
		{
			throw new NotImplementedException ();
		}

		[MonoTODO]
		[EditorBrowsableAttribute (EditorBrowsableState.Never)]
		public IDataReader GetData (int ordinal)
		{
			throw new NotImplementedException ();
		}

		public string GetDataTypeName (int index)
		{
			return GetColumn(index).OdbcType.ToString();
		}

		public DateTime GetDate(int ordinal) {
			return GetDateTime(ordinal);
		}

		public DateTime GetDateTime (int ordinal)
		{
			return (DateTime) GetValue(ordinal);
		}

		[MonoTODO]
		public decimal GetDecimal (int ordinal)
		{
			throw new NotImplementedException ();
		}

		public double GetDouble (int ordinal)
		{
			return (double) GetValue(ordinal);
		}

		public Type GetFieldType (int index)
		{
			return GetColumn(index).DataType;
		}

		public float GetFloat (int ordinal)
		{
			return (float) GetValue(ordinal);
		}

		[MonoTODO]
		public Guid GetGuid (int ordinal)
		{
			throw new NotImplementedException ();
		}

		public short GetInt16 (int ordinal)
		{
			return (short) GetValue(ordinal);
		}

		public int GetInt32 (int ordinal)
		{
			return (int) GetValue(ordinal);
		}

		public long GetInt64 (int ordinal)
		{
			return (long) GetValue(ordinal);
		}

		public string GetName (int index)
		{
			return GetColumn(index).ColumnName;
		}

		public int GetOrdinal (string name)
		{
			if (currentRow == -1)
				throw new IndexOutOfRangeException ();

			int i=ColIndex(name);

			if (i==-1)
				throw new IndexOutOfRangeException ();
			else
				return i;
		}

		[MonoTODO]
		public DataTable GetSchemaTable() 
		{	

			DataTable dataTableSchema = null;
			// Only Results from SQL SELECT Queries 
			// get a DataTable for schema of the result
			// otherwise, DataTable is null reference
			if(cols.Length > 0) 
			{
				
				dataTableSchema = new DataTable ();
				
				dataTableSchema.Columns.Add ("ColumnName", typeof (string));
				dataTableSchema.Columns.Add ("ColumnOrdinal", typeof (int));
				dataTableSchema.Columns.Add ("ColumnSize", typeof (int));
				dataTableSchema.Columns.Add ("NumericPrecision", typeof (int));
				dataTableSchema.Columns.Add ("NumericScale", typeof (int));
				dataTableSchema.Columns.Add ("IsUnique", typeof (bool));
				dataTableSchema.Columns.Add ("IsKey", typeof (bool));
				DataColumn dc = dataTableSchema.Columns["IsKey"];
				dc.AllowDBNull = true; // IsKey can have a DBNull
				dataTableSchema.Columns.Add ("BaseCatalogName", typeof (string));
				dataTableSchema.Columns.Add ("BaseColumnName", typeof (string));
				dataTableSchema.Columns.Add ("BaseSchemaName", typeof (string));
				dataTableSchema.Columns.Add ("BaseTableName", typeof (string));
				dataTableSchema.Columns.Add ("DataType", typeof(Type));
				dataTableSchema.Columns.Add ("AllowDBNull", typeof (bool));
				dataTableSchema.Columns.Add ("ProviderType", typeof (int));
				dataTableSchema.Columns.Add ("IsAliased", typeof (bool));
				dataTableSchema.Columns.Add ("IsExpression", typeof (bool));
				dataTableSchema.Columns.Add ("IsIdentity", typeof (bool));
				dataTableSchema.Columns.Add ("IsAutoIncrement", typeof (bool));
				dataTableSchema.Columns.Add ("IsRowVersion", typeof (bool));
				dataTableSchema.Columns.Add ("IsHidden", typeof (bool));
				dataTableSchema.Columns.Add ("IsLong", typeof (bool));
				dataTableSchema.Columns.Add ("IsReadOnly", typeof (bool));

				DataRow schemaRow;
								
				for (int i = 0; i < cols.Length; i += 1 ) 
				{
					OdbcColumn col=GetColumn(i);

					schemaRow = dataTableSchema.NewRow ();
					dataTableSchema.Rows.Add (schemaRow);
										
					schemaRow["ColumnName"] = col.ColumnName;
					schemaRow["ColumnOrdinal"] = i + 1;
					
					schemaRow["ColumnSize"] = col.MaxLength;
					schemaRow["NumericPrecision"] = 0;
					schemaRow["NumericScale"] = 0;
					// TODO: need to get KeyInfo
					schemaRow["IsUnique"] = false;
					schemaRow["IsKey"] = DBNull.Value;					

					schemaRow["BaseCatalogName"] = "";				
					schemaRow["BaseColumnName"] = col.ColumnName;
					schemaRow["BaseSchemaName"] = "";
					schemaRow["BaseTableName"] = "";
					schemaRow["DataType"] = col.DataType;

					schemaRow["AllowDBNull"] = col.AllowDBNull;
					
					schemaRow["ProviderType"] = (int) col.OdbcType;
					// TODO: all of these
					schemaRow["IsAliased"] = false;
					schemaRow["IsExpression"] = false;
					schemaRow["IsIdentity"] = false;
					schemaRow["IsAutoIncrement"] = false;
					schemaRow["IsRowVersion"] = false;
					schemaRow["IsHidden"] = false;
					schemaRow["IsLong"] = false;
					schemaRow["IsReadOnly"] = false;
					
					// FIXME: according to Brian, 
					// this does not work on MS .NET
					// however, we need it for Mono 
					// for now
					schemaRow.AcceptChanges();
					
				}

			}
			dataTableSchema.AcceptChanges();
			return dataTableSchema;
		}

		public string GetString (int ordinal)
		{
			return (string) GetValue(ordinal);
		}

		[MonoTODO]
		public TimeSpan GetTime (int ordinal)
		{
			throw new NotImplementedException ();
		}

		public object GetValue (int ordinal)
		{
			if (currentRow == -1)
				throw new IndexOutOfRangeException ();

			if (ordinal>cols.Length-1 || ordinal<0)
				throw new IndexOutOfRangeException ();

			OdbcReturn ret;
			int outsize=0, bufsize;
			byte[] buffer;
			OdbcColumn col=GetColumn(ordinal);
			object DataValue=null;
			ushort ColIndex=Convert.ToUInt16(ordinal+1);

			// Check cached values
			if (col.Value==null)
			{

				// odbc help file
				// mk:@MSITStore:C:\program%20files\Microsoft%20Data%20Access%20SDK\Docs\odbc.chm::/htm/odbcc_data_types.htm
				switch (col.OdbcType)
				{
					case OdbcType.Decimal:
						bufsize=50;
						buffer=new byte[bufsize];  // According to sqlext.h, use SQL_CHAR for decimal
						ret=libodbc.SQLGetData(hstmt, ColIndex, OdbcType.Char, buffer, bufsize, ref outsize);
						byte[] temp = new byte[outsize];
                                                for (int i=0;i<outsize;i++)
                                                        temp[i]=buffer[i];
                                                                                                    
						if (outsize!=-1)
							DataValue=Decimal.Parse(System.Text.Encoding.Default.GetString(temp));
						break;
					case OdbcType.TinyInt:
						short short_data=0;
						ret=libodbc.SQLGetData(hstmt, ColIndex, OdbcType.TinyInt, ref short_data, 0, ref outsize);
						DataValue = System.Convert.ToByte (short_data);
						break;
					 case OdbcType.SmallInt:
                                                short sint_data=0;
                                                ret=libodbc.SQLGetData(hstmt, ColIndex, OdbcType.SmallInt, ref sint_data, 0, ref outsize);
                                                DataValue=sint_data;
                                                break;
					case OdbcType.Int:
						int int_data=0;
						ret=libodbc.SQLGetData(hstmt, ColIndex, OdbcType.Int, ref int_data, 0, ref outsize);
						DataValue=int_data;
						break;
					case OdbcType.BigInt:
						long long_data=0;
						ret=libodbc.SQLGetData(hstmt, ColIndex, OdbcType.BigInt, ref long_data, 0, ref outsize);
						DataValue=long_data;
						break;
					case OdbcType.NVarChar:
						bufsize=col.MaxLength*2+1; // Unicode is double byte
						buffer=new byte[bufsize];
						ret=libodbc.SQLGetData(hstmt, ColIndex, OdbcType.NVarChar, buffer, bufsize, ref outsize);
						if (outsize!=-1)
							DataValue=System.Text.Encoding.Unicode.GetString(buffer,0,outsize);
						break;
					case OdbcType.VarChar:
						bufsize=col.MaxLength+1;
						buffer=new byte[bufsize];  // According to sqlext.h, use SQL_CHAR for both char and varchar
						ret=libodbc.SQLGetData(hstmt, ColIndex, OdbcType.Char, buffer, bufsize, ref outsize);
						if (outsize!=-1)
							DataValue=System.Text.Encoding.Default.GetString(buffer,0,outsize);
						break;
					case OdbcType.Real:
						float float_data=0;
						ret=libodbc.SQLGetData(hstmt, ColIndex, OdbcType.Real, ref float_data, 0, ref outsize);
						DataValue=float_data;
						break;
					case OdbcType.Timestamp:
					case OdbcType.DateTime:
					case OdbcType.Date:
					case OdbcType.Time:
						OdbcTimestamp ts_data=new OdbcTimestamp();
						if (col.OdbcType == OdbcType.Timestamp) 
							ret=libodbc.SQLGetData(hstmt, ColIndex, OdbcType.Timestamp, ref ts_data, 0, ref outsize);
						else if (col.OdbcType == OdbcType.DateTime)
							ret=libodbc.SQLGetData(hstmt, ColIndex, OdbcType.DateTime, ref ts_data, 0, ref outsize);
						else if (col.OdbcType == OdbcType.Date)
							ret=libodbc.SQLGetData(hstmt, ColIndex, OdbcType.Date, ref ts_data, 0, ref outsize);
						else  // FIXME: how to get TIME datatype ??
							ret=libodbc.SQLGetData(hstmt, ColIndex, OdbcType.DateTime, ref ts_data, 0, ref outsize);
						if (outsize!=-1) // This means SQL_NULL_DATA 
							DataValue=new DateTime(ts_data.year,ts_data.month,ts_data.day,ts_data.hour,
								ts_data.minute,ts_data.second,Convert.ToInt32(ts_data.fraction));
						break;
                                        case OdbcType.Binary :
                                        case OdbcType.Image :
                                                bufsize = col.MaxLength + 1;
                                                buffer = new byte [bufsize];
                                                long read = GetBytes (ordinal, 0, buffer, 0, bufsize);
                                                ret = OdbcReturn.Success;
                                                DataValue = buffer;
                                                break;
					default:
						bufsize=255;
						buffer=new byte[bufsize];
						ret=libodbc.SQLGetData(hstmt, ColIndex, OdbcType.Char, buffer, bufsize, ref outsize);
						DataValue=System.Text.Encoding.Default.GetString(buffer);
						break;
				}

				if ((ret!=OdbcReturn.Success) && (ret!=OdbcReturn.SuccessWithInfo)) 
					throw new OdbcException(new OdbcError("SQLGetData",OdbcHandleType.Stmt,hstmt));

				if (outsize==-1) // This means SQL_NULL_DATA 
					col.Value=DBNull.Value;
				else
					col.Value=DataValue;
			}
			return col.Value;
		}
		
		public int GetValues (object[] values)
		{
			int numValues = 0;

			// copy values
			for (int i = 0; i < values.Length; i++) {
				if (i < FieldCount) {
					values[i] = GetValue(i);
				}
				else {
					values[i] = null;
				}
			}

			// get number of object instances in array
			if (values.Length < FieldCount)
				numValues = values.Length;
			else if (values.Length == FieldCount)
				numValues = FieldCount;
			else
				numValues = FieldCount;

			return numValues;
		}

		[MonoTODO]
		IDataReader IDataRecord.GetData (int ordinal)
		{
			throw new NotImplementedException ();
		}

		[MonoTODO]
		void IDisposable.Dispose ()
		{
		}

		[MonoTODO]
		IEnumerator IEnumerable.GetEnumerator ()
		{
			return new DbEnumerator (this);
		}

		public bool IsDBNull (int ordinal)
		{
			return (GetValue(ordinal) is DBNull);
		}

		public bool NextResult ()
		{
			OdbcReturn ret=libodbc.SQLFetch(hstmt);
			if (ret!=OdbcReturn.Success)
				currentRow=-1;
			else
				currentRow++;
			GetSchemaTable();
			
			// Clear cached values from last record
			foreach (OdbcColumn col in cols)
			{
				if (col!=null)
					col.Value=null;
			}
			return (ret==OdbcReturn.Success);
		}

		public bool Read ()
		{
			return NextResult();
		}

		#endregion
	}
}