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 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955
|
//
// This file is part of the OpenLink Software Virtuoso Open-Source (VOS)
// project.
//
// Copyright (C) 1998-2024 OpenLink Software
//
// This project is free software; you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by the
// Free Software Foundation; only version 2 of the License, dated June 1991.
//
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public License along
// with this program; if not, write to the Free Software Foundation, Inc.,
// 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
//
//
//
// $Id$
//
using System;
using System.Data;
using System.Data.Common;
using System.Text;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Collections;
#if ODBC_CLIENT
namespace OpenLink.Data.VirtuosoOdbcClient
#elif CLIENT
namespace OpenLink.Data.VirtuosoClient
#else
namespace OpenLink.Data.Virtuoso
#endif
{
internal class ColumnData
{
internal enum ColumnFlags
{
IsLong = 1,
IsNullable = 2,
IsReadOnly = 4,
IsKey = 8,
IsUnique = 16,
IsRowVersion = 32,
IsAutoIncrement = 64,
IsHidden = 128,
IsExpression = 256,
};
internal string columnName;
internal DataType columnType;
internal BufferType bufferType;
internal int columnSize;
internal short precision;
internal short scale;
internal ColumnFlags flags;
internal String baseSchemaName;
internal String baseCatalogName;
internal String baseTableName;
internal String baseColumnName;
internal int lobLength;
internal int lobOffset;
internal object data;
internal bool IsLong
{
get { return GetFlag (ColumnFlags.IsLong); }
set { SetFlag (ColumnFlags.IsLong, value); }
}
internal bool IsNullable
{
get { return GetFlag (ColumnFlags.IsNullable); }
set { SetFlag (ColumnFlags.IsNullable, value); }
}
internal bool IsReadOnly
{
get { return GetFlag (ColumnFlags.IsReadOnly); }
set { SetFlag (ColumnFlags.IsReadOnly, value); }
}
internal bool IsKey
{
get { return GetFlag (ColumnFlags.IsKey); }
set { SetFlag (ColumnFlags.IsKey, value); }
}
internal bool IsUnique
{
get { return GetFlag (ColumnFlags.IsUnique); }
set { SetFlag (ColumnFlags.IsUnique, value); }
}
internal bool IsRowVersion
{
get { return GetFlag (ColumnFlags.IsRowVersion); }
set { SetFlag (ColumnFlags.IsRowVersion, value); }
}
internal bool IsAutoIncrement
{
get { return GetFlag (ColumnFlags.IsAutoIncrement); }
set { SetFlag (ColumnFlags.IsAutoIncrement, value); }
}
internal bool IsHidden
{
get { return GetFlag (ColumnFlags.IsHidden); }
set { SetFlag (ColumnFlags.IsHidden, value); }
}
internal bool IsExpression
{
get { return GetFlag (ColumnFlags.IsExpression); }
set { SetFlag (ColumnFlags.IsExpression, value); }
}
private bool GetFlag (ColumnFlags f)
{
return (flags & f) != 0;
}
private void SetFlag (ColumnFlags f, bool value)
{
if (value)
flags |= f;
else
flags &= ~f;
}
};
/// <summary>
/// Summary description for VirtuosoDataReader.
/// </summary>
public sealed class VirtuosoDataReader :
#if ADONET2
DbDataReader, IDataReader, IDataRecord, IDisposable, IEnumerable
#else
MarshalByRefObject, IDataReader, IDataRecord, IDisposable, IEnumerable
#endif
{
// The DataReader should always be open when returned to the user.
private bool open = true;
// The current resultset is over.
private bool over = false;
// The last resultset was retrieved.
private bool last = false;
private int rows = 0;
private ColumnData[] columns = null;
private VirtuosoConnection connection;
private VirtuosoCommand command;
private IInnerCommand innerCommand;
private System.Data.CommandBehavior commandBehavior;
/*
* Because the user should not be able to directly create a
* DataReader object, the constructors are
* marked as internal.
*/
internal VirtuosoDataReader (
VirtuosoConnection connection,
IInnerCommand innerCommand,
VirtuosoCommand command,
CommandBehavior commandBehavior,
bool schemaOnly)
{
Debug.WriteLineIf (CLI.FnTrace.Enabled, "VirtuosoDataReader.ctor()");
Debug.Assert (connection != null);
Debug.Assert (innerCommand != null);
this.connection = connection;
this.command = command;
this.innerCommand = innerCommand;
this.commandBehavior = commandBehavior;
InitializeResultInfo (schemaOnly);
if (schemaOnly)
{
over = true;
last = true;
}
}
~VirtuosoDataReader ()
{
Dispose (false);
}
void IDisposable.Dispose ()
{
Dispose (true);
GC.SuppressFinalize (this);
}
/****
* METHODS / PROPERTIES FROM IDataReader.
****/
#if ADONET2
public override int Depth
#else
public int Depth
#endif
{
/*
* Always return a value of zero if nesting is not supported.
*/
get { return 0; }
}
#if ADONET2
public override bool IsClosed
#else
public bool IsClosed
#endif
{
/*
* Keep track of the reader state - some methods should be
* disallowed if the reader is closed.
*/
get { return !open; }
}
#if ADONET2
public override int RecordsAffected
#else
public int RecordsAffected
#endif
{
/*
* RecordsAffected is only applicable to batch statements
* that include inserts/updates/deletes.
*/
get
{
Debug.WriteLineIf (CLI.FnTrace.Enabled, "VirtuosoDataReader.RecordsAffected");
return rows;
}
}
IEnumerator IEnumerable.GetEnumerator ()
{
return new System.Data.Common.DbEnumerator (this, (commandBehavior & CommandBehavior.CloseConnection) != 0);
}
#if ADONET2
public override void Close ()
#else
public void Close ()
#endif
{
Debug.WriteLineIf (CLI.FnTrace.Enabled, "VirtuosoDataReader.Close()");
if (open)
{
if (command != null)
{
command.CloseDataReader ();
command = null;
}
if (connection != null && (commandBehavior & CommandBehavior.CloseConnection) != 0)
{
connection.Close ();
connection = null;
}
columns = null;
open = false;
}
}
#if ADONET2
public override bool NextResult ()
#else
public bool NextResult ()
#endif
{
Debug.WriteLineIf (CLI.FnTrace.Enabled, "VirtuosoDataReader.NextResult()");
if (!open)
throw new InvalidOperationException ("The VirtuosoDataReader object is closed.");
if (last)
return false;
bool next = command.GetNextResult ();
if (next)
InitializeResultInfo (false);
else
last = true;
return next;
}
#if ADONET2
public override bool Read ()
#else
public bool Read ()
#endif
{
Debug.WriteLineIf (CLI.FnTrace.Enabled, "VirtuosoDataReader.Read()");
// Return true if it is possible to advance and if you are still positioned
// on a valid row.
if (over)
return false;
if (!innerCommand.Fetch ())
{
over = true;
return false;
}
int n = FieldCount;
for (int i = 0; i < n; i++)
ResetColumn (i);
GC.KeepAlive (this);
return true;
}
#if ADONET2
public override DataTable GetSchemaTable ()
#else
public DataTable GetSchemaTable ()
#endif
{
Debug.WriteLineIf (CLI.FnTrace.Enabled, "VirtuosoDataReader.GetSchemaTable()");
if (columns == null)
throw new InvalidOperationException ();
int n = columns.Length;
DataTable table = new DataTable ("SchemaTable");
table.MinimumCapacity = n;
DataColumnCollection metaColumns = table.Columns;
DataColumn columnName = metaColumns.Add ("ColumnName", typeof (System.String));
DataColumn columnOrdinal = metaColumns.Add ("ColumnOrdinal", typeof (System.Int32));
DataColumn columnSize = metaColumns.Add ("ColumnSize", typeof (System.Int32));
DataColumn numericPrecision = metaColumns.Add ("NumericPrecision", typeof (System.Int16));
DataColumn numericScale = metaColumns.Add ("NumericScale", typeof (System.Int16));
DataColumn dataType = metaColumns.Add ("DataType", typeof (System.Type));
DataColumn providerType = metaColumns.Add ("ProviderType", typeof (System.Int32));
DataColumn isLong = metaColumns.Add ("IsLong", typeof (System.Boolean));
DataColumn allowDBNull = metaColumns.Add ("AllowDBNull", typeof (System.Boolean));
DataColumn isReadOnly = metaColumns.Add ("IsReadOnly", typeof (System.Boolean));
DataColumn isRowVersion = metaColumns.Add ("IsRowVersion", typeof (System.Boolean));
DataColumn isUnique = metaColumns.Add ("IsUnique", typeof (System.Boolean));
DataColumn isKey = metaColumns.Add ("IsKey", typeof (System.Boolean));
DataColumn isAutoIncrement = metaColumns.Add ("IsAutoIncrement", typeof (System.Boolean));
DataColumn baseSchemaName = metaColumns.Add ("BaseSchemaName", typeof (System.String));
DataColumn baseCatalogName = metaColumns.Add ("BaseCatalogName", typeof (System.String));
DataColumn baseTableName = metaColumns.Add ("BaseTableName", typeof (System.String));
DataColumn baseColumnName = metaColumns.Add ("BaseColumnName", typeof (System.String));
DataColumn isHidden = metaColumns.Add ("IsHidden", typeof (System.Boolean));
DataRowCollection rows = table.Rows;
for (int i = 0; i < n; i++)
{
DataRow row = table.NewRow ();
row[columnName] = columns[i].columnName;
row[columnOrdinal] = i + 1;
row[columnSize] = columns[i].columnSize;
row[dataType] = columns[i].columnType.bufferType.type;
row[providerType] = columns[i].columnType.sqlType;
switch (Type.GetTypeCode(columns[i].columnType.bufferType.type))
{
case TypeCode.Int16:
case TypeCode.UInt16:
case TypeCode.Int32:
case TypeCode.UInt32:
case TypeCode.Int64:
case TypeCode.UInt64:
case TypeCode.Single:
case TypeCode.Double:
case TypeCode.Byte:
case TypeCode.SByte:
case TypeCode.DateTime:
row[numericPrecision] = columns[i].precision;
break;
case TypeCode.Decimal:
row[numericPrecision] = columns[i].precision;
row[numericScale] = columns[i].scale;
break;
default:
break;
}
row[isLong] = columns[i].IsLong;
row[allowDBNull] = columns[i].IsNullable;
row[isReadOnly] = columns[i].IsReadOnly;
row[isRowVersion] = columns[i].IsRowVersion;
row[isUnique] = columns[i].IsUnique;
row[isKey] = columns[i].IsKey;
// Assume key columns are not nullable.
// If IDataReader.GetSchema table reports a key column as
// IsKeyColumn==true but with AllowDBNull==true,
// DataRowCollection.Find() reports that the table
// associated with the parent DataTable doesn't
// have a primary key.
row[isAutoIncrement] = columns[i].IsAutoIncrement;
row[baseSchemaName] = columns[i].baseSchemaName;
row[baseCatalogName] = columns[i].baseCatalogName;
row[baseTableName] = columns[i].baseTableName;
if (columns[i].baseColumnName == null || columns[i].baseColumnName == String.Empty)
row[baseColumnName] = columns[i].columnName;
else
row[baseColumnName] = columns[i].baseColumnName;
row[isHidden] = columns[i].IsHidden;
// Virtuoso returns the key as an additional column with
// the IsHidden property set to true. However, if there
// are two columns with IsKey set to true I get errors saying
// that there is not primary key for the table. This hack
// fixes the problem...
if ((bool)row[isHidden])
row[isKey] = false;
rows.Add (row);
row.AcceptChanges ();
}
return table;
}
/****
* METHODS / PROPERTIES FROM IDataRecord.
****/
#if ADONET2
public override int FieldCount
#else
public int FieldCount
#endif
{
// Return the count of the number of columns, which in
// this case is the size of the column metadata
// array.
get {
int col;
col = (columns == null ? 0 : columns.Length);
Debug.WriteLineIf (CLI.FnTrace.Enabled, "VirtuosoDataReader.FieldCount() = " + col);
return columns == null ? 0 : columns.Length; }
}
#if ADONET2
public override String GetName (int i)
#else
public String GetName (int i)
#endif
{
Debug.WriteLineIf (CLI.FnTrace.Enabled, "VirtuosoDataReader.GetName()");
return columns[i].columnName;
}
#if ADONET2
public override String GetDataTypeName (int i)
#else
public String GetDataTypeName (int i)
#endif
{
Debug.WriteLineIf (CLI.FnTrace.Enabled, "VirtuosoDataReader.GetDataTypeName()");
return columns[i].columnType.typeName;
}
#if ADONET2
public override Type GetFieldType (int i)
#else
public Type GetFieldType (int i)
#endif
{
Debug.WriteLineIf (CLI.FnTrace.Enabled, "VirtuosoDataReader.GetFieldType()");
// Return the actual Type class for the data type.
return columns[i].columnType.bufferType.type;
}
#if ADONET2
public override Object GetValue (int i)
#else
public Object GetValue (int i)
#endif
{
Debug.WriteLineIf (CLI.FnTrace.Enabled, "VirtuosoDataReader.GetValue()");
return GetColumnData (i);
}
#if ADONET2
public override int GetValues (object[] values)
#else
public int GetValues (object[] values)
#endif
{
Debug.WriteLineIf (CLI.FnTrace.Enabled, "VirtuosoDataReader.GetValues()");
int n = Math.Min (values.Length, FieldCount);
for (int i = 0; i < n; i++)
{
Debug.WriteLineIf (CLI.FnTrace.Enabled, String.Format ("VirtuosoDataReader.GetValues() : getting col {0} ({1})",
i, columns[i].columnName));
values[i] = GetColumnData (i);
}
return n;
}
#if ADONET2
public override int GetOrdinal (string name)
#else
public int GetOrdinal (string name)
#endif
{
Debug.WriteLineIf (CLI.FnTrace.Enabled, "VirtuosoDataReader.GetOrdinal()");
// Look for the ordinal of the column with the same name and return it.
int n = FieldCount;
for (int i = 0; i < n; i++)
{
if (0 == Platform.CaseInsensitiveCompare (name, columns[i].columnName))
{
return i;
}
}
// Throw an exception if the ordinal cannot be found.
throw new IndexOutOfRangeException ("Could not find specified column in results");
}
#if ADONET2
public override object this [ int i ]
#else
public object this [ int i ]
#endif
{
get { return GetColumnData (i); }
}
#if ADONET2
public override object this [ String name ]
#else
public object this [ String name ]
#endif
{
// Look up the ordinal and return
// the value at that position.
get { return GetColumnData (GetOrdinal (name)); }
}
#if ADONET2
public override bool GetBoolean (int i)
#else
public bool GetBoolean (int i)
#endif
{
Debug.WriteLineIf (CLI.FnTrace.Enabled, "VirtuosoDataReader.GetBoolean()");
/*
* Force the cast to return the type. InvalidCastException
* should be thrown if the data is not already of the correct type.
*/
return Convert.ToBoolean(GetColumnData(i));
}
#if ADONET2
public override byte GetByte (int i)
#else
public byte GetByte (int i)
#endif
{
Debug.WriteLineIf (CLI.FnTrace.Enabled, "VirtuosoDataReader.GetByte()");
/*
* Force the cast to return the type. InvalidCastException
* should be thrown if the data is not already of the correct type.
*/
return Convert.ToByte(GetColumnData(i));
}
#if ADONET2
public override long GetBytes (int i, long fieldOffset, byte[] buffer, int bufferOffset, int length)
#else
public long GetBytes (int i, long fieldOffset, byte[] buffer, int bufferOffset, int length)
#endif
{
Debug.WriteLineIf (CLI.FnTrace.Enabled, "VirtuosoDataReader.GetBytes()");
if (fieldOffset < 0)
throw new ArgumentException ("Invalid fieldOffset value");
ColumnData column = columns[i];
if (column.data == null
&& column.columnType.isLong
&& (commandBehavior & CommandBehavior.SequentialAccess) != 0)
return innerCommand.GetBytes (i, columns, fieldOffset, buffer, bufferOffset, length);
byte[] data = (byte[]) GetColumnData (i);
if (buffer == null)
return data.Length < fieldOffset ? 0 : data.Length - fieldOffset;
if (length > (data.Length - fieldOffset))
length = (int) (data.Length - fieldOffset);
Array.Copy (data, (int) fieldOffset, buffer, bufferOffset, length);
return length;
}
#if ADONET2
public override char GetChar (int i)
#else
public char GetChar (int i)
#endif
{
/*
* Force the cast to return the type. InvalidCastException
* should be thrown if the data is not already of the correct type.
*/
Debug.WriteLineIf (CLI.FnTrace.Enabled, "VirtuosoDataReader.GetChar()");
return Convert.ToChar(GetColumnData (i));
}
#if ADONET2
public override long GetChars (int i, long fieldOffset, char[] buffer, int bufferOffset, int length)
#else
public long GetChars (int i, long fieldOffset, char[] buffer, int bufferOffset, int length)
#endif
{
Debug.WriteLineIf (CLI.FnTrace.Enabled, "VirtuosoDataReader.GetChars()");
if (fieldOffset < 0)
throw new ArgumentException ("Invalid fieldOffset value");
ColumnData column = columns[i];
if (column.data == null
&& column.columnType.isLong
&& (commandBehavior & CommandBehavior.SequentialAccess) != 0)
return innerCommand.GetChars (i, columns, fieldOffset, buffer, bufferOffset, length);
char[] data = ((string) GetColumnData (i)).ToCharArray ();
if (buffer == null)
return data.Length < fieldOffset ? 0 : data.Length - fieldOffset;
if (length > (data.Length - fieldOffset))
length = (int) (data.Length - fieldOffset);
Array.Copy (data, (int) fieldOffset, buffer, bufferOffset, length);
return length;
}
#if ADONET2
public override Guid GetGuid (int i)
#else
public Guid GetGuid (int i)
#endif
{
/*
* Force the cast to return the type. InvalidCastException
* should be thrown if the data is not already of the correct type.
*/
Debug.WriteLineIf (CLI.FnTrace.Enabled, "VirtuosoDataReader.GetGuid(})");
return (Guid) GetColumnData (i);
}
#if ADONET2
public override Int16 GetInt16 (int i)
#else
public Int16 GetInt16 (int i)
#endif
{
/*
* Force the cast to return the type. InvalidCastException
* should be thrown if the data is not already of the correct type.
*/
Debug.WriteLineIf (CLI.FnTrace.Enabled, "VirtuosoDataReader.GetInt16()");
return Convert.ToInt16(GetColumnData (i));
}
#if ADONET2
public override Int32 GetInt32 (int i)
#else
public Int32 GetInt32 (int i)
#endif
{
/*
* Force the cast to return the type. InvalidCastException
* should be thrown if the data is not already of the correct type.
*/
Debug.WriteLineIf (CLI.FnTrace.Enabled, "VirtuosoDataReader.GetInt32()");
return Convert.ToInt32(GetColumnData (i));
}
#if ADONET2
public override Int64 GetInt64 (int i)
#else
public Int64 GetInt64 (int i)
#endif
{
/*
* Force the cast to return the type. InvalidCastException
* should be thrown if the data is not already of the correct type.
*/
Debug.WriteLineIf (CLI.FnTrace.Enabled, "VirtuosoDataReader.GetInt64()");
return Convert.ToInt64(GetColumnData (i));
}
#if ADONET2
public override float GetFloat (int i)
#else
public float GetFloat (int i)
#endif
{
/*
* Force the cast to return the type. InvalidCastException
* should be thrown if the data is not already of the correct type.
*/
Debug.WriteLineIf (CLI.FnTrace.Enabled, "VirtuosoDataReader.GetFloat()");
return Convert.ToSingle(GetColumnData (i));
}
#if ADONET2
public override double GetDouble (int i)
#else
public double GetDouble (int i)
#endif
{
/*
* Force the cast to return the type. InvalidCastException
* should be thrown if the data is not already of the correct type.
*/
Debug.WriteLineIf (CLI.FnTrace.Enabled, "VirtuosoDataReader.GetDouble()");
return Convert.ToDouble(GetColumnData(i));
}
#if ADONET2
public override String GetString (int i)
#else
public String GetString (int i)
#endif
{
/*
* Force the cast to return the type. InvalidCastException
* should be thrown if the data is not already of the correct type.
*/
Debug.WriteLineIf (CLI.FnTrace.Enabled, "VirtuosoDataReader.GetString()");
object cd = GetColumnData (i);
if (cd is String)
return (String) cd;
else if (cd is System.Byte[])
{
byte[] v = (byte[])cd;
StringBuilder buf = new StringBuilder(v.Length*2);
string hex = "0123456789ABCDEF";
for (int j = 0; j < v.Length; j++)
{
buf.Append(hex[(v[j] >> 4) & 0x0f]);
buf.Append(hex[v[j] & 0x0f]);
}
return buf.ToString();
}
else
return cd.ToString();
}
#if ADONET2
public override Decimal GetDecimal (int i)
#else
public Decimal GetDecimal (int i)
#endif
{
/*
* Force the cast to return the type. InvalidCastException
* should be thrown if the data is not already of the correct type.
*/
Debug.WriteLineIf (CLI.FnTrace.Enabled, "VirtuosoDataReader.GetDecimal()");
return Convert.ToDecimal(GetColumnData (i));
}
#if ADONET2
public override DateTime GetDateTime (int i)
#else
public DateTime GetDateTime (int i)
#endif
{
/*
* Force the cast to return the type. InvalidCastException
* should be thrown if the data is not already of the correct type.
*/
Debug.WriteLineIf (CLI.FnTrace.Enabled, "VirtuosoDataReader.GetDateTime()");
return Convert.ToDateTime(GetColumnData (i));
}
public SqlXml GetSqlXml (int i)
{
/*
* Force the cast to return the type. InvalidCastException
* should be thrown if the data is not already of the correct type.
*/
Debug.WriteLineIf (CLI.FnTrace.Enabled, "VirtuosoDataReader.GetSqlXml()");
return (SqlXml) GetColumnData (i);
}
#if !ADONET2
public IDataReader GetData (int i)
{
/*
* The sample code does not support this method. Normally,
* this would be used to expose nested tables and
* other hierarchical data.
*/
Debug.WriteLineIf (CLI.FnTrace.Enabled, "VirtuosoDataReader.GetData()");
throw new NotSupportedException("GetData not supported.");
}
#endif
#if ADONET2
public override bool IsDBNull (int i)
#else
public bool IsDBNull (int i)
#endif
{
Debug.WriteLineIf (CLI.FnTrace.Enabled, "VirtuosoDataReader.IsDBNull()");
ColumnData column = columns[i];
if (column.data == null && column.columnType.isLong)
return innerCommand.IsDBNull (i, columns);
return Convert.IsDBNull (GetColumnData (i));
}
/*
* Implementation specific methods.
*/
internal ColumnData[] Columns
{
get { return columns; }
}
private void InitializeResultInfo (bool schemaOnly)
{
if (schemaOnly)
rows = 0;
else
rows = innerCommand.GetRowCount ();
columns = innerCommand.GetColumnMetaData ();
if (columns == null)
over = true;
else
over = false;
}
private object GetColumnData (int i)
{
ColumnData column = columns[i];
Type type = column.columnType.bufferType.type;
if (column.data == null)
column.data = innerCommand.GetColumnData (i, columns);
if (column.data != null && !Convert.IsDBNull(column.data) && type != column.data.GetType())
{
if (column.data is IConvertData)
return ((IConvertData)column.data).ConvertData(type);
else if (column.data is IConvertible)
{
switch (column.data.GetType().FullName)
{
case "System.Int64":
case "System.Int32":
case "System.Single":
case "System.Double":
case "System.Decimal":
case "System.DateTime":
case "OpenLink.Data.Virtuoso.VirtuosoDateTime":
case "System.DateTimeOffset":
case "OpenLink.Data.Virtuoso.VirtuosoDateTimeOffset":
case "System.TimeSpan":
case "OpenLink.Data.Virtuoso.VirtuosoTimeSpan":
return column.data;
default:
return Convert.ChangeType(column.data, type);
}
}
else if (column.data is SqlExtendedString)
return column.data;
else if (column.data is SqlRdfBox)
return column.data;
else
{
switch (column.data.GetType().FullName)
{
case "System.DateTime":
case "OpenLink.Data.Virtuoso.VirtuosoDateTime":
case "System.DateTimeOffset":
case "OpenLink.Data.Virtuoso.VirtuosoDateTimeOffset":
case "System.TimeSpan":
case "OpenLink.Data.Virtuoso.VirtuosoTimeSpan":
case "System.Byte[]":
return column.data;
default:
return column.data.ToString();
}
}
}
else
return column.data;
}
private void ResetColumn (int i)
{
ColumnData column = columns[i];
column.data = null;
column.lobLength = 0;
column.lobOffset = 0;
}
#if ADONET2
protected override void Dispose (bool disposing)
#else
private void Dispose (bool disposing)
#endif
{
try
{
if (disposing)
Close ();
connection = null;
}
catch (Exception e)
{
Debug.WriteLineIf(CLI.FnTrace.Enabled,
"VirtousoDataReader.Dispose caught exception: " + e.Message);
}
}
#region ADO.NET 2.0
#if ADONET2
public override bool HasRows
{
get {
//TODO:
throw new global::System.NotImplementedException();
}
}
public new void Dispose ()
{
}
public override IEnumerator GetEnumerator()
{
//TODO:
throw new global::System.NotImplementedException();
}
#endif
#endregion
}
}
|