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 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154
|
//
// System.Data.Odbc.OdbcDataReader
//
// Author:
// Brian Ritchie (brianlritchie@hotmail.com)
// Daniel Morgan <danmorg@sc.rr.com>
// Sureshkumar T <tsureshkumar@novell.com> (2004)
//
// 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.Globalization;
using System.Text;
namespace System.Data.Odbc
{
#if NET_2_0
public sealed class OdbcDataReader : DbDataReader
#else
public sealed class OdbcDataReader : MarshalByRefObject, IDataReader, IDisposable, IDataRecord, IEnumerable
#endif
{
#region Fields
private OdbcCommand command;
private bool open;
private int currentRow;
private OdbcColumn[] cols;
private IntPtr hstmt;
private int _recordsAffected = -1;
bool disposed;
private DataTable _dataTableSchema;
private CommandBehavior behavior;
#endregion
#region Constructors
internal OdbcDataReader (OdbcCommand command, CommandBehavior behavior)
{
this.command = command;
this.CommandBehavior = behavior;
open = true;
currentRow = -1;
hstmt = command.hStmt;
// Init columns array;
short colcount = 0;
libodbc.SQLNumResultCols (hstmt, ref colcount);
cols = new OdbcColumn [colcount];
GetColumns ();
}
internal OdbcDataReader (OdbcCommand command, CommandBehavior behavior,
int recordAffected) : this (command, behavior)
{
_recordsAffected = recordAffected;
}
#endregion
#region Properties
private CommandBehavior CommandBehavior {
get { return behavior; }
set { behavior = value; }
}
public
#if NET_2_0
override
#endif // NET_2_0
int Depth {
get {
return 0; // no nested selects supported
}
}
public
#if NET_2_0
override
#endif // NET_2_0
int FieldCount {
get {
if (IsClosed)
throw new InvalidOperationException ("The reader is closed.");
return cols.Length;
}
}
public
#if NET_2_0
override
#endif // NET_2_0
bool IsClosed {
get {
return !open;
}
}
public
#if NET_2_0
override
#endif // NET_2_0
object this [string value] {
get {
int pos = GetOrdinal (value);
return this [pos];
}
}
public
#if NET_2_0
override
#endif // NET_2_0
object this [int i] {
get {
return GetValue (i);
}
}
public
#if NET_2_0
override
#endif // NET_2_0
int RecordsAffected {
get {
return _recordsAffected;
}
}
[MonoTODO]
public
#if NET_2_0
override
#endif // NET_2_0
bool HasRows {
get { throw new NotImplementedException(); }
}
private OdbcConnection Connection {
get {
if (command != null)
return command.Connection;
return null;
}
}
#endregion
#region Methods
private int ColIndex (string colname)
{
int i = 0;
foreach (OdbcColumn col in cols) {
if (col != null) {
if (col.ColumnName == colname)
return i;
if (String.Compare (col.ColumnName, colname, true) == 0)
return i;
}
i++;
}
return -1;
}
// 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 Connection.CreateOdbcException (OdbcHandleType.Stmt, hstmt);
colname = RemoveTrailingNullChar (Encoding.Unicode.GetString (colname_buffer));
OdbcColumn c = new OdbcColumn (colname, (SQL_TYPE) dt);
c.AllowDBNull = (Nullable != 0);
c.Digits = DecDigits;
if (c.IsVariableSizeType)
c.MaxLength = (int) ColSize;
cols [ordinal] = c;
}
return cols [ordinal];
}
// Load all column descriptions
private void GetColumns ()
{
for(int i = 0; i < cols.Length; i++)
GetColumn (i);
}
public
#if NET_2_0
override
#endif // NET_2_0
void Close ()
{
// FIXME : have to implement output parameter binding
open = false;
currentRow = -1;
this.command.FreeIfNotPrepared ();
if ((this.CommandBehavior & CommandBehavior.CloseConnection) == CommandBehavior.CloseConnection)
this.command.Connection.Close ();
}
#if ONLY_1_1
~OdbcDataReader ()
{
this.Dispose (false);
}
#endif
public
#if NET_2_0
override
#endif // NET_2_0
bool GetBoolean (int i)
{
return (bool) GetValue (i);
}
public
#if NET_2_0
override
#endif // NET_2_0
byte GetByte (int i)
{
return Convert.ToByte (GetValue (i));
}
public
#if NET_2_0
override
#endif // NET_2_0
long GetBytes (int i, long dataIndex, byte[] buffer, int bufferIndex, int length)
{
if (IsClosed)
throw new InvalidOperationException ("Reader is not open.");
if (currentRow == -1)
throw new InvalidOperationException ("No data available.");
OdbcReturn ret = OdbcReturn.Error;
bool copyBuffer = false;
int returnVal = 0, outsize = 0;
byte [] tbuff = new byte [length+1];
if (buffer == null)
length = 0;
ret=libodbc.SQLGetData (hstmt, (ushort) (i + 1), SQL_C_TYPE.BINARY, tbuff, length,
ref outsize);
if (ret == OdbcReturn.NoData)
return 0;
if ( (ret != OdbcReturn.Success) && (ret != OdbcReturn.SuccessWithInfo))
throw Connection.CreateOdbcException (OdbcHandleType.Stmt, hstmt);
OdbcException odbcException = null;
if ( (ret == OdbcReturn.SuccessWithInfo))
odbcException = Connection.CreateOdbcException (
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 = odbcException.Errors [0].SQLState;
//SQLState: String Data, Right truncated
if (sqlstate != libodbc.SQLSTATE_RIGHT_TRUNC)
throw odbcException;
copyBuffer = true;
}
} else {
copyBuffer = outsize == -1 ? false : true;
returnVal = outsize;
}
if (copyBuffer) {
if (outsize == (int) OdbcLengthIndicator.NoTotal) {
int j = 0;
while (tbuff [j] != libodbc.C_NULL) {
buffer [bufferIndex + j] = tbuff [j];
j++;
}
returnVal = j;
} else {
int read_bytes = Math.Min (outsize, length);
for (int j = 0; j < read_bytes; j++)
buffer [bufferIndex + j] = tbuff [j];
returnVal = read_bytes;
}
}
return returnVal;
}
[MonoTODO]
public
#if NET_2_0
override
#endif // NET_2_0
char GetChar (int i)
{
throw new NotImplementedException ();
}
[MonoTODO]
public
#if NET_2_0
override
#endif // NET_2_0
long GetChars (int i, long dataIndex, char[] buffer, int bufferIndex, int length)
{
if (IsClosed)
throw new InvalidOperationException ("The reader is closed.");
if (currentRow == -1)
throw new InvalidOperationException ("No data available.");
if (i < 0 || i >= FieldCount)
throw new IndexOutOfRangeException ();
throw new NotImplementedException ();
}
[MonoTODO]
[EditorBrowsableAttribute (EditorBrowsableState.Never)]
#if ONLY_1_1
public
#endif
#if NET_2_0
new
#endif
IDataReader GetData (int i)
{
throw new NotImplementedException ();
}
public
#if NET_2_0
override
#endif // NET_2_0
string GetDataTypeName (int i)
{
if (IsClosed)
throw new InvalidOperationException ("The reader is closed.");
if (i < 0 || i >= FieldCount)
throw new IndexOutOfRangeException ();
return GetColumnAttributeStr (i + 1, FieldIdentifier.TypeName);
}
public DateTime GetDate (int i)
{
return GetDateTime (i);
}
public
#if NET_2_0
override
#endif // NET_2_0
DateTime GetDateTime (int i)
{
return (DateTime) GetValue (i);
}
public
#if NET_2_0
override
#endif // NET_2_0
decimal GetDecimal (int i)
{
return (decimal) GetValue (i);
}
public
#if NET_2_0
override
#endif // NET_2_0
double GetDouble (int i)
{
return (double) GetValue (i);
}
public
#if NET_2_0
override
#endif // NET_2_0
Type GetFieldType (int i)
{
if (IsClosed)
throw new InvalidOperationException ("The reader is closed.");
return GetColumn (i).DataType;
}
public
#if NET_2_0
override
#endif // NET_2_0
float GetFloat (int i)
{
return (float) GetValue (i);
}
[MonoTODO]
public
#if NET_2_0
override
#endif // NET_2_0
Guid GetGuid (int i)
{
throw new NotImplementedException ();
}
public
#if NET_2_0
override
#endif // NET_2_0
short GetInt16 (int i)
{
return (short) GetValue (i);
}
public
#if NET_2_0
override
#endif // NET_2_0
int GetInt32 (int i)
{
return (int) GetValue (i);
}
public
#if NET_2_0
override
#endif // NET_2_0
long GetInt64 (int i)
{
return (long) GetValue (i);
}
public
#if NET_2_0
override
#endif // NET_2_0
string GetName (int i)
{
if (IsClosed)
throw new InvalidOperationException ("The reader is closed.");
return GetColumn (i).ColumnName;
}
public
#if NET_2_0
override
#endif // NET_2_0
int GetOrdinal (string value)
{
if (IsClosed)
throw new InvalidOperationException ("The reader is closed.");
if (value == null)
throw new ArgumentNullException ("fieldName");
int i = ColIndex (value);
if (i == -1)
throw new IndexOutOfRangeException ();
return i;
}
[MonoTODO]
public
#if NET_2_0
override
#endif // NET_2_0
DataTable GetSchemaTable ()
{
if (IsClosed)
throw new InvalidOperationException ("The reader is closed.");
// FIXME :
// * Map OdbcType to System.Type and assign to DataType.
// This will eliminate the need for IsStringType in
// OdbcColumn
if (_dataTableSchema != null)
return _dataTableSchema;
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;
schemaRow ["ColumnSize"] = col.MaxLength;
schemaRow ["NumericPrecision"] = GetColumnAttribute (i+1, FieldIdentifier.Precision);
schemaRow ["NumericScale"] = GetColumnAttribute (i+1, FieldIdentifier.Scale);
schemaRow ["BaseTableName"] = GetColumnAttributeStr (i+1, FieldIdentifier.TableName);
schemaRow ["BaseSchemaName"] = GetColumnAttributeStr (i+1, FieldIdentifier.SchemaName);
schemaRow ["BaseCatalogName"] = GetColumnAttributeStr (i+1, FieldIdentifier.CatelogName);
schemaRow ["BaseColumnName"] = GetColumnAttributeStr (i+1, FieldIdentifier.BaseColumnName);
schemaRow ["DataType"] = col.DataType;
schemaRow ["IsUnique"] = false;
schemaRow ["IsKey"] = DBNull.Value;
schemaRow ["AllowDBNull"] = GetColumnAttribute (i+1, FieldIdentifier.Nullable) != libodbc.SQL_NO_NULLS;
schemaRow ["ProviderType"] = (int) col.OdbcType;
schemaRow ["IsAutoIncrement"] = GetColumnAttribute (i+1, FieldIdentifier.AutoUniqueValue) == libodbc.SQL_TRUE;
schemaRow ["IsExpression"] = schemaRow.IsNull ("BaseTableName") || (string) schemaRow ["BaseTableName"] == String.Empty;
schemaRow ["IsAliased"] = (string) schemaRow ["BaseColumnName"] != (string) schemaRow ["ColumnName"];
schemaRow ["IsReadOnly"] = ((bool) schemaRow ["IsExpression"]
|| GetColumnAttribute (i+1, FieldIdentifier.Updatable) == libodbc.SQL_ATTR_READONLY);
// FIXME: all of these
schemaRow ["IsIdentity"] = false;
schemaRow ["IsRowVersion"] = false;
schemaRow ["IsHidden"] = false;
schemaRow ["IsLong"] = false;
// FIXME: according to Brian,
// this does not work on MS .NET
// however, we need it for Mono
// for now
// schemaRow.AcceptChanges();
}
// set primary keys
DataRow [] rows = dataTableSchema.Select ("BaseTableName <> ''",
"BaseCatalogName, BaseSchemaName, BaseTableName ASC");
string lastTableName = String.Empty,
lastSchemaName = String.Empty,
lastCatalogName = String.Empty;
string [] keys = null; // assumed to be sorted.
foreach (DataRow row in rows) {
string tableName = (string) row ["BaseTableName"];
string schemaName = (string) row ["BaseSchemaName"];
string catalogName = (string) row ["BaseCatalogName"];
if (tableName != lastTableName || schemaName != lastSchemaName
|| catalogName != lastCatalogName)
keys = GetPrimaryKeys (catalogName, schemaName, tableName);
if (keys != null &&
Array.BinarySearch (keys, (string) row ["BaseColumnName"]) >= 0) {
row ["IsKey"] = true;
row ["IsUnique"] = true;
row ["AllowDBNull"] = false;
GetColumn ( ColIndex ( (string) row ["ColumnName"])).AllowDBNull = false;
}
lastTableName = tableName;
lastSchemaName = schemaName;
lastCatalogName = catalogName;
}
dataTableSchema.AcceptChanges ();
}
return (_dataTableSchema = dataTableSchema);
}
public
#if NET_2_0
override
#endif // NET_2_0
string GetString (int i)
{
object ret = GetValue (i);
if (ret != null && ret.GetType () != typeof (string))
return Convert.ToString (ret);
else
return (string) GetValue (i);
}
[MonoTODO]
public TimeSpan GetTime (int i)
{
throw new NotImplementedException ();
}
public
#if NET_2_0
override
#endif // NET_2_0
object GetValue (int i)
{
if (IsClosed)
throw new InvalidOperationException ("The reader is closed.");
if (currentRow == -1)
throw new InvalidOperationException ("No data available.");
if (i > cols.Length-1 || i < 0)
throw new IndexOutOfRangeException ();
OdbcReturn ret;
int outsize = 0, bufsize;
byte[] buffer;
OdbcColumn col = GetColumn (i);
object DataValue = null;
ushort ColIndex = Convert.ToUInt16 (i + 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.Bit:
short bit_data = 0;
ret = libodbc.SQLGetData (hstmt, ColIndex, col.SqlCType, ref bit_data, 0, ref outsize);
if (outsize != (int) OdbcLengthIndicator.NullData)
DataValue = bit_data == 0 ? "False" : "True";
break;
case OdbcType.Numeric:
case OdbcType.Decimal:
bufsize = 50;
buffer = new byte [bufsize]; // According to sqlext.h, use SQL_CHAR for decimal.
// FIXME : use Numeric.
ret = libodbc.SQLGetData (hstmt, ColIndex, SQL_C_TYPE.CHAR, buffer, bufsize, ref outsize);
if (outsize!=-1) {
byte [] temp = new byte [outsize];
for (int j = 0; j < outsize; j++)
temp [j] = buffer [j];
DataValue = Decimal.Parse (Encoding.Default.GetString (temp),
CultureInfo.InvariantCulture);
}
break;
case OdbcType.TinyInt:
short short_data = 0;
ret = libodbc.SQLGetData (hstmt, ColIndex, col.SqlCType, ref short_data, 0, ref outsize);
DataValue = Convert.ToByte (short_data);
break;
case OdbcType.Int:
int int_data = 0;
ret = libodbc.SQLGetData (hstmt, ColIndex, col.SqlCType, ref int_data, 0, ref outsize);
DataValue = int_data;
break;
case OdbcType.SmallInt:
short sint_data = 0;
ret = libodbc.SQLGetData (hstmt, ColIndex, col.SqlCType, ref sint_data, 0, ref outsize);
DataValue = sint_data;
break;
case OdbcType.BigInt:
long long_data = 0;
ret = libodbc.SQLGetData (hstmt, ColIndex, col.SqlCType, ref long_data, 0, ref outsize);
DataValue = long_data;
break;
case OdbcType.NChar:
bufsize = 255;
buffer = new byte [bufsize];
ret = libodbc.SQLGetData (hstmt, ColIndex, SQL_C_TYPE.WCHAR, buffer, bufsize, ref outsize);
if (outsize != (int) OdbcLengthIndicator.NullData)
if (!(ret == OdbcReturn.SuccessWithInfo
&& outsize == (int) OdbcLengthIndicator.NoTotal))
DataValue = Encoding.Unicode.GetString (buffer, 0, outsize);
break;
case OdbcType.NText:
case OdbcType.NVarChar:
bufsize = (col.MaxLength < 127 ? (col.MaxLength*2+1) : 255);
buffer = new byte[bufsize]; // According to sqlext.h, use SQL_CHAR for both char and varchar
StringBuilder sb = new StringBuilder ();
do {
ret = libodbc.SQLGetData (hstmt, ColIndex, col.SqlCType, buffer, bufsize, ref outsize);
if (ret == OdbcReturn.Error)
break;
// Fix for strance ODBC drivers (like psqlODBC)
if (ret == OdbcReturn.Success && outsize==-1)
ret = OdbcReturn.NoData;
if (ret != OdbcReturn.NoData && outsize > 0) {
string value = null;
if (outsize < bufsize)
value = Encoding.Unicode.GetString (buffer, 0, outsize);
else
value = Encoding.Unicode.GetString (buffer, 0, bufsize);
sb.Append (RemoveTrailingNullChar (value));
}
} while (ret != OdbcReturn.NoData);
DataValue = sb.ToString ();
break;
case OdbcType.Text:
case OdbcType.VarChar:
bufsize = (col.MaxLength < 255 ? (col.MaxLength+1) : 255);
buffer = new byte[bufsize]; // According to sqlext.h, use SQL_CHAR for both char and varchar
StringBuilder sb1 = new StringBuilder ();
do {
ret = libodbc.SQLGetData (hstmt, ColIndex, col.SqlCType, buffer, bufsize, ref outsize);
if (ret == OdbcReturn.Error)
break;
// Fix for strance ODBC drivers (like psqlODBC)
if (ret == OdbcReturn.Success && outsize==-1)
ret = OdbcReturn.NoData;
if (ret != OdbcReturn.NoData && outsize > 0) {
if (outsize < bufsize)
sb1.Append (Encoding.Default.GetString (buffer, 0, outsize));
else
sb1.Append (Encoding.Default.GetString (buffer, 0, bufsize - 1));
}
} while (ret != OdbcReturn.NoData);
DataValue = sb1.ToString ();
break;
case OdbcType.Real:
float float_data = 0;
ret = libodbc.SQLGetData (hstmt, ColIndex, col.SqlCType, ref float_data, 0, ref outsize);
DataValue = float_data;
break;
case OdbcType.Double:
double double_data = 0;
ret = libodbc.SQLGetData (hstmt, ColIndex, col.SqlCType, ref double_data, 0, ref outsize);
DataValue = double_data;
break;
case OdbcType.Timestamp:
case OdbcType.DateTime:
case OdbcType.Date:
case OdbcType.Time:
OdbcTimestamp ts_data = new OdbcTimestamp();
ret = libodbc.SQLGetData (hstmt, ColIndex, col.SqlCType, ref ts_data, 0, ref outsize);
if (outsize != -1) {// This means SQL_NULL_DATA
if (col.OdbcType == OdbcType.Time) {
// libodbc returns value in first three fields for OdbcType.Time
DataValue = new System.TimeSpan (ts_data.year, ts_data.month, ts_data.day);
} else {
DataValue = new DateTime(ts_data.year, ts_data.month,
ts_data.day, ts_data.hour, ts_data.minute,
ts_data.second);
if (ts_data.fraction != 0)
DataValue = ((DateTime) DataValue).AddTicks ((long)ts_data.fraction / 100);
}
}
break;
case OdbcType.VarBinary :
case OdbcType.Image :
bufsize = (col.MaxLength < 255 && col.MaxLength > 0 ? col.MaxLength : 255);
buffer= new byte [bufsize];
ArrayList al = new ArrayList ();
//get the size of data to be returned.
ret = libodbc.SQLGetData (hstmt, ColIndex, SQL_C_TYPE.BINARY, buffer, 0, ref outsize);
if (outsize != (int) OdbcLengthIndicator.NullData) {
do {
ret = libodbc.SQLGetData (hstmt, ColIndex, SQL_C_TYPE.BINARY, buffer, bufsize, ref outsize);
if (ret == OdbcReturn.Error)
break;
if (ret != OdbcReturn.NoData && outsize != -1) {
if (outsize < bufsize) {
byte [] tmparr = new byte [outsize];
Array.Copy (buffer, 0, tmparr, 0, outsize);
al.AddRange (tmparr);
} else
al.AddRange (buffer);
} else {
break;
}
} while (ret != OdbcReturn.NoData);
}
DataValue = al.ToArray (typeof (byte));
break;
case OdbcType.Binary :
bufsize = col.MaxLength;
buffer = new byte [bufsize];
long read = GetBytes (i, 0, buffer, 0, bufsize);
ret = OdbcReturn.Success;
DataValue = buffer;
break;
default:
bufsize = 255;
buffer = new byte[bufsize];
ret = libodbc.SQLGetData (hstmt, ColIndex, SQL_C_TYPE.CHAR, buffer, bufsize, ref outsize);
if (outsize != (int) OdbcLengthIndicator.NullData)
if (! (ret == OdbcReturn.SuccessWithInfo
&& outsize == (int) OdbcLengthIndicator.NoTotal))
DataValue = Encoding.Default.GetString (buffer, 0, outsize);
break;
}
if ((ret!=OdbcReturn.Success) && (ret!=OdbcReturn.SuccessWithInfo) && (ret!=OdbcReturn.NoData))
throw Connection.CreateOdbcException (OdbcHandleType.Stmt, hstmt);
if (outsize == -1) // This means SQL_NULL_DATA
col.Value = DBNull.Value;
else
col.Value = DataValue;
}
return col.Value;
}
public
#if NET_2_0
override
#endif // NET_2_0
int GetValues (object [] values)
{
int numValues = 0;
if (IsClosed)
throw new InvalidOperationException ("The reader is closed.");
if (currentRow == -1)
throw new InvalidOperationException ("No data available.");
// 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;
}
#if ONLY_1_1
void IDisposable.Dispose ()
{
Dispose (true);
GC.SuppressFinalize (this);
}
IEnumerator IEnumerable.GetEnumerator ()
{
return new DbEnumerator (this);
}
#endif // ONLY_1_1
#if NET_2_0
public override IEnumerator GetEnumerator ()
{
return new DbEnumerator (this);
}
#endif
#if NET_2_0
protected override
#endif
void Dispose (bool disposing)
{
if (disposed)
return;
if (disposing) {
// dispose managed resources
Close ();
}
command = null;
cols = null;
_dataTableSchema = null;
disposed = true;
}
public
#if NET_2_0
override
#endif // NET_2_0
bool IsDBNull (int i)
{
return (GetValue (i) is DBNull);
}
/// <remarks>
/// Move to the next result set.
/// </remarks>
public
#if NET_2_0
override
#endif // NET_2_0
bool NextResult ()
{
OdbcReturn ret = OdbcReturn.Success;
ret = libodbc.SQLMoreResults (hstmt);
if (ret == OdbcReturn.Success) {
short colcount = 0;
libodbc.SQLNumResultCols (hstmt, ref colcount);
cols = new OdbcColumn [colcount];
_dataTableSchema = null; // force fresh creation
GetColumns ();
}
return (ret == OdbcReturn.Success);
}
/// <remarks>
/// Load the next row in the current result set.
/// </remarks>
private bool NextRow ()
{
OdbcReturn ret = libodbc.SQLFetch (hstmt);
if (ret != OdbcReturn.Success)
currentRow = -1;
else
currentRow++;
// Clear cached values from last record
foreach (OdbcColumn col in cols) {
if (col != null)
col.Value = null;
}
return (ret == OdbcReturn.Success);
}
private int GetColumnAttribute (int column, FieldIdentifier fieldId)
{
OdbcReturn ret = OdbcReturn.Error;
byte [] buffer = new byte [255];
short outsize = 0;
int val = 0;
ret = libodbc.SQLColAttribute (hstmt, (short)column, fieldId,
buffer, (short)buffer.Length,
ref outsize, ref val);
if (ret != OdbcReturn.Success && ret != OdbcReturn.SuccessWithInfo)
throw Connection.CreateOdbcException (
OdbcHandleType.Stmt, hstmt);
return val;
}
private string GetColumnAttributeStr (int column, FieldIdentifier fieldId)
{
OdbcReturn ret = OdbcReturn.Error;
byte [] buffer = new byte [255];
short outsize = 0;
int val = 0;
ret = libodbc.SQLColAttribute (hstmt, (short)column, fieldId,
buffer, (short)buffer.Length,
ref outsize, ref val);
if (ret != OdbcReturn.Success && ret != OdbcReturn.SuccessWithInfo)
throw Connection.CreateOdbcException (
OdbcHandleType.Stmt, hstmt);
string value = string.Empty;
if (outsize > 0)
value = Encoding.Unicode.GetString (buffer, 0, outsize);
return value;
}
private string [] GetPrimaryKeys (string catalog, string schema, string table)
{
if (cols.Length <= 0)
return new string [0];
ArrayList keys = null;
try {
keys = GetPrimaryKeysBySQLPrimaryKey (catalog, schema, table);
} catch (OdbcException) {
try {
keys = GetPrimaryKeysBySQLStatistics (catalog, schema, table);
} catch (OdbcException) {
}
}
if (keys == null)
return null;
keys.Sort ();
return (string []) keys.ToArray (typeof (string));
}
private ArrayList GetPrimaryKeysBySQLPrimaryKey (string catalog, string schema, string table)
{
ArrayList keys = new ArrayList ();
IntPtr handle = IntPtr.Zero;
OdbcReturn ret;
try {
ret=libodbc.SQLAllocHandle(OdbcHandleType.Stmt,
command.Connection.hDbc, ref handle);
if ((ret!=OdbcReturn.Success) && (ret!=OdbcReturn.SuccessWithInfo))
throw Connection.CreateOdbcException (
OdbcHandleType.Dbc, Connection.hDbc);
ret = libodbc.SQLPrimaryKeys (handle, catalog, -3,
schema, -3, table, -3);
if (ret != OdbcReturn.Success && ret != OdbcReturn.SuccessWithInfo)
throw Connection.CreateOdbcException (OdbcHandleType.Stmt, handle);
int length = 0;
byte [] primaryKey = new byte [255];
ret = libodbc.SQLBindCol (handle, 4, SQL_C_TYPE.CHAR, primaryKey, primaryKey.Length, ref length);
if (ret != OdbcReturn.Success && ret != OdbcReturn.SuccessWithInfo)
throw Connection.CreateOdbcException (OdbcHandleType.Stmt, handle);
while (true) {
ret = libodbc.SQLFetch (handle);
if (ret != OdbcReturn.Success && ret != OdbcReturn.SuccessWithInfo)
break;
string pkey = Encoding.Default.GetString (primaryKey, 0, length);
keys.Add (pkey);
}
} finally {
if (handle != IntPtr.Zero) {
ret = libodbc.SQLFreeStmt (handle, libodbc.SQLFreeStmtOptions.Close);
if ((ret!=OdbcReturn.Success) && (ret!=OdbcReturn.SuccessWithInfo))
throw Connection.CreateOdbcException (OdbcHandleType.Stmt, handle);
ret = libodbc.SQLFreeHandle( (ushort) OdbcHandleType.Stmt, handle);
if ((ret!=OdbcReturn.Success) && (ret!=OdbcReturn.SuccessWithInfo))
throw Connection.CreateOdbcException (OdbcHandleType.Stmt, handle);
}
}
return keys;
}
private unsafe ArrayList GetPrimaryKeysBySQLStatistics (string catalog, string schema, string table)
{
ArrayList keys = new ArrayList ();
IntPtr handle = IntPtr.Zero;
OdbcReturn ret;
try {
ret=libodbc.SQLAllocHandle(OdbcHandleType.Stmt,
command.Connection.hDbc, ref handle);
if ((ret!=OdbcReturn.Success) && (ret!=OdbcReturn.SuccessWithInfo))
throw Connection.CreateOdbcException (
OdbcHandleType.Dbc, Connection.hDbc);
ret = libodbc.SQLStatistics (handle, catalog, -3,
schema, -3,
table, -3,
libodbc.SQL_INDEX_UNIQUE,
libodbc.SQL_QUICK);
if (ret != OdbcReturn.Success && ret != OdbcReturn.SuccessWithInfo)
throw Connection.CreateOdbcException (OdbcHandleType.Stmt, handle);
// NON_UNIQUE
int nonUniqueLength = 0;
short nonUnique = libodbc.SQL_FALSE;
ret = libodbc.SQLBindCol (handle, 4, SQL_C_TYPE.SHORT, ref (short) nonUnique, sizeof (short), ref nonUniqueLength);
if (ret != OdbcReturn.Success && ret != OdbcReturn.SuccessWithInfo)
throw Connection.CreateOdbcException (OdbcHandleType.Stmt, handle);
// COLUMN_NAME
int length = 0;
byte [] colName = new byte [255];
ret = libodbc.SQLBindCol (handle, 9, SQL_C_TYPE.CHAR, colName, colName.Length, ref length);
if (ret != OdbcReturn.Success && ret != OdbcReturn.SuccessWithInfo)
throw Connection.CreateOdbcException (OdbcHandleType.Stmt, handle);
while (true) {
ret = libodbc.SQLFetch (handle);
if (ret != OdbcReturn.Success && ret != OdbcReturn.SuccessWithInfo)
break;
if (nonUnique == libodbc.SQL_TRUE) {
string pkey = Encoding.Default.GetString (colName, 0, length);
keys.Add (pkey);
break;
}
}
} finally {
if (handle != IntPtr.Zero) {
ret = libodbc.SQLFreeStmt (handle, libodbc.SQLFreeStmtOptions.Close);
if ((ret!=OdbcReturn.Success) && (ret!=OdbcReturn.SuccessWithInfo))
throw Connection.CreateOdbcException (OdbcHandleType.Stmt, handle);
ret = libodbc.SQLFreeHandle ((ushort) OdbcHandleType.Stmt, handle);
if ((ret!=OdbcReturn.Success) && (ret!=OdbcReturn.SuccessWithInfo))
throw Connection.CreateOdbcException (OdbcHandleType.Stmt, handle);
}
}
return keys;
}
public
#if NET_2_0
override
#endif // NET_2_0
bool Read ()
{
return NextRow ();
}
static string RemoveTrailingNullChar (string value)
{
return value.TrimEnd ('\0');
}
#endregion
}
}
|