private void SetColumnMetaData(object compilationColumns, int hiddenColumns) { Debug.WriteLineIf(CLI.FnTrace.Enabled, "ManagedCommand.SetColumnMetaData"); object[] descriptions = null; if (compilationColumns is object[]) { descriptions = (object[])compilationColumns; } if (descriptions == null || descriptions.Length == 0) { columns = null; return; } bool hasKeyColumns = false; columns = new ColumnData[descriptions.Length]; for (int i = 0; i < columns.Length; i++) { object[] description = (object[])descriptions[i]; ColumnData column = new ColumnData(); columns[i] = column; ColumnFlags flags = (ColumnFlags)description[11]; column.columnName = (string)description[0]; column.columnType = DataTypeInfo.MapDvType((BoxTag)description[1]); if (0 != (flags & ColumnFlags.CDF_XMLTYPE)) { Debug.WriteLineIf(SqlXml.Switch.TraceVerbose, String.Format("Set XML type for {0}", column.columnName)); column.columnType = DataTypeInfo.Xml; } if (column.columnType == null) { throw new SystemException("Unknown data type"); } column.bufferType = column.columnType.bufferType; column.columnSize = column.columnType.GetFieldSize((int)description[3]); column.precision = (short)column.columnSize; column.scale = (short)((int)description[2]); column.IsLong = column.columnType.isLong; column.IsNullable = (0 != (int)description[4]); CLI.Updatable updatable = (CLI.Updatable)(int) description[5]; column.IsReadOnly = (updatable == CLI.Updatable.SQL_ATTR_READONLY); column.IsAutoIncrement = (0 != (flags & ColumnFlags.CDF_AUTOINCREMENT)); column.IsKey = (0 != (flags & ColumnFlags.CDF_KEY)); if (column.IsKey) { hasKeyColumns = true; } column.IsHidden = (i >= (columns.Length - hiddenColumns)); column.IsRowVersion = (column.columnType == DataTypeInfo.Timestamp); // TODO: check for unique columns as well. column.IsUnique = false; column.baseCatalogName = (string)Values.NullIfZero(description[7]); column.baseColumnName = (string)Values.NullIfZero(description[8]); column.baseSchemaName = (string)Values.NullIfZero(description[9]); column.baseTableName = (string)Values.NullIfZero(description[10]); if (column.baseTableName == null || column.baseTableName == "") { column.IsExpression = true; } else { column.IsExpression = false; } } if (uniqueRows && !hasKeyColumns) { uniqueRows = false; } }
public ColumnData[] GetColumnMetaData() { int columnCount = GetResultColumns(); if (columnCount == 0) { return(null); } ColumnData[] columns = new ColumnData[columnCount]; MemoryHandle name = new MemoryHandle((CLI.SQL_MAX_COLUMN_NAME_LEN + 1) * Platform.WideCharSize); try { for (int i = 0; i < columnCount; i++) { CLI.ReturnCode rc; short length, dataType, decimalDigits, nullable; uint columnSize; IntPtr iVal; ColumnData column = new ColumnData(); columns[i] = column; rc = (CLI.ReturnCode)CLI.SQLDescribeCol( hstmt, (ushort)(i + 1), name.Handle, (short)name.Length, out length, out dataType, out columnSize, out decimalDigits, out nullable); if (rc != CLI.ReturnCode.SQL_SUCCESS) { Diagnostics.HandleResult(rc, this, outerCommand.Connection); } length = (short)(length * Platform.WideCharSize); column.columnName = Platform.WideCharsToString(name.Handle, length); //System.Console.WriteLine ("name.Length: {0}, length: {1}, columnName: {2}", name.Length, length, column.columnName); Debug.WriteLineIf(SqlXml.Switch.TraceVerbose, String.Format("SQLColAttribute col={0} data_type={1}", column.columnName, dataType)); column.columnType = DataTypeInfo.MapSqlType((CLI.SqlType)dataType); if (((CLI.SqlType)dataType) == CLI.SqlType.SQL_WLONGVARCHAR) { MemoryHandle bcolt = new MemoryHandle( (CLI.SQL_MAX_COLUMN_NAME_LEN + 1) * Platform.WideCharSize); short blength; IntPtr biVal; Debug.WriteLineIf(SqlXml.Switch.TraceVerbose, String.Format("Calling SQLColAttribute for col {0}", column.columnName)); rc = (CLI.ReturnCode)CLI.SQLColAttribute( hstmt, (ushort)(i + 1), (ushort)CLI.DescriptorField.SQL_DESC_LOCAL_TYPE_NAME, bcolt.Handle, (short)bcolt.Length, out blength, out biVal); Debug.WriteLineIf(SqlXml.Switch.TraceInfo, String.Format( "Calling SQLColAttribute for col {0} returned rc={1}", column.columnName, rc)); if (rc == CLI.ReturnCode.SQL_SUCCESS) { String baseColumnType = Platform.WideCharsToString( bcolt.Handle, blength); Debug.WriteLineIf(SqlXml.Switch.TraceInfo, String.Format( "Calling SQLColAttribute for col {0} returned type={1}", column.columnName, baseColumnType)); if (baseColumnType == "XMLType") { column.columnType = DataTypeInfo.Xml; } } } if (column.columnType == null) { throw new SystemException("Unknown data type"); } column.bufferType = column.columnType.bufferType; column.columnSize = column.columnType.GetFieldSize((int)columnSize); column.precision = (short)column.columnSize; column.scale = decimalDigits; column.IsLong = column.columnType.isLong; column.IsNullable = nullable == 0 ? false : true; rc = (CLI.ReturnCode)CLI.SQLColAttribute( hstmt, (ushort)(i + 1), (ushort)CLI.ColumnAttribute.SQL_COLUMN_UPDATABLE, IntPtr.Zero, (short)0, out length, out iVal); if (rc != CLI.ReturnCode.SQL_SUCCESS) { Diagnostics.HandleResult(rc, this, outerCommand.Connection); } CLI.Updatable updatable = (CLI.Updatable)(int) iVal; column.IsReadOnly = (updatable == CLI.Updatable.SQL_ATTR_READONLY); rc = (CLI.ReturnCode)CLI.SQLColAttribute( hstmt, (ushort)(i + 1), (ushort)CLI.ColumnAttribute.SQL_COLUMN_KEY, IntPtr.Zero, (short)0, out length, out iVal); if (rc != CLI.ReturnCode.SQL_SUCCESS) { Diagnostics.HandleResult(rc, this, outerCommand.Connection); } column.IsKey = (((int)iVal) != 0); rc = (CLI.ReturnCode)CLI.SQLColAttribute( hstmt, (ushort)(i + 1), (ushort)CLI.ColumnAttribute.SQL_COLUMN_HIDDEN, IntPtr.Zero, (short)0, out length, out iVal); if (rc != CLI.ReturnCode.SQL_SUCCESS) { Diagnostics.HandleResult(rc, this, outerCommand.Connection); } column.IsHidden = (((int)iVal) != 0); rc = (CLI.ReturnCode)CLI.SQLColAttribute( hstmt, (ushort)(i + 1), (ushort)CLI.ColumnAttribute.SQL_COLUMN_AUTO_INCREMENT, IntPtr.Zero, (short)0, out length, out iVal); if (rc != CLI.ReturnCode.SQL_SUCCESS) { Diagnostics.HandleResult(rc, this, outerCommand.Connection); } column.IsAutoIncrement = (((int)iVal) != 0); rc = (CLI.ReturnCode)CLI.SQLColAttribute( hstmt, (ushort)(i + 1), (ushort)CLI.DescriptorField.SQL_DESC_ROWVER, IntPtr.Zero, (short)0, out length, out iVal); if (rc != CLI.ReturnCode.SQL_SUCCESS) { Diagnostics.HandleResult(rc, this, outerCommand.Connection); } column.IsRowVersion = (((int)iVal) != 0); // TODO: check for unique columns as well. column.IsUnique = false; rc = (CLI.ReturnCode)CLI.SQLColAttribute( hstmt, (ushort)(i + 1), (ushort)CLI.DescriptorField.SQL_DESC_BASE_COLUMN_NAME, name.Handle, (short)name.Length, out length, out iVal); if (rc != CLI.ReturnCode.SQL_SUCCESS) { Diagnostics.HandleResult(rc, this, outerCommand.Connection); } column.baseColumnName = Platform.WideCharsToString(name.Handle, length); //System.Console.WriteLine ("name.Length: {0}, length: {1}, baseColumnName: {2}", name.Length, length, column.baseColumnName); rc = (CLI.ReturnCode)CLI.SQLColAttribute( hstmt, (ushort)(i + 1), (ushort)CLI.DescriptorField.SQL_DESC_BASE_TABLE_NAME, name.Handle, (short)name.Length, out length, out iVal); if (rc != CLI.ReturnCode.SQL_SUCCESS) { Diagnostics.HandleResult(rc, this, outerCommand.Connection); } column.baseTableName = Platform.WideCharsToString(name.Handle, length); rc = (CLI.ReturnCode)CLI.SQLColAttribute( hstmt, (ushort)(i + 1), (ushort)CLI.DescriptorField.SQL_DESC_SCHEMA_NAME, name.Handle, (short)name.Length, out length, out iVal); if (rc != CLI.ReturnCode.SQL_SUCCESS) { Diagnostics.HandleResult(rc, this, outerCommand.Connection); } column.baseSchemaName = Platform.WideCharsToString(name.Handle, length); rc = (CLI.ReturnCode)CLI.SQLColAttribute( hstmt, (ushort)(i + 1), (ushort)CLI.DescriptorField.SQL_DESC_CATALOG_NAME, name.Handle, (short)name.Length, out length, out iVal); if (rc != CLI.ReturnCode.SQL_SUCCESS) { Diagnostics.HandleResult(rc, this, outerCommand.Connection); } column.baseCatalogName = Platform.WideCharsToString(name.Handle, length); if (column.baseTableName == null || column.baseTableName == "") { column.IsExpression = true; } else { column.IsExpression = false; } } GC.KeepAlive(this); } finally { name.Dispose(); } return(columns); }