2 // System.Data.Odbc.OdbcDataReader
5 // Brian Ritchie (brianlritchie@hotmail.com)
6 // Daniel Morgan <danmorg@sc.rr.com>
7 // Sureshkumar T <tsureshkumar@novell.com> (2004)
9 // Copyright (C) Brian Ritchie, 2002
10 // Copyright (C) Daniel Morgan, 2002
14 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
16 // Permission is hereby granted, free of charge, to any person obtaining
17 // a copy of this software and associated documentation files (the
18 // "Software"), to deal in the Software without restriction, including
19 // without limitation the rights to use, copy, modify, merge, publish,
20 // distribute, sublicense, and/or sell copies of the Software, and to
21 // permit persons to whom the Software is furnished to do so, subject to
22 // the following conditions:
24 // The above copyright notice and this permission notice shall be
25 // included in all copies or substantial portions of the Software.
27 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
28 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
29 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
30 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
31 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
32 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
33 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
36 using System.Collections;
37 using System.ComponentModel;
39 using System.Data.Common;
42 namespace System.Data.Odbc
45 public sealed class OdbcDataReader : DbDataReader
47 public sealed class OdbcDataReader : MarshalByRefObject, IDataReader, IDisposable, IDataRecord, IEnumerable
52 private OdbcCommand command;
54 private int currentRow;
55 private OdbcColumn[] cols;
57 private int _recordsAffected = -1;
58 bool disposed = false;
59 private DataTable _dataTableSchema;
60 private CommandBehavior behavior;
66 internal OdbcDataReader (OdbcCommand command, CommandBehavior behavior)
68 this.command = command;
69 this.CommandBehavior=behavior;
73 // Init columns array;
75 libodbc.SQLNumResultCols(hstmt, ref colcount);
76 cols=new OdbcColumn[colcount];
80 internal OdbcDataReader (OdbcCommand command, CommandBehavior behavior,
81 int recordAffected) : this (command, behavior)
83 _recordsAffected = recordAffected;
91 private CommandBehavior CommandBehavior
93 get { return behavior; }
94 set { value = behavior; }
103 return 0; // no nested selects supported
131 object this[string name] {
135 if (currentRow == -1)
136 throw new InvalidOperationException ();
138 pos = ColIndex(name);
141 throw new IndexOutOfRangeException ();
151 object this[int index] {
153 return (object) GetValue (index);
162 int RecordsAffected {
164 return _recordsAffected;
174 get { throw new NotImplementedException(); }
181 private int ColIndex(string colname)
184 foreach (OdbcColumn col in cols)
187 if (col.ColumnName == colname)
189 if (String.Compare (col.ColumnName, colname, true) == 0)
198 // Dynamically load column descriptions as needed.
199 private OdbcColumn GetColumn(int ordinal)
201 if (cols[ordinal]==null)
204 byte[] colname_buffer=new byte[bufsize];
206 short colname_size=0;
208 short DecDigits=0, Nullable=0, dt=0;
209 OdbcReturn ret=libodbc.SQLDescribeCol(hstmt, Convert.ToUInt16(ordinal+1),
210 colname_buffer, bufsize, ref colname_size, ref dt, ref ColSize,
211 ref DecDigits, ref Nullable);
212 if ((ret!=OdbcReturn.Success) && (ret!=OdbcReturn.SuccessWithInfo))
213 throw new OdbcException(new OdbcError("SQLDescribeCol",OdbcHandleType.Stmt,hstmt));
214 colname=System.Text.Encoding.Default.GetString(colname_buffer);
215 colname=colname.Replace((char) 0,' ').Trim();
216 OdbcColumn c=new OdbcColumn(colname, (SQL_TYPE) dt);
217 c.AllowDBNull=(Nullable!=0);
219 if (c.IsVariableSizeType)
220 c.MaxLength=(int)ColSize;
223 return cols[ordinal];
232 // FIXME : have to implement output parameter binding
236 this.command.FreeIfNotPrepared ();
238 if ((this.CommandBehavior & CommandBehavior.CloseConnection)==CommandBehavior.CloseConnection) {
239 this.command.Connection.Close();
245 this.Dispose (false);
252 bool GetBoolean (int ordinal)
254 return (bool) GetValue(ordinal);
261 byte GetByte (int ordinal)
263 return (byte) Convert.ToByte(GetValue(ordinal));
270 long GetBytes (int ordinal, long dataIndex, byte[] buffer, int bufferIndex, int length)
272 OdbcReturn ret = OdbcReturn.Error;
273 bool copyBuffer = false;
274 int returnVal = 0, outsize = 0;
275 byte [] tbuff = new byte [length+1];
277 length = buffer == null ? 0 : length;
278 ret=libodbc.SQLGetData (hstmt, (ushort) (ordinal+1), SQL_C_TYPE.BINARY, tbuff, length,
281 if (ret == OdbcReturn.NoData)
284 if ( (ret != OdbcReturn.Success) && (ret != OdbcReturn.SuccessWithInfo))
285 throw new OdbcException (new OdbcError ("SQLGetData", OdbcHandleType.Stmt, hstmt));
287 OdbcError odbcErr = null;
288 if ( (ret == OdbcReturn.SuccessWithInfo))
289 odbcErr = new OdbcError ("SQLGetData", OdbcHandleType.Stmt, hstmt);
292 return outsize; //if buffer is null,return length of the field
294 if (ret == OdbcReturn.SuccessWithInfo) {
295 if (outsize == (int) OdbcLengthIndicator.NoTotal)
297 else if (outsize == (int) OdbcLengthIndicator.NullData) {
301 string sqlstate = odbcErr.SQLState;
302 //SQLState: String Data, Right truncated
303 if (sqlstate != libodbc.SQLSTATE_RIGHT_TRUNC)
304 throw new OdbcException ( odbcErr);
308 copyBuffer = outsize == -1 ? false : true;
314 while (tbuff [i] != libodbc.C_NULL) {
315 buffer [bufferIndex + i] = tbuff [i];
328 char GetChar (int ordinal)
330 throw new NotImplementedException ();
338 long GetChars (int ordinal, long dataIndex, char[] buffer, int bufferIndex, int length)
340 throw new NotImplementedException ();
344 [EditorBrowsableAttribute (EditorBrowsableState.Never)]
349 IDataReader GetData (int ordinal)
351 throw new NotImplementedException ();
358 string GetDataTypeName (int index)
360 return GetColumn(index).OdbcType.ToString();
363 public DateTime GetDate(int ordinal) {
364 return GetDateTime(ordinal);
371 DateTime GetDateTime (int ordinal)
373 return (DateTime) GetValue(ordinal);
381 decimal GetDecimal (int ordinal)
383 throw new NotImplementedException ();
390 double GetDouble (int ordinal)
392 return (double) GetValue(ordinal);
399 Type GetFieldType (int index)
401 return GetColumn(index).DataType;
408 float GetFloat (int ordinal)
410 return (float) GetValue(ordinal);
418 Guid GetGuid (int ordinal)
420 throw new NotImplementedException ();
427 short GetInt16 (int ordinal)
429 return (short) GetValue(ordinal);
436 int GetInt32 (int ordinal)
438 return (int) GetValue(ordinal);
445 long GetInt64 (int ordinal)
447 return (long) GetValue(ordinal);
454 string GetName (int index)
456 return GetColumn(index).ColumnName;
463 int GetOrdinal (string name)
465 int i=ColIndex(name);
468 throw new IndexOutOfRangeException ();
478 DataTable GetSchemaTable()
481 // * Map OdbcType to System.Type and assign to DataType.
482 // This will eliminate the need for IsStringType in
485 if (_dataTableSchema != null)
486 return _dataTableSchema;
488 DataTable dataTableSchema = null;
489 // Only Results from SQL SELECT Queries
490 // get a DataTable for schema of the result
491 // otherwise, DataTable is null reference
494 dataTableSchema = new DataTable ();
496 dataTableSchema.Columns.Add ("ColumnName", typeof (string));
497 dataTableSchema.Columns.Add ("ColumnOrdinal", typeof (int));
498 dataTableSchema.Columns.Add ("ColumnSize", typeof (int));
499 dataTableSchema.Columns.Add ("NumericPrecision", typeof (int));
500 dataTableSchema.Columns.Add ("NumericScale", typeof (int));
501 dataTableSchema.Columns.Add ("IsUnique", typeof (bool));
502 dataTableSchema.Columns.Add ("IsKey", typeof (bool));
503 DataColumn dc = dataTableSchema.Columns["IsKey"];
504 dc.AllowDBNull = true; // IsKey can have a DBNull
505 dataTableSchema.Columns.Add ("BaseCatalogName", typeof (string));
506 dataTableSchema.Columns.Add ("BaseColumnName", typeof (string));
507 dataTableSchema.Columns.Add ("BaseSchemaName", typeof (string));
508 dataTableSchema.Columns.Add ("BaseTableName", typeof (string));
509 dataTableSchema.Columns.Add ("DataType", typeof(Type));
510 dataTableSchema.Columns.Add ("AllowDBNull", typeof (bool));
511 dataTableSchema.Columns.Add ("ProviderType", typeof (int));
512 dataTableSchema.Columns.Add ("IsAliased", typeof (bool));
513 dataTableSchema.Columns.Add ("IsExpression", typeof (bool));
514 dataTableSchema.Columns.Add ("IsIdentity", typeof (bool));
515 dataTableSchema.Columns.Add ("IsAutoIncrement", typeof (bool));
516 dataTableSchema.Columns.Add ("IsRowVersion", typeof (bool));
517 dataTableSchema.Columns.Add ("IsHidden", typeof (bool));
518 dataTableSchema.Columns.Add ("IsLong", typeof (bool));
519 dataTableSchema.Columns.Add ("IsReadOnly", typeof (bool));
523 for (int i = 0; i < cols.Length; i += 1 )
525 string baseTableName = String.Empty;
527 OdbcColumn col=GetColumn(i);
529 schemaRow = dataTableSchema.NewRow ();
530 dataTableSchema.Rows.Add (schemaRow);
532 schemaRow ["ColumnName"] = col.ColumnName;
533 schemaRow ["ColumnOrdinal"] = i;
534 schemaRow ["ColumnSize"] = col.MaxLength;
535 schemaRow ["NumericPrecision"] = GetColumnAttribute (i+1, FieldIdentifier.Precision);
536 schemaRow ["NumericScale"] = GetColumnAttribute (i+1, FieldIdentifier.Scale);
537 schemaRow ["BaseTableName"] = GetColumnAttributeStr (i+1, FieldIdentifier.TableName);
538 schemaRow ["BaseSchemaName"] = GetColumnAttributeStr (i+1, FieldIdentifier.SchemaName);
539 schemaRow ["BaseCatalogName"] = GetColumnAttributeStr (i+1, FieldIdentifier.CatelogName);
540 schemaRow ["BaseColumnName"] = GetColumnAttributeStr (i+1, FieldIdentifier.BaseColumnName);
541 schemaRow ["DataType"] = col.DataType;
542 schemaRow ["IsUnique"] = false;
543 schemaRow ["IsKey"] = DBNull.Value;
544 schemaRow ["AllowDBNull"] = GetColumnAttribute (i+1, FieldIdentifier.Nullable) != libodbc.SQL_NO_NULLS;
545 schemaRow ["ProviderType"] = (int) col.OdbcType;
546 schemaRow ["IsAutoIncrement"] = GetColumnAttribute (i+1, FieldIdentifier.AutoUniqueValue) == libodbc.SQL_TRUE;
547 schemaRow ["IsExpression"] = schemaRow.IsNull ("BaseTableName") || (string) schemaRow ["BaseTableName"] == String.Empty;
548 schemaRow ["IsAliased"] = (string) schemaRow ["BaseColumnName"] != (string) schemaRow ["ColumnName"];
549 schemaRow ["IsReadOnly"] = ((bool) schemaRow ["IsExpression"]
550 || GetColumnAttribute (i+1, FieldIdentifier.Updatable) == libodbc.SQL_ATTR_READONLY);
552 // FIXME: all of these
553 schemaRow ["IsIdentity"] = false;
554 schemaRow ["IsRowVersion"] = false;
555 schemaRow ["IsHidden"] = false;
556 schemaRow ["IsLong"] = false;
559 // FIXME: according to Brian,
560 // this does not work on MS .NET
561 // however, we need it for Mono
563 // schemaRow.AcceptChanges();
568 DataRow [] rows = dataTableSchema.Select ("BaseTableName <> ''",
569 "BaseCatalogName, BaseSchemaName, BaseTableName ASC");
571 string lastTableName = String.Empty,
572 lastSchemaName = String.Empty,
573 lastCatalogName = String.Empty;
574 string [] keys = null; // assumed to be sorted.
575 foreach (DataRow row in rows) {
576 string tableName = (string) row ["BaseTableName"];
577 string schemaName = (string) row ["BaseSchemaName"];
578 string catalogName = (string) row ["BaseCatalogName"];
580 if (tableName != lastTableName || schemaName != lastSchemaName
581 || catalogName != lastCatalogName)
582 keys = GetPrimaryKeys (catalogName, schemaName, tableName);
585 Array.BinarySearch (keys, (string) row ["BaseColumnName"]) >= 0) {
586 row ["IsKey"] = true;
587 row ["IsUnique"] = true;
588 row ["AllowDBNull"] = false;
589 GetColumn ( ColIndex ( (string) row ["ColumnName"])).AllowDBNull = false;
591 lastTableName = tableName;
592 lastSchemaName = schemaName;
593 lastCatalogName = catalogName;
595 dataTableSchema.AcceptChanges();
597 return (_dataTableSchema = dataTableSchema);
604 string GetString (int ordinal)
606 return (string) GetValue(ordinal);
610 public TimeSpan GetTime (int ordinal)
612 throw new NotImplementedException ();
619 object GetValue (int ordinal)
621 if (currentRow == -1)
622 throw new IndexOutOfRangeException ();
624 if (ordinal>cols.Length-1 || ordinal<0)
625 throw new IndexOutOfRangeException ();
628 int outsize=0, bufsize;
630 OdbcColumn col=GetColumn(ordinal);
631 object DataValue=null;
632 ushort ColIndex=Convert.ToUInt16(ordinal+1);
634 // Check cached values
635 if (col.Value==null) {
637 // mk:@MSITStore:C:\program%20files\Microsoft%20Data%20Access%20SDK\Docs\odbc.chm::/htm/odbcc_data_types.htm
638 switch (col.OdbcType) {
641 ret=libodbc.SQLGetData(hstmt, ColIndex, col.SqlCType, ref bit_data, 0, ref outsize);
642 if (outsize != (int) OdbcLengthIndicator.NullData)
643 DataValue = bit_data == 0 ? "False" : "True";
645 case OdbcType.Numeric:
646 case OdbcType.Decimal:
648 buffer=new byte[bufsize]; // According to sqlext.h, use SQL_CHAR for decimal.
649 // FIXME : use Numeric.
650 ret=libodbc.SQLGetData(hstmt, ColIndex, SQL_C_TYPE.CHAR, buffer, bufsize, ref outsize);
652 byte[] temp = new byte[outsize];
653 for (int i=0;i<outsize;i++)
655 DataValue=Decimal.Parse(System.Text.Encoding.Default.GetString(temp));
658 case OdbcType.TinyInt:
660 ret=libodbc.SQLGetData(hstmt, ColIndex, col.SqlCType, ref short_data, 0, ref outsize);
661 DataValue=System.Convert.ToByte(short_data);
665 ret=libodbc.SQLGetData(hstmt, ColIndex, col.SqlCType, ref int_data, 0, ref outsize);
669 case OdbcType.SmallInt:
671 ret=libodbc.SQLGetData(hstmt, ColIndex, col.SqlCType, ref sint_data, 0, ref outsize);
675 case OdbcType.BigInt:
677 ret=libodbc.SQLGetData(hstmt, ColIndex, col.SqlCType, ref long_data, 0, ref outsize);
681 case OdbcType.NVarChar:
682 bufsize=(col.MaxLength < 127 ? (col.MaxLength*2+1) : 255);
683 buffer=new byte[bufsize]; // According to sqlext.h, use SQL_CHAR for both char and varchar
684 StringBuilder sb = new StringBuilder ();
686 ret=libodbc.SQLGetData(hstmt, ColIndex, col.SqlCType, buffer, bufsize, ref outsize);
687 if (ret == OdbcReturn.Error)
689 if (ret != OdbcReturn.NoData && outsize!=-1) {
690 if (outsize < bufsize)
691 sb.Append (System.Text.Encoding.Unicode.GetString(buffer,0,outsize));
693 sb.Append (System.Text.Encoding.Unicode.GetString(buffer,0,bufsize));
695 } while (ret != OdbcReturn.NoData);
696 DataValue = sb.ToString ();
699 case OdbcType.VarChar:
700 bufsize=(col.MaxLength < 255 ? (col.MaxLength+1) : 255);
701 buffer=new byte[bufsize]; // According to sqlext.h, use SQL_CHAR for both char and varchar
702 StringBuilder sb1 = new StringBuilder ();
704 ret=libodbc.SQLGetData(hstmt, ColIndex, col.SqlCType, buffer, bufsize, ref outsize);
705 if (ret == OdbcReturn.Error)
707 if (ret != OdbcReturn.NoData && outsize!=-1) {
708 if (outsize < bufsize)
709 sb1.Append (System.Text.Encoding.Default.GetString(buffer,0,outsize));
711 sb1.Append (System.Text.Encoding.Default.GetString(buffer,0,bufsize));
713 } while (ret != OdbcReturn.NoData);
714 DataValue = sb1.ToString ();
718 ret=libodbc.SQLGetData(hstmt, ColIndex, col.SqlCType, ref float_data, 0, ref outsize);
719 DataValue=float_data;
721 case OdbcType.Double:
722 double double_data=0;
723 ret=libodbc.SQLGetData(hstmt, ColIndex, col.SqlCType, ref double_data, 0, ref outsize);
724 DataValue=double_data;
726 case OdbcType.Timestamp:
727 case OdbcType.DateTime:
730 OdbcTimestamp ts_data=new OdbcTimestamp();
731 ret=libodbc.SQLGetData(hstmt, ColIndex, col.SqlCType, ref ts_data, 0, ref outsize);
732 if (outsize!=-1) // This means SQL_NULL_DATA
733 DataValue=new DateTime(ts_data.year,ts_data.month,ts_data.day,ts_data.hour,
734 ts_data.minute,ts_data.second,Convert.ToInt32(ts_data.fraction));
736 case OdbcType.VarBinary :
737 case OdbcType.Image :
738 bufsize= (col.MaxLength < 255 ? col.MaxLength : 255);
739 buffer= new byte [bufsize];
740 ArrayList al = new ArrayList ();
742 ret=libodbc.SQLGetData (hstmt, ColIndex, SQL_C_TYPE.BINARY, buffer, bufsize, ref outsize);
743 if (ret == OdbcReturn.Error)
745 if (ret != OdbcReturn.NoData && outsize!=-1) {
746 if (outsize < bufsize) {
747 byte[] tmparr = new byte [outsize];
748 Array.Copy (buffer, 0, tmparr, 0, outsize);
749 al.AddRange (tmparr);
751 al.AddRange (buffer);
753 } while (ret != OdbcReturn.NoData);
754 DataValue = al.ToArray (typeof (byte));
756 case OdbcType.Binary :
757 bufsize = col.MaxLength;
758 buffer = new byte [bufsize];
759 long read = GetBytes (ordinal, 0, buffer, 0, bufsize);
760 ret = OdbcReturn.Success;
765 buffer=new byte[bufsize];
766 ret=libodbc.SQLGetData(hstmt, ColIndex, SQL_C_TYPE.CHAR, buffer, bufsize, ref outsize);
767 if (outsize != (int) OdbcLengthIndicator.NullData)
768 if (! (ret == OdbcReturn.SuccessWithInfo
769 && outsize == (int) OdbcLengthIndicator.NoTotal))
770 DataValue=System.Text.Encoding.Default.GetString(buffer, 0, outsize);
774 if ((ret!=OdbcReturn.Success) && (ret!=OdbcReturn.SuccessWithInfo) && (ret!=OdbcReturn.NoData))
775 throw new OdbcException(new OdbcError("SQLGetData",OdbcHandleType.Stmt,hstmt));
777 if (outsize==-1) // This means SQL_NULL_DATA
778 col.Value=DBNull.Value;
789 int GetValues (object[] values)
794 for (int i = 0; i < values.Length; i++) {
795 if (i < FieldCount) {
796 values[i] = GetValue(i);
803 // get number of object instances in array
804 if (values.Length < FieldCount)
805 numValues = values.Length;
806 else if (values.Length == FieldCount)
807 numValues = FieldCount;
809 numValues = FieldCount;
816 void IDisposable.Dispose ()
819 GC.SuppressFinalize (this);
822 IEnumerator IEnumerable.GetEnumerator ()
824 return new DbEnumerator (this);
830 public override IEnumerator GetEnumerator ()
832 return new DbEnumerator (this);
839 void Dispose (bool disposing)
845 // dispose managed resources
851 _dataTableSchema = null;
859 bool IsDBNull (int ordinal)
861 return (GetValue(ordinal) is DBNull);
865 /// Move to the next result set.
873 OdbcReturn ret = OdbcReturn.Success;
874 ret = libodbc.SQLMoreResults (hstmt);
875 if (ret == OdbcReturn.Success) {
877 libodbc.SQLNumResultCols (hstmt, ref colcount);
878 cols = new OdbcColumn [colcount];
879 _dataTableSchema = null; // force fresh creation
882 return (ret==OdbcReturn.Success);
886 /// Load the next row in the current result set.
888 private bool NextRow ()
890 OdbcReturn ret=libodbc.SQLFetch (hstmt);
891 if (ret != OdbcReturn.Success)
896 // Clear cached values from last record
897 foreach (OdbcColumn col in cols)
902 return (ret == OdbcReturn.Success);
906 private int GetColumnAttribute (int column, FieldIdentifier fieldId)
908 OdbcReturn ret = OdbcReturn.Error;
909 byte [] buffer = new byte [255];
912 ret = libodbc.SQLColAttribute (hstmt, column, fieldId,
913 buffer, buffer.Length,
914 ref outsize, ref val);
915 if (ret != OdbcReturn.Success && ret != OdbcReturn.SuccessWithInfo)
916 throw new OdbcException (new OdbcError ("SQLColAttribute",
924 private string GetColumnAttributeStr (int column, FieldIdentifier fieldId)
926 OdbcReturn ret = OdbcReturn.Error;
927 byte [] buffer = new byte [255];
930 ret = libodbc.SQLColAttribute (hstmt, column, fieldId,
931 buffer, buffer.Length,
932 ref outsize, ref val);
933 if (ret != OdbcReturn.Success && ret != OdbcReturn.SuccessWithInfo)
934 throw new OdbcException (new OdbcError ("SQLColAttribute",
940 value = Encoding.Default.GetString (buffer, 0, outsize);
944 private string [] GetPrimaryKeys (string catalog, string schema, string table)
946 if (cols.Length <= 0)
947 return new string [0];
949 ArrayList keys = null;
951 keys = GetPrimaryKeysBySQLPrimaryKey (catalog, schema, table);
952 } catch (OdbcException){
954 keys = GetPrimaryKeysBySQLStatistics (catalog, schema, table);
955 } catch (OdbcException) {
959 return (string []) keys.ToArray (typeof (string));
962 private ArrayList GetPrimaryKeysBySQLPrimaryKey (string catalog, string schema, string table)
964 ArrayList keys = new ArrayList ();
965 IntPtr handle = IntPtr.Zero;
968 ret=libodbc.SQLAllocHandle(OdbcHandleType.Stmt,
969 command.Connection.hDbc, ref handle);
970 if ((ret!=OdbcReturn.Success) && (ret!=OdbcReturn.SuccessWithInfo))
971 throw new OdbcException(new OdbcError("SQLAllocHandle",
973 command.Connection.hDbc));
975 ret = libodbc.SQLPrimaryKeys (handle, catalog, -3,
978 if (ret != OdbcReturn.Success && ret != OdbcReturn.SuccessWithInfo)
979 throw new OdbcException (new OdbcError ("SQLPrimaryKeys", OdbcHandleType.Stmt, handle));
982 byte [] primaryKey = new byte [255];
984 ret = libodbc.SQLBindCol (handle, 4, SQL_C_TYPE.CHAR, primaryKey, primaryKey.Length, ref length);
985 if (ret != OdbcReturn.Success && ret != OdbcReturn.SuccessWithInfo)
986 throw new OdbcException (new OdbcError ("SQLBindCol", OdbcHandleType.Stmt, handle));
990 ret = libodbc.SQLFetch (handle);
991 if (ret != OdbcReturn.Success && ret != OdbcReturn.SuccessWithInfo)
993 string pkey = Encoding.Default.GetString (primaryKey, 0, length);
997 if (handle != IntPtr.Zero) {
998 ret = libodbc.SQLFreeStmt (handle, libodbc.SQLFreeStmtOptions.Close);
999 if ((ret!=OdbcReturn.Success) && (ret!=OdbcReturn.SuccessWithInfo))
1000 throw new OdbcException(new OdbcError("SQLFreeStmt",OdbcHandleType.Stmt,handle));
1002 ret = libodbc.SQLFreeHandle( (ushort) OdbcHandleType.Stmt, handle);
1003 if ((ret!=OdbcReturn.Success) && (ret!=OdbcReturn.SuccessWithInfo))
1004 throw new OdbcException(new OdbcError("SQLFreeHandle",OdbcHandleType.Stmt,handle));
1010 private unsafe ArrayList GetPrimaryKeysBySQLStatistics (string catalog, string schema, string table)
1012 ArrayList keys = new ArrayList ();
1013 IntPtr handle = IntPtr.Zero;
1016 ret=libodbc.SQLAllocHandle(OdbcHandleType.Stmt,
1017 command.Connection.hDbc, ref handle);
1018 if ((ret!=OdbcReturn.Success) && (ret!=OdbcReturn.SuccessWithInfo))
1019 throw new OdbcException(new OdbcError("SQLAllocHandle",
1021 command.Connection.hDbc));
1023 ret = libodbc.SQLStatistics (handle, catalog, -3,
1026 libodbc.SQL_INDEX_UNIQUE,
1028 if (ret != OdbcReturn.Success && ret != OdbcReturn.SuccessWithInfo)
1029 throw new OdbcException (new OdbcError ("SQLStatistics", OdbcHandleType.Stmt, handle));
1032 int nonUniqueLength = 0;
1033 short nonUnique = libodbc.SQL_FALSE;
1034 ret = libodbc.SQLBindCol (handle, 4, SQL_C_TYPE.SHORT, ref (short) nonUnique, sizeof (short), ref nonUniqueLength);
1035 if (ret != OdbcReturn.Success && ret != OdbcReturn.SuccessWithInfo)
1036 throw new OdbcException (new OdbcError ("SQLBindCol", OdbcHandleType.Stmt, handle));
1040 byte [] colName = new byte [255];
1041 ret = libodbc.SQLBindCol (handle, 9, SQL_C_TYPE.CHAR, colName, colName.Length, ref length);
1042 if (ret != OdbcReturn.Success && ret != OdbcReturn.SuccessWithInfo)
1043 throw new OdbcException (new OdbcError ("SQLBindCol", OdbcHandleType.Stmt, handle));
1047 ret = libodbc.SQLFetch (handle);
1048 if (ret != OdbcReturn.Success && ret != OdbcReturn.SuccessWithInfo)
1050 if (nonUnique == libodbc.SQL_TRUE) {
1051 string pkey = Encoding.Default.GetString (colName, 0, length);
1057 if (handle != IntPtr.Zero) {
1058 ret = libodbc.SQLFreeStmt (handle, libodbc.SQLFreeStmtOptions.Close);
1059 if ((ret!=OdbcReturn.Success) && (ret!=OdbcReturn.SuccessWithInfo))
1060 throw new OdbcException(new OdbcError("SQLFreeStmt",OdbcHandleType.Stmt,handle));
1062 ret = libodbc.SQLFreeHandle( (ushort) OdbcHandleType.Stmt, handle);
1063 if ((ret!=OdbcReturn.Success) && (ret!=OdbcReturn.SuccessWithInfo))
1064 throw new OdbcException(new OdbcError("SQLFreeHandle",OdbcHandleType.Stmt,handle));
1081 public override object GetProviderSpecificValue (int i)
1083 throw new NotImplementedException ();
1087 public override int GetProviderSpecificValues (object[] values)
1089 throw new NotImplementedException ();
1093 public override Type GetProviderSpecificFieldType (int i)
1095 throw new NotImplementedException ();